diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
commit | 5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch) | |
tree | 982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/tests/dolphins_tests.erl | |
parent | 05ae56e5e89abf2993f84e6d52b250131f247c35 (diff) | |
download | erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2 erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip |
wip
Diffstat (limited to 'learn-you-some-erlang/tests/dolphins_tests.erl')
-rw-r--r-- | learn-you-some-erlang/tests/dolphins_tests.erl | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/learn-you-some-erlang/tests/dolphins_tests.erl b/learn-you-some-erlang/tests/dolphins_tests.erl new file mode 100644 index 0000000..670f6a2 --- /dev/null +++ b/learn-you-some-erlang/tests/dolphins_tests.erl @@ -0,0 +1,87 @@ +-module(dolphins_tests). +-include_lib("eunit/include/eunit.hrl"). +%% sorry, this test library is a bit dirty, but it should do +%% the job. + +%% cannot test dolphin1/0 for lack of any results outside of I/O. +-test_warnings([dolphin1/0]). + +dolphin2_test_() -> + [?_assertEqual("How about no?", d2_do_a_flip()), + ?_assertEqual("So long and thanks for all the fish!", + d2_fish())]. + +d2_do_a_flip() -> + Pid = spawn(dolphins, dolphin2, []), + Pid ! Pid ! {self(), do_a_flip}, + %% this function receives a message and that's it + F = fun() -> + receive + M -> M + after 500 -> + error + end + end, + %% receive the first message + Msg1 = F(), + %% only one message should be received. If the second one + %% is anything except 'error' (no message), return an error. + %% otherwise send the message for validation. + case F() of + error -> Msg1; + _ -> error + end. + +d2_fish() -> + Pid = spawn(dolphins, dolphin2, []), + Pid ! Pid ! {self(), fish}, + %% this function receives a message and that's it + F = fun() -> + receive + M -> M + after 500 -> + error + end + end, + %% receive the first message + Msg1 = F(), + %% only one message should be received. If the second one + %% is anything except 'error' (no message), return an error. + %% otherwise send the message for validation. + case F() of + error -> Msg1; + _ -> error + end. + +dolphin3_test_() -> + [?_assertEqual(["How about no?", + "How about no?", + "So long and thanks for all the fish!"], + d3())]. + +d3() -> + Pid = spawn(dolphins, dolphin3, []), + Pid ! Pid ! {self(), do_a_flip}, % both should be received + Pid ! invalid, % should be ignored, but keep the process going + Pid ! {self(), fish}, % should terminate the process + Pid ! {self(), do_a_flip}, % should return nothing + %% this function receives a message and that's it + F = fun() -> + receive + M -> M + after 500 -> + error + end + end, + %% receive the expected messages + Msg1 = F(), + Msg2 = F(), + Msg3 = F(), + Msgs = [Msg1, Msg2, Msg3], + %% Additional messages should now fail. If a message is + %% received, add it to the list and let the test fail, + %% otherwise send the normal message list. + case F() of + error -> Msgs; + M -> Msgs ++ [M] + end. |