Skip to content

Commit 49f883d

Browse files
author
José Valim
committed
Do not show variables on error reports due to verbosity
1 parent c94327c commit 49f883d

File tree

4 files changed

+2
-107
lines changed

4 files changed

+2
-107
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ defmodule ExUnit.AssertionError do
88
defexception left: @no_value,
99
right: @no_value,
1010
message: @no_value,
11-
expr: @no_value,
12-
binding: @no_value
11+
expr: @no_value
1312

1413
@doc """
1514
Indicates no meaningful value for a field.
@@ -105,7 +104,6 @@ defmodule ExUnit.Assertions do
105104
left = Macro.expand(left, __CALLER__)
106105
vars = collect_vars_from_pattern(left)
107106
pins = collect_pins_from_pattern(left, __CALLER__.vars)
108-
rhs_binding = collect_vars_used_in_expression(right, __CALLER__.vars)
109107

110108
# If the match works, we need to check if the value
111109
# is not nil nor false. We need to rewrite the if
@@ -116,7 +114,6 @@ defmodule ExUnit.Assertions do
116114
x when x in [nil, false] ->
117115
raise ExUnit.AssertionError,
118116
expr: expr,
119-
binding: unquote(rhs_binding),
120117
message: "Expected truthy, got #{inspect right}"
121118
_ ->
122119
:ok
@@ -133,7 +130,6 @@ defmodule ExUnit.Assertions do
133130
raise ExUnit.AssertionError,
134131
right: right,
135132
expr: expr,
136-
binding: unquote(rhs_binding),
137133
message: "match (=) failed" <>
138134
ExUnit.Assertions.__pins__(unquote(pins))
139135
end
@@ -151,14 +147,12 @@ defmodule ExUnit.Assertions do
151147
code = escape_quoted(:assert, assertion)
152148
match? = {:match?, meta, [left, Macro.var(:right, __MODULE__)]}
153149
pins = collect_pins_from_pattern(left, __CALLER__.vars)
154-
rhs_binding = collect_vars_used_in_expression(right, __CALLER__.vars)
155150

156151
quote do
157152
right = unquote(right)
158153
assert unquote(match?),
159154
right: right,
160155
expr: unquote(code),
161-
binding: unquote(rhs_binding),
162156
message: "match (match?) failed" <>
163157
ExUnit.Assertions.__pins__(unquote(pins))
164158
end
@@ -167,15 +161,12 @@ defmodule ExUnit.Assertions do
167161
defmacro assert(assertion) do
168162
case translate_assertion(:assert, assertion, __CALLER__) do
169163
nil ->
170-
binding = collect_vars_used_in_expression(assertion, __CALLER__.vars)
171-
172164
quote do
173165
value = unquote(assertion)
174166

175167
unless value do
176168
raise ExUnit.AssertionError,
177169
expr: unquote(escape_quoted(:assert, assertion)),
178-
binding: unquote(binding),
179170
message: "Expected truthy, got #{inspect value}"
180171
end
181172

@@ -212,14 +203,12 @@ defmodule ExUnit.Assertions do
212203
code = escape_quoted(:refute, assertion)
213204
match? = {:match?, meta, [left, Macro.var(:right, __MODULE__)]}
214205
pins = collect_pins_from_pattern(left, __CALLER__.vars)
215-
rhs_binding = collect_vars_used_in_expression(right, __CALLER__.vars)
216206

217207
quote do
218208
right = unquote(right)
219209
refute unquote(match?),
220210
right: right,
221211
expr: unquote(code),
222-
binding: unquote(rhs_binding),
223212
message: "match (match?) succeeded, but should have failed" <>
224213
ExUnit.Assertions.__pins__(unquote(pins))
225214
end
@@ -228,15 +217,12 @@ defmodule ExUnit.Assertions do
228217
defmacro refute(assertion) do
229218
case translate_assertion(:refute, assertion, __CALLER__) do
230219
nil ->
231-
binding = collect_vars_used_in_expression(assertion, __CALLER__.vars)
232-
233220
quote do
234221
value = unquote(assertion)
235222

236223
if value do
237224
raise ExUnit.AssertionError,
238225
expr: unquote(escape_quoted(:refute, assertion)),
239-
binding: unquote(binding),
240226
message: "Expected false or nil, got #{inspect value}"
241227
end
242228

@@ -277,26 +263,19 @@ defmodule ExUnit.Assertions do
277263
defp translate_assertion(kind, {_, _, [left, right]} = expr, call, message, true, caller) do
278264
expr = escape_quoted(kind, expr)
279265

280-
# We collect the binding for LHS/RHS separately because we want top-level
281-
# variables to not show up in the binding, but if we pass "expr" we're sure
282-
# there won't be any top-level variables.
283-
binding = Enum.uniq(collect_vars_used_in_expression(left, caller.vars) ++ collect_vars_used_in_expression(right, caller.vars))
284-
285266
quote do
286267
left = unquote(left)
287268
right = unquote(right)
288269
if ExUnit.Assertions.__equal__?(left, right) do
289270
assert false,
290271
left: left,
291272
expr: unquote(expr),
292-
binding: unquote(binding),
293273
message: unquote(message <> ", both sides are exactly equal")
294274
else
295275
assert unquote(call),
296276
left: left,
297277
right: right,
298278
expr: unquote(expr),
299-
binding: unquote(binding),
300279
message: unquote(message)
301280
end
302281
end
@@ -305,19 +284,13 @@ defmodule ExUnit.Assertions do
305284
defp translate_assertion(kind, {_, _, [left, right]} = expr, call, message, false, caller) do
306285
expr = escape_quoted(kind, expr)
307286

308-
# We collect the binding for LHS/RHS separately because we want top-level
309-
# variables to not show up in the binding, but if we pass "expr" we're sure
310-
# there won't be any top-level variables.
311-
binding = Enum.uniq(collect_vars_used_in_expression(left, caller.vars) ++ collect_vars_used_in_expression(right, caller.vars))
312-
313287
quote do
314288
left = unquote(left)
315289
right = unquote(right)
316290
assert unquote(call),
317291
left: left,
318292
right: right,
319293
expr: unquote(expr),
320-
binding: unquote(binding),
321294
message: unquote(message)
322295
end
323296
end

lib/ex_unit/lib/ex_unit/formatter.ex

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,13 @@ defmodule ExUnit.Formatter do
126126
padding_size = label_padding_size + byte_size(@counter_padding)
127127
inspect = &inspect_multiline(&1, padding_size, width)
128128
{left, right} = format_sides(struct, formatter, inspect)
129-
binding = if(struct.binding == [], do: ExUnit.AssertionError.no_value(), else: struct.binding)
130129

131130
[
132131
note: if_value(struct.message, &format_message(&1, formatter)),
133132
code: if_value(struct.expr, &code_multiline(&1, padding_size)),
134133
code: unless_value(struct.expr, fn -> get_code(test, stack) || @no_value end),
135134
left: left,
136-
right: right,
137-
variables: if_value(binding, &format_binding(&1, width)),
135+
right: right
138136
]
139137
|> format_meta(formatter, label_padding_size)
140138
|> make_into_lines(counter_padding)
@@ -263,18 +261,6 @@ defmodule ExUnit.Formatter do
263261
code_multiline(Macro.to_string(expr), padding_size)
264262
end
265263

266-
defp format_binding(binding, width) do
267-
padding = @counter_padding <> " "
268-
padding_size = byte_size(padding)
269-
270-
result =
271-
Enum.map_join(binding, "\n" <> padding, fn {var, value} ->
272-
"#{var} = #{inspect_multiline(value, padding_size, width)}"
273-
end)
274-
275-
"\n" <> padding <> result
276-
end
277-
278264
defp inspect_multiline(expr, padding_size, width) do
279265
padding = String.duplicate(" ", padding_size)
280266
width = if width == :infinity, do: width, else: width - padding_size

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -74,53 +74,6 @@ defmodule ExUnit.AssertionsTest do
7474
end
7575
end
7676

77-
test "assert shows binding in the asserted expression" do
78-
# No binding is shown because the RHS is only a top-level variable
79-
# and thus it's already printed as the RHS of the match.
80-
try do
81-
int1 = 1
82-
int2 = 2
83-
assert ^int1 = int2
84-
rescue
85-
error in [ExUnit.AssertionError] ->
86-
[] = error.binding
87-
end
88-
89-
try do
90-
int1 = 1
91-
int2 = 2
92-
assert ^int1 = int2 * 2
93-
rescue
94-
error in [ExUnit.AssertionError] ->
95-
[int2: 2] = error.binding
96-
end
97-
98-
try do
99-
int1 = 1
100-
int2 = 2
101-
assert int1 * 10 == int2
102-
rescue
103-
error in [ExUnit.AssertionError] ->
104-
[int1: 1] = error.binding
105-
end
106-
end
107-
108-
test "assert shows only binding in the asserted expression when assertion fails" do
109-
try do
110-
bin1 = <<1, 2, 3>>
111-
bin2 = <<1, 2, 4>>
112-
113-
# Let's have a variable that we don't use in the assertion in the __ENV__.
114-
bin3 = bin1 <> bin2
115-
_ = bin3
116-
117-
assert String.starts_with?(bin1, bin2)
118-
rescue
119-
error in [ExUnit.AssertionError] ->
120-
[bin1: <<1, 2, 3>>, bin2: <<1, 2, 4>>] = error.binding
121-
end
122-
end
123-
12477
test "refute when value is false" do
12578
false = refute false
12679
end

lib/ex_unit/test/ex_unit/formatter_test.exs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,6 @@ defmodule ExUnit.FormatterTest do
143143
"""
144144
end
145145

146-
test "formats binding when there is some" do
147-
many_ids = Enum.to_list(1..30)
148-
no_ids = []
149-
failure = [{:error, catch_assertion(assert Enum.take(many_ids, 3) == no_ids), []}]
150-
assert format_test_failure(test(), failure, 1, 80, &formatter/2) =~ """
151-
1) world (Hello)
152-
test/ex_unit/formatter_test.exs:1
153-
Assertion with == failed
154-
code: assert Enum.take(many_ids, 3) == no_ids
155-
left: [1, 2, 3]
156-
right: []
157-
variables:
158-
many_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
159-
21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
160-
"""
161-
end
162-
163146
test "formats multiple assertions" do
164147
failure = [{:error, catch_assertion(assert ExUnit.FormatterTest.falsy), []},
165148
{:error, catch_assertion(assert 1 == 2), []}]

0 commit comments

Comments
 (0)