Skip to content

Commit 6085ed0

Browse files
committed
Do not diff guards if pattern does not match
1 parent 57be943 commit 6085ed0

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/ex_unit/lib/ex_unit/diff.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,18 @@ defmodule ExUnit.Diff do
180180
defp diff_guard({:when, _, [expression, clause]}, right, env) do
181181
{diff_expression, post_env} = diff_quoted(expression, right, env)
182182

183-
bindings = Map.merge(post_env.pins, post_env.current_vars)
184-
{diff_clause, clause_equivalent?} = diff_guard_clause(clause, Map.to_list(bindings))
183+
{guard_clause, guard_equivalent?} =
184+
if diff_expression.equivalent? do
185+
bindings = Map.merge(post_env.pins, post_env.current_vars)
186+
diff_guard_clause(clause, Map.to_list(bindings))
187+
else
188+
{clause, false}
189+
end
185190

186191
diff = %__MODULE__{
187192
diff_expression
188-
| left: {:when, [], [diff_expression.left, diff_clause]},
189-
equivalent?: diff_expression.equivalent? and clause_equivalent?
193+
| left: {:when, [], [diff_expression.left, guard_clause]},
194+
equivalent?: guard_equivalent?
190195
}
191196

192197
{diff, post_env}

lib/ex_unit/test/ex_unit/diff_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ defmodule ExUnit.DiffTest do
914914
refute_diff((x when x == 1 or x == 2) = 0, "x when -x == 1- or -x == 2-", "0")
915915
refute_diff((x when x == 1 when x == 2) = 0, "x when -x == 1- when -x == 2-", "0")
916916
refute_diff((x when x in [1, 2]) = 0, "x when -x in [1, 2]-", "0")
917+
refute_diff(({:ok, x} when x == 1) = :error, "-{:ok, x}- when x == 1", "+:error+")
917918
end
918919

919920
test "charlists" do

0 commit comments

Comments
 (0)