Created
December 2, 2010 23:48
-
-
Save nruth/726335 to your computer and use it in GitHub Desktop.
basic test spy for plain Erlang, now promoted to a full repo @ https://github.com/nruth/nspy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module (auctioneer_tests). | |
-include_lib("eunit/include/eunit.hrl"). | |
check_auctioneer_announces_item_to_room_test() -> | |
AuctionRoom = nspy:new(), | |
Auctioneer = auctioneer:start("Penguin", 20, AuctionRoom), | |
timer:sleep(1000), | |
Auctioneer ! stop, | |
ExpectedMessage = {starting_auction, {lot, 1, 20, "Penguin"}, "Next up lot 1, a fine Penguin, starting at 20\n"}, | |
nspy:assert_message_received(AuctionRoom, ExpectedMessage). | |
check_the_auctioneer_halts_auction_after_given_time_test() -> | |
AuctionRoom = nspy:new(), | |
Auctioneer = auctioneer:start("Penguin", 20, AuctionRoom), | |
timer:sleep(1000), | |
Auctioneer ! stop, | |
ExpectedMessage = {ending_auction, 1, "Going once... Going twice... *bang*\n"}, | |
nspy:assert_message_received(AuctionRoom, ExpectedMessage). | |
check_the_auctioneer_stops_on_sale_test() -> | |
AuctionRoom = nspy:new(), | |
Auctioneer = auctioneer:start("Penguin", 20, AuctionRoom), | |
BidderSpy = nspy:new(), | |
Auctioneer ! {new_bid, 1, BidderSpy, 100}, | |
timer:sleep(1000), | |
Auctioneer ! stop, | |
Banter = lists:flatten(io_lib:format("Sold, to ~p for ~B!~n", [BidderSpy, 100])), | |
ExpectedMessage = {lot_sold, Banter}, | |
nspy:assert_message_received(AuctionRoom, ExpectedMessage). | |
test_the_auctioneer_halves_the_price_on_no_sale_test() -> | |
AuctionRoom = nspy:new(), | |
Auctioneer = auctioneer:start("Penguin", 20, AuctionRoom), | |
timer:sleep(1000), | |
Auctioneer ! stop, | |
ExpectedMessage = {starting_auction, {lot, 2, 10, "Penguin"}, "Next up lot 2, a fine Penguin, starting at 10\n"}, | |
nspy:assert_message_received(AuctionRoom, ExpectedMessage). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module (nspy). | |
-export ([new/0, spy/1, assert_message_received/2]). | |
-define (NODEBUG, true). | |
-include_lib("eunit/include/eunit.hrl"). | |
new() -> | |
spawn(nspy, spy, [[]]). | |
spy (Messages) -> | |
receive | |
{nspy_list_messages, ReplyTo} -> | |
ReplyTo ! {nspy_messages, Messages}, | |
?debugFmt("Node ~p requested message received list, sending: ~p~n", [ReplyTo, Messages]), | |
spy(Messages); | |
Message -> | |
?debugFmt("Spy ~p received message: ~p~n", [self(), Message]), | |
spy([Message | Messages]) | |
end. | |
assert_message_received(Spy, Expected) -> | |
Spy ! {nspy_list_messages, self()}, | |
receive | |
{nspy_messages, Messages} -> | |
io:format("~n[SPY] expected ~p received ~p~n", [Expected, Messages]), | |
MessageFound = lists:any(fun(Elem) -> Elem =:= Expected end, Messages), | |
?assert(MessageFound) | |
end. | |
message_received_test() -> | |
Spy = nspy:new(), | |
Spy ! hi, | |
Spy ! ho, | |
timer:sleep(200), | |
assert_message_received(Spy, hi), | |
assert_message_received(Spy, ho). | |
message_not_received_test() -> | |
Spy = nspy:new(), | |
Spy ! hi, | |
timer:sleep(200), | |
?assertError({assertion_failed, _}, assert_message_received(Spy, ho)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment