| 1 | -module(hope_result_SUITE). |
| 2 | |
| 3 | %% Callbacks |
| 4 | -export( |
| 5 | [ all/0 |
| 6 | , groups/0 |
| 7 | , init_per_group/2 |
| 8 | , end_per_group/2 |
| 9 | ]). |
| 10 | |
| 11 | %% Test cases |
| 12 | -export( |
| 13 | [ t_pipe_ok/1 |
| 14 | , t_pipe_error/1 |
| 15 | , t_hope_result_specs/1 |
| 16 | , t_lift_exn/1 |
| 17 | , t_lift_map_exn/1 |
| 18 | , t_return/1 |
| 19 | , t_map/1 |
| 20 | , t_map_error/1 |
| 21 | , t_tag_error/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 | , t_lift_map_exn |
| 53 | ], |
| 54 | OtherTests = |
| 55 | [ t_return |
| 56 | , t_map |
| 57 | , t_map_error |
| 58 | , t_tag_error |
| 59 | ], |
| 60 | Properties = [parallel], |
| 61 | [ {?GROUP_PIPE, Properties, PipeTests} |
| 62 | , {?GROUP_SPEC, Properties, SpecTests} |
| 63 | , {?GROUP_LIFT, Properties, LiftTests} |
| 64 | , {?GROUP_OTHER, Properties, OtherTests} |
| 65 | ]. |
| 66 | |
| 67 | init_per_group(?GROUP_PIPE, Cfg) -> |
| 68 | Steps = |
| 69 | [ fun (0) -> {ok, 1}; (X) -> {error, X} end |
| 70 | , fun (1) -> {ok, 2}; (X) -> {error, X} end |
| 71 | , fun (2) -> {ok, 3}; (X) -> {error, X} end |
| 72 | ], |
| 73 | hope_kv_list:set(Cfg, steps, Steps); |
| 74 | init_per_group(_, Cfg) -> |
| 75 | Cfg. |
| 76 | |
| 77 | end_per_group(_, _Cfg) -> |
| 78 | ok. |
| 79 | |
| 80 | |
| 81 | %% ============================================================================= |
| 82 | %% Test cases |
| 83 | %% ============================================================================= |
| 84 | |
| 85 | t_pipe_ok(Cfg) -> |
| 86 | {some, Steps} = hope_kv_list:get(Cfg, steps), |
| 87 | {ok, 3} = hope_result:pipe(Steps, 0). |
| 88 | |
| 89 | t_pipe_error(Cfg) -> |
| 90 | {some, Steps} = hope_kv_list:get(Cfg, steps), |
| 91 | {error, 1} = hope_result:pipe(Steps, 1). |
| 92 | |
| 93 | t_hope_result_specs(_) -> |
| 94 | [] = proper:check_specs(hope_result). |
| 95 | |
| 96 | t_lift_exn(_Cfg) -> |
| 97 | Class = throw, |
| 98 | Reason = foofoo, |
| 99 | Label = bar, |
| 100 | F = fun (ok) -> apply(erlang, Class, [Reason]) end, |
| 101 | G = hope_result:lift_exn(F), |
| 102 | H = hope_result:lift_exn(F, Label), |
| 103 | {error, {Class, Reason}} = G(ok), |
| 104 | {error, {Label, {Class, Reason}}} = H(ok). |
| 105 | |
| 106 | t_lift_map_exn(_Cfg) -> |
| 107 | FOk = fun ({}) -> foo end, |
| 108 | FExn = fun ({}) -> throw(baz) end, |
| 109 | MapOk = fun (foo) -> bar end, |
| 110 | MapOkThrows = fun (foo) -> throw(exn_from_ok_map) end, |
| 111 | MapError = fun ({throw, baz}) -> qux end, |
| 112 | MapErrorThrows = fun ({throw, baz}) -> throw(exn_from_error_map) end, |
| 113 | GOk = hope_result:lift_map_exn(FOk , MapOk , MapError), |
| 114 | GOkThrows = hope_result:lift_map_exn(FOk , MapOkThrows, MapError), |
| 115 | GError = hope_result:lift_map_exn(FExn, MapOk , MapError), |
| 116 | GErrorThrows = hope_result:lift_map_exn(FExn, MapOk , MapErrorThrows), |
| 117 | {ok, bar} = GOk({}), |
| 118 | {error, qux} = GError({}), |
| 119 | ok = |
| 120 | try |
| 121 | must_not_return = GOkThrows({}) |
| 122 | catch throw:exn_from_ok_map -> |
| 123 | ok |
| 124 | end, |
| 125 | ok = |
| 126 | try |
| 127 | must_not_return = GErrorThrows({}) |
| 128 | catch throw:exn_from_error_map -> |
| 129 | ok |
| 130 | end. |
| 131 | |
| 132 | t_return(_Cfg) -> |
| 133 | X = foo, |
| 134 | {ok, X} = hope_result:return(X). |
| 135 | |
| 136 | t_map(_Cfg) -> |
| 137 | X = foo, |
| 138 | Y = bar, |
| 139 | F = fun (foo) -> Y end, |
| 140 | {ok, Y} = hope_result:map({ok, X}, F), |
| 141 | {error, X} = hope_result:map({error, X}, F). |
| 142 | |
| 143 | t_map_error(_Cfg) -> |
| 144 | X = foo, |
| 145 | Y = bar, |
| 146 | XtoY = fun (foo) -> Y end, |
| 147 | {ok , X} = hope_result:map_error({ok , X}, XtoY), |
| 148 | {error, Y} = hope_result:map_error({error, X}, XtoY). |
| 149 | |
| 150 | t_tag_error(_Cfg) -> |
| 151 | X = foo, |
| 152 | Tag = bar, |
| 153 | {ok , X } = hope_result:tag_error({ok , X}, Tag), |
| 154 | {error, {Tag, X}} = hope_result:tag_error({error, X}, Tag). |