Commit | Line | Data |
---|---|---|
7e9d7f2c SK |
1 | -module(hope_result_SUITE). |
2 | ||
8857d0f0 | 3 | %% TODO: Import only what is used. |
8bbf6f4d PO |
4 | -include_lib("proper/include/proper.hrl"). |
5 | ||
7e9d7f2c SK |
6 | %% Callbacks |
7 | -export( | |
8 | [ all/0 | |
9 | , groups/0 | |
126c8880 SK |
10 | , init_per_group/2 |
11 | , end_per_group/2 | |
7e9d7f2c SK |
12 | ]). |
13 | ||
14 | %% Test cases | |
15 | -export( | |
126c8880 SK |
16 | [ t_pipe_ok/1 |
17 | , t_pipe_error/1 | |
8bbf6f4d | 18 | , t_hope_result_specs/1 |
371e6333 | 19 | , t_lift_exn/1 |
4af0774b SK |
20 | , t_return/1 |
21 | , t_map/1 | |
7e9d7f2c SK |
22 | ]). |
23 | ||
24 | ||
25 | -define(GROUP_PIPE, result_pipe). | |
8857d0f0 | 26 | -define(GROUP_SPEC, result_spec). |
371e6333 | 27 | -define(GROUP_LIFT, result_lift_exn). |
4af0774b | 28 | -define(GROUP_OTHER, result_other). |
7e9d7f2c SK |
29 | |
30 | ||
31 | %% ============================================================================ | |
32 | %% Common Test callbacks | |
33 | %% ============================================================================ | |
34 | ||
35 | all() -> | |
8857d0f0 SK |
36 | [ {group, ?GROUP_PIPE} |
37 | , {group, ?GROUP_SPEC} | |
371e6333 | 38 | , {group, ?GROUP_LIFT} |
4af0774b | 39 | , {group, ?GROUP_OTHER} |
8857d0f0 | 40 | ]. |
7e9d7f2c SK |
41 | |
42 | groups() -> | |
8857d0f0 | 43 | PipeTests = |
126c8880 SK |
44 | [ t_pipe_ok |
45 | , t_pipe_error | |
8857d0f0 SK |
46 | ], |
47 | SpecTests = | |
48 | [ t_hope_result_specs | |
7e9d7f2c | 49 | ], |
371e6333 SK |
50 | LiftTests = |
51 | [ t_lift_exn | |
52 | ], | |
4af0774b SK |
53 | OtherTests = |
54 | [ t_return | |
55 | , t_map | |
56 | ], | |
8bbf6f4d | 57 | Properties = [parallel], |
8857d0f0 SK |
58 | [ {?GROUP_PIPE, Properties, PipeTests} |
59 | , {?GROUP_SPEC, Properties, SpecTests} | |
371e6333 | 60 | , {?GROUP_LIFT, Properties, LiftTests} |
4af0774b | 61 | , {?GROUP_OTHER, Properties, OtherTests} |
8857d0f0 | 62 | ]. |
7e9d7f2c | 63 | |
126c8880 | 64 | init_per_group(?GROUP_PIPE, Cfg) -> |
7e9d7f2c | 65 | Steps = |
55182c1b SK |
66 | [ fun (0) -> {ok, 1}; (X) -> {error, X} end |
67 | , fun (1) -> {ok, 2}; (X) -> {error, X} end | |
68 | , fun (2) -> {ok, 3}; (X) -> {error, X} end | |
7e9d7f2c | 69 | ], |
4af0774b SK |
70 | hope_kv_list:set(Cfg, steps, Steps); |
71 | init_per_group(_, Cfg) -> | |
72 | Cfg. | |
126c8880 | 73 | |
4af0774b | 74 | end_per_group(_, _Cfg) -> |
126c8880 SK |
75 | ok. |
76 | ||
77 | ||
78 | %% ============================================================================= | |
79 | %% Test cases | |
80 | %% ============================================================================= | |
81 | ||
82 | t_pipe_ok(Cfg) -> | |
83 | {some, Steps} = hope_kv_list:get(Cfg, steps), | |
55182c1b | 84 | {ok, 3} = hope_result:pipe(Steps, 0). |
126c8880 SK |
85 | |
86 | t_pipe_error(Cfg) -> | |
87 | {some, Steps} = hope_kv_list:get(Cfg, steps), | |
88 | {error, 1} = hope_result:pipe(Steps, 1). | |
8bbf6f4d PO |
89 | |
90 | t_hope_result_specs(_) -> | |
91 | [] = proper:check_specs(hope_result). | |
371e6333 SK |
92 | |
93 | t_lift_exn(_Cfg) -> | |
94 | Class = throw, | |
95 | Reason = foofoo, | |
b79afea0 | 96 | Label = bar, |
371e6333 SK |
97 | F = fun (ok) -> apply(erlang, Class, [Reason]) end, |
98 | G = hope_result:lift_exn(F), | |
b79afea0 SK |
99 | H = hope_result:lift_exn(F, Label), |
100 | {error, {Class, Reason}} = G(ok), | |
101 | {error, {Label, {Class, Reason}}} = H(ok). | |
4af0774b SK |
102 | |
103 | t_return(_Cfg) -> | |
104 | X = foo, | |
105 | {ok, X} = hope_result:return(X). | |
106 | ||
107 | t_map(_Cfg) -> | |
108 | X = foo, | |
109 | Y = bar, | |
110 | F = fun (foo) -> Y end, | |
111 | {ok, Y} = hope_result:map({ok, X}, F), | |
112 | {error, X} = hope_result:map({error, X}, F). |