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