summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2019-12-24 11:50:35 +0100
committerJosé Valim <jose.valim@plataformatec.com.br>2019-12-24 11:50:35 +0100
commitd7002a1a99b2502084c23bf1db66ace18f4f247c (patch)
tree3ebd80aa65cd86c9756ce1131e66e384542c383c
parentd2c9824ef9cd5071a3c43a4da546c1da635a232c (diff)
downloadelixir-d7002a1a99b2502084c23bf1db66ace18f4f247c.tar.gz
Use composition instead of conditionals
-rw-r--r--lib/ex_unit/lib/ex_unit/assertions.ex88
-rw-r--r--lib/ex_unit/lib/ex_unit/doc_test.ex10
2 files changed, 50 insertions, 48 deletions
diff --git a/lib/ex_unit/lib/ex_unit/assertions.ex b/lib/ex_unit/lib/ex_unit/assertions.ex
index 72040f19a..b34586c7e 100644
--- a/lib/ex_unit/lib/ex_unit/assertions.ex
+++ b/lib/ex_unit/lib/ex_unit/assertions.ex
@@ -107,61 +107,25 @@ defmodule ExUnit.Assertions do
defmacro assert({:=, meta, [left, right]} = assertion) do
code = escape_quoted(:assert, meta, assertion)
- left = __expand_pattern__(left, __CALLER__)
- vars = collect_vars_from_pattern(left)
- pins = collect_pins_from_pattern(left, Macro.Env.vars(__CALLER__))
-
# If the match works, we need to check if the value
# is not nil nor false. We need to rewrite the if
# to avoid silly warnings though.
- return =
- if meta[:skip_boolean_assert] do
- quote do
- :ok
- end
- else
- suppress_warning(
- quote do
- case right do
- x when x in [nil, false] ->
- raise ExUnit.AssertionError,
- expr: expr,
- message: "Expected truthy, got #{inspect(right)}"
-
- _ ->
- :ok
- end
- end
- )
- end
-
- match_expr =
+ check =
suppress_warning(
quote do
case right do
- unquote(left) ->
- unquote(return)
- unquote(vars)
-
- _ ->
- left = unquote(Macro.escape(left))
-
+ x when x in [nil, false] ->
raise ExUnit.AssertionError,
- left: left,
- right: right,
expr: expr,
- message: "match (=) failed" <> ExUnit.Assertions.__pins__(unquote(pins)),
- context: {:match, unquote(pins)}
+ message: "Expected truthy, got #{inspect(right)}"
+
+ _ ->
+ :ok
end
end
)
- quote do
- right = unquote(right)
- expr = unquote(code)
- unquote(vars) = unquote(match_expr)
- right
- end
+ __match__(left, right, code, check, __CALLER__)
end
defmacro assert({:match?, meta, [left, right]} = assertion) do
@@ -344,8 +308,7 @@ defmodule ExUnit.Assertions do
end
defp escape_quoted(kind, meta, expr) do
- to_escape = if meta[:skip_assert_in_code], do: expr, else: {kind, [], [expr]}
- Macro.escape(to_escape, prune_metadata: true)
+ Macro.escape({kind, meta, [expr]}, prune_metadata: true)
end
defp extract_args({root, meta, [_ | _] = args} = expr, env) do
@@ -377,6 +340,41 @@ defmodule ExUnit.Assertions do
{ExUnit.AssertionError.no_value(), expr}
end
+ @doc false
+ def __match__(left, right, code, check, caller) do
+ left = __expand_pattern__(left, caller)
+ vars = collect_vars_from_pattern(left)
+ pins = collect_pins_from_pattern(left, Macro.Env.vars(caller))
+
+ match_expr =
+ suppress_warning(
+ quote do
+ case right do
+ unquote(left) ->
+ unquote(check)
+ unquote(vars)
+
+ _ ->
+ left = unquote(Macro.escape(left))
+
+ raise ExUnit.AssertionError,
+ left: left,
+ right: right,
+ expr: expr,
+ message: "match (=) failed" <> ExUnit.Assertions.__pins__(unquote(pins)),
+ context: {:match, unquote(pins)}
+ end
+ end
+ )
+
+ quote do
+ right = unquote(right)
+ expr = unquote(code)
+ unquote(vars) = unquote(match_expr)
+ right
+ end
+ end
+
## END HELPERS
@doc """
diff --git a/lib/ex_unit/lib/ex_unit/doc_test.ex b/lib/ex_unit/lib/ex_unit/doc_test.ex
index 282a73286..2a9636b25 100644
--- a/lib/ex_unit/lib/ex_unit/doc_test.ex
+++ b/lib/ex_unit/lib/ex_unit/doc_test.ex
@@ -857,15 +857,19 @@ defmodule ExUnit.DocTest do
defp insert_assertions(ast),
do: insert_match_assertion(ast)
- @match_meta [skip_assert_in_code: true, skip_boolean_assert: true]
-
defp insert_match_assertion({:=, _, [{var, _, context}, _]} = ast)
when is_atom(var) and is_atom(context),
do: ast
defp insert_match_assertion({:=, meta, [left, right]}),
- do: {:assert, meta, [{:=, @match_meta ++ meta, [left, right]}]}
+ do: {{:., meta, [__MODULE__, :__assert__]}, meta, [{:=, meta, [left, right]}]}
defp insert_match_assertion(ast),
do: ast
+
+ @doc false
+ defmacro __assert__({:=, _, [left, right]} = assertion) do
+ code = Macro.escape(assertion, prune_metadata: true)
+ ExUnit.Assertions.__match__(left, right, code, :ok, __CALLER__)
+ end
end