Implement hope_result:(map_error and tag_error)
[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
4af0774b
SK
17 , t_return/1
18 , t_map/1
a80ca0b2
SK
19 , t_map_error/1
20 , t_tag_error/1
7e9d7f2c
SK
21 ]).
22
23
24-define(GROUP_PIPE, result_pipe).
8857d0f0 25-define(GROUP_SPEC, result_spec).
371e6333 26-define(GROUP_LIFT, result_lift_exn).
4af0774b 27-define(GROUP_OTHER, result_other).
7e9d7f2c
SK
28
29
30%% ============================================================================
31%% Common Test callbacks
32%% ============================================================================
33
34all() ->
8857d0f0
SK
35 [ {group, ?GROUP_PIPE}
36 , {group, ?GROUP_SPEC}
371e6333 37 , {group, ?GROUP_LIFT}
4af0774b 38 , {group, ?GROUP_OTHER}
8857d0f0 39 ].
7e9d7f2c
SK
40
41groups() ->
8857d0f0 42 PipeTests =
126c8880
SK
43 [ t_pipe_ok
44 , t_pipe_error
8857d0f0
SK
45 ],
46 SpecTests =
47 [ t_hope_result_specs
7e9d7f2c 48 ],
371e6333
SK
49 LiftTests =
50 [ t_lift_exn
51 ],
4af0774b
SK
52 OtherTests =
53 [ t_return
54 , t_map
a80ca0b2
SK
55 , t_map_error
56 , t_tag_error
4af0774b 57 ],
8bbf6f4d 58 Properties = [parallel],
8857d0f0
SK
59 [ {?GROUP_PIPE, Properties, PipeTests}
60 , {?GROUP_SPEC, Properties, SpecTests}
371e6333 61 , {?GROUP_LIFT, Properties, LiftTests}
4af0774b 62 , {?GROUP_OTHER, Properties, OtherTests}
8857d0f0 63 ].
7e9d7f2c 64
126c8880 65init_per_group(?GROUP_PIPE, Cfg) ->
7e9d7f2c 66 Steps =
55182c1b
SK
67 [ fun (0) -> {ok, 1}; (X) -> {error, X} end
68 , fun (1) -> {ok, 2}; (X) -> {error, X} end
69 , fun (2) -> {ok, 3}; (X) -> {error, X} end
7e9d7f2c 70 ],
4af0774b
SK
71 hope_kv_list:set(Cfg, steps, Steps);
72init_per_group(_, Cfg) ->
73 Cfg.
126c8880 74
4af0774b 75end_per_group(_, _Cfg) ->
126c8880
SK
76 ok.
77
78
79%% =============================================================================
80%% Test cases
81%% =============================================================================
82
83t_pipe_ok(Cfg) ->
84 {some, Steps} = hope_kv_list:get(Cfg, steps),
55182c1b 85 {ok, 3} = hope_result:pipe(Steps, 0).
126c8880
SK
86
87t_pipe_error(Cfg) ->
88 {some, Steps} = hope_kv_list:get(Cfg, steps),
89 {error, 1} = hope_result:pipe(Steps, 1).
8bbf6f4d
PO
90
91t_hope_result_specs(_) ->
92 [] = proper:check_specs(hope_result).
371e6333
SK
93
94t_lift_exn(_Cfg) ->
95 Class = throw,
96 Reason = foofoo,
b79afea0 97 Label = bar,
371e6333
SK
98 F = fun (ok) -> apply(erlang, Class, [Reason]) end,
99 G = hope_result:lift_exn(F),
b79afea0
SK
100 H = hope_result:lift_exn(F, Label),
101 {error, {Class, Reason}} = G(ok),
102 {error, {Label, {Class, Reason}}} = H(ok).
4af0774b
SK
103
104t_return(_Cfg) ->
105 X = foo,
106 {ok, X} = hope_result:return(X).
107
108t_map(_Cfg) ->
109 X = foo,
110 Y = bar,
111 F = fun (foo) -> Y end,
112 {ok, Y} = hope_result:map({ok, X}, F),
113 {error, X} = hope_result:map({error, X}, F).
a80ca0b2
SK
114
115t_map_error(_Cfg) ->
116 X = foo,
117 Y = bar,
118 XtoY = fun (foo) -> Y end,
119 {ok , X} = hope_result:map_error({ok , X}, XtoY),
120 {error, Y} = hope_result:map_error({error, X}, XtoY).
121
122t_tag_error(_Cfg) ->
123 X = foo,
124 Tag = bar,
125 {ok , X } = hope_result:tag_error({ok , X}, Tag),
126 {error, {Tag, X}} = hope_result:tag_error({error, X}, Tag).
This page took 0.045394 seconds and 4 git commands to generate.