Skip to content

Commit c6fc934

Browse files
committed
Add tests for set shim
1 parent f773f18 commit c6fc934

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"unittests/reuseProgramStructure.ts",
7474
"unittests/semver.ts",
7575
"unittests/createMapShim.ts",
76+
"unittests/createSetShim.ts",
7677
"unittests/transform.ts",
7778
"unittests/config/commandLineParsing.ts",
7879
"unittests/config/configurationExtension.ts",
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
namespace ts {
2+
describe("unittests:: createSetShim", () => {
3+
const stringKeys = [
4+
"1",
5+
"3",
6+
"2",
7+
"4",
8+
"0",
9+
"999",
10+
"A",
11+
"B",
12+
"C",
13+
"Z",
14+
"X",
15+
"X1",
16+
"X2",
17+
"Y"
18+
];
19+
20+
const mixedKeys = [
21+
true,
22+
3,
23+
{ toString() { return "2"; } },
24+
"4",
25+
false,
26+
null, // eslint-disable-line no-null/no-null
27+
undefined,
28+
"B",
29+
{ toString() { return "C"; } },
30+
"Z",
31+
"X",
32+
{ toString() { return "X1"; } },
33+
"X2",
34+
"Y"
35+
];
36+
37+
function testSetIterationAddedValues<K>(keys: K[], set: Set<K>, useForEach: boolean): string {
38+
let resultString = "";
39+
40+
set.add(keys[0]);
41+
set.add(keys[1]);
42+
set.add(keys[2]);
43+
set.add(keys[3]);
44+
45+
let addedThree = false;
46+
const doForEach = (key: K) => {
47+
resultString += `${key};`;
48+
49+
// Add a new key ("0") - the map should provide this
50+
// one in the next iteration.
51+
if (key === keys[0]) {
52+
set.add(keys[0]);
53+
set.add(keys[4]);
54+
set.add(keys[3]);
55+
}
56+
else if (key === keys[1]) {
57+
if (!addedThree) {
58+
addedThree = true;
59+
60+
// Remove and re-add key "3"; the map should
61+
// visit it after "0".
62+
set.delete(keys[1]);
63+
set.add(keys[1]);
64+
65+
// Change the value of "2"; the map should provide
66+
// it when visiting the key.
67+
set.add(keys[2]);
68+
}
69+
else {
70+
// Check that an entry added when we visit the
71+
// currently last entry will still be visited.
72+
set.add(keys[5]);
73+
}
74+
}
75+
else if (key === keys[5]) {
76+
// Ensure that clear() behaves correctly same as removing all keys.
77+
set.add(keys[6]);
78+
set.add(keys[7]);
79+
set.add(keys[8]);
80+
}
81+
else if (key === keys[6]) {
82+
set.clear();
83+
set.add(keys[9]);
84+
}
85+
else if (key === keys[9]) {
86+
// Check that the map behaves correctly when two items are
87+
// added and removed immediately.
88+
set.add(keys[10]);
89+
set.add(keys[11]);
90+
set.add(keys[12]);
91+
set.delete(keys[11]);
92+
set.delete(keys[12]);
93+
set.add(keys[13]);
94+
}
95+
};
96+
97+
if (useForEach) {
98+
set.forEach(doForEach);
99+
}
100+
else {
101+
// Use an iterator.
102+
const iterator = set.values();
103+
while (true) {
104+
const iterResult = iterator.next();
105+
if (iterResult.done) {
106+
break;
107+
}
108+
109+
doForEach(iterResult.value);
110+
}
111+
}
112+
113+
return resultString;
114+
}
115+
116+
it("iterates values in insertion order and handles changes with string keys", () => {
117+
const expectedResult = "1;3;2;4;0;3;999;A;Z;X;Y;";
118+
119+
// First, ensure the test actually has the same behavior as a native Map.
120+
let nativeSet = new Set<string>();
121+
const nativeSetForEachResult = testSetIterationAddedValues(stringKeys, nativeSet, /* useForEach */ true);
122+
assert.equal(nativeSetForEachResult, expectedResult, "nativeSet-forEach");
123+
124+
nativeSet = new Set<string>();
125+
const nativeSetIteratorResult = testSetIterationAddedValues(stringKeys, nativeSet, /* useForEach */ false);
126+
assert.equal(nativeSetIteratorResult, expectedResult, "nativeSet-iterator");
127+
128+
// Then, test the map shim.
129+
const SetShim = ShimCollections.createSetShim(getIterator);
130+
let localShimSet = new SetShim<string>();
131+
const shimSetForEachResult = testSetIterationAddedValues(stringKeys, localShimSet, /* useForEach */ true);
132+
assert.equal(shimSetForEachResult, expectedResult, "shimSet-forEach");
133+
134+
localShimSet = new SetShim<string>();
135+
const shimSetIteratorResult = testSetIterationAddedValues(stringKeys, localShimSet, /* useForEach */ false);
136+
assert.equal(shimSetIteratorResult, expectedResult, "shimSet-iterator");
137+
});
138+
139+
it("iterates values in insertion order and handles changes with mixed-type keys", () => {
140+
const expectedResult = "true;3;2;4;false;3;null;undefined;Z;X;Y;";
141+
142+
// First, ensure the test actually has the same behavior as a native Map.
143+
let nativeSet = new Set<any>();
144+
const nativeSetForEachResult = testSetIterationAddedValues(mixedKeys, nativeSet, /* useForEach */ true);
145+
assert.equal(nativeSetForEachResult, expectedResult, "nativeSet-forEach");
146+
147+
nativeSet = new Set<any>();
148+
const nativeSetIteratorResult = testSetIterationAddedValues(mixedKeys, nativeSet, /* useForEach */ false);
149+
assert.equal(nativeSetIteratorResult, expectedResult, "nativeSet-iterator");
150+
151+
// Then, test the map shim.
152+
const SetShim = ShimCollections.createSetShim(getIterator); // tslint:disable-line variable-name
153+
let localshimSet = new SetShim<any>();
154+
const shimSetForEachResult = testSetIterationAddedValues(mixedKeys, localshimSet, /* useForEach */ true);
155+
assert.equal(shimSetForEachResult, expectedResult, "shimSet-forEach");
156+
157+
localshimSet = new SetShim<any>();
158+
const shimSetIteratorResult = testSetIterationAddedValues(mixedKeys, localshimSet, /* useForEach */ false);
159+
assert.equal(shimSetIteratorResult, expectedResult, "shimSet-iterator");
160+
});
161+
});
162+
}

0 commit comments

Comments
 (0)