Skip to content

Commit ab61c09

Browse files
lexmagJosé Valim
authored andcommitted
Fix compiler warnings in ExUnit.assert_receive/3
Closes #6429.
1 parent ccf939e commit ab61c09

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ defmodule ExUnit.Assertions do
109109
# is not nil nor false. We need to rewrite the if
110110
# to avoid silly warnings though.
111111
return =
112-
no_warning(quote do
112+
suppress_warning(quote do
113113
case right do
114114
x when x in [nil, false] ->
115115
raise ExUnit.AssertionError,
@@ -121,7 +121,7 @@ defmodule ExUnit.Assertions do
121121
end)
122122

123123
match_expr =
124-
no_warning(quote do
124+
suppress_warning(quote do
125125
case right do
126126
unquote(left) ->
127127
unquote(return)
@@ -401,6 +401,40 @@ defmodule ExUnit.Assertions do
401401
quote(do: unquote(left) = received)
402402
end
403403

404+
pattern_finder =
405+
quote do
406+
fn message ->
407+
unquote(suppress_warning(quote do
408+
case message do
409+
unquote(pattern) ->
410+
_ = unquote(vars)
411+
true
412+
413+
_ ->
414+
false
415+
end
416+
end))
417+
end
418+
end
419+
420+
failure_message_hit =
421+
failure_message || quote do
422+
"""
423+
Found message matching #{unquote(binary)} after #{timeout}ms.
424+
425+
This means the message was delivered too close to the timeout value, you may want to either:
426+
427+
1. Give an increased timeout to `assert_receive/2`
428+
2. Increase the default timeout to all `assert_receive` in your
429+
test_helper.exs by setting ExUnit.configure(assert_receive_timeout: ...)
430+
"""
431+
end
432+
433+
failure_message_miss =
434+
failure_message || quote do
435+
"No message matching #{unquote(binary)} after #{timeout}ms."
436+
end
437+
404438
quote do
405439
timeout = unquote(timeout)
406440

@@ -412,29 +446,10 @@ defmodule ExUnit.Assertions do
412446
timeout ->
413447
{:messages, messages} = Process.info(self(), :messages)
414448

415-
pattern_finder = fn message ->
416-
case message do
417-
unquote(pattern) ->
418-
_ = unquote(vars)
419-
true
420-
_ ->
421-
false
422-
end
423-
end
424-
425-
if Enum.any?(messages, pattern_finder) do
426-
flunk(unquote(failure_message) || """
427-
Found message matching #{unquote(binary)} after #{timeout}ms.
428-
429-
This means the message was delivered too close to the timeout value, you may want to either:
430-
431-
1. Give an increased timeout to `assert_receive/2`
432-
2. Increase the default timeout to all `assert_receive` in your
433-
test_helper.exs by setting ExUnit.configure(assert_receive_timeout: ...)
434-
""")
449+
if Enum.any?(messages, unquote(pattern_finder)) do
450+
flunk(unquote(failure_message_hit))
435451
else
436-
failure_message = unquote(failure_message) || "No message matching #{unquote(binary)} after #{timeout}ms."
437-
flunk(failure_message <>
452+
flunk(unquote(failure_message_miss) <>
438453
ExUnit.Assertions.__pins__(unquote(pins)) <>
439454
ExUnit.Assertions.__mailbox__(messages))
440455
end
@@ -515,7 +530,7 @@ defmodule ExUnit.Assertions do
515530
|> elem(1)
516531
end
517532

518-
defp no_warning({name, meta, [expr, [do: clauses]]}) do
533+
defp suppress_warning({name, meta, [expr, [do: clauses]]}) do
519534
clauses = Enum.map clauses, fn {:->, meta, args} ->
520535
{:->, [generated: true] ++ meta, args}
521536
end

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ defmodule ExUnit.AssertionsTest do
179179
:hello = assert_receive :hello
180180
end
181181

182+
test "assert receive accepts custom failure message" do
183+
send self(), :hello
184+
assert_receive message, 0, "failure message"
185+
:hello = message
186+
end
187+
182188
test "assert receive with message in mailbox after timeout, but before reading mailbox tells user to increase timeout" do
183189
parent = self()
184190
# This is testing a race condition, so it's not
@@ -297,7 +303,7 @@ defmodule ExUnit.AssertionsTest do
297303
end
298304
end
299305

300-
test "assert received leaks" do
306+
test "assert received binds variables" do
301307
send self(), {:hello, :world}
302308
assert_received {:hello, world}
303309
:world = world

0 commit comments

Comments
 (0)