Add get with default to dictionary interface.
[hope.git] / test / hope_dictionary_SUITE.erl
CommitLineData
f399d3fc 1-module(hope_dictionary_SUITE).
56b05e81
SK
2
3%% Callbacks
4-export(
5 [ all/0
6 , groups/0
5e835f3e
SK
7 , init_per_group/2
8 , end_per_group/2
56b05e81
SK
9 ]).
10
11%% Test cases
12-export(
39273fbe
SK
13 [ t_set_new/1
14 , t_set_existing/1
870172d6 15 , t_get/1
70cf8e86 16 , t_pop/1
e75cf601 17 , t_fold/1
8bbf6f4d 18 , t_dictionary_specs/1
56b05e81
SK
19 ]).
20
21
5e835f3e
SK
22-define(DICT_MODULE , dict_module).
23-define(DICT_MODULE_KV_LIST , hope_kv_list).
56b05e81
SK
24
25
26%% ============================================================================
27%% Common Test callbacks
28%% ============================================================================
29
30all() ->
5e835f3e 31 [{group, ?DICT_MODULE_KV_LIST}].
56b05e81
SK
32
33groups() ->
34 Tests =
39273fbe
SK
35 [ t_set_new
36 , t_set_existing
870172d6 37 , t_get
70cf8e86 38 , t_pop
e75cf601 39 , t_fold
8bbf6f4d 40 , t_dictionary_specs
56b05e81 41 ],
8bbf6f4d 42 Properties = [parallel],
5e835f3e
SK
43 [{?DICT_MODULE_KV_LIST, Properties, Tests}].
44
45init_per_group(DictModule, Cfg) ->
46 hope_kv_list:set(Cfg, ?DICT_MODULE, DictModule).
47
48end_per_group(_DictModule, _Cfg) ->
49 ok.
56b05e81
SK
50
51
52%% =============================================================================
53%% Test cases
54%% =============================================================================
39273fbe 55
870172d6
SK
56t_get(Cfg) ->
57 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
58 K1 = k1,
59 K2 = k2,
60 V1 = v1,
61 V2 = v2,
62 D = DictModule:set(DictModule:empty(), K1, V1),
63 {some, V1} = DictModule:get(D, K1),
64 V1 = DictModule:get(D, K1, V2),
65 none = DictModule:get(D, K2),
66 V2 = DictModule:get(D, K2, V2).
67
5e835f3e
SK
68t_set_new(Cfg) ->
69 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
39273fbe
SK
70 Key = key,
71 ValExpected = bar,
5e835f3e
SK
72 ListInitial = DictModule:empty(),
73 ListResulting = DictModule:set(ListInitial, Key, ValExpected),
74 {some, ValResulting} = DictModule:get(ListResulting, Key),
39273fbe
SK
75 ValResulting = ValExpected.
76
5e835f3e
SK
77t_set_existing(Cfg) ->
78 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
39273fbe
SK
79 Key = key,
80 ValInitial = foo,
81 ValExpected = bar,
82 ListInitial = [{donald, duck}, {Key, ValInitial}],
5e835f3e
SK
83 ListResulting = DictModule:set(ListInitial, Key, ValExpected),
84 {some, ValResulting} = DictModule:get(ListResulting, Key),
39273fbe 85 ValResulting = ValExpected.
70cf8e86 86
5e835f3e
SK
87t_pop(Cfg) ->
88 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
70cf8e86 89 KVList = [{a, 1}, {b, 2}, {c, 3}],
5e835f3e
SK
90 Dict1 = DictModule:of_kv_list(KVList),
91 {{some, 1} , Dict2} = DictModule:pop(Dict1, a),
92 {none , Dict3} = DictModule:pop(Dict2, a),
93 {{some, 2} , Dict4} = DictModule:pop(Dict3, b),
94 {none , Dict5} = DictModule:pop(Dict4, b),
95 {{some, 3} , Dict6} = DictModule:pop(Dict5, c),
96 {none , Dict7} = DictModule:pop(Dict6, c),
97 [] = DictModule:to_kv_list(Dict7).
e75cf601
SK
98
99t_fold(Cfg) ->
100 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
101 KVList = [{a, 1}, {a, 5}, {b, 3}, {c, 4}, {c, 4}],
102 Dict = DictModule:of_kv_list(KVList),
103 17 = DictModule:fold(Dict, fun (_K, V, Acc) -> V + Acc end, 0).
8bbf6f4d
PO
104
105t_dictionary_specs(Cfg) ->
106 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
107 [] = proper:check_specs(DictModule).
This page took 0.039626 seconds and 4 git commands to generate.