| 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_undefined/1 |
| 13 | , t_put/1 |
| 14 | , t_get/1 |
| 15 | , t_map/1 |
| 16 | , t_iter/1 |
| 17 | , t_pipe/1 |
| 18 | , t_validate/1 |
| 19 | ]). |
| 20 | |
| 21 | |
| 22 | -define(GROUP, option). |
| 23 | |
| 24 | |
| 25 | %% ============================================================================ |
| 26 | %% Common Test callbacks |
| 27 | %% ============================================================================ |
| 28 | |
| 29 | all() -> |
| 30 | [ {group, ?GROUP} |
| 31 | ]. |
| 32 | |
| 33 | groups() -> |
| 34 | Tests = |
| 35 | [ t_of_result |
| 36 | , t_undefined |
| 37 | , t_put |
| 38 | , t_get |
| 39 | , t_map |
| 40 | , t_iter |
| 41 | , t_pipe |
| 42 | , t_validate |
| 43 | ], |
| 44 | Properties = [parallel], |
| 45 | [ {?GROUP, Properties, Tests} |
| 46 | ]. |
| 47 | |
| 48 | |
| 49 | %% ============================================================================= |
| 50 | %% Test cases |
| 51 | %% ============================================================================= |
| 52 | |
| 53 | t_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 | |
| 58 | t_get(_Cfg) -> |
| 59 | foo = hope_option:get({some, foo}, bar), |
| 60 | bar = hope_option:get(none , bar). |
| 61 | |
| 62 | t_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 | |
| 67 | t_iter(_Cfg) -> |
| 68 | Key = key, |
| 69 | Put = fun (Val) -> put(Key, Val) end, |
| 70 | Get = fun () -> get(Key) end, |
| 71 | Val = foo, |
| 72 | {} = hope_option:iter(none , Put), |
| 73 | undefined = Get(), |
| 74 | {} = hope_option:iter({some, Val}, Put), |
| 75 | Val = Get(). |
| 76 | |
| 77 | t_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). |
| 84 | |
| 85 | t_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). |
| 95 | |
| 96 | t_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). |
| 102 | |
| 103 | t_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). |