1 -module(hope_result_SUITE).
15 , t_hope_result_specs/1
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).
31 %% ============================================================================
32 %% Common Test callbacks
33 %% ============================================================================
36 [ {group, ?GROUP_PIPE}
37 , {group, ?GROUP_SPEC}
38 , {group, ?GROUP_LIFT}
39 , {group, ?GROUP_OTHER}
60 Properties = [parallel],
61 [ {?GROUP_PIPE, Properties, PipeTests}
62 , {?GROUP_SPEC, Properties, SpecTests}
63 , {?GROUP_LIFT, Properties, LiftTests}
64 , {?GROUP_OTHER, Properties, OtherTests}
67 init_per_group(?GROUP_PIPE, Cfg) ->
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
73 hope_kv_list:set(Cfg, steps, Steps);
74 init_per_group(_, Cfg) ->
77 end_per_group(_, _Cfg) ->
81 %% =============================================================================
83 %% =============================================================================
86 {some, Steps} = hope_kv_list:get(Cfg, steps),
87 {ok, 3} = hope_result:pipe(Steps, 0).
90 {some, Steps} = hope_kv_list:get(Cfg, steps),
91 {error, 1} = hope_result:pipe(Steps, 1).
93 t_hope_result_specs(_) ->
94 [] = proper:check_specs(hope_result).
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).
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),
118 {error, qux} = GError({}),
121 must_not_return = GOkThrows({})
122 catch throw:exn_from_ok_map ->
127 must_not_return = GErrorThrows({})
128 catch throw:exn_from_error_map ->
134 {ok, X} = hope_result:return(X).
139 F = fun (foo) -> Y end,
140 {ok, Y} = hope_result:map({ok, X}, F),
141 {error, X} = hope_result:map({error, X}, F).
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).
153 {ok , X } = hope_result:tag_error({ok , X}, Tag),
154 {error, {Tag, X}} = hope_result:tag_error({error, X}, Tag).