Skip to content

Commit

Permalink
Support longer test names (#17)
Browse files Browse the repository at this point in the history
Fallback to a simple row number in the case of failure (ideally this
would be the source file line number instead)
  • Loading branch information
axelson authored Jul 22, 2024
1 parent 90e74ce commit 83f60d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lib/parameterized_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,23 @@ defmodule ParameterizedTest do
already_escaped
end

max_describe_length_to_fit_on_one_line = 82
test_name_length = String.length(test_name)
# Theoretically an atom can be 255 characters, but there's extra stuff appended to the test
# name that eats up extra characters.
max_context_length = 255 - test_name_length - max_describe_length_to_fit_on_one_line - 22

quote location: :keep do
for example <- unquote(escaped_examples) do
for {example, index} <- Enum.with_index(unquote(escaped_examples)) do
for {key, val} <- example do
@tag [{key, val}]
end

un_truncated_name = "#{unquote(test_name)} (#{inspect(example)})"

full_test_name =
if String.length(un_truncated_name) < 255 do
un_truncated_name
else
"#{unquote(test_name)} row #{index}"
end

@tag param_test: true
test "#{unquote(test_name)} (#{example |> inspect() |> String.slice(0..unquote(max_context_length))})",
unquote(context_ast) do
test "#{full_test_name}", unquote(context_ast) do
unquote(blocks)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/parameterized_test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule ParameterizedTestTest do
assert String.length(test_name) <= 255

assert test_name ==
"test with a very, very, very, very, very, very, very, very, very long `describe` title truncates extremely long contexts to avoid overflowing the atom length limit (%{variable_1: \"foo\", variable_2: \"012345678911234567892123456789312345678941)"
"test with a very, very, very, very, very, very, very, very, very long `describe` title truncates extremely long contexts to avoid overflowing the atom length limit row 0"
end
end

Expand Down

0 comments on commit 83f60d0

Please sign in to comment.