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