Skip to content

Commit 860c056

Browse files
author
José Valim
committed
Clean up new formatter implementation
1 parent 897424b commit 860c056

File tree

6 files changed

+123
-342
lines changed

6 files changed

+123
-342
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 47 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
defmodule ExUnit.NoValueSupplied do
2-
def no_value, do: {:__no__, :__meaningful__, :__value__}
3-
end
4-
5-
defexception ExUnit.AssertionError, [
6-
left: ExUnit.NoValueSupplied.no_value,
7-
right: ExUnit.NoValueSupplied.no_value,
8-
value: ExUnit.NoValueSupplied.no_value,
9-
message: ExUnit.NoValueSupplied.no_value,
10-
operator: ExUnit.NoValueSupplied.no_value,
11-
expr: "missing failing expression" ] do
1+
defexception ExUnit.AssertionError,
2+
left: :ex_unit_no_meaningful_value,
3+
right: :ex_unit_no_meaningful_value,
4+
message: :ex_unit_no_meaningful_value,
5+
expr: "missing failing expression" do
126

7+
@doc """
8+
Indicates no meaningful value for a field.
9+
"""
10+
def no_value do
11+
:ex_unit_no_meaningful_value
12+
end
1313
end
1414

1515
defmodule ExUnit.Assertions do
@@ -56,12 +56,12 @@ defmodule ExUnit.Assertions do
5656
5757
will fail with the message:
5858
59-
Comparison (using >) failed in:
59+
Assertion with > failed
6060
code: 1+2+3+4 > 15
6161
lhs: 10
6262
rhs: 15
6363
"""
64-
defmacro assert(assertion = { :=, _, [left, right] }) do
64+
defmacro assert({ :=, _, [left, right] } = assertion) do
6565
code = Macro.to_string(assertion)
6666
{ :case, meta, args } =
6767
quote do
@@ -70,29 +70,27 @@ defmodule ExUnit.Assertions do
7070
right
7171
_ ->
7272
raise ExUnit.AssertionError,
73-
right: Macro.to_string(right),
73+
right: right,
7474
expr: unquote(code),
7575
message: "match (=) failed"
7676
end
7777
end
7878

7979
quote do
8080
right = unquote(right)
81-
unquote({ :case, [{:export_head,true}|meta], args })
81+
unquote({ :case, [{ :export_head, true }|meta], args })
8282
end
8383
end
8484

85-
8685
defmacro assert(assertion) do
8786
case translate_assertion(assertion) do
8887
nil ->
89-
# Default message in case no transform was performed
9088
quote do
9189
value = unquote(assertion)
9290

9391
unless value do
9492
raise ExUnit.AssertionError,
95-
expr: unquote(Macro.to_string(assertion)),
93+
expr: unquote(Macro.to_string(assertion)),
9694
message: "#{inspect value} is not truthy"
9795
end
9896

@@ -113,15 +111,14 @@ defmodule ExUnit.Assertions do
113111
refute age < 0
114112
115113
"""
116-
117-
defmacro refute(assertion = { :=, _, [left, right] }) do
114+
defmacro refute({ :=, _, [left, right] } = assertion) do
118115
code = Macro.to_string(assertion)
119116
{ :case, meta, args } =
120117
quote do
121118
case right do
122119
unquote(left) ->
123120
raise ExUnit.AssertionError,
124-
right: Macro.to_string(right),
121+
right: right,
125122
expr: unquote(code),
126123
message: "match (=) succeeded, but should have failed"
127124
_ ->
@@ -131,21 +128,19 @@ defmodule ExUnit.Assertions do
131128

132129
quote do
133130
right = unquote(right)
134-
unquote({ :case, [{:export_head,true}|meta], args })
131+
unquote({ :case, [{ :export_head, true }|meta], args })
135132
end
136133
end
137134

138-
139135
defmacro refute(assertion) do
140136
case translate_assertion({ :!, [], [assertion] }) do
141137
nil ->
142-
# Default message in case no transform was performed
143138
quote do
144139
value = unquote(assertion)
145140

146141
if value do
147142
raise ExUnit.AssertionError,
148-
expr: unquote(Macro.to_string(assertion)),
143+
expr: unquote(Macro.to_string(assertion)),
149144
message: "#{inspect value} should be false or nil"
150145
end
151146

@@ -159,86 +154,38 @@ defmodule ExUnit.Assertions do
159154

160155
## START HELPERS
161156

157+
@operator [:==, :<, :>, :<=, :>=, :===, :=~, :!==, :!=, :in]
162158

163-
164-
defp translate_assertion(expr = { operator, _, [left, right] })
165-
when operator in [ :==, :<, :>, :<=, :>=, :===, :=~, :!==, :!= ] do
166-
assert_operator operator, left, right, expr
167-
end
168-
169-
170-
defp translate_assertion(expr = { :in, _, [left, right] }) do
171-
code = Macro.to_string(expr)
159+
defp translate_assertion({ operator, _, [left, right] } = expr) when operator in @operator do
160+
expr = Macro.to_string(expr)
172161
quote do
173162
left = unquote(left)
174163
right = unquote(right)
175-
assert_internal Enum.member?(right, left),
176-
left: left, right: right, expr: unquote(code)
164+
assert unquote(operator)(left, right),
165+
left: left,
166+
right: right,
167+
expr: unquote(expr),
168+
message: unquote("Assertion with #{operator} failed")
177169
end
178170
end
179171

180-
## Negative versions
181-
182-
defp translate_assertion({ :!, _, [{ :=, _, [left, right] }] }) do
183-
quote do
184-
right = unquote(right)
185-
case right do
186-
unquote(left) ->
187-
raise ExUnit.AssertionError,
188-
expected: inspect(right),
189-
actual: unquote(Macro.to_string(left)),
190-
assertion: "match pattern (=)",
191-
negation: true
192-
_ ->
193-
nil
194-
end
195-
end
196-
end
197-
198-
defp translate_assertion(expr = { negation, _, [{ :in, _, [left, right] }] })
199-
when negation in [:!, :not] do
200-
code = Macro.to_string(expr)
172+
defp translate_assertion({ :!, [], [{ operator, _, [left, right] } = expr] }) when operator in @operator do
173+
expr = Macro.to_string(expr)
201174
quote do
202175
left = unquote(left)
203176
right = unquote(right)
204-
assert_internal !Enum.member?(right, left),
205-
left: left, right: right, expr: unquote(code)
177+
assert not(unquote(operator)(left, right)),
178+
left: left,
179+
right: right,
180+
expr: unquote(expr),
181+
message: unquote("Refute with #{operator} failed")
206182
end
207183
end
208184

209-
defp translate_assertion(expr = {:!, [], [{ operator, _, [left, right] }]})
210-
when operator in [ :==, :<, :>, :<=, :>=, :===, :=~, :!==, :!= ] do
211-
refute_operator operator, left, right, expr
212-
end
213-
214-
## Fallback
215-
216185
defp translate_assertion(_expected) do
217186
nil
218187
end
219188

220-
defp assert_operator(operator, left, right, expr) do
221-
expr = Macro.to_string(expr)
222-
quote location: :keep do
223-
left = unquote(left)
224-
right = unquote(right)
225-
assert_internal unquote(operator)(left, right), left: left, right: right, expr: unquote(expr), operator: to_string(unquote(operator))
226-
end
227-
end
228-
229-
defp refute_operator(operator, left, right, expr) do
230-
expr = Macro.to_string(expr)
231-
quote location: :keep do
232-
left = unquote(left)
233-
right = unquote(right)
234-
assert_internal not(unquote(operator)(left, right)), left: left, right: right, expr: unquote(expr), operator: to_string(unquote(operator))
235-
end
236-
end
237-
238-
def assert_internal(successful, opts) do
239-
unless successful, do: raise(ExUnit.AssertionError, opts)
240-
true
241-
end
242189
## END HELPERS
243190

244191
@doc """
@@ -250,7 +197,12 @@ defmodule ExUnit.Assertions do
250197
251198
"""
252199
def assert(value, message) when is_binary(message) do
253-
assert_internal(value, message: message, expr: ExUnit.NoValueSupplied.no_value)
200+
assert(value, message: message, expr: ExUnit.AssertionError.no_value)
201+
end
202+
203+
def assert(value, opts) when is_list(opts) do
204+
unless value, do: raise(ExUnit.AssertionError, opts)
205+
true
254206
end
255207

256208
@doc """
@@ -267,16 +219,8 @@ defmodule ExUnit.Assertions do
267219
268220
"""
269221
def assert(value, left, right, message) when is_binary(message) do
270-
assert_internal(value, left: left, right: right,
271-
message: message, expr: ExUnit.NoValueSupplied.no_value)
272-
end
273-
274-
def assert(value, expected, actual, opts) do
275-
unless value do
276-
raise ExUnit.AssertionError,
277-
Keyword.merge([expected: inspect(expected), actual: inspect(actual)], opts)
278-
end
279-
true
222+
assert(value, left: left, right: right,
223+
message: message, expr: ExUnit.AssertionError.no_value)
280224
end
281225

282226
@doc """
@@ -363,7 +307,7 @@ defmodule ExUnit.Assertions do
363307
end
364308

365309
msg = "Wrong message for #{inspect exception}. Expected #{inspect message}, got #{inspect error.message}"
366-
assert_internal is_match, message: msg, expr: ExUnit.NoValueSupplied.no_value
310+
assert is_match, message: msg, expr: ExUnit.AssertionError.no_value
367311

368312
error
369313
end
@@ -388,7 +332,7 @@ defmodule ExUnit.Assertions do
388332
error ->
389333
name = error.__record__(:name)
390334

391-
if name in [ExUnit.AssertionError, ExUnit.AssertionError] do
335+
if name in [ExUnit.AssertionError] do
392336
raise(error)
393337
else
394338
flunk "Expected exception '#{inspect exception}' but got #{inspect name}(#{error.message})"
@@ -459,7 +403,7 @@ defmodule ExUnit.Assertions do
459403
unquote(expr)
460404
flunk "Expected to catch #{unquote(kind)}, got nothing"
461405
rescue
462-
e in [ExUnit.AssertionError, ExUnit.AssertionError] -> raise(e)
406+
e in [ExUnit.AssertionError] -> raise(e)
463407
catch
464408
unquote(kind), what_we_got -> what_we_got
465409
end
@@ -576,6 +520,6 @@ defmodule ExUnit.Assertions do
576520
@spec flunk :: no_return
577521
@spec flunk(String.t) :: no_return
578522
def flunk(message \\ "Flunked!") do
579-
assert_internal false, message: message, expr: ExUnit.NoValueSupplied.no_value
523+
assert false, message: message, expr: ExUnit.AssertionError.no_value
580524
end
581525
end

lib/ex_unit/lib/ex_unit/cli_formatter.ex

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ defmodule ExUnit.CLIFormatter do
66
import ExUnit.Formatter, only: [format_time: 2, format_filters: 2, format_test_failure: 5, format_test_case_failure: 4]
77

88
defrecord Config, tests_counter: 0, invalids_counter: 0, failures_counter: 0,
9-
skips_counter: 0, trace: false, seed: nil, color: true, width: nil
9+
skips_counter: 0, trace: false, seed: nil, color: true
1010

1111
## Callbacks
1212

1313
def init(opts) do
1414
print_filters(Keyword.take(opts, [:include, :exclude]))
15-
{ :ok, Config.new(add_terminal_width_to(opts)) }
15+
{ :ok, Config.new(opts) }
1616
end
1717

1818
def handle_event({ :suite_finished, run_us, load_us }, config = Config[]) do
@@ -136,14 +136,8 @@ defmodule ExUnit.CLIFormatter do
136136
end
137137

138138
defp print_test_failure(ExUnit.Test[name: name, case: mod, state: { :failed, tuple }], config) do
139-
try do
140-
format_test_failure(mod, name, tuple, config.failures_counter + 1, &formatter(&1, &2, config))
141-
|> print_any_failure config
142-
143-
rescue e ->
144-
IO.puts e.message
145-
146-
end
139+
formatted = format_test_failure(mod, name, tuple, config.failures_counter + 1, &formatter(&1, &2, config))
140+
print_any_failure formatted, config
147141
end
148142

149143
defp print_test_case_failure(ExUnit.TestCase[name: name, state: { :failed, tuple }], config) do
@@ -156,7 +150,7 @@ defmodule ExUnit.CLIFormatter do
156150
config.trace -> IO.puts ""
157151
true -> IO.puts "\n"
158152
end
159-
IO.puts formatted
153+
IO.write formatted
160154
config.update_failures_counter(&(&1 + 1))
161155
end
162156

@@ -180,18 +174,7 @@ defmodule ExUnit.CLIFormatter do
180174
colorize("red", msg, config)
181175
end
182176

183-
defp formatter(:width, _, config), do: config.width
184-
185177
defp formatter(:error_info, msg, config), do: colorize("red", msg, config)
186178
defp formatter(:location_info, msg, config), do: colorize("cyan", msg, config)
187179
defp formatter(_, msg, _config), do: msg
188-
189-
defp add_terminal_width_to(opts) do
190-
case :io.columns do
191-
{ :ok, width } ->
192-
Dict.merge(opts, [width: min(width, 80)])
193-
_ ->
194-
opts
195-
end
196-
end
197180
end

0 commit comments

Comments
 (0)