Skip to content

Commit 6c02ebe

Browse files
author
José Valim
committed
Merge pull request #2720 from edgurgel/print-mailbox-on-failing-assert-receive
Print process mailbox on failing assert_receive
2 parents a08e44c + 60bcf1c commit 6c02ebe

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,30 @@ defmodule ExUnit.Assertions do
281281
unquote(expected) = received -> received
282282
after
283283
unquote(timeout) ->
284-
flunk unquote(message)
284+
flunk(unquote(message) <> mailbox(self))
285285
end
286286
end
287287

288288
{:receive, [{:export_head, true}|meta], args}
289289
end
290290

291+
@max_mailbox_length 10
292+
293+
@doc false
294+
def mailbox(pid) do
295+
{:messages, messages} = Process.info(pid, :messages)
296+
length = length(messages)
297+
mailbox = Enum.take(messages, @max_mailbox_length)
298+
|> Enum.map_join("\n", &inspect/1)
299+
". Process mailbox (#{length}):" <> mailbox_message(length, mailbox)
300+
end
301+
302+
defp mailbox_message(0, _mailbox), do: " []"
303+
defp mailbox_message(length, mailbox) when length > 10 do
304+
"\n" <> mailbox <> "\nShowing only #{@max_mailbox_length} first messages."
305+
end
306+
defp mailbox_message(_length, mailbox), do: "\n" <> mailbox
307+
291308
@doc """
292309
Asserts the `exception` is raised during `function` execution with
293310
the `expected_message`. Returns the rescued exception, fails otherwise.

lib/ex_unit/lib/ex_unit/formatter.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ defmodule ExUnit.Formatter do
171171
end
172172

173173
defp format_banner(value, formatter) do
174+
value = String.replace(value, "\n", "\n" <> @counter_padding)
174175
formatter.(:error_info, value)
175176
end
176177

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,32 @@ defmodule ExUnit.AssertionsTest do
8686
:hello = assert_received :hello
8787
end
8888

89-
test "assert received when different" do
89+
test "assert received when empty mailbox" do
9090
try do
9191
"This should never be tested" = assert_received :hello
9292
rescue
9393
error in [ExUnit.AssertionError] ->
94-
"No message matching :hello" = error.message
94+
"No message matching :hello. Process mailbox (0): []" = error.message
95+
end
96+
end
97+
98+
test "assert received when different message" do
99+
send self, {:message, :not_expected, :at_all}
100+
try do
101+
"This should never be tested" = assert_receive :hello, 5_000
102+
rescue
103+
error in [ExUnit.AssertionError] ->
104+
"No message matching :hello. Process mailbox (1):\n{:message, :not_expected, :at_all}" = error.message
105+
end
106+
end
107+
108+
test "assert received when different message having more than 10 on mailbox" do
109+
for i <- 1..11, do: send(self, {:message, i})
110+
try do
111+
"This should never be tested" = assert_receive :hello, 5_000
112+
rescue
113+
error in [ExUnit.AssertionError] ->
114+
"No message matching :hello. Process mailbox (11):\n{:message, 1}\n{:message, 2}\n{:message, 3}\n{:message, 4}\n{:message, 5}\n{:message, 6}\n{:message, 7}\n{:message, 8}\n{:message, 9}\n{:message, 10}\nShowing only 10 first messages." = error.message
95115
end
96116
end
97117

lib/ex_unit/test/ex_unit/formatter_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,15 @@ defmodule ExUnit.FormatterTest do
133133
6]
134134
"""
135135
end
136+
137+
test "formats assertions with message with multiple lines" do
138+
message = "Some meaningful error:\nuseful info\nanother useful info"
139+
failure = {:error, catch_assertion(assert(false, message)), []}
140+
assert format_test_case_failure(case(), failure, 1, :infinity, &formatter/2) =~ """
141+
1) Hello: failure on setup_all callback, tests invalidated
142+
Some meaningful error:
143+
useful info
144+
another useful info
145+
"""
146+
end
136147
end

0 commit comments

Comments
 (0)