Add hope_option:to_undefined/1
[hope.git] / test / hope_option_SUITE.erl
CommitLineData
2dc71691
SK
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
5fab324a 12 , t_undefined/1
2dc71691
SK
13 , t_put/1
14 , t_get/1
15 , t_map/1
16 , t_iter/1
4af0774b 17 , t_pipe/1
352ddeb4 18 , t_validate/1
2dc71691
SK
19 ]).
20
21
22-define(GROUP, option).
23
24
25%% ============================================================================
26%% Common Test callbacks
27%% ============================================================================
28
29all() ->
30 [ {group, ?GROUP}
31 ].
32
33groups() ->
34 Tests =
35 [ t_of_result
5fab324a 36 , t_undefined
2dc71691
SK
37 , t_put
38 , t_get
39 , t_map
40 , t_iter
4af0774b 41 , t_pipe
352ddeb4 42 , t_validate
2dc71691
SK
43 ],
44 Properties = [parallel],
45 [ {?GROUP, Properties, Tests}
46 ].
47
48
49%% =============================================================================
50%% Test cases
51%% =============================================================================
52
53t_put(_Cfg) ->
54 IsFoo = fun (foo) -> true; (_) -> false end,
55 {some, foo} = hope_option:put(foo, IsFoo),
56 none = hope_option:put(bar, IsFoo).
57
58t_get(_Cfg) ->
59 foo = hope_option:get({some, foo}, bar),
60 bar = hope_option:get(none , bar).
61
62t_map(_Cfg) ->
63 FooToBar = fun (foo) -> bar end,
64 {some, bar} = hope_option:map({some, foo}, FooToBar),
65 none = hope_option:map(none , FooToBar).
66
67t_iter(_Cfg) ->
68 Key = key,
4744fed9 69 Put = fun (Val) -> put(Key, Val) end,
2dc71691
SK
70 Get = fun () -> get(Key) end,
71 Val = foo,
4744fed9 72 {} = hope_option:iter(none , Put),
2dc71691 73 undefined = Get(),
4744fed9 74 {} = hope_option:iter({some, Val}, Put),
2dc71691
SK
75 Val = Get().
76
77t_of_result(_Cfg) ->
78 Foo = foo,
79 Bar = bar,
80 ResultOk = {ok, Foo},
81 ResultError = {error, Bar},
82 {some, Foo} = hope_option:of_result(ResultOk),
83 none = hope_option:of_result(ResultError).
4af0774b
SK
84
85t_pipe(_Cfg) ->
86 Steps =
87 [ fun (0) -> hope_option:return(1); (_) -> none end
88 , fun (1) -> hope_option:return(2); (_) -> none end
89 , fun (2) -> hope_option:return(3); (_) -> none end
90 ],
91 {some, 3} = hope_option:pipe(Steps, 0),
92 none = hope_option:pipe(Steps, 1),
93 none = hope_option:pipe(Steps, 2),
94 none = hope_option:pipe(Steps, 3).
f2e1fffc 95
5fab324a
SK
96t_undefined(_Cfg) ->
97 X = foo,
98 {some, X} = hope_option:of_undefined(X),
99 X = hope_option:to_undefined({some, X}),
100 none = hope_option:of_undefined(undefined),
101 undefined = hope_option:to_undefined(none).
352ddeb4
SK
102
103t_validate(_Cfg) ->
104 IsFoo = fun (X) -> X =:= foo end,
105 none = hope_option:validate(none, IsFoo),
106 none = hope_option:validate({some, bar}, IsFoo),
107 {some, foo} = hope_option:validate({some, foo}, IsFoo).
This page took 0.025639 seconds and 4 git commands to generate.