Skip to content
mager-bench1.3

GPT-6 Astra × elixir-test

9.7

ExUnit describe blocks + assert_raise + unicode handling

correctness 10.0quality 9.0documentation 10.025408ms
$ cat elixir-test.prompt — what the model was asked
Write an ExUnit test module for the following Elixir function:

```elixir
defmodule StringUtils do
  @doc """
  Truncates a string to at most `max_len` characters.
  If truncated, appends `suffix` (default: "...").
  Returns the original string if it is already within `max_len`.
  Raises ArgumentError if max_len is negative.
  """
  @spec truncate(String.t(), non_neg_integer(), String.t()) :: String.t()
  def truncate(str, max_len, suffix \\ "...")
end
```

Requirements:
- Module name: `StringUtilsTest`, `use ExUnit.Case`
- Use `describe` blocks to group related cases
- At least 2 `describe` blocks: one for happy path, one for edge/error cases
- At least 8 test cases total covering: string shorter than limit, exact length, longer string (suffix appended), empty string, custom suffix, max_len of 0, negative max_len (should raise), unicode string
- Use `assert`, `assert_raise`, and pattern matching appropriately
- Add at least one doctest-style `## Examples` in a module comment showing usage

run 1 of 1

9.7
correctness 10.0quality 9.0documentation 10.025408ms

# judge: The response thoroughly covers every required case with correct Unicode grapheme expectations, clear describe blocks and test names, valid exception assertions, and accurate module-level examples; the only minor limitation is that pattern matching is used somewhat stylistically rather than adding substantial clarity.

$ cat codex-cli/gpt-6-astra.elixir-test.r1.response
```elixir
defmodule StringUtilsTest do
  use ExUnit.Case, async: true

  @moduledoc """
  Tests for `StringUtils.truncate/3`.

  ## Examples

      iex> StringUtils.truncate("Hello, world!", 5)
      "Hello..."

      iex> StringUtils.truncate("Hello", 10)
      "Hello"
  """

  describe "truncate/3 happy path" do
    test "returns a string shorter than the limit unchanged" do
      assert StringUtils.truncate("Hello", 10) == "Hello"
    end

    test "returns a string exactly at the limit unchanged" do
      assert StringUtils.truncate("Hello", 5) == "Hello"
    end

    test "truncates a longer string and appends the default suffix" do
      assert "Hello..." = StringUtils.truncate("Hello, world!", 5)
    end

    test "appends a custom suffix when truncating" do
      assert StringUtils.truncate("Hello, world!", 5, "…") == "Hello…"
    end

    test "does not append a custom suffix when truncation is unnecessary" do
      assert StringUtils.truncate("Hello", 5, " [more]") == "Hello"
    end

    test "counts Unicode graphemes rather than bytes" do
      assert StringUtils.truncate("é🙂界abc", 3) == "é🙂界..."
    end
  end

  describe "truncate/3 edge and error cases" do
    test "returns an empty string unchanged" do
      assert StringUtils.truncate("", 5) == ""
    end

    test "returns only the suffix for a nonempty string and a zero limit" do
      assert StringUtils.truncate("Hello", 0) == "..."
      assert StringUtils.truncate("Hello", 0, "…") == "…"
    end

    test "returns an empty string unchanged at a zero limit" do
      assert StringUtils.truncate("", 0) == ""
    end

    test "supports an empty suffix" do
      assert StringUtils.truncate("Hello, world!", 5, "") == "Hello"
    end

    test "preserves combining characters within a grapheme" do
      assert StringUtils.truncate("e\u0301clair", 1) == "e\u0301..."
    end

    test "raises ArgumentError for a negative limit" do
      assert_raise ArgumentError, fn ->
        StringUtils.truncate("Hello", -1)
      end
    end

    test "raises ArgumentError for a negative limit even with an empty string" do
      assert_raise ArgumentError, fn ->
        StringUtils.truncate("", -1)
      end
    end
  end
end
```