-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringmap.test.ts
201 lines (151 loc) · 5.83 KB
/
stringmap.test.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { NarrowStringMap, StringMap } from "./stringmap";
import { NumberMap } from "./numbermap"
import { NumberMap as NM2, StringMap as SM2 } from "./index"
import { assert } from "chai";
const A = {id: "A"};
const B = {id: "B"};
type Val = {id: string};
describe("stringmap", function() {
it("counts changes to key size", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
assert.equal(map.size, 2);
map.set("c", A);
assert.equal(map.size, 3);
map.delete("a");
assert.equal(map.size, 2);
map.delete("c");
map.delete("b");
assert.equal(map.size, 0);
map.set("c", A);
assert.equal(map.size, 1);
map.delete("z");
assert.equal(map.size, 1, "size should change only when key was present");
map.delete("c");
assert.equal(map.size, 0);
});
it("calls forEach with values", function() {
const calls: [string,any][] = [];
const map = new StringMap<Val>([["a",A], ["b",B]]);
map.forEach((v, k, m) => {
assert.equal(m, map, "should have called with map");
calls.push([k,v]);
})
assert.sameDeepMembers(map.entries(), [["a",A], ["b",B]]);
})
it("clear empties", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
assert.equal(map.size, 2);
map.clear();
assert.equal(map.size, 0);
})
it("can get values", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
assert.sameMembers(map.values(), [A,B]);
})
it("can get keys", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
assert.sameMembers(map.keys(), ['a','b']);
})
it("can get entires", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
assert.sameDeepMembers(map.entries(), [["a",A], ["b",B]]);
})
it("can check membership", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
assert.isTrue(map.has('a'), "has false negative");
assert.isFalse(map.has('z'), "has false positive");
map.delete('a');
assert.isFalse(map.has('a'), "has + delete not working togther");
});
it("has a spec compliant 'delete'", function() {
const map = new StringMap<Val>([["a",A], ["b",B]]);
const had = map.delete('a');
assert.isTrue(had, "delete didn't recognize previous inclusion");
assert.isFalse(map.has('a'), 'had should be false after a delete');
const notPresent = map.delete('z');
assert.isFalse(notPresent, "map false positive on previous inclusion");
});
});
// Number map
describe("numbermap", function() {
it("counts changes to key size", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
assert.equal(map.size, 2);
map.set(6, A);
assert.equal(map.size, 3);
map.delete(6);
assert.equal(map.size, 2);
map.delete(5);
map.delete(9);
assert.equal(map.size, 0);
map.set(6, A);
assert.equal(map.size, 1);
map.delete(100);
assert.equal(map.size, 1, "size should change only when key was present");
map.delete(6);
assert.equal(map.size, 0);
});
it("calls forEach with values", function() {
const calls: [number,any][] = [];
const map = new NumberMap<Val>([[5,A], [9,B]]);
map.forEach((v, k, m) => {
assert.equal(m, map, "should have called with map");
calls.push([k,v]);
})
assert.sameDeepMembers(map.entries(), [[5,A], [9,B]]);
})
it("clear empties", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
assert.equal(map.size, 2);
map.clear();
assert.equal(map.size, 0);
})
it("can get values", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
assert.sameMembers(map.values(), [A,B]);
})
it("can get keys", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
assert.sameMembers(map.keys(), [5,9]);
})
it("can get entires", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
assert.sameDeepMembers(map.entries(), [[5,A], [9,B]]);
})
it("can check membership", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
assert.isTrue(map.has(5), "has false negative");
assert.isFalse(map.has(100), "has false positive");
map.delete(5);
assert.isFalse(map.has(5), "has + delete not working togther");
});
it("has a spec compliant 'delete'", function() {
const map = new NumberMap<Val>([[5,A], [9,B]]);
const had = map.delete(5);
assert.isTrue(had, "delete didn't recognize previous inclusion");
assert.isFalse(map.has(5), 'had should be false after a delete');
const notPresent = map.delete(100);
assert.isFalse(notPresent, "map false positive on previous inclusion");
});
it("can import from rollup", () => {
const n = new NM2();
const s = new SM2();
assert.equal(typeof s.get, 'function')
assert.equal(typeof n.get, 'function')
});
});
describe("README", () => {
it("has a working example", () => {
// commented out the lines that should type error
const map = new StringMap<number>();
map.set('a', 5); // fine
//map.set(10, 5); // errors - key is not string
//map.set(10, 'a'); // errors - value is not number
type Type = "a" | "b";
const byType = new NarrowStringMap<Type, number>();
byType.set('a', 10) // fine
// byType.set('z', 10) // errors - 'z' is not 'a' | 'b'
// byType.get('z') // errors - 'z' is not 'a' | 'b'
const types: Type[] = byType.keys() // fine - specific typings
});
});