diff options
author | José Valim <jose.valim@dashbit.co> | 2021-12-29 12:05:37 +0100 |
---|---|---|
committer | José Valim <jose.valim@dashbit.co> | 2021-12-29 12:06:41 +0100 |
commit | 8af30017f2192133311d6d3ee9685316724b6147 (patch) | |
tree | a2802d9299d1d167edc9e57913ca53b716aedcfc | |
parent | fd135405a4b7d8aa17bac5bcebd41d7ea7e6ac90 (diff) | |
download | elixir-8af30017f2192133311d6d3ee9685316724b6147.tar.gz |
Do not raise when diffing unknown bindings in guards
-rw-r--r-- | lib/ex_unit/lib/ex_unit/diff.ex | 22 | ||||
-rw-r--r-- | lib/ex_unit/test/ex_unit/diff_test.exs | 2 |
2 files changed, 19 insertions, 5 deletions
diff --git a/lib/ex_unit/lib/ex_unit/diff.ex b/lib/ex_unit/lib/ex_unit/diff.ex index 8e1b952f1..fff5860de 100644 --- a/lib/ex_unit/lib/ex_unit/diff.ex +++ b/lib/ex_unit/lib/ex_unit/diff.ex @@ -192,7 +192,7 @@ defmodule ExUnit.Diff do {guard_clause, guard_equivalent?} = if diff_expression.equivalent? do bindings = Map.merge(post_env.pins, post_env.current_vars) - diff_guard_clause(clause, Map.to_list(bindings)) + diff_guard_clause(clause, bindings) else {clause, false} end @@ -223,12 +223,24 @@ defmodule ExUnit.Diff do defp diff_guard_clause(quoted, bindings) do expanded = Macro.prewalk(quoted, fn - {_, [{:expanded, expanded} | _], _} -> expanded - other -> other + {_, [{:expanded, expanded} | _], _} -> + expanded + + {var, _, context} = expr when is_atom(var) and is_atom(context) -> + if Map.has_key?(bindings, var_context(expr)) do + expr + else + throw(:abort) + end + + other -> + other end) - {equivalent?, _bindings} = Code.eval_quoted(expanded, bindings) + {equivalent?, _bindings} = Code.eval_quoted(expanded, Map.to_list(bindings)) {update_diff_meta(quoted, !equivalent?), equivalent?} + catch + :abort -> {quoted, false} end # Pins @@ -245,7 +257,7 @@ defmodule ExUnit.Diff do # Vars defp diff_var({name, meta, context} = left, right, env) do - identifier = {name, meta[:counter] || context} + identifier = var_context(left) case env.current_vars do %{^identifier => ^right} -> diff --git a/lib/ex_unit/test/ex_unit/diff_test.exs b/lib/ex_unit/test/ex_unit/diff_test.exs index 2bddf7061..b3bef01f9 100644 --- a/lib/ex_unit/test/ex_unit/diff_test.exs +++ b/lib/ex_unit/test/ex_unit/diff_test.exs @@ -961,6 +961,8 @@ defmodule ExUnit.DiffTest do refute_diff((x when x == 1 when x == 2) = 0, "x when -x == 1- when -x == 2-", "0") refute_diff((x when x in [1, 2]) = 0, "x when -x in [1, 2]-", "0") refute_diff(({:ok, x} when x == 1) = :error, "-{:ok, x}- when x == 1", "+:error+") + + refute_diff((x when x == z) = 0, "x when x == z", "0", z: 1) end test "charlists" do |