Skip to content

Commit 5439a2b

Browse files
ericentinjosevalim
authored andcommitted
Enhance the ExUnit.AssertionError message (#4709)
1 parent e8db08a commit 5439a2b

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule ExUnit.AssertionError do
33
Raised to signal an assertion error.
44
"""
55

6+
alias ExUnit.Formatter, as: F
7+
68
@no_value :ex_unit_no_meaningful_value
79

810
defexception left: @no_value,
@@ -16,6 +18,12 @@ defmodule ExUnit.AssertionError do
1618
def no_value do
1719
@no_value
1820
end
21+
22+
def message(assertion_error) do
23+
"\n\n" <> F.format_assertion_error(assertion_error, :infinity, &formatter/2, "")
24+
end
25+
26+
defp formatter(_, msg), do: msg
1927
end
2028

2129
defmodule ExUnit.MultiError do

lib/ex_unit/lib/ex_unit/cli_formatter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ defmodule ExUnit.CLIFormatter do
197197
colorize(:red, msg, config)
198198
end
199199

200-
defp formatter(:colors_enabled?, _, %{colors: colors}),
200+
defp formatter(:diff_enabled?, _, %{colors: colors}),
201201
do: colors[:enabled]
202202

203203
defp formatter(:error_info, msg, config),

lib/ex_unit/lib/ex_unit/formatter.ex

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ defmodule ExUnit.Formatter do
118118
<> report(tags, failures, width, formatter)
119119
end
120120

121+
@doc false
122+
def format_assertion_error(%ExUnit.AssertionError{} = struct, width, formatter, counter_padding \\ @counter_padding) do
123+
padding_size = byte_size(@inspect_padding)
124+
125+
fields = [
126+
note: if_value(struct.message, &format_banner(&1, formatter)),
127+
code: if_value(struct.expr, &code_multiline(&1, padding_size)),
128+
lhs: if_value(struct.left, &inspect_multiline(&1, padding_size, width)),
129+
rhs: if_value(struct.right, &inspect_multiline(&1, padding_size, width))
130+
]
131+
132+
fields =
133+
if formatter.(:diff_enabled?, nil) == true do
134+
fields ++ [diff: format_diff(struct, formatter)]
135+
else
136+
fields
137+
end
138+
139+
fields
140+
|> filter_interesting_fields()
141+
|> format_each_field(formatter)
142+
|> make_into_lines(counter_padding)
143+
end
144+
121145
defp report(tags, failures, width, formatter) do
122146
case Map.take(tags, List.wrap(tags[:report])) do
123147
report when map_size(report) == 0 ->
@@ -149,22 +173,7 @@ defmodule ExUnit.Formatter do
149173
end
150174

151175
defp format_kind_reason(:error, %ExUnit.AssertionError{} = struct, width, formatter) do
152-
padding_size = byte_size(@inspect_padding)
153-
154-
fields = [
155-
note: if_value(struct.message, &format_banner(&1, formatter)),
156-
code: if_value(struct.expr, &code_multiline(&1, padding_size)),
157-
lhs: if_value(struct.left, &inspect_multiline(&1, padding_size, width)),
158-
rhs: if_value(struct.right, &inspect_multiline(&1, padding_size, width))
159-
]
160-
if formatter.(:colors_enabled?, nil) do
161-
fields ++ [diff: format_diff(struct, formatter)]
162-
else
163-
fields
164-
end
165-
|> filter_interesting_fields()
166-
|> format_each_field(formatter)
167-
|> make_into_lines(@counter_padding)
176+
format_assertion_error(struct, width, formatter)
168177
end
169178

170179
defp format_kind_reason(kind, reason, _width, formatter) do

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,19 @@ defmodule ExUnit.AssertionsTest do
593593
"no function clause matching in ExUnit.Assertions.flunk/1" = FunctionClauseError.message error
594594
end
595595

596+
test "AssertionError message should include nice formatting" do
597+
assert :a = :b
598+
rescue
599+
error in [ExUnit.AssertionError] ->
600+
"""
601+
602+
603+
match (=) failed
604+
code: :a = :b
605+
rhs: :b
606+
""" = Exception.message(error)
607+
end
608+
596609
defp ok(val), do: {:ok, val}
597610
defp error(val), do: {:error, val}
598611
end

0 commit comments

Comments
 (0)