8789d3d8e021fba5569b8552a87f6ad36a399534
[hope.git] / test / hope_option_SUITE.erl
1 -module(hope_option_SUITE).
2
3 %% Callbacks
4 -export(
5 [ all/0
6 , groups/0
7 ]).
8
9 %% Test cases
10 -export(
11 [ t_of_result/1
12 , t_put/1
13 , t_get/1
14 , t_map/1
15 , t_iter/1
16 , t_pipe/1
17 ]).
18
19
20 -define(GROUP, option).
21
22
23 %% ============================================================================
24 %% Common Test callbacks
25 %% ============================================================================
26
27 all() ->
28 [ {group, ?GROUP}
29 ].
30
31 groups() ->
32 Tests =
33 [ t_of_result
34 , t_put
35 , t_get
36 , t_map
37 , t_iter
38 , t_pipe
39 ],
40 Properties = [parallel],
41 [ {?GROUP, Properties, Tests}
42 ].
43
44
45 %% =============================================================================
46 %% Test cases
47 %% =============================================================================
48
49 t_put(_Cfg) ->
50 IsFoo = fun (foo) -> true; (_) -> false end,
51 {some, foo} = hope_option:put(foo, IsFoo),
52 none = hope_option:put(bar, IsFoo).
53
54 t_get(_Cfg) ->
55 foo = hope_option:get({some, foo}, bar),
56 bar = hope_option:get(none , bar).
57
58 t_map(_Cfg) ->
59 FooToBar = fun (foo) -> bar end,
60 {some, bar} = hope_option:map({some, foo}, FooToBar),
61 none = hope_option:map(none , FooToBar).
62
63 t_iter(_Cfg) ->
64 Key = key,
65 Put = fun (Val) -> _ = put(Key, Val), ok end,
66 Get = fun () -> get(Key) end,
67 Val = foo,
68 ok = hope_option:iter(none , Put),
69 undefined = Get(),
70 ok = hope_option:iter({some, Val}, Put),
71 Val = Get().
72
73 t_of_result(_Cfg) ->
74 Foo = foo,
75 Bar = bar,
76 ResultOk = {ok, Foo},
77 ResultError = {error, Bar},
78 {some, Foo} = hope_option:of_result(ResultOk),
79 none = hope_option:of_result(ResultError).
80
81 t_pipe(_Cfg) ->
82 Steps =
83 [ fun (0) -> hope_option:return(1); (_) -> none end
84 , fun (1) -> hope_option:return(2); (_) -> none end
85 , fun (2) -> hope_option:return(3); (_) -> none end
86 ],
87 {some, 3} = hope_option:pipe(Steps, 0),
88 none = hope_option:pipe(Steps, 1),
89 none = hope_option:pipe(Steps, 2),
90 none = hope_option:pipe(Steps, 3).
This page took 0.040771 seconds and 3 git commands to generate.