@@ -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
0 commit comments