-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtypelib_tests.erl
52 lines (46 loc) · 2.53 KB
/
typelib_tests.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
%% @doc This is an eunit test suite. Run the tests using
%% eunit:test(typelib, [verbose]).
-module(typelib_tests).
-include_lib("eunit/include/eunit.hrl").
-include("typelib.hrl").
-define(_am(E, A), ?_assertMatch(E, A)).
reduce(F, Acc, Ty) ->
typelib:reduce_type(F, Acc, Ty).
reduce_test_() ->
F1 = fun (E, Acc) -> {E, Acc} end,
F2 = fun
({atom, _, _} = At, Acc) -> {At, [At | Acc]};
(Ty, Acc) -> {Ty, Acc}
end,
ComplexTy = typelib:parse_type("atom1 | atom2 | "
"{complex, integer(), [{atom() | string(), number()}]}"),
F3 = fun
({type, _, tuple, [{atom, _, complex} | _]}, Acc) -> { {type, 0, none, []}, Acc };
({atom, _, Name} = Ty, Acc) -> {Ty, [Name | Acc]};
(Ty, Acc) -> {Ty, Acc}
end,
[
?_am({{atom, _, _}, ok}, reduce(F1, ok, typelib:parse_type("my_atom"))),
?_am({{type, _, _, any}, ok}, reduce(F1, ok, {type, 0, fun_name, any})),
?_am({{integer, _, _}, ok}, reduce(F1, ok, typelib:parse_type("5"))),
?_am({{char, _, _}, ok}, reduce(F1, ok, typelib:parse_type("$a"))),
?_am({{type, _, any}, ok}, reduce(F1, ok, {type, 0, any})),
?_am({pos_inf, ok}, reduce(F1, ok, pos_inf)),
?_am({neg_inf, ok}, reduce(F1, ok, neg_inf)),
?_am({{var, _, _}, ok}, reduce(F1, ok, typelib:parse_type("A"))),
?_am({{atom, _, _}, ok}, reduce(F1, ok, typelib:parse_type("A :: {my_atom}"))),
?_am({{atom, _, _}, ok}, reduce(F1, ok, typelib:parse_type("{my_atom}"))),
?_am({{atom, _, _}, ok}, reduce(F1, ok, typelib:parse_type("remote:t(my_atom)"))),
?_am({{atom, _, _}, ok}, reduce(F1, ok, typelib:parse_type("t(my_atom)"))),
%% TODO: recursive types `{op, _1, _2, _3}' and `{op, _1, _2, _3, _4}' are not tested.
?_am({_, [{atom, _, my_atom}]}, reduce(F2, [], typelib:parse_type("A :: {my_atom}"))),
?_am({_, [{atom, _, my_atom}]}, reduce(F2, [], typelib:parse_type("my_atom"))),
?_am({_, [{atom, _, my_atom},
{atom, _, t},
{atom, _, remote}]}, reduce(F2, [], typelib:parse_type("remote:t(my_atom)"))),
?_am({_, [{atom, _, my_atom}]}, reduce(F2, [], typelib:parse_type("t(my_atom)"))),
?_am({_, [atom2, atom1]}, reduce(F3, [], ComplexTy))
].
pp_type_test_() ->
[?_am("ok" ++ _,
lists:flatten(io_lib:format("ok: ~s\n", [ ["ok", typelib:pp_type(typelib:parse_type("T"))] ])))].