| 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_of_undefined/1 |
| 13 | , t_put/1 |
| 14 | , t_get/1 |
| 15 | , t_map/1 |
| 16 | , t_iter/1 |
| 17 | , t_pipe/1 |
| 18 | ]). |
| 19 | |
| 20 | |
| 21 | -define(GROUP, option). |
| 22 | |
| 23 | |
| 24 | %% ============================================================================ |
| 25 | %% Common Test callbacks |
| 26 | %% ============================================================================ |
| 27 | |
| 28 | all() -> |
| 29 | [ {group, ?GROUP} |
| 30 | ]. |
| 31 | |
| 32 | groups() -> |
| 33 | Tests = |
| 34 | [ t_of_result |
| 35 | , t_of_undefined |
| 36 | , t_put |
| 37 | , t_get |
| 38 | , t_map |
| 39 | , t_iter |
| 40 | , t_pipe |
| 41 | ], |
| 42 | Properties = [parallel], |
| 43 | [ {?GROUP, Properties, Tests} |
| 44 | ]. |
| 45 | |
| 46 | |
| 47 | %% ============================================================================= |
| 48 | %% Test cases |
| 49 | %% ============================================================================= |
| 50 | |
| 51 | t_put(_Cfg) -> |
| 52 | IsFoo = fun (foo) -> true; (_) -> false end, |
| 53 | {some, foo} = hope_option:put(foo, IsFoo), |
| 54 | none = hope_option:put(bar, IsFoo). |
| 55 | |
| 56 | t_get(_Cfg) -> |
| 57 | foo = hope_option:get({some, foo}, bar), |
| 58 | bar = hope_option:get(none , bar). |
| 59 | |
| 60 | t_map(_Cfg) -> |
| 61 | FooToBar = fun (foo) -> bar end, |
| 62 | {some, bar} = hope_option:map({some, foo}, FooToBar), |
| 63 | none = hope_option:map(none , FooToBar). |
| 64 | |
| 65 | t_iter(_Cfg) -> |
| 66 | Key = key, |
| 67 | Put = fun (Val) -> _ = put(Key, Val), ok end, |
| 68 | Get = fun () -> get(Key) end, |
| 69 | Val = foo, |
| 70 | ok = hope_option:iter(none , Put), |
| 71 | undefined = Get(), |
| 72 | ok = hope_option:iter({some, Val}, Put), |
| 73 | Val = Get(). |
| 74 | |
| 75 | t_of_result(_Cfg) -> |
| 76 | Foo = foo, |
| 77 | Bar = bar, |
| 78 | ResultOk = {ok, Foo}, |
| 79 | ResultError = {error, Bar}, |
| 80 | {some, Foo} = hope_option:of_result(ResultOk), |
| 81 | none = hope_option:of_result(ResultError). |
| 82 | |
| 83 | t_pipe(_Cfg) -> |
| 84 | Steps = |
| 85 | [ fun (0) -> hope_option:return(1); (_) -> none end |
| 86 | , fun (1) -> hope_option:return(2); (_) -> none end |
| 87 | , fun (2) -> hope_option:return(3); (_) -> none end |
| 88 | ], |
| 89 | {some, 3} = hope_option:pipe(Steps, 0), |
| 90 | none = hope_option:pipe(Steps, 1), |
| 91 | none = hope_option:pipe(Steps, 2), |
| 92 | none = hope_option:pipe(Steps, 3). |
| 93 | |
| 94 | t_of_undefined(_Cfg) -> |
| 95 | Foo = foo, |
| 96 | Bar = bar, |
| 97 | Baz = baz, |
| 98 | {some, Foo} = hope_option:of_undefined(Foo), |
| 99 | {some, Bar} = hope_option:of_undefined(Bar), |
| 100 | {some, Baz} = hope_option:of_undefined(Baz), |
| 101 | none = hope_option:of_undefined(undefined). |