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 | |
f2e1fffc | 12 | , t_of_undefined/1 |
2dc71691 SK |
13 | , t_put/1 |
14 | , t_get/1 | |
15 | , t_map/1 | |
16 | , t_iter/1 | |
4af0774b | 17 | , t_pipe/1 |
352ddeb4 | 18 | , t_validate/1 |
2dc71691 SK |
19 | ]). |
20 | ||
21 | ||
22 | -define(GROUP, option). | |
23 | ||
24 | ||
25 | %% ============================================================================ | |
26 | %% Common Test callbacks | |
27 | %% ============================================================================ | |
28 | ||
29 | all() -> | |
30 | [ {group, ?GROUP} | |
31 | ]. | |
32 | ||
33 | groups() -> | |
34 | Tests = | |
35 | [ t_of_result | |
f2e1fffc | 36 | , t_of_undefined |
2dc71691 SK |
37 | , t_put |
38 | , t_get | |
39 | , t_map | |
40 | , t_iter | |
4af0774b | 41 | , t_pipe |
352ddeb4 | 42 | , t_validate |
2dc71691 SK |
43 | ], |
44 | Properties = [parallel], | |
45 | [ {?GROUP, Properties, Tests} | |
46 | ]. | |
47 | ||
48 | ||
49 | %% ============================================================================= | |
50 | %% Test cases | |
51 | %% ============================================================================= | |
52 | ||
53 | t_put(_Cfg) -> | |
54 | IsFoo = fun (foo) -> true; (_) -> false end, | |
55 | {some, foo} = hope_option:put(foo, IsFoo), | |
56 | none = hope_option:put(bar, IsFoo). | |
57 | ||
58 | t_get(_Cfg) -> | |
59 | foo = hope_option:get({some, foo}, bar), | |
60 | bar = hope_option:get(none , bar). | |
61 | ||
62 | t_map(_Cfg) -> | |
63 | FooToBar = fun (foo) -> bar end, | |
64 | {some, bar} = hope_option:map({some, foo}, FooToBar), | |
65 | none = hope_option:map(none , FooToBar). | |
66 | ||
67 | t_iter(_Cfg) -> | |
68 | Key = key, | |
4744fed9 | 69 | Put = fun (Val) -> put(Key, Val) end, |
2dc71691 SK |
70 | Get = fun () -> get(Key) end, |
71 | Val = foo, | |
4744fed9 | 72 | {} = hope_option:iter(none , Put), |
2dc71691 | 73 | undefined = Get(), |
4744fed9 | 74 | {} = hope_option:iter({some, Val}, Put), |
2dc71691 SK |
75 | Val = Get(). |
76 | ||
77 | t_of_result(_Cfg) -> | |
78 | Foo = foo, | |
79 | Bar = bar, | |
80 | ResultOk = {ok, Foo}, | |
81 | ResultError = {error, Bar}, | |
82 | {some, Foo} = hope_option:of_result(ResultOk), | |
83 | none = hope_option:of_result(ResultError). | |
4af0774b SK |
84 | |
85 | t_pipe(_Cfg) -> | |
86 | Steps = | |
87 | [ fun (0) -> hope_option:return(1); (_) -> none end | |
88 | , fun (1) -> hope_option:return(2); (_) -> none end | |
89 | , fun (2) -> hope_option:return(3); (_) -> none end | |
90 | ], | |
91 | {some, 3} = hope_option:pipe(Steps, 0), | |
92 | none = hope_option:pipe(Steps, 1), | |
93 | none = hope_option:pipe(Steps, 2), | |
94 | none = hope_option:pipe(Steps, 3). | |
f2e1fffc SK |
95 | |
96 | t_of_undefined(_Cfg) -> | |
97 | Foo = foo, | |
98 | Bar = bar, | |
99 | Baz = baz, | |
100 | {some, Foo} = hope_option:of_undefined(Foo), | |
101 | {some, Bar} = hope_option:of_undefined(Bar), | |
102 | {some, Baz} = hope_option:of_undefined(Baz), | |
103 | none = hope_option:of_undefined(undefined). | |
352ddeb4 SK |
104 | |
105 | t_validate(_Cfg) -> | |
106 | IsFoo = fun (X) -> X =:= foo end, | |
107 | none = hope_option:validate(none, IsFoo), | |
108 | none = hope_option:validate({some, bar}, IsFoo), | |
109 | {some, foo} = hope_option:validate({some, foo}, IsFoo). |