Skip to content

Commit a6723dc

Browse files
ericentinJosé Valim
authored andcommitted
Fix assertion errors with more than 1 pinned var
Signed-off-by: José Valim <[email protected]>
1 parent e878dde commit a6723dc

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,17 @@ defmodule ExUnit.Assertions do
353353

354354
defp pinned_vars([]), do: ""
355355
defp pinned_vars(pins) do
356-
content = Enum.map(pins, fn(var) ->
356+
content = Enum.reverse(pins)
357+
|> Enum.uniq_by(&elem(&1, 0))
358+
|> Enum.map(fn(var) ->
357359
binary = Macro.to_string(var)
358360
quote do
359361
"\n " <> unquote(binary) <> " = " <> inspect(unquote(var))
360362
end
361363
end)
362364

363365
quote do
364-
"\nThe following variables were pinned:" <> unquote_splicing(content)
366+
<<"\nThe following variables were pinned:", unquote_splicing(content)>>
365367
end
366368
end
367369

@@ -384,7 +386,7 @@ defmodule ExUnit.Assertions do
384386
assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
385387
1 + "test"
386388
end
387-
389+
388390
assert_raise RuntimeError, ~r/^Today's lucky number is 0\.\d+!$/, fn ->
389391
raise "Today's lucky number is #{:random.uniform}!"
390392
end

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,46 @@ defmodule ExUnit.AssertionsTest do
127127
end
128128
end
129129

130+
test "assert received with multiple identical pinned variables" do
131+
status = :valid
132+
send self(), {:status, :invalid, :invalid}
133+
try do
134+
"This should never be tested" = assert_received {
135+
:status,
136+
^status,
137+
^status
138+
}
139+
rescue
140+
error in [ExUnit.AssertionError] ->
141+
"No message matching {:status, ^status, ^status} after 0ms.\n" <>
142+
"The following variables were pinned:\n" <>
143+
" status = :valid\n" <>
144+
"Process mailbox:\n" <>
145+
" {:status, :invalid, :invalid}" = error.message
146+
end
147+
end
148+
149+
test "assert received with multiple unique pinned variables" do
150+
status = :valid
151+
other_status = :invalid
152+
send self(), {:status, :invalid, :invalid}
153+
try do
154+
"This should never be tested" = assert_received {
155+
:status,
156+
^status,
157+
^other_status
158+
}
159+
rescue
160+
error in [ExUnit.AssertionError] ->
161+
"No message matching {:status, ^status, ^other_status} after 0ms.\n" <>
162+
"The following variables were pinned:\n" <>
163+
" status = :valid\n" <>
164+
" other_status = :invalid\n" <>
165+
"Process mailbox:\n" <>
166+
" {:status, :invalid, :invalid}" = error.message
167+
end
168+
end
169+
130170
test "assert received when empty mailbox" do
131171
try do
132172
"This should never be tested" = assert_received :hello

0 commit comments

Comments
 (0)