From e56cf8d9846e51ae88fa72c8b0992d81da6fd7d5 Mon Sep 17 00:00:00 2001 From: Serge Aleynikov Date: Sat, 14 Feb 2015 17:19:17 -0500 Subject: Add ?assertReceive(Guard, Timeout) macro to eunit A convenience macro used for testing message passing logic by allowing to fail if a message matching a `Guard` is not received in the mailbox of the current process. --- lib/eunit/include/eunit.hrl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/eunit/include/eunit.hrl') diff --git a/lib/eunit/include/eunit.hrl b/lib/eunit/include/eunit.hrl index 53d291430d..8a829396ec 100644 --- a/lib/eunit/include/eunit.hrl +++ b/lib/eunit/include/eunit.hrl @@ -163,6 +163,26 @@ -define(_assertNot(BoolExpr), ?_assert(not (BoolExpr))). +%% This is mostly a convenience which gives more detailed reports. +%% Note: Guard is a guarded pattern, and can not be used for value. +-ifdef(NOASSERT). +-define(assertReceive(Guard, Timeout), ok). +-else. +-define(assertReceive(Guard, Timeout), + begin + ((fun () -> + receive (Guard) -> ok + after Timeout -> erlang:error({assertReceive_timedout, + [{module, ?MODULE}, + {line, ?LINE}, + {pattern, (??Guard)}, + {timeout, __V}]}) + end + end)()) + end). +-endif. +-define(_assertReceive(Guard, Timeout), ?_test(?assertReceive(Guard, Timeout))). + %% This is mostly a convenience which gives more detailed reports. %% Note: Guard is a guarded pattern, and can not be used for value. -ifdef(NOASSERT). -- cgit v1.2.3