| 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 | , t_return/1 |
| 21 | , t_map/1 |
| 22 | ]). |
| 23 | |
| 24 | |
| 25 | -define(GROUP_PIPE, result_pipe). |
| 26 | -define(GROUP_SPEC, result_spec). |
| 27 | -define(GROUP_LIFT, result_lift_exn). |
| 28 | -define(GROUP_OTHER, result_other). |
| 29 | |
| 30 | |
| 31 | %% ============================================================================ |
| 32 | %% Common Test callbacks |
| 33 | %% ============================================================================ |
| 34 | |
| 35 | all() -> |
| 36 | [ {group, ?GROUP_PIPE} |
| 37 | , {group, ?GROUP_SPEC} |
| 38 | , {group, ?GROUP_LIFT} |
| 39 | , {group, ?GROUP_OTHER} |
| 40 | ]. |
| 41 | |
| 42 | groups() -> |
| 43 | PipeTests = |
| 44 | [ t_pipe_ok |
| 45 | , t_pipe_error |
| 46 | ], |
| 47 | SpecTests = |
| 48 | [ t_hope_result_specs |
| 49 | ], |
| 50 | LiftTests = |
| 51 | [ t_lift_exn |
| 52 | ], |
| 53 | OtherTests = |
| 54 | [ t_return |
| 55 | , t_map |
| 56 | ], |
| 57 | Properties = [parallel], |
| 58 | [ {?GROUP_PIPE, Properties, PipeTests} |
| 59 | , {?GROUP_SPEC, Properties, SpecTests} |
| 60 | , {?GROUP_LIFT, Properties, LiftTests} |
| 61 | , {?GROUP_OTHER, Properties, OtherTests} |
| 62 | ]. |
| 63 | |
| 64 | init_per_group(?GROUP_PIPE, Cfg) -> |
| 65 | Steps = |
| 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 |
| 69 | ], |
| 70 | hope_kv_list:set(Cfg, steps, Steps); |
| 71 | init_per_group(_, Cfg) -> |
| 72 | Cfg. |
| 73 | |
| 74 | end_per_group(_, _Cfg) -> |
| 75 | ok. |
| 76 | |
| 77 | |
| 78 | %% ============================================================================= |
| 79 | %% Test cases |
| 80 | %% ============================================================================= |
| 81 | |
| 82 | t_pipe_ok(Cfg) -> |
| 83 | {some, Steps} = hope_kv_list:get(Cfg, steps), |
| 84 | {ok, 3} = hope_result:pipe(Steps, 0). |
| 85 | |
| 86 | t_pipe_error(Cfg) -> |
| 87 | {some, Steps} = hope_kv_list:get(Cfg, steps), |
| 88 | {error, 1} = hope_result:pipe(Steps, 1). |
| 89 | |
| 90 | t_hope_result_specs(_) -> |
| 91 | [] = proper:check_specs(hope_result). |
| 92 | |
| 93 | t_lift_exn(_Cfg) -> |
| 94 | Class = throw, |
| 95 | Reason = foofoo, |
| 96 | Label = bar, |
| 97 | F = fun (ok) -> apply(erlang, Class, [Reason]) end, |
| 98 | G = hope_result:lift_exn(F), |
| 99 | H = hope_result:lift_exn(F, Label), |
| 100 | {error, {Class, Reason}} = G(ok), |
| 101 | {error, {Label, {Class, Reason}}} = H(ok). |
| 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). |