| 1 | -module(hope_dictionary_SUITE). |
| 2 | |
| 3 | -include_lib("proper/include/proper.hrl"). |
| 4 | |
| 5 | %% Callbacks |
| 6 | -export( |
| 7 | [ all/0 |
| 8 | , groups/0 |
| 9 | , init_per_group/2 |
| 10 | , end_per_group/2 |
| 11 | ]). |
| 12 | |
| 13 | %% Test cases |
| 14 | -export( |
| 15 | [ t_set_new/1 |
| 16 | , t_set_existing/1 |
| 17 | , t_pop/1 |
| 18 | , t_fold/1 |
| 19 | , t_dictionary_specs/1 |
| 20 | ]). |
| 21 | |
| 22 | |
| 23 | -define(DICT_MODULE , dict_module). |
| 24 | -define(DICT_MODULE_KV_LIST , hope_kv_list). |
| 25 | |
| 26 | |
| 27 | %% ============================================================================ |
| 28 | %% Common Test callbacks |
| 29 | %% ============================================================================ |
| 30 | |
| 31 | all() -> |
| 32 | [{group, ?DICT_MODULE_KV_LIST}]. |
| 33 | |
| 34 | groups() -> |
| 35 | Tests = |
| 36 | [ t_set_new |
| 37 | , t_set_existing |
| 38 | , t_pop |
| 39 | , t_fold |
| 40 | , t_dictionary_specs |
| 41 | ], |
| 42 | Properties = [parallel], |
| 43 | [{?DICT_MODULE_KV_LIST, Properties, Tests}]. |
| 44 | |
| 45 | init_per_group(DictModule, Cfg) -> |
| 46 | hope_kv_list:set(Cfg, ?DICT_MODULE, DictModule). |
| 47 | |
| 48 | end_per_group(_DictModule, _Cfg) -> |
| 49 | ok. |
| 50 | |
| 51 | |
| 52 | %% ============================================================================= |
| 53 | %% Test cases |
| 54 | %% ============================================================================= |
| 55 | |
| 56 | t_set_new(Cfg) -> |
| 57 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 58 | Key = key, |
| 59 | ValExpected = bar, |
| 60 | ListInitial = DictModule:empty(), |
| 61 | ListResulting = DictModule:set(ListInitial, Key, ValExpected), |
| 62 | {some, ValResulting} = DictModule:get(ListResulting, Key), |
| 63 | ValResulting = ValExpected. |
| 64 | |
| 65 | t_set_existing(Cfg) -> |
| 66 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 67 | Key = key, |
| 68 | ValInitial = foo, |
| 69 | ValExpected = bar, |
| 70 | ListInitial = [{donald, duck}, {Key, ValInitial}], |
| 71 | ListResulting = DictModule:set(ListInitial, Key, ValExpected), |
| 72 | {some, ValResulting} = DictModule:get(ListResulting, Key), |
| 73 | ValResulting = ValExpected. |
| 74 | |
| 75 | t_pop(Cfg) -> |
| 76 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 77 | KVList = [{a, 1}, {b, 2}, {c, 3}], |
| 78 | Dict1 = DictModule:of_kv_list(KVList), |
| 79 | {{some, 1} , Dict2} = DictModule:pop(Dict1, a), |
| 80 | {none , Dict3} = DictModule:pop(Dict2, a), |
| 81 | {{some, 2} , Dict4} = DictModule:pop(Dict3, b), |
| 82 | {none , Dict5} = DictModule:pop(Dict4, b), |
| 83 | {{some, 3} , Dict6} = DictModule:pop(Dict5, c), |
| 84 | {none , Dict7} = DictModule:pop(Dict6, c), |
| 85 | [] = DictModule:to_kv_list(Dict7). |
| 86 | |
| 87 | t_fold(Cfg) -> |
| 88 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 89 | KVList = [{a, 1}, {a, 5}, {b, 3}, {c, 4}, {c, 4}], |
| 90 | Dict = DictModule:of_kv_list(KVList), |
| 91 | 17 = DictModule:fold(Dict, fun (_K, V, Acc) -> V + Acc end, 0). |
| 92 | |
| 93 | t_dictionary_specs(Cfg) -> |
| 94 | {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE), |
| 95 | [] = proper:check_specs(DictModule). |