Add dictionary get with validation.
[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),
e0fbc1da
SK
66 V2 = DictModule:get(D, K2, V2),
67 default = DictModule:get(D, K1, default, fun (X) -> X =:= foo end),
68 V1 = DictModule:get(D, K1, default, fun (X) -> X =:= V1 end).
870172d6 69
5e835f3e
SK
70t_set_new(Cfg) ->
71 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
39273fbe
SK
72 Key = key,
73 ValExpected = bar,
5e835f3e
SK
74 ListInitial = DictModule:empty(),
75 ListResulting = DictModule:set(ListInitial, Key, ValExpected),
76 {some, ValResulting} = DictModule:get(ListResulting, Key),
39273fbe
SK
77 ValResulting = ValExpected.
78
5e835f3e
SK
79t_set_existing(Cfg) ->
80 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
39273fbe
SK
81 Key = key,
82 ValInitial = foo,
83 ValExpected = bar,
84 ListInitial = [{donald, duck}, {Key, ValInitial}],
5e835f3e
SK
85 ListResulting = DictModule:set(ListInitial, Key, ValExpected),
86 {some, ValResulting} = DictModule:get(ListResulting, Key),
39273fbe 87 ValResulting = ValExpected.
70cf8e86 88
5e835f3e
SK
89t_pop(Cfg) ->
90 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
70cf8e86 91 KVList = [{a, 1}, {b, 2}, {c, 3}],
5e835f3e
SK
92 Dict1 = DictModule:of_kv_list(KVList),
93 {{some, 1} , Dict2} = DictModule:pop(Dict1, a),
94 {none , Dict3} = DictModule:pop(Dict2, a),
95 {{some, 2} , Dict4} = DictModule:pop(Dict3, b),
96 {none , Dict5} = DictModule:pop(Dict4, b),
97 {{some, 3} , Dict6} = DictModule:pop(Dict5, c),
98 {none , Dict7} = DictModule:pop(Dict6, c),
99 [] = DictModule:to_kv_list(Dict7).
e75cf601
SK
100
101t_fold(Cfg) ->
102 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
103 KVList = [{a, 1}, {a, 5}, {b, 3}, {c, 4}, {c, 4}],
104 Dict = DictModule:of_kv_list(KVList),
105 17 = DictModule:fold(Dict, fun (_K, V, Acc) -> V + Acc end, 0).
8bbf6f4d
PO
106
107t_dictionary_specs(Cfg) ->
108 {some, DictModule} = hope_kv_list:get(Cfg, ?DICT_MODULE),
109 [] = proper:check_specs(DictModule).
This page took 0.034991 seconds and 4 git commands to generate.