Skip to content

Commit d7002a1

Browse files
author
José Valim
committed
Use composition instead of conditionals
1 parent d2c9824 commit d7002a1

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -107,61 +107,25 @@ defmodule ExUnit.Assertions do
107107
defmacro assert({:=, meta, [left, right]} = assertion) do
108108
code = escape_quoted(:assert, meta, assertion)
109109

110-
left = __expand_pattern__(left, __CALLER__)
111-
vars = collect_vars_from_pattern(left)
112-
pins = collect_pins_from_pattern(left, Macro.Env.vars(__CALLER__))
113-
114110
# If the match works, we need to check if the value
115111
# is not nil nor false. We need to rewrite the if
116112
# to avoid silly warnings though.
117-
return =
118-
if meta[:skip_boolean_assert] do
119-
quote do
120-
:ok
121-
end
122-
else
123-
suppress_warning(
124-
quote do
125-
case right do
126-
x when x in [nil, false] ->
127-
raise ExUnit.AssertionError,
128-
expr: expr,
129-
message: "Expected truthy, got #{inspect(right)}"
130-
131-
_ ->
132-
:ok
133-
end
134-
end
135-
)
136-
end
137-
138-
match_expr =
113+
check =
139114
suppress_warning(
140115
quote do
141116
case right do
142-
unquote(left) ->
143-
unquote(return)
144-
unquote(vars)
145-
146-
_ ->
147-
left = unquote(Macro.escape(left))
148-
117+
x when x in [nil, false] ->
149118
raise ExUnit.AssertionError,
150-
left: left,
151-
right: right,
152119
expr: expr,
153-
message: "match (=) failed" <> ExUnit.Assertions.__pins__(unquote(pins)),
154-
context: {:match, unquote(pins)}
120+
message: "Expected truthy, got #{inspect(right)}"
121+
122+
_ ->
123+
:ok
155124
end
156125
end
157126
)
158127

159-
quote do
160-
right = unquote(right)
161-
expr = unquote(code)
162-
unquote(vars) = unquote(match_expr)
163-
right
164-
end
128+
__match__(left, right, code, check, __CALLER__)
165129
end
166130

167131
defmacro assert({:match?, meta, [left, right]} = assertion) do
@@ -344,8 +308,7 @@ defmodule ExUnit.Assertions do
344308
end
345309

346310
defp escape_quoted(kind, meta, expr) do
347-
to_escape = if meta[:skip_assert_in_code], do: expr, else: {kind, [], [expr]}
348-
Macro.escape(to_escape, prune_metadata: true)
311+
Macro.escape({kind, meta, [expr]}, prune_metadata: true)
349312
end
350313

351314
defp extract_args({root, meta, [_ | _] = args} = expr, env) do
@@ -377,6 +340,41 @@ defmodule ExUnit.Assertions do
377340
{ExUnit.AssertionError.no_value(), expr}
378341
end
379342

343+
@doc false
344+
def __match__(left, right, code, check, caller) do
345+
left = __expand_pattern__(left, caller)
346+
vars = collect_vars_from_pattern(left)
347+
pins = collect_pins_from_pattern(left, Macro.Env.vars(caller))
348+
349+
match_expr =
350+
suppress_warning(
351+
quote do
352+
case right do
353+
unquote(left) ->
354+
unquote(check)
355+
unquote(vars)
356+
357+
_ ->
358+
left = unquote(Macro.escape(left))
359+
360+
raise ExUnit.AssertionError,
361+
left: left,
362+
right: right,
363+
expr: expr,
364+
message: "match (=) failed" <> ExUnit.Assertions.__pins__(unquote(pins)),
365+
context: {:match, unquote(pins)}
366+
end
367+
end
368+
)
369+
370+
quote do
371+
right = unquote(right)
372+
expr = unquote(code)
373+
unquote(vars) = unquote(match_expr)
374+
right
375+
end
376+
end
377+
380378
## END HELPERS
381379

382380
@doc """

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,15 +857,19 @@ defmodule ExUnit.DocTest do
857857
defp insert_assertions(ast),
858858
do: insert_match_assertion(ast)
859859

860-
@match_meta [skip_assert_in_code: true, skip_boolean_assert: true]
861-
862860
defp insert_match_assertion({:=, _, [{var, _, context}, _]} = ast)
863861
when is_atom(var) and is_atom(context),
864862
do: ast
865863

866864
defp insert_match_assertion({:=, meta, [left, right]}),
867-
do: {:assert, meta, [{:=, @match_meta ++ meta, [left, right]}]}
865+
do: {{:., meta, [__MODULE__, :__assert__]}, meta, [{:=, meta, [left, right]}]}
868866

869867
defp insert_match_assertion(ast),
870868
do: ast
869+
870+
@doc false
871+
defmacro __assert__({:=, _, [left, right]} = assertion) do
872+
code = Macro.escape(assertion, prune_metadata: true)
873+
ExUnit.Assertions.__match__(left, right, code, :ok, __CALLER__)
874+
end
871875
end

0 commit comments

Comments
 (0)