Commit | Line | Data |
---|---|---|
2dc71691 SK |
1 | -module(hope_option_SUITE). |
2 | ||
3 | %% Callbacks | |
4 | -export( | |
5 | [ all/0 | |
6 | , groups/0 | |
7 | ]). | |
8 | ||
9 | %% Test cases | |
10 | -export( | |
11 | [ t_of_result/1 | |
12 | , t_put/1 | |
13 | , t_get/1 | |
14 | , t_map/1 | |
15 | , t_iter/1 | |
16 | ]). | |
17 | ||
18 | ||
19 | -define(GROUP, option). | |
20 | ||
21 | ||
22 | %% ============================================================================ | |
23 | %% Common Test callbacks | |
24 | %% ============================================================================ | |
25 | ||
26 | all() -> | |
27 | [ {group, ?GROUP} | |
28 | ]. | |
29 | ||
30 | groups() -> | |
31 | Tests = | |
32 | [ t_of_result | |
33 | , t_put | |
34 | , t_get | |
35 | , t_map | |
36 | , t_iter | |
37 | ], | |
38 | Properties = [parallel], | |
39 | [ {?GROUP, Properties, Tests} | |
40 | ]. | |
41 | ||
42 | ||
43 | %% ============================================================================= | |
44 | %% Test cases | |
45 | %% ============================================================================= | |
46 | ||
47 | t_put(_Cfg) -> | |
48 | IsFoo = fun (foo) -> true; (_) -> false end, | |
49 | {some, foo} = hope_option:put(foo, IsFoo), | |
50 | none = hope_option:put(bar, IsFoo). | |
51 | ||
52 | t_get(_Cfg) -> | |
53 | foo = hope_option:get({some, foo}, bar), | |
54 | bar = hope_option:get(none , bar). | |
55 | ||
56 | t_map(_Cfg) -> | |
57 | FooToBar = fun (foo) -> bar end, | |
58 | {some, bar} = hope_option:map({some, foo}, FooToBar), | |
59 | none = hope_option:map(none , FooToBar). | |
60 | ||
61 | t_iter(_Cfg) -> | |
62 | Key = key, | |
63 | Put = fun (Val) -> _ = put(Key, Val), ok end, | |
64 | Get = fun () -> get(Key) end, | |
65 | Val = foo, | |
66 | ok = hope_option:iter(none , Put), | |
67 | undefined = Get(), | |
68 | ok = hope_option:iter({some, Val}, Put), | |
69 | Val = Get(). | |
70 | ||
71 | t_of_result(_Cfg) -> | |
72 | Foo = foo, | |
73 | Bar = bar, | |
74 | ResultOk = {ok, Foo}, | |
75 | ResultError = {error, Bar}, | |
76 | {some, Foo} = hope_option:of_result(ResultOk), | |
77 | none = hope_option:of_result(ResultError). |