Skip to content

Commit

Permalink
Do not expand variables to argless macros in assert
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Oct 17, 2017
1 parent 6f7b759 commit c7e3420
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/ex_unit/lib/ex_unit/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule ExUnit.Assertions do
defmacro assert({:=, _, [left, right]} = assertion) do
code = escape_quoted(:assert, assertion)

left = Macro.prewalk(left, &Macro.expand(&1, __CALLER__))
left = expand_pattern(left, __CALLER__)
vars = collect_vars_from_pattern(left)
pins = collect_pins_from_pattern(left, __CALLER__.vars)

Expand Down Expand Up @@ -398,7 +398,7 @@ defmodule ExUnit.Assertions do
binary = Macro.to_string(pattern)

# Expand before extracting metadata
pattern = Macro.prewalk(pattern, &Macro.expand(&1, caller))
pattern = expand_pattern(pattern, caller)
vars = collect_vars_from_pattern(pattern)
pins = collect_pins_from_pattern(pattern, caller.vars)

Expand Down Expand Up @@ -560,6 +560,16 @@ defmodule ExUnit.Assertions do
|> elem(1)
end

defp expand_pattern(expr, caller) do
Macro.prewalk(expr, fn
{var, _, context} = node when is_atom(var) and is_atom(context) ->
node

other ->
Macro.expand(other, caller)
end)
end

defp suppress_warning({name, meta, [expr, [do: clauses]]}) do
clauses =
Enum.map(clauses, fn {:->, meta, args} ->
Expand Down
6 changes: 6 additions & 0 deletions lib/ex_unit/test/ex_unit/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule ExUnit.AssertionsTest do
use ExUnit.Case, async: true

defmacro sigil_l({:<<>>, _, [string]}, _), do: Code.string_to_quoted!(string, [])
defmacro argless_macro(), do: raise "should not be invoked"

defmacrop assert_ok(arg) do
quote do
Expand Down Expand Up @@ -85,6 +86,11 @@ defmodule ExUnit.AssertionsTest do
assert c == 3
end

test "assert does not expand variables" do
assert argless_macro = 1
assert argless_macro == 1
end

test "refute when value is false" do
false = refute false
end
Expand Down

0 comments on commit c7e3420

Please sign in to comment.