Skip to content

Commit

Permalink
Show hints for mix test --no-color
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck committed Jan 18, 2025
1 parent c22c907 commit d4e6704
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/ex_unit/lib/ex_unit/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,19 @@ defmodule ExUnit.Formatter do
content_width = if width == :infinity, do: width, else: width - padding_size

case format_diff(left, right, context, formatter) do
{result, env} ->
{nil, hints} when is_atom(context) ->
{if_value(left, inspect), if_value(right, inspect), hints}

{nil, hints} ->
left =
Macro.prewalk(left, fn
{_, [original: original], _} -> original
other -> other
end)

{if_value(left, &code_multiline(&1, padding_size)), if_value(right, inspect), hints}

{result, hints} ->
left =
result.left
|> Diff.to_algebra(&colorize_diff_delete(&1, formatter))
Expand All @@ -490,32 +502,25 @@ defmodule ExUnit.Formatter do
|> Algebra.nest(padding_size)
|> Algebra.format(content_width)

{left, right, Enum.map(env.hints, &{:hint, format_hint(&1)})}

nil when is_atom(context) ->
{if_value(left, inspect), if_value(right, inspect), []}

nil ->
left =
Macro.prewalk(left, fn
{_, [original: original], _} -> original
other -> other
end)
{left, right, hints}
end
end

{if_value(left, &code_multiline(&1, padding_size)), if_value(right, inspect), []}
defp format_diff(left, right, context, formatter) do
if has_value?(left) and has_value?(right) do
{result, env} = find_diff(left, right, context)
result = if formatter.(:diff_enabled?, false), do: result
hints = Enum.map(env.hints, &{:hint, format_hint(&1)})
{result, hints}
else
{nil, []}
end
end

defp format_hint(:equivalent_but_different_strings) do
"you are comparing strings that have the same visual representation but are made of different Unicode codepoints"
end

defp format_diff(left, right, context, formatter) do
if has_value?(left) and has_value?(right) and formatter.(:diff_enabled?, false) do
find_diff(left, right, context)
end
end

defp colorize_diff_delete(doc, formatter) do
format = colorize_format(doc, :diff_delete, :diff_delete_whitespace)
formatter.(format, doc)
Expand Down
3 changes: 3 additions & 0 deletions lib/ex_unit/test/ex_unit/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ defmodule ExUnit.FormatterTest do
hint: you are comparing strings that have the same visual representation but are made of different Unicode codepoints
"""

assert format_test_failure(test(), failure, 1, 80, &diff_formatter/2) ==
format_test_failure(test(), failure, 1, 80, &formatter/2)

assert format_assertion_diff(assertion_error, 0, :infinity, &diff_formatter/2)
|> kw_to_string() ==
[
Expand Down

0 comments on commit d4e6704

Please sign in to comment.