-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_dictionaries.dry
113 lines (94 loc) · 4.57 KB
/
test_dictionaries.dry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class TestDictionary {
def test_init() {
let empty_dict = {};
assert_equals("Empty dictionary string representation", str(empty_dict), "{}");
let map = { "one": 1, "two": 2 };
assert_equals("Non-empty dictionary string representation", str(map),
"{one: 1, two: 2}");
}
def test_size() {
let empty_dict = {};
let non_empty_dict = {"number": 2, "str": "hello", 3: 56};
assert_equals("Count empty dict", empty_dict.size(), 0);
assert_equals("Count non-empty dict", non_empty_dict.size(), 3);
}
def test_get() {
let dict = {"number": 1, 1: "one", true: 11, false: 10, none: "none"};
assert_equals("Get dictionary field value by string key", dict["number"], 1);
assert_equals("Get dictionary field value by number key", dict[1], "one");
assert_equals("Get dictionary field value by true key", dict[true], 11);
assert_equals("Get dictionary field value by false key", dict[false], 10);
assert_equals("Get dictionary field value by none key", dict[none], "none");
assert_error_type("Accessing undefined dictionary key", UndefinedKeyError, lambda() {
dict["foo"];
});
assert_error_type("Accessing from a non-dictionary by key", CanNotApplyIndexOperatorError, lambda() {
let foo = lambda() {};
foo["numbers"];
});
}
def test_set() {
let dict = {"number": 1, 1: "one", true: 11, false: 10, none: "none"};
let new_field_added = dict["number"] = 3;
assert_false("Dictionary is updated", new_field_added);
assert_equals("Updated dictionary", {"number": 3, 1: "one", true: 11, false: 10, none: "none"}, dict);
new_field_added = dict["food"] = "burger";
assert_true("New field is added to dictionary", new_field_added);
assert_equals("Updated dictionary with new fields",
{"number": 3, 1: "one", true: 11, false: 10, none: "none", "food": "burger"}, dict);
assert_error_type("Updating a non-dictionary by key", CanNotApplyIndexOperatorError, lambda() {
let foo = lambda() {};
foo["numbers"] = 30;
});
}
def test_delete() {
let dict = {"first": 1, "second": 2};
del dict["first"];
assert_equals("Delete existing field from a dictionary", {"second": 2}, dict);
assert_error_type("Delete non-existing field from a dictionary", UndefinedKeyError, lambda() {
del dict["third"];
});
}
def test_nested() {
let nested_dict = {"outer": 1, "inner": { "inner_field": 3 }};
assert_equals("Access field from a nested dictionary", 3, nested_dict["inner"]["inner_field"]);
nested_dict["inner"]["inner_field"] = 10;
assert_equals("Update field in a nested dictionary",
{"outer": 1, "inner": { "inner_field": 10 }}, nested_dict);
del nested_dict["inner"]["inner_field"];
assert_equals("Delete field from a nested dictionary", {"outer": 1, "inner": {}}, nested_dict);
}
def test_type() {
let dict = {1: 2};
assert_equals("Dictionary type", "dictionary", typeof(dict));
}
def test_fields_retrieval() {
// let's stick to one element for checking field values for now,
// because a dictionary is not an ordered collection so a multi-element one
// might produce a list with a different order from what we expect.
// We can improve this test case later on once we have an unordered collection
let dict = {1: 2};
assert_equals("Retrieving fields from a dictionary", [(1, 2)], dict.fields());
assert_equals("Retrieving keys from a dictionary", [1], dict.keys());
assert_equals("Retrieving values from a dictionary", [2], dict.values());
// this will at least check if it works on a non-singleton, non-empty dictionary
assert_equals("Size of retrieved fields", 2, {1: 2, 3: 4}.fields().size());
}
def test_non_constant_keys() {
let dict_arith = {1 + 1: 1};
assert_equals("Dictionary with an arithmetic expression key", 1, dict_arith[2]);
def foo() { return "hello"; }
let dict_call = {foo(): 10};
assert_equals("Dictionary with a function call key", 10, dict_call[foo()]);
}
}
let dictionaries = TestDictionary();
dictionaries.test_init();
dictionaries.test_size();
dictionaries.test_get();
dictionaries.test_set();
dictionaries.test_delete();
dictionaries.test_nested();
dictionaries.test_type();
dictionaries.test_fields_retrieval();
dictionaries.test_non_constant_keys();