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/erlcount-1.0/test | |
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/erlcount-1.0/test')
-rw-r--r-- | learn-you-some-erlang/erlcount-1.0/test/erlcount_tests.erl | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/learn-you-some-erlang/erlcount-1.0/test/erlcount_tests.erl b/learn-you-some-erlang/erlcount-1.0/test/erlcount_tests.erl new file mode 100644 index 0000000..7459a39 --- /dev/null +++ b/learn-you-some-erlang/erlcount-1.0/test/erlcount_tests.erl @@ -0,0 +1,35 @@ +-module(erlcount_tests). +-include_lib("eunit/include/eunit.hrl"). +-ifndef(TESTDIR). +%% Assumes we're running from the app's directory. We want to target the +%% 'learn-you-some-erlang' directory. +-define(TESTDIR, ".."). +-endif. + +%% NOTE: +%% Because we do not want the tests to be bound to a specific snapshot in time +%% of our app, we will instead compare it to an oracle built with unix +%% commands. Users running windows sadly won't be able to run these tests. + +%% We'll be forcing the design to be continuation-based when it comes to +%% reading files. This will require some explaining to the user, but will +%% allow to show how we can read files and schedule them at the same time, +%% but without breaking functional principles of referential transparency +%% and while allowing specialised functions to be written in a testable manner. +find_erl_test_() -> + ?_assertEqual(lists:sort(string:tokens(os:cmd("find "++?TESTDIR++" -name *.erl"), "\n")), + lists:sort(build_list(erlcount_lib:find_erl(?TESTDIR)))). + +build_list(Term) -> build_list(Term, []). + +build_list(done, List) -> List; +build_list({continue, Entry, Fun}, List) -> + build_list(Fun(), [Entry|List]). + +regex_count_test_() -> + [?_assertEqual(5, erlcount_lib:regex_count("a", "a a a a a")), + ?_assertEqual(0, erlcount_lib:regex_count("o", "a a a a a")), + ?_assertEqual(2, erlcount_lib:regex_count("a.*", "a a a\na a a")), + ?_assertEqual(3, erlcount_lib:regex_count("if", "myiffun() ->\n if 1 < \" if \" -> ok;\n true -> other\n end.\n")), + ?_assertEqual(1, erlcount_lib:regex_count("if[\\s]{1}(?:.+)->", "myiffun() ->\n if 1 < \" if \" -> ok;\n true -> other\n end.\n")), + ?_assertEqual(2, erlcount_lib:regex_count("if[\\s]{1}(?:.+)->", "myiffun() ->\n if 1 < \" if \" -> ok;\n true -> other\n end,\n if true -> ok end.\n"))]. |