@everyonesoftware/common 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.c8rc.json +12 -0
- package/.github/workflows/publish.yml +38 -0
- package/.mocharc.json +9 -0
- package/README.md +9 -0
- package/package.json +36 -0
- package/sources/assertMessageParameters.ts +22 -0
- package/sources/asyncIterator.ts +437 -0
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +48 -0
- package/sources/asyncResult.ts +95 -0
- package/sources/basicDisposable.ts +57 -0
- package/sources/byteList.ts +202 -0
- package/sources/byteListStream.ts +121 -0
- package/sources/byteReadStream.ts +24 -0
- package/sources/byteWriteStream.ts +16 -0
- package/sources/bytes.ts +25 -0
- package/sources/characterList.ts +195 -0
- package/sources/characterListStream.ts +151 -0
- package/sources/characterReadStream.ts +81 -0
- package/sources/characterReadStreamIterator.ts +128 -0
- package/sources/characterWriteStream.ts +45 -0
- package/sources/commandLineParameter.ts +45 -0
- package/sources/commandLineParameters.ts +21 -0
- package/sources/comparable.ts +144 -0
- package/sources/comparer.ts +133 -0
- package/sources/comparison.ts +20 -0
- package/sources/concatenateIterable.ts +119 -0
- package/sources/concatenateIterator.ts +165 -0
- package/sources/condition.ts +329 -0
- package/sources/currentProcess.ts +158 -0
- package/sources/dateTime.ts +130 -0
- package/sources/depthFirstSearch.ts +230 -0
- package/sources/disposable.ts +31 -0
- package/sources/emptyError.ts +10 -0
- package/sources/english.ts +45 -0
- package/sources/equalFunctions.ts +123 -0
- package/sources/fetchHttpClient.ts +89 -0
- package/sources/fetchHttpResponse.ts +106 -0
- package/sources/flatMapIterable.ts +104 -0
- package/sources/flatMapIterator.ts +152 -0
- package/sources/generator.ts +251 -0
- package/sources/httpClient.ts +36 -0
- package/sources/httpHeader.ts +37 -0
- package/sources/httpHeaders.ts +216 -0
- package/sources/httpIncomingRequest.ts +30 -0
- package/sources/httpIncomingResponse.ts +19 -0
- package/sources/httpMethod.ts +164 -0
- package/sources/httpOutgoingRequest.ts +119 -0
- package/sources/httpOutgoingResponse.ts +113 -0
- package/sources/httpServer.ts +34 -0
- package/sources/inMemoryCharacterWriteStream.ts +78 -0
- package/sources/index.ts +101 -0
- package/sources/iterable.ts +345 -0
- package/sources/iterator.ts +481 -0
- package/sources/iteratorToJavascriptIteratorAdapter.ts +48 -0
- package/sources/javascript.ts +59 -0
- package/sources/javascriptArrayList.ts +175 -0
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +124 -0
- package/sources/javascriptIteratorToIteratorAdapter.ts +133 -0
- package/sources/javascriptMapMap.ts +143 -0
- package/sources/javascriptSetSet.ts +134 -0
- package/sources/list.ts +330 -0
- package/sources/listQueue.ts +62 -0
- package/sources/listStack.ts +62 -0
- package/sources/luxonDateTime.ts +109 -0
- package/sources/map.ts +302 -0
- package/sources/mapAsyncIterator.ts +141 -0
- package/sources/mapIterable.ts +105 -0
- package/sources/mapIterator.ts +145 -0
- package/sources/mutableCondition.ts +451 -0
- package/sources/mutableHttpHeaders.ts +204 -0
- package/sources/mutableMap.ts +292 -0
- package/sources/network.ts +18 -0
- package/sources/node.ts +37 -0
- package/sources/nodeJSCharacterWriteStream.ts +42 -0
- package/sources/nodeJSHttpIncomingRequest.ts +132 -0
- package/sources/nodeJSHttpServer.ts +134 -0
- package/sources/notFoundError.ts +12 -0
- package/sources/postCondition.ts +284 -0
- package/sources/postConditionError.ts +12 -0
- package/sources/preCondition.ts +284 -0
- package/sources/preConditionError.ts +12 -0
- package/sources/promiseAsyncResult.ts +174 -0
- package/sources/property.ts +63 -0
- package/sources/queue.ts +49 -0
- package/sources/realNetwork.ts +28 -0
- package/sources/recreationDotGovClient.ts +259 -0
- package/sources/searchControl.ts +42 -0
- package/sources/set.ts +244 -0
- package/sources/skipAsyncIterator.ts +145 -0
- package/sources/skipIterator.ts +155 -0
- package/sources/stack.ts +48 -0
- package/sources/stringComparer.ts +33 -0
- package/sources/stringIterator.ts +149 -0
- package/sources/strings.ts +322 -0
- package/sources/syncResult.ts +300 -0
- package/sources/takeAsyncIterator.ts +141 -0
- package/sources/takeIterator.ts +151 -0
- package/sources/toStringFunctions.ts +185 -0
- package/sources/types.ts +371 -0
- package/sources/whereAsyncIterator.ts +143 -0
- package/sources/whereIterable.ts +108 -0
- package/sources/whereIterator.ts +157 -0
- package/sources/wonderlandTrailClient.ts +1503 -0
- package/tests/assertTest.ts +113 -0
- package/tests/assertTestTests.ts +75 -0
- package/tests/basicTestSkip.ts +51 -0
- package/tests/byteListStreamTests.ts +390 -0
- package/tests/byteListTests.ts +27 -0
- package/tests/bytesTests.ts +43 -0
- package/tests/characterListStreamTests.ts +391 -0
- package/tests/characterListTests.ts +250 -0
- package/tests/characterWriteStreamTests.ts +12 -0
- package/tests/comparerTests.ts +92 -0
- package/tests/conditionTests.ts +877 -0
- package/tests/consoleTestRunner.ts +404 -0
- package/tests/consoleTestRunnerTests.ts +651 -0
- package/tests/dateTimeTests.ts +30 -0
- package/tests/depthFirstSearchTests.ts +106 -0
- package/tests/disposableTests.ts +121 -0
- package/tests/englishTests.ts +103 -0
- package/tests/equalFunctionsTests.ts +223 -0
- package/tests/failedTest.ts +43 -0
- package/tests/fetchHttpClientTests.ts +33 -0
- package/tests/generatorTests.ts +86 -0
- package/tests/httpClientTests.ts +18 -0
- package/tests/inMemoryCharacterWriteStreamTests.ts +117 -0
- package/tests/iterableTests.ts +141 -0
- package/tests/iteratorTests.ts +1086 -0
- package/tests/javascriptMapMapTests.ts +21 -0
- package/tests/listTests.ts +338 -0
- package/tests/mapIteratorTests.ts +55 -0
- package/tests/mapTests.ts +104 -0
- package/tests/mutableConditionTests.ts +273 -0
- package/tests/mutableMapTests.ts +154 -0
- package/tests/nodeJSHttpServerTests.ts +75 -0
- package/tests/notFoundErrorTests.ts +24 -0
- package/tests/postConditionErrorTests.ts +24 -0
- package/tests/preConditionErrorTests.ts +24 -0
- package/tests/promiseAsyncResultTests.ts +688 -0
- package/tests/propertyTests.ts +63 -0
- package/tests/queueTests.ts +29 -0
- package/tests/recreationDotGovClientTests.ts +191 -0
- package/tests/setTests.ts +140 -0
- package/tests/skippedTest.ts +39 -0
- package/tests/stackTests.ts +66 -0
- package/tests/stringComparerTests.ts +60 -0
- package/tests/stringIteratorTests.ts +156 -0
- package/tests/stringsTests.ts +516 -0
- package/tests/syncResultTests.ts +1251 -0
- package/tests/test.ts +228 -0
- package/tests/testAction.ts +75 -0
- package/tests/testActionTests.ts +93 -0
- package/tests/testFailureTests.ts +12 -0
- package/tests/testRunner.ts +267 -0
- package/tests/testRunnerTests.ts +895 -0
- package/tests/testSkip.ts +34 -0
- package/tests/tests.ts +103 -0
- package/tests/toStringFunctionsTests.ts +55 -0
- package/tests/typesTests.ts +257 -0
- package/tests/whereIteratorTests.ts +77 -0
- package/tests/wonderlandTrailClientTests.ts +452 -0
- package/tsconfig.json +17 -0
- package/tsup.config.ts +13 -0
|
@@ -0,0 +1,1086 @@
|
|
|
1
|
+
import { EmptyError } from "../sources/emptyError";
|
|
2
|
+
import { Iterator } from "../sources/iterator";
|
|
3
|
+
import
|
|
4
|
+
{
|
|
5
|
+
JavascriptIterable, JavascriptIterator, JavascriptIteratorResult
|
|
6
|
+
} from "../sources/javascript";
|
|
7
|
+
import { NotFoundError } from "../sources/notFoundError";
|
|
8
|
+
import { PreConditionError } from "../sources/preConditionError";
|
|
9
|
+
import { Test } from "./test";
|
|
10
|
+
import { TestRunner } from "./testRunner";
|
|
11
|
+
|
|
12
|
+
export function test(runner: TestRunner): void
|
|
13
|
+
{
|
|
14
|
+
runner.testFile("iterator.ts", () =>
|
|
15
|
+
{
|
|
16
|
+
runner.testType("Iterator<T>", () =>
|
|
17
|
+
{
|
|
18
|
+
runner.testFunction("create(T[])", () =>
|
|
19
|
+
{
|
|
20
|
+
function createErrorTest<T>(values: T[], expected: Error): void
|
|
21
|
+
{
|
|
22
|
+
runner.test(`with ${runner.toString(values)}`, (test: Test) =>
|
|
23
|
+
{
|
|
24
|
+
test.assertThrows(() => Iterator.create(values), expected);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
createErrorTest(undefined!, new PreConditionError(
|
|
29
|
+
"Expression: values",
|
|
30
|
+
"Expected: not undefined and not null",
|
|
31
|
+
"Actual: undefined",
|
|
32
|
+
));
|
|
33
|
+
createErrorTest(null!, new PreConditionError(
|
|
34
|
+
"Expression: values",
|
|
35
|
+
"Expected: not undefined and not null",
|
|
36
|
+
"Actual: null",
|
|
37
|
+
));
|
|
38
|
+
|
|
39
|
+
function createTest<T>(values: T[]): void
|
|
40
|
+
{
|
|
41
|
+
runner.test(`with ${runner.toString(values)}`, (test: Test) =>
|
|
42
|
+
{
|
|
43
|
+
const iterator: Iterator<T> = Iterator.create(values);
|
|
44
|
+
test.assertEqual(iterator.toArray().await(), values);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
createTest([]);
|
|
49
|
+
createTest([1, 2, 3]);
|
|
50
|
+
createTest([false, true]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
runner.testFunction("next()", () =>
|
|
54
|
+
{
|
|
55
|
+
runner.testGroup("not empty", () =>
|
|
56
|
+
{
|
|
57
|
+
runner.test("not started", (test: Test) =>
|
|
58
|
+
{
|
|
59
|
+
const values: number[] = [1, 2, 3];
|
|
60
|
+
const iterator: Iterator<number> = Iterator.create(values);
|
|
61
|
+
test.assertFalse(iterator.hasStarted());
|
|
62
|
+
test.assertFalse(iterator.hasCurrent());
|
|
63
|
+
|
|
64
|
+
for (const value of values)
|
|
65
|
+
{
|
|
66
|
+
test.assertTrue(iterator.next().await());
|
|
67
|
+
test.assertTrue(iterator.hasCurrent());
|
|
68
|
+
test.assertTrue(iterator.hasStarted());
|
|
69
|
+
test.assertEqual(value, iterator.getCurrent());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (let i = 0; i < 2; i++)
|
|
73
|
+
{
|
|
74
|
+
test.assertFalse(iterator.next().await());
|
|
75
|
+
test.assertFalse(iterator.hasCurrent());
|
|
76
|
+
test.assertTrue(iterator.hasStarted());
|
|
77
|
+
test.assertThrows(
|
|
78
|
+
() => iterator.getCurrent(),
|
|
79
|
+
new PreConditionError(
|
|
80
|
+
"Expression: this.hasCurrent()",
|
|
81
|
+
"Expected: true",
|
|
82
|
+
"Actual: false",
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
runner.test("started", (test: Test) =>
|
|
89
|
+
{
|
|
90
|
+
const values: number[] = [1, 2, 3];
|
|
91
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
92
|
+
test.assertTrue(iterator.hasStarted());
|
|
93
|
+
test.assertTrue(iterator.hasCurrent());
|
|
94
|
+
test.assertEqual(1, iterator.getCurrent());
|
|
95
|
+
|
|
96
|
+
for (const value of values.slice(1))
|
|
97
|
+
{
|
|
98
|
+
test.assertTrue(iterator.next().await());
|
|
99
|
+
test.assertTrue(iterator.hasCurrent());
|
|
100
|
+
test.assertTrue(iterator.hasStarted());
|
|
101
|
+
test.assertEqual(value, iterator.getCurrent());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
for (let i = 0; i < 2; i++)
|
|
105
|
+
{
|
|
106
|
+
test.assertFalse(iterator.next().await());
|
|
107
|
+
test.assertFalse(iterator.hasCurrent());
|
|
108
|
+
test.assertTrue(iterator.hasStarted());
|
|
109
|
+
test.assertThrows(
|
|
110
|
+
() => iterator.getCurrent(),
|
|
111
|
+
new PreConditionError(
|
|
112
|
+
"Expression: this.hasCurrent()",
|
|
113
|
+
"Expected: true",
|
|
114
|
+
"Actual: false",
|
|
115
|
+
),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
runner.testFunction("start()", (test: Test) =>
|
|
123
|
+
{
|
|
124
|
+
const iterator: Iterator<number> = Iterator.create([1, 2, 3]);
|
|
125
|
+
for (let i: number = 0; i < 2; i++)
|
|
126
|
+
{
|
|
127
|
+
const startResult: Iterator<number> = iterator.start().await();
|
|
128
|
+
test.assertSame(startResult, iterator);
|
|
129
|
+
test.assertTrue(iterator.hasStarted());
|
|
130
|
+
test.assertTrue(iterator.hasCurrent());
|
|
131
|
+
test.assertEqual(1, iterator.getCurrent());
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
runner.testFunction("takeCurrent()", () =>
|
|
136
|
+
{
|
|
137
|
+
runner.testGroup("with non-empty", () =>
|
|
138
|
+
{
|
|
139
|
+
runner.test("not started", (test: Test) =>
|
|
140
|
+
{
|
|
141
|
+
const iterator: Iterator<number> = Iterator.create([1, 2, 3]);
|
|
142
|
+
test.assertFalse(iterator.hasStarted());
|
|
143
|
+
test.assertFalse(iterator.hasCurrent());
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < 2; i++)
|
|
146
|
+
{
|
|
147
|
+
test.assertThrows(() => iterator.takeCurrent(),
|
|
148
|
+
new PreConditionError(
|
|
149
|
+
"Expression: iterator.hasCurrent()",
|
|
150
|
+
"Expected: true",
|
|
151
|
+
"Actual: false",
|
|
152
|
+
));
|
|
153
|
+
test.assertFalse(iterator.hasStarted());
|
|
154
|
+
test.assertFalse(iterator.hasCurrent());
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
runner.test("started", (test: Test) =>
|
|
159
|
+
{
|
|
160
|
+
const values: number[] = [1, 2, 3];
|
|
161
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
162
|
+
test.assertTrue(iterator.hasStarted());
|
|
163
|
+
test.assertTrue(iterator.hasCurrent());
|
|
164
|
+
test.assertEqual(1, iterator.getCurrent());
|
|
165
|
+
|
|
166
|
+
for (let i = 0; i < values.length - 1; i++)
|
|
167
|
+
{
|
|
168
|
+
test.assertEqual(values[i], iterator.takeCurrent().await());
|
|
169
|
+
test.assertTrue(iterator.hasStarted());
|
|
170
|
+
test.assertTrue(iterator.hasCurrent());
|
|
171
|
+
test.assertEqual(values[i + 1], iterator.getCurrent());
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
test.assertEqual(values[values.length - 1], iterator.takeCurrent().await());
|
|
175
|
+
test.assertTrue(iterator.hasStarted());
|
|
176
|
+
test.assertFalse(iterator.hasCurrent());
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
runner.testFunction("any()", () =>
|
|
182
|
+
{
|
|
183
|
+
runner.testGroup("with non-empty", () =>
|
|
184
|
+
{
|
|
185
|
+
runner.test("not started", (test: Test) =>
|
|
186
|
+
{
|
|
187
|
+
const values: number[] = [1, 2, 3];
|
|
188
|
+
const iterator: Iterator<number> = Iterator.create(values);
|
|
189
|
+
test.assertFalse(iterator.hasStarted());
|
|
190
|
+
test.assertFalse(iterator.hasCurrent());
|
|
191
|
+
|
|
192
|
+
for (let i = 0; i < 2; i++)
|
|
193
|
+
{
|
|
194
|
+
test.assertTrue(iterator.any().await());
|
|
195
|
+
test.assertTrue(iterator.hasStarted());
|
|
196
|
+
test.assertTrue(iterator.hasCurrent());
|
|
197
|
+
test.assertEqual(values[0], iterator.getCurrent());
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
runner.test("started", (test: Test) =>
|
|
202
|
+
{
|
|
203
|
+
const values: number[] = [1, 2, 3];
|
|
204
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
205
|
+
test.assertTrue(iterator.hasStarted());
|
|
206
|
+
test.assertTrue(iterator.hasCurrent());
|
|
207
|
+
test.assertEqual(values[0], iterator.getCurrent());
|
|
208
|
+
|
|
209
|
+
for (let i = 0; i < 2; i++)
|
|
210
|
+
{
|
|
211
|
+
test.assertTrue(iterator.any().await());
|
|
212
|
+
test.assertTrue(iterator.hasStarted());
|
|
213
|
+
test.assertTrue(iterator.hasCurrent());
|
|
214
|
+
test.assertEqual(values[0], iterator.getCurrent());
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
runner.testFunction("getCount()", () =>
|
|
221
|
+
{
|
|
222
|
+
runner.testGroup("with non-empty", () =>
|
|
223
|
+
{
|
|
224
|
+
runner.test("not started", (test: Test) =>
|
|
225
|
+
{
|
|
226
|
+
const values: number[] = [1, 2, 3];
|
|
227
|
+
const iterator: Iterator<number> = Iterator.create(values);
|
|
228
|
+
test.assertFalse(iterator.hasStarted());
|
|
229
|
+
test.assertFalse(iterator.hasCurrent());
|
|
230
|
+
|
|
231
|
+
test.assertEqual(values.length, iterator.getCount().await());
|
|
232
|
+
test.assertTrue(iterator.hasStarted());
|
|
233
|
+
test.assertFalse(iterator.hasCurrent());
|
|
234
|
+
|
|
235
|
+
for (let i = 0; i < 2; i++)
|
|
236
|
+
{
|
|
237
|
+
test.assertEqual(iterator.getCount().await(), 0);
|
|
238
|
+
test.assertTrue(iterator.hasStarted());
|
|
239
|
+
test.assertFalse(iterator.hasCurrent());
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
runner.test("started", (test: Test) =>
|
|
244
|
+
{
|
|
245
|
+
const values: number[] = [1, 2, 3];
|
|
246
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
247
|
+
test.assertTrue(iterator.hasStarted());
|
|
248
|
+
test.assertTrue(iterator.hasCurrent());
|
|
249
|
+
test.assertEqual(values[0], iterator.getCurrent());
|
|
250
|
+
|
|
251
|
+
test.assertEqual(values.length, iterator.getCount().await());
|
|
252
|
+
test.assertTrue(iterator.hasStarted());
|
|
253
|
+
test.assertFalse(iterator.hasCurrent());
|
|
254
|
+
|
|
255
|
+
for (let i = 0; i < 2; i++)
|
|
256
|
+
{
|
|
257
|
+
test.assertEqual(iterator.getCount().await(), 0);
|
|
258
|
+
test.assertTrue(iterator.hasStarted());
|
|
259
|
+
test.assertFalse(iterator.hasCurrent());
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
runner.testFunction("toArray()", () =>
|
|
266
|
+
{
|
|
267
|
+
runner.testGroup("not empty", () =>
|
|
268
|
+
{
|
|
269
|
+
runner.test("not started", (test: Test) =>
|
|
270
|
+
{
|
|
271
|
+
const values: number[] = [1, 2, 3];
|
|
272
|
+
const iterator: Iterator<number> = Iterator.create(values);
|
|
273
|
+
|
|
274
|
+
test.assertEqual(values, iterator.toArray().await());
|
|
275
|
+
test.assertTrue(iterator.hasStarted());
|
|
276
|
+
test.assertFalse(iterator.hasCurrent());
|
|
277
|
+
|
|
278
|
+
test.assertEqual([], iterator.toArray().await());
|
|
279
|
+
test.assertTrue(iterator.hasStarted());
|
|
280
|
+
test.assertFalse(iterator.hasCurrent());
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
runner.test("started", (test: Test) =>
|
|
284
|
+
{
|
|
285
|
+
const values: number[] = [1, 2, 3];
|
|
286
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
287
|
+
|
|
288
|
+
test.assertEqual(values, iterator.toArray().await());
|
|
289
|
+
test.assertTrue(iterator.hasStarted());
|
|
290
|
+
test.assertFalse(iterator.hasCurrent());
|
|
291
|
+
|
|
292
|
+
test.assertEqual([], iterator.toArray().await());
|
|
293
|
+
test.assertTrue(iterator.hasStarted());
|
|
294
|
+
test.assertFalse(iterator.hasCurrent());
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
runner.testGroup("concatenate()", () =>
|
|
300
|
+
{
|
|
301
|
+
runner.testGroup("with non-started", () =>
|
|
302
|
+
{
|
|
303
|
+
runner.testGroup("empty and", () =>
|
|
304
|
+
{
|
|
305
|
+
runner.test("empty", (test: Test) =>
|
|
306
|
+
{
|
|
307
|
+
const iterator: Iterator<number> = Iterator.create([]);
|
|
308
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([]);
|
|
309
|
+
test.assertEqual(concatenateIterator.toArray().await(), []);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
runner.test("non-empty", (test: Test) =>
|
|
313
|
+
{
|
|
314
|
+
const iterator: Iterator<number> = Iterator.create([]);
|
|
315
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([5]);
|
|
316
|
+
test.assertEqual(concatenateIterator.toArray().await(), [5]);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
runner.test("two non-emptys", (test: Test) =>
|
|
320
|
+
{
|
|
321
|
+
const iterator: Iterator<number> = Iterator.create([]);
|
|
322
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([5], [6, 7]);
|
|
323
|
+
test.assertEqual(concatenateIterator.toArray().await(), [5, 6, 7]);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
runner.testGroup("non-empty and", () =>
|
|
328
|
+
{
|
|
329
|
+
runner.test("empty", (test: Test) =>
|
|
330
|
+
{
|
|
331
|
+
const iterator: Iterator<number> = Iterator.create([1, 2]);
|
|
332
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([]);
|
|
333
|
+
test.assertEqual(concatenateIterator.toArray().await(), [1, 2]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
runner.test("non-empty", (test: Test) =>
|
|
337
|
+
{
|
|
338
|
+
const iterator: Iterator<number> = Iterator.create([3]);
|
|
339
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([4, 5, 6]);
|
|
340
|
+
test.assertEqual(concatenateIterator.toArray().await(), [3, 4, 5, 6]);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
runner.test("two non-emptys", (test: Test) =>
|
|
344
|
+
{
|
|
345
|
+
const iterator: Iterator<number> = Iterator.create([7, 8, 9]);
|
|
346
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([10, 11, 12], [13]);
|
|
347
|
+
test.assertEqual(concatenateIterator.toArray().await(), [7, 8, 9, 10, 11, 12, 13]);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
runner.testGroup("with started", () =>
|
|
353
|
+
{
|
|
354
|
+
runner.testGroup("empty and", () =>
|
|
355
|
+
{
|
|
356
|
+
runner.test("empty", (test: Test) =>
|
|
357
|
+
{
|
|
358
|
+
const iterator: Iterator<number> = Iterator.create([]).start().await();
|
|
359
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([]);
|
|
360
|
+
test.assertEqual(concatenateIterator.toArray().await(), []);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
runner.test("non-empty", (test: Test) =>
|
|
364
|
+
{
|
|
365
|
+
const iterator: Iterator<number> = Iterator.create([]).start().await();
|
|
366
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([5]);
|
|
367
|
+
test.assertEqual(concatenateIterator.toArray().await(), [5]);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
runner.test("two non-emptys", (test: Test) =>
|
|
371
|
+
{
|
|
372
|
+
const iterator: Iterator<number> = Iterator.create([]).start().await();
|
|
373
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([5], [6, 7]);
|
|
374
|
+
test.assertEqual(concatenateIterator.toArray().await(), [5, 6, 7]);
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
runner.testGroup("non-empty and", () =>
|
|
379
|
+
{
|
|
380
|
+
runner.test("empty", (test: Test) =>
|
|
381
|
+
{
|
|
382
|
+
const iterator: Iterator<number> = Iterator.create([1, 2]).start().await();
|
|
383
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([]);
|
|
384
|
+
test.assertEqual(concatenateIterator.toArray().await(), [1, 2]);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
runner.test("non-empty", (test: Test) =>
|
|
388
|
+
{
|
|
389
|
+
const iterator: Iterator<number> = Iterator.create([3]).start().await();
|
|
390
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([4, 5, 6]);
|
|
391
|
+
test.assertEqual(concatenateIterator.toArray().await(), [3, 4, 5, 6]);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
runner.test("two non-emptys", (test: Test) =>
|
|
395
|
+
{
|
|
396
|
+
const iterator: Iterator<number> = Iterator.create([7, 8, 9]).start().await();
|
|
397
|
+
const concatenateIterator: Iterator<number> = iterator.concatenate([10, 11, 12], [13]);
|
|
398
|
+
test.assertEqual(concatenateIterator.toArray().await(), [7, 8, 9, 10, 11, 12, 13]);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
runner.testFunction("where()", () =>
|
|
405
|
+
{
|
|
406
|
+
runner.testGroup("with non-empty", () =>
|
|
407
|
+
{
|
|
408
|
+
runner.test("not started", (test: Test) =>
|
|
409
|
+
{
|
|
410
|
+
const values: number[] = [1, 2, 3, 4, 5];
|
|
411
|
+
const iterator: Iterator<number> = Iterator.create(values);
|
|
412
|
+
const result: Iterator<number> = iterator.where(x => x % 2 === 1);
|
|
413
|
+
test.assertNotUndefinedAndNotNull(result);
|
|
414
|
+
test.assertNotSame(result, iterator);
|
|
415
|
+
test.assertFalse(iterator.hasStarted());
|
|
416
|
+
test.assertFalse(iterator.hasCurrent());
|
|
417
|
+
test.assertFalse(result.hasStarted());
|
|
418
|
+
test.assertFalse(result.hasCurrent());
|
|
419
|
+
|
|
420
|
+
for (const expectedValue of [1, 3, 5])
|
|
421
|
+
{
|
|
422
|
+
test.assertTrue(result.next().await());
|
|
423
|
+
test.assertTrue(result.hasStarted());
|
|
424
|
+
test.assertTrue(result.hasCurrent());
|
|
425
|
+
test.assertEqual(expectedValue, result.getCurrent());
|
|
426
|
+
test.assertTrue(iterator.hasStarted());
|
|
427
|
+
test.assertTrue(iterator.hasCurrent());
|
|
428
|
+
test.assertEqual(expectedValue, iterator.getCurrent());
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
for (let i = 0; i < 2; i++)
|
|
432
|
+
{
|
|
433
|
+
test.assertFalse(result.next().await());
|
|
434
|
+
test.assertTrue(result.hasStarted());
|
|
435
|
+
test.assertFalse(result.hasCurrent());
|
|
436
|
+
test.assertTrue(iterator.hasStarted());
|
|
437
|
+
test.assertFalse(iterator.hasCurrent());
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
runner.test("started", (test: Test) =>
|
|
442
|
+
{
|
|
443
|
+
const values: number[] = [1, 2, 3, 4, 5];
|
|
444
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
445
|
+
const result: Iterator<number> = iterator.where(x => x % 2 === 1);
|
|
446
|
+
test.assertNotUndefinedAndNotNull(result);
|
|
447
|
+
test.assertNotSame(result, iterator);
|
|
448
|
+
test.assertTrue(iterator.hasStarted());
|
|
449
|
+
test.assertTrue(iterator.hasCurrent());
|
|
450
|
+
test.assertEqual(values[0], iterator.getCurrent())
|
|
451
|
+
test.assertFalse(result.hasStarted());
|
|
452
|
+
test.assertFalse(result.hasCurrent());
|
|
453
|
+
|
|
454
|
+
for (const expectedValue of [1, 3, 5])
|
|
455
|
+
{
|
|
456
|
+
test.assertTrue(result.next().await());
|
|
457
|
+
test.assertTrue(result.hasStarted());
|
|
458
|
+
test.assertTrue(result.hasCurrent());
|
|
459
|
+
test.assertEqual(expectedValue, result.getCurrent());
|
|
460
|
+
test.assertTrue(iterator.hasStarted());
|
|
461
|
+
test.assertTrue(iterator.hasCurrent());
|
|
462
|
+
test.assertEqual(expectedValue, iterator.getCurrent());
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
for (let i = 0; i < 2; i++)
|
|
466
|
+
{
|
|
467
|
+
test.assertFalse(result.next().await());
|
|
468
|
+
test.assertTrue(result.hasStarted());
|
|
469
|
+
test.assertFalse(result.hasCurrent());
|
|
470
|
+
test.assertTrue(iterator.hasStarted());
|
|
471
|
+
test.assertFalse(iterator.hasCurrent());
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
runner.testFunction("map()", () =>
|
|
478
|
+
{
|
|
479
|
+
runner.testGroup("with non-empty", () =>
|
|
480
|
+
{
|
|
481
|
+
runner.test("not started", (test: Test) =>
|
|
482
|
+
{
|
|
483
|
+
const values: number[] = [1, 2, 3];
|
|
484
|
+
const iterator: Iterator<number> = Iterator.create(values);
|
|
485
|
+
const result: Iterator<number> = iterator.map(x => x + 10);
|
|
486
|
+
test.assertFalse(iterator.hasStarted());
|
|
487
|
+
test.assertFalse(iterator.hasCurrent());
|
|
488
|
+
test.assertFalse(result.hasStarted());
|
|
489
|
+
test.assertFalse(result.hasCurrent());
|
|
490
|
+
|
|
491
|
+
for (const value of values)
|
|
492
|
+
{
|
|
493
|
+
test.assertTrue(result.next().await());
|
|
494
|
+
test.assertTrue(result.hasStarted());
|
|
495
|
+
test.assertTrue(result.hasCurrent());
|
|
496
|
+
test.assertEqual(value + 10, result.getCurrent());
|
|
497
|
+
test.assertTrue(iterator.hasStarted());
|
|
498
|
+
test.assertTrue(iterator.hasCurrent());
|
|
499
|
+
test.assertEqual(value, iterator.getCurrent());
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
for (let i = 0; i < 2; i++)
|
|
503
|
+
{
|
|
504
|
+
test.assertFalse(result.next().await());
|
|
505
|
+
test.assertTrue(result.hasStarted());
|
|
506
|
+
test.assertFalse(result.hasCurrent());
|
|
507
|
+
test.assertTrue(iterator.hasStarted());
|
|
508
|
+
test.assertFalse(iterator.hasCurrent());
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
runner.test("started", (test: Test) =>
|
|
513
|
+
{
|
|
514
|
+
const values: number[] = [1, 2, 3];
|
|
515
|
+
const iterator: Iterator<number> = Iterator.create(values).start().await();
|
|
516
|
+
const result: Iterator<number> = iterator.map(x => x + 10);
|
|
517
|
+
test.assertTrue(iterator.hasStarted());
|
|
518
|
+
test.assertTrue(iterator.hasCurrent());
|
|
519
|
+
test.assertEqual(values[0], iterator.getCurrent());
|
|
520
|
+
test.assertFalse(result.hasStarted());
|
|
521
|
+
test.assertFalse(result.hasCurrent());
|
|
522
|
+
|
|
523
|
+
for (const value of values)
|
|
524
|
+
{
|
|
525
|
+
test.assertTrue(result.next().await());
|
|
526
|
+
test.assertTrue(result.hasStarted());
|
|
527
|
+
test.assertTrue(result.hasCurrent());
|
|
528
|
+
test.assertEqual(value + 10, result.getCurrent());
|
|
529
|
+
test.assertTrue(iterator.hasStarted());
|
|
530
|
+
test.assertTrue(iterator.hasCurrent());
|
|
531
|
+
test.assertEqual(value, iterator.getCurrent());
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
for (let i = 0; i < 2; i++)
|
|
535
|
+
{
|
|
536
|
+
test.assertFalse(result.next().await());
|
|
537
|
+
test.assertTrue(result.hasStarted());
|
|
538
|
+
test.assertFalse(result.hasCurrent());
|
|
539
|
+
test.assertTrue(iterator.hasStarted());
|
|
540
|
+
test.assertFalse(iterator.hasCurrent());
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
runner.testFunction("flatMap()", () =>
|
|
547
|
+
{
|
|
548
|
+
runner.test("with empty non-started iterator", (test: Test) =>
|
|
549
|
+
{
|
|
550
|
+
const iterator: Iterator<number> = Iterator.create([]);
|
|
551
|
+
const flatMapIterator: Iterator<string> = iterator.flatMap((value: number) =>
|
|
552
|
+
{
|
|
553
|
+
const result: string[] = [];
|
|
554
|
+
for (let i = 0; i < value; i++)
|
|
555
|
+
{
|
|
556
|
+
result.push(value.toString());
|
|
557
|
+
}
|
|
558
|
+
return result;
|
|
559
|
+
});
|
|
560
|
+
test.assertEqual(flatMapIterator.toArray().await(), []);
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
runner.test("with empty started iterator", (test: Test) =>
|
|
564
|
+
{
|
|
565
|
+
const iterator: Iterator<number> = Iterator.create([]).start().await();
|
|
566
|
+
const flatMapIterator: Iterator<string> = iterator.flatMap((value: number) =>
|
|
567
|
+
{
|
|
568
|
+
const result: string[] = [];
|
|
569
|
+
for (let i = 0; i < value; i++)
|
|
570
|
+
{
|
|
571
|
+
result.push(value.toString());
|
|
572
|
+
}
|
|
573
|
+
return result;
|
|
574
|
+
});
|
|
575
|
+
test.assertEqual(flatMapIterator.toArray().await(), []);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
runner.test("with non-empty non-started iterator", (test: Test) =>
|
|
579
|
+
{
|
|
580
|
+
const iterator: Iterator<number> = Iterator.create([1, 2, 3]);
|
|
581
|
+
const flatMapIterator: Iterator<string> = iterator.flatMap((value: number) =>
|
|
582
|
+
{
|
|
583
|
+
const result: string[] = [];
|
|
584
|
+
for (let i = 0; i < value; i++)
|
|
585
|
+
{
|
|
586
|
+
result.push(value.toString());
|
|
587
|
+
}
|
|
588
|
+
return result;
|
|
589
|
+
});
|
|
590
|
+
test.assertEqual(flatMapIterator.toArray().await(), ["1", "2", "2", "3", "3", "3"]);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
runner.test("with non-empty started iterator", (test: Test) =>
|
|
594
|
+
{
|
|
595
|
+
const iterator: Iterator<number> = Iterator.create([1, 2, 3]).start().await();
|
|
596
|
+
const flatMapIterator: Iterator<string> = iterator.flatMap((value: number) =>
|
|
597
|
+
{
|
|
598
|
+
const result: string[] = [];
|
|
599
|
+
for (let i = 0; i < value; i++)
|
|
600
|
+
{
|
|
601
|
+
result.push(value.toString());
|
|
602
|
+
}
|
|
603
|
+
return result;
|
|
604
|
+
});
|
|
605
|
+
test.assertEqual(flatMapIterator.toArray().await(), ["1", "2", "2", "3", "3", "3"]);
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
runner.test("with mapping that sometimes returns an empty iterable", (test: Test) =>
|
|
609
|
+
{
|
|
610
|
+
const iterator: Iterator<number> = Iterator.create([1, 2, 3, 4]).start().await();
|
|
611
|
+
const flatMapIterator: Iterator<string> = iterator.flatMap((value: number) =>
|
|
612
|
+
{
|
|
613
|
+
const result: string[] = [];
|
|
614
|
+
if (value % 2 === 1)
|
|
615
|
+
{
|
|
616
|
+
for (let i = 0; i < value; i++)
|
|
617
|
+
{
|
|
618
|
+
result.push(value.toString());
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return result;
|
|
622
|
+
});
|
|
623
|
+
test.assertEqual(flatMapIterator.toArray().await(), ["1", "3", "3", "3"]);
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
runner.testFunction("first()", () =>
|
|
628
|
+
{
|
|
629
|
+
runner.testGroup("with no condition", () =>
|
|
630
|
+
{
|
|
631
|
+
function firstErrorTest(iterable: JavascriptIterable<string>, expected: Error): void
|
|
632
|
+
{
|
|
633
|
+
runner.test(`with ${runner.toString(iterable)}`, (test: Test) =>
|
|
634
|
+
{
|
|
635
|
+
const iterator: Iterator<string> = Iterator.create(iterable);
|
|
636
|
+
for (let i = 0; i < 3; i++)
|
|
637
|
+
{
|
|
638
|
+
test.assertThrows(() => { iterator.first().await(); }, expected);
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
firstErrorTest(
|
|
644
|
+
[],
|
|
645
|
+
new EmptyError(),
|
|
646
|
+
);
|
|
647
|
+
|
|
648
|
+
function firstTest<T>(iterable: JavascriptIterable<T>, expected: T): void
|
|
649
|
+
{
|
|
650
|
+
runner.test(`with ${runner.toString(iterable)}`, (test: Test) =>
|
|
651
|
+
{
|
|
652
|
+
const iterator: Iterator<T> = Iterator.create(iterable);
|
|
653
|
+
for (let i = 0; i < 3; i++)
|
|
654
|
+
{
|
|
655
|
+
test.assertEqual(iterator.first().await(), expected);
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
firstTest([1], 1);
|
|
661
|
+
firstTest([2, 3], 2);
|
|
662
|
+
firstTest([4, 5, 6, 7, 8], 4);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
runner.testGroup("with condition argument", () =>
|
|
666
|
+
{
|
|
667
|
+
function firstErrorTest<T>(iterable: JavascriptIterable<T>, condition: (value: T) => boolean, expected: Error): void
|
|
668
|
+
{
|
|
669
|
+
runner.test(`with ${runner.toString(iterable)} and ${condition?.name} condition`, (test: Test) =>
|
|
670
|
+
{
|
|
671
|
+
const iterator: Iterator<T> = Iterator.create(iterable);
|
|
672
|
+
for (let i = 0; i < 3; i++)
|
|
673
|
+
{
|
|
674
|
+
test.assertThrows(() =>
|
|
675
|
+
{
|
|
676
|
+
iterator.first(condition).await();
|
|
677
|
+
}, expected);
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function isOdd(value: number): boolean
|
|
683
|
+
{
|
|
684
|
+
return value % 2 === 1;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function isEven(value: number): boolean
|
|
688
|
+
{
|
|
689
|
+
return value % 2 === 0;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
firstErrorTest(
|
|
693
|
+
[],
|
|
694
|
+
undefined!,
|
|
695
|
+
new EmptyError(),
|
|
696
|
+
);
|
|
697
|
+
firstErrorTest(
|
|
698
|
+
[],
|
|
699
|
+
null!,
|
|
700
|
+
new EmptyError(),
|
|
701
|
+
);
|
|
702
|
+
firstErrorTest(
|
|
703
|
+
[1],
|
|
704
|
+
isEven,
|
|
705
|
+
new NotFoundError("No value was found in the Iterator that matched the provided condition."),
|
|
706
|
+
);
|
|
707
|
+
firstErrorTest(
|
|
708
|
+
[2, 4],
|
|
709
|
+
isOdd,
|
|
710
|
+
new NotFoundError("No value was found in the Iterator that matched the provided condition."),
|
|
711
|
+
);
|
|
712
|
+
|
|
713
|
+
function firstTest<T>(iterable: JavascriptIterable<T>, condition: (value: T) => boolean, expected: T): void
|
|
714
|
+
{
|
|
715
|
+
runner.test(`with ${runner.toString(iterable)} and ${condition?.name} condition`, (test: Test) =>
|
|
716
|
+
{
|
|
717
|
+
const iterator: Iterator<T> = Iterator.create(iterable);
|
|
718
|
+
for (let i = 0; i < 3; i++)
|
|
719
|
+
{
|
|
720
|
+
test.assertEqual(iterator.first(condition).await(), expected);
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
firstTest([1], isOdd, 1);
|
|
726
|
+
firstTest([2, 3], isEven, 2);
|
|
727
|
+
firstTest([2, 3], isOdd, 3);
|
|
728
|
+
firstTest([4, 6, 7, 8], isEven, 4);
|
|
729
|
+
firstTest([4, 6, 7, 8], isOdd, 7);
|
|
730
|
+
});
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
runner.testFunction("skip(number)", () =>
|
|
734
|
+
{
|
|
735
|
+
function skipErrorTest(iterable: JavascriptIterable<string>, maximumToSkip: number, expected: Error): void
|
|
736
|
+
{
|
|
737
|
+
runner.test(`with ${runner.andList([iterable, maximumToSkip])}`, (test: Test) =>
|
|
738
|
+
{
|
|
739
|
+
const iterator: Iterator<string> = Iterator.create(iterable);
|
|
740
|
+
test.assertThrows(() => iterator.skip(maximumToSkip), expected);
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
skipErrorTest([], undefined!, new PreConditionError(
|
|
745
|
+
"Expression: maximumToSkip",
|
|
746
|
+
"Expected: not undefined and not null",
|
|
747
|
+
"Actual: undefined",
|
|
748
|
+
));
|
|
749
|
+
skipErrorTest([], null!, new PreConditionError(
|
|
750
|
+
"Expression: maximumToSkip",
|
|
751
|
+
"Expected: not undefined and not null",
|
|
752
|
+
"Actual: null",
|
|
753
|
+
));
|
|
754
|
+
skipErrorTest([], 0.5, new PreConditionError(
|
|
755
|
+
"Expression: maximumToSkip",
|
|
756
|
+
"Expected: integer",
|
|
757
|
+
"Actual: 0.5",
|
|
758
|
+
));
|
|
759
|
+
skipErrorTest([], -1, new PreConditionError(
|
|
760
|
+
"Expression: maximumToSkip",
|
|
761
|
+
"Expected: greater than or equal to 0",
|
|
762
|
+
"Actual: -1",
|
|
763
|
+
));
|
|
764
|
+
|
|
765
|
+
function skipTest(iterable: JavascriptIterable<string>, maximumToSkip: number, expected: JavascriptIterable<string>): void
|
|
766
|
+
{
|
|
767
|
+
runner.test(`with ${runner.andList([iterable, maximumToSkip])}`, (test: Test) =>
|
|
768
|
+
{
|
|
769
|
+
const iterator: Iterator<string> = Iterator.create(iterable);
|
|
770
|
+
const skipIterator: Iterator<string> = iterator.skip(maximumToSkip);
|
|
771
|
+
test.assertEqual(expected, skipIterator.toArray().await());
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
skipTest([], 0, []);
|
|
776
|
+
skipTest([], 1, []);
|
|
777
|
+
skipTest([], 2, []);
|
|
778
|
+
skipTest(["a", "b", "c"], 0, ["a", "b", "c"]);
|
|
779
|
+
skipTest(["a", "b", "c"], 1, ["b", "c"]);
|
|
780
|
+
skipTest(["a", "b", "c"], 2, ["c"]);
|
|
781
|
+
skipTest(["a", "b", "c"], 3, []);
|
|
782
|
+
skipTest(["a", "b", "c"], 4, []);
|
|
783
|
+
});
|
|
784
|
+
});
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
export function iteratorTests<T>(runner: TestRunner, creator: () => Iterator<T>): void
|
|
789
|
+
{
|
|
790
|
+
runner.testType("Iterator<T>", () =>
|
|
791
|
+
{
|
|
792
|
+
runner.testFunction("creator()", (test: Test) =>
|
|
793
|
+
{
|
|
794
|
+
const iterator: Iterator<T> = creator();
|
|
795
|
+
test.assertNotUndefinedAndNotNull(iterator);
|
|
796
|
+
test.assertFalse(iterator.hasStarted());
|
|
797
|
+
test.assertFalse(iterator.hasCurrent());
|
|
798
|
+
test.assertThrows(() => iterator.getCurrent(), new PreConditionError(
|
|
799
|
+
"Expression: this.hasCurrent()",
|
|
800
|
+
"Expected: true",
|
|
801
|
+
"Actual: false",
|
|
802
|
+
));
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
runner.testFunction("start()", (test: Test) =>
|
|
806
|
+
{
|
|
807
|
+
const iterator: Iterator<T> = creator();
|
|
808
|
+
for (let i: number = 0; i < 2; i++)
|
|
809
|
+
{
|
|
810
|
+
const startResult: Iterator<T> = iterator.start().await();
|
|
811
|
+
test.assertSame(startResult, iterator);
|
|
812
|
+
test.assertTrue(iterator.hasStarted());
|
|
813
|
+
test.assertFalse(iterator.hasCurrent());
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
runner.testFunction("takeCurrent()", () =>
|
|
818
|
+
{
|
|
819
|
+
runner.testGroup("with empty", () =>
|
|
820
|
+
{
|
|
821
|
+
runner.test("not started", (test: Test) =>
|
|
822
|
+
{
|
|
823
|
+
const iterator: Iterator<T> = creator();
|
|
824
|
+
for (let i = 0; i < 2; i++)
|
|
825
|
+
{
|
|
826
|
+
test.assertThrows(() => iterator.takeCurrent(),
|
|
827
|
+
new PreConditionError(
|
|
828
|
+
"Expression: iterator.hasCurrent()",
|
|
829
|
+
"Expected: true",
|
|
830
|
+
"Actual: false",
|
|
831
|
+
));
|
|
832
|
+
test.assertFalse(iterator.hasStarted());
|
|
833
|
+
test.assertFalse(iterator.hasCurrent());
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
runner.test("started", (test: Test) =>
|
|
838
|
+
{
|
|
839
|
+
const iterator: Iterator<T> = creator().start().await();
|
|
840
|
+
test.assertTrue(iterator.hasStarted());
|
|
841
|
+
test.assertFalse(iterator.hasCurrent());
|
|
842
|
+
|
|
843
|
+
for (let i = 0; i < 2; i++)
|
|
844
|
+
{
|
|
845
|
+
test.assertThrows(() => iterator.takeCurrent(),
|
|
846
|
+
new PreConditionError(
|
|
847
|
+
"Expression: iterator.hasCurrent()",
|
|
848
|
+
"Expected: true",
|
|
849
|
+
"Actual: false",
|
|
850
|
+
));
|
|
851
|
+
test.assertTrue(iterator.hasStarted());
|
|
852
|
+
test.assertFalse(iterator.hasCurrent());
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
runner.testFunction("next()", () =>
|
|
859
|
+
{
|
|
860
|
+
runner.testGroup("with empty", () =>
|
|
861
|
+
{
|
|
862
|
+
runner.test("not started", (test: Test) =>
|
|
863
|
+
{
|
|
864
|
+
const iterator: Iterator<T> = creator();
|
|
865
|
+
test.assertFalse(iterator.hasStarted());
|
|
866
|
+
test.assertFalse(iterator.hasCurrent());
|
|
867
|
+
|
|
868
|
+
for (let i = 0; i < 2; i++)
|
|
869
|
+
{
|
|
870
|
+
test.assertFalse(iterator.next().await());
|
|
871
|
+
test.assertTrue(iterator.hasStarted());
|
|
872
|
+
test.assertFalse(iterator.hasCurrent());
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
|
|
876
|
+
runner.test("started", (test: Test) =>
|
|
877
|
+
{
|
|
878
|
+
const iterator: Iterator<T> = creator().start().await();
|
|
879
|
+
test.assertTrue(iterator.hasStarted());
|
|
880
|
+
test.assertFalse(iterator.hasCurrent());
|
|
881
|
+
|
|
882
|
+
for (let i = 0; i < 2; i++)
|
|
883
|
+
{
|
|
884
|
+
test.assertFalse(iterator.next().await());
|
|
885
|
+
test.assertTrue(iterator.hasStarted());
|
|
886
|
+
test.assertFalse(iterator.hasCurrent());
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
runner.testFunction("[Symbol.iterator]()", (test: Test) =>
|
|
894
|
+
{
|
|
895
|
+
const iterator: Iterator<T> = creator();
|
|
896
|
+
test.assertFalse(iterator.hasStarted());
|
|
897
|
+
test.assertFalse(iterator.hasCurrent());
|
|
898
|
+
|
|
899
|
+
const jsIterator: JavascriptIterator<T> = iterator[Symbol.iterator]();
|
|
900
|
+
test.assertFalse(iterator.hasStarted());
|
|
901
|
+
test.assertFalse(iterator.hasCurrent());
|
|
902
|
+
|
|
903
|
+
for (let i = 0; i < 2; i++)
|
|
904
|
+
{
|
|
905
|
+
const result: JavascriptIteratorResult<T> = jsIterator.next();
|
|
906
|
+
test.assertEqual(result.done, true);
|
|
907
|
+
test.assertUndefined(result.value);
|
|
908
|
+
test.assertTrue(iterator.hasStarted());
|
|
909
|
+
test.assertFalse(iterator.hasCurrent());
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
runner.testFunction("any()", (test: Test) =>
|
|
914
|
+
{
|
|
915
|
+
const iterator: Iterator<T> = creator();
|
|
916
|
+
test.assertFalse(iterator.hasStarted());
|
|
917
|
+
test.assertFalse(iterator.hasCurrent());
|
|
918
|
+
|
|
919
|
+
test.assertFalse(iterator.any().await());
|
|
920
|
+
|
|
921
|
+
test.assertTrue(iterator.hasStarted());
|
|
922
|
+
test.assertFalse(iterator.hasCurrent());
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
runner.testFunction("getCount()", (test: Test) =>
|
|
926
|
+
{
|
|
927
|
+
const iterator: Iterator<T> = creator();
|
|
928
|
+
test.assertFalse(iterator.hasStarted());
|
|
929
|
+
test.assertFalse(iterator.hasCurrent());
|
|
930
|
+
|
|
931
|
+
for (let i = 0; i < 2; i++)
|
|
932
|
+
{
|
|
933
|
+
test.assertEqual(iterator.getCount().await(), 0);
|
|
934
|
+
test.assertTrue(iterator.hasStarted());
|
|
935
|
+
test.assertFalse(iterator.hasCurrent());
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
runner.testFunction("toArray()", (test: Test) =>
|
|
940
|
+
{
|
|
941
|
+
const iterator: Iterator<T> = creator();
|
|
942
|
+
test.assertFalse(iterator.hasStarted());
|
|
943
|
+
test.assertFalse(iterator.hasCurrent());
|
|
944
|
+
|
|
945
|
+
for (let i = 0; i < 2; i++)
|
|
946
|
+
{
|
|
947
|
+
const array: T[] = iterator.toArray().await();
|
|
948
|
+
test.assertEqual(array, []);
|
|
949
|
+
test.assertTrue(iterator.hasStarted());
|
|
950
|
+
test.assertFalse(iterator.hasCurrent());
|
|
951
|
+
}
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
runner.testFunction("where()", () =>
|
|
955
|
+
{
|
|
956
|
+
runner.test("with undefined condition", (test: Test) =>
|
|
957
|
+
{
|
|
958
|
+
const iterator: Iterator<T> = creator();
|
|
959
|
+
test.assertThrows(() => iterator.where(undefined!), new PreConditionError(
|
|
960
|
+
"Expression: condition",
|
|
961
|
+
"Expected: not undefined and not null",
|
|
962
|
+
"Actual: undefined",
|
|
963
|
+
));
|
|
964
|
+
test.assertFalse(iterator.hasStarted());
|
|
965
|
+
test.assertFalse(iterator.hasCurrent());
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
runner.test("with null condition", (test: Test) =>
|
|
969
|
+
{
|
|
970
|
+
const iterator: Iterator<T> = creator();
|
|
971
|
+
test.assertThrows(() => iterator.where(null!), new PreConditionError(
|
|
972
|
+
"Expression: condition",
|
|
973
|
+
"Expected: not undefined and not null",
|
|
974
|
+
"Actual: null",
|
|
975
|
+
));
|
|
976
|
+
test.assertFalse(iterator.hasStarted());
|
|
977
|
+
test.assertFalse(iterator.hasCurrent());
|
|
978
|
+
});
|
|
979
|
+
|
|
980
|
+
runner.testGroup("with empty", () =>
|
|
981
|
+
{
|
|
982
|
+
runner.test("not started", (test: Test) =>
|
|
983
|
+
{
|
|
984
|
+
const iterator: Iterator<T> = creator();
|
|
985
|
+
|
|
986
|
+
const result: Iterator<T> = iterator.where((value: T) => value !== undefined);
|
|
987
|
+
test.assertNotUndefinedAndNotNull(result);
|
|
988
|
+
test.assertNotSame(result, iterator);
|
|
989
|
+
test.assertFalse(result.hasStarted());
|
|
990
|
+
test.assertFalse(result.hasCurrent());
|
|
991
|
+
test.assertFalse(iterator.hasStarted());
|
|
992
|
+
test.assertFalse(iterator.hasCurrent());
|
|
993
|
+
|
|
994
|
+
test.assertFalse(result.next().await());
|
|
995
|
+
test.assertTrue(result.hasStarted());
|
|
996
|
+
test.assertFalse(result.hasCurrent());
|
|
997
|
+
test.assertTrue(iterator.hasStarted());
|
|
998
|
+
test.assertFalse(iterator.hasCurrent());
|
|
999
|
+
});
|
|
1000
|
+
|
|
1001
|
+
runner.test("started", (test: Test) =>
|
|
1002
|
+
{
|
|
1003
|
+
const iterator: Iterator<T> = creator().start().await();
|
|
1004
|
+
test.assertTrue(iterator.hasStarted());
|
|
1005
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1006
|
+
|
|
1007
|
+
const result: Iterator<T> = iterator.where((value: T) => value !== undefined);
|
|
1008
|
+
test.assertNotUndefinedAndNotNull(result);
|
|
1009
|
+
test.assertNotSame(result, iterator);
|
|
1010
|
+
test.assertFalse(result.hasStarted());
|
|
1011
|
+
test.assertFalse(result.hasCurrent());
|
|
1012
|
+
test.assertTrue(iterator.hasStarted());
|
|
1013
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1014
|
+
|
|
1015
|
+
test.assertFalse(result.next().await());
|
|
1016
|
+
test.assertTrue(result.hasStarted());
|
|
1017
|
+
test.assertFalse(result.hasCurrent());
|
|
1018
|
+
test.assertTrue(iterator.hasStarted());
|
|
1019
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1020
|
+
});
|
|
1021
|
+
});
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
runner.testFunction("map()", () =>
|
|
1025
|
+
{
|
|
1026
|
+
runner.test("with undefined", (test: Test) =>
|
|
1027
|
+
{
|
|
1028
|
+
const iterator: Iterator<T> = creator();
|
|
1029
|
+
test.assertThrows(() => iterator.map(undefined!), new PreConditionError(
|
|
1030
|
+
"Expression: mapping",
|
|
1031
|
+
"Expected: not undefined and not null",
|
|
1032
|
+
"Actual: undefined",
|
|
1033
|
+
));
|
|
1034
|
+
test.assertFalse(iterator.hasStarted());
|
|
1035
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
runner.test("with null", (test: Test) =>
|
|
1039
|
+
{
|
|
1040
|
+
const iterator: Iterator<T> = creator();
|
|
1041
|
+
test.assertThrows(() => iterator.map(null!), new PreConditionError(
|
|
1042
|
+
"Expression: mapping",
|
|
1043
|
+
"Expected: not undefined and not null",
|
|
1044
|
+
"Actual: null",
|
|
1045
|
+
));
|
|
1046
|
+
test.assertFalse(iterator.hasStarted());
|
|
1047
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
runner.testGroup("with empty", () =>
|
|
1051
|
+
{
|
|
1052
|
+
runner.test("not started", (test: Test) =>
|
|
1053
|
+
{
|
|
1054
|
+
const iterator: Iterator<T> = creator();
|
|
1055
|
+
test.assertFalse(iterator.hasStarted());
|
|
1056
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1057
|
+
|
|
1058
|
+
const mapIterator: Iterator<number> = iterator.map(_ => 5);
|
|
1059
|
+
test.assertFalse(mapIterator.hasStarted());
|
|
1060
|
+
test.assertFalse(mapIterator.hasCurrent());
|
|
1061
|
+
test.assertFalse(iterator.hasStarted());
|
|
1062
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
runner.test("started", (test: Test) =>
|
|
1066
|
+
{
|
|
1067
|
+
const iterator: Iterator<T> = creator().start().await();
|
|
1068
|
+
test.assertTrue(iterator.hasStarted());
|
|
1069
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1070
|
+
|
|
1071
|
+
const mapIterator: Iterator<number> = iterator.map(_ => 5);
|
|
1072
|
+
test.assertFalse(mapIterator.hasStarted());
|
|
1073
|
+
test.assertFalse(mapIterator.hasCurrent());
|
|
1074
|
+
test.assertTrue(iterator.hasStarted());
|
|
1075
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1076
|
+
|
|
1077
|
+
test.assertFalse(mapIterator.next().await());
|
|
1078
|
+
test.assertTrue(mapIterator.hasStarted());
|
|
1079
|
+
test.assertFalse(mapIterator.hasCurrent());
|
|
1080
|
+
test.assertTrue(iterator.hasStarted());
|
|
1081
|
+
test.assertFalse(iterator.hasCurrent());
|
|
1082
|
+
});
|
|
1083
|
+
});
|
|
1084
|
+
});
|
|
1085
|
+
});
|
|
1086
|
+
}
|