Implement hope_result:lift_map_exn/3
[hope.git] / test / hope_result_SUITE.erl
CommitLineData
7e9d7f2c
SK
1-module(hope_result_SUITE).
2
3%% Callbacks
4-export(
5 [ all/0
6 , groups/0
126c8880
SK
7 , init_per_group/2
8 , end_per_group/2
7e9d7f2c
SK
9 ]).
10
11%% Test cases
12-export(
126c8880
SK
13 [ t_pipe_ok/1
14 , t_pipe_error/1
8bbf6f4d 15 , t_hope_result_specs/1
371e6333 16 , t_lift_exn/1
64617423 17 , t_lift_map_exn/1
4af0774b
SK
18 , t_return/1
19 , t_map/1
a80ca0b2
SK
20 , t_map_error/1
21 , t_tag_error/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
35all() ->
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
42groups() ->
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
64617423 52 , t_lift_map_exn
371e6333 53 ],
4af0774b
SK
54 OtherTests =
55 [ t_return
56 , t_map
a80ca0b2
SK
57 , t_map_error
58 , t_tag_error
4af0774b 59 ],
8bbf6f4d 60 Properties = [parallel],
8857d0f0
SK
61 [ {?GROUP_PIPE, Properties, PipeTests}
62 , {?GROUP_SPEC, Properties, SpecTests}
371e6333 63 , {?GROUP_LIFT, Properties, LiftTests}
4af0774b 64 , {?GROUP_OTHER, Properties, OtherTests}
8857d0f0 65 ].
7e9d7f2c 66
126c8880 67init_per_group(?GROUP_PIPE, Cfg) ->
7e9d7f2c 68 Steps =
55182c1b
SK
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
7e9d7f2c 72 ],
4af0774b
SK
73 hope_kv_list:set(Cfg, steps, Steps);
74init_per_group(_, Cfg) ->
75 Cfg.
126c8880 76
4af0774b 77end_per_group(_, _Cfg) ->
126c8880
SK
78 ok.
79
80
81%% =============================================================================
82%% Test cases
83%% =============================================================================
84
85t_pipe_ok(Cfg) ->
86 {some, Steps} = hope_kv_list:get(Cfg, steps),
55182c1b 87 {ok, 3} = hope_result:pipe(Steps, 0).
126c8880
SK
88
89t_pipe_error(Cfg) ->
90 {some, Steps} = hope_kv_list:get(Cfg, steps),
91 {error, 1} = hope_result:pipe(Steps, 1).
8bbf6f4d
PO
92
93t_hope_result_specs(_) ->
94 [] = proper:check_specs(hope_result).
371e6333
SK
95
96t_lift_exn(_Cfg) ->
97 Class = throw,
98 Reason = foofoo,
b79afea0 99 Label = bar,
371e6333
SK
100 F = fun (ok) -> apply(erlang, Class, [Reason]) end,
101 G = hope_result:lift_exn(F),
b79afea0
SK
102 H = hope_result:lift_exn(F, Label),
103 {error, {Class, Reason}} = G(ok),
104 {error, {Label, {Class, Reason}}} = H(ok).
4af0774b 105
64617423
SK
106t_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
4af0774b
SK
132t_return(_Cfg) ->
133 X = foo,
134 {ok, X} = hope_result:return(X).
135
136t_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).
a80ca0b2
SK
142
143t_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
150t_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).
This page took 0.034409 seconds and 4 git commands to generate.