@everyonesoftware/common 2.0.0 → 3.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/.github/workflows/publish.yml +54 -36
- package/package.json +6 -2
- package/sources/asyncIterator.ts +436 -436
- package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +47 -47
- package/sources/asyncResult.ts +95 -95
- package/sources/byteList.ts +201 -201
- package/sources/byteListStream.ts +120 -120
- package/sources/byteReadStream.ts +23 -23
- package/sources/byteWriteStream.ts +15 -15
- package/sources/characterList.ts +194 -194
- package/sources/characterListStream.ts +150 -150
- package/sources/characterReadStream.ts +80 -80
- package/sources/characterReadStreamIterator.ts +127 -127
- package/sources/concatenateIterable.ts +118 -118
- package/sources/concatenateIterator.ts +164 -164
- package/sources/currentProcess.ts +157 -157
- package/sources/dateTime.ts +129 -129
- package/sources/depthFirstSearch.ts +229 -229
- package/sources/flatMapIterable.ts +103 -103
- package/sources/flatMapIterator.ts +151 -151
- package/sources/generator.ts +250 -250
- package/sources/index.ts +1 -1
- package/sources/iterator.ts +480 -480
- package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +123 -123
- package/sources/javascriptSetSet.ts +133 -133
- package/sources/listQueue.ts +61 -61
- package/sources/listStack.ts +61 -61
- package/sources/luxonDateTime.ts +108 -108
- package/sources/mapAsyncIterator.ts +140 -140
- package/sources/mutableMap.ts +291 -291
- package/sources/node.ts +36 -36
- package/sources/promiseAsyncResult.ts +173 -173
- package/sources/queue.ts +48 -48
- package/sources/recreationDotGovClient.ts +258 -258
- package/sources/searchControl.ts +41 -41
- package/sources/set.ts +243 -243
- package/sources/skipAsyncIterator.ts +144 -144
- package/sources/stack.ts +47 -47
- package/sources/syncResult.ts +299 -299
- package/sources/takeAsyncIterator.ts +140 -140
- package/sources/whereAsyncIterator.ts +142 -142
- package/sources/wonderlandTrailClient.ts +1502 -1502
- package/tests/assertTestTests.ts +74 -74
- package/tests/byteListStreamTests.ts +389 -389
- package/tests/byteListTests.ts +26 -26
- package/tests/characterListStreamTests.ts +390 -390
- package/tests/characterListTests.ts +249 -249
- package/tests/dateTimeTests.ts +29 -29
- package/tests/depthFirstSearchTests.ts +105 -105
- package/tests/generatorTests.ts +85 -85
- package/tests/mutableMapTests.ts +153 -153
- package/tests/promiseAsyncResultTests.ts +687 -687
- package/tests/queueTests.ts +28 -28
- package/tests/recreationDotGovClientTests.ts +190 -190
- package/tests/setTests.ts +139 -139
- package/tests/stackTests.ts +65 -65
- package/tests/syncResultTests.ts +1250 -1250
- package/tests/wonderlandTrailClientTests.ts +451 -451
- package/tsup.config.ts +12 -12
|
@@ -1,688 +1,688 @@
|
|
|
1
|
-
import { PromiseAsyncResult } from "../sources/promiseAsyncResult";
|
|
2
|
-
import { NotFoundError } from "../sources/notFoundError";
|
|
3
|
-
import { PreConditionError } from "../sources/preConditionError";
|
|
4
|
-
import { Test } from "./test";
|
|
5
|
-
import { TestRunner } from "./testRunner";
|
|
6
|
-
|
|
7
|
-
export function test(runner: TestRunner): void
|
|
8
|
-
{
|
|
9
|
-
runner.testFile("promiseAsyncResult.ts", () =>
|
|
10
|
-
{
|
|
11
|
-
runner.testType("PromiseAsyncResult<T>", () =>
|
|
12
|
-
{
|
|
13
|
-
runner.testFunction("create()", () =>
|
|
14
|
-
{
|
|
15
|
-
function createErrorTest<T>(testName: string, promise: Promise<T>, expected: Error): void
|
|
16
|
-
{
|
|
17
|
-
runner.test(testName, (test: Test) =>
|
|
18
|
-
{
|
|
19
|
-
test.assertThrows(() => PromiseAsyncResult.create(promise), expected);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
createErrorTest("with undefined", undefined!, new PreConditionError(
|
|
24
|
-
"Expression: action or promise",
|
|
25
|
-
"Expected: not undefined and not null",
|
|
26
|
-
"Actual: undefined",
|
|
27
|
-
));
|
|
28
|
-
createErrorTest("with null", null!, new PreConditionError(
|
|
29
|
-
"Expression: action or promise",
|
|
30
|
-
"Expected: not undefined and not null",
|
|
31
|
-
"Actual: null",
|
|
32
|
-
));
|
|
33
|
-
|
|
34
|
-
runner.test("with Promise", async (test: Test) =>
|
|
35
|
-
{
|
|
36
|
-
const result: PromiseAsyncResult<number> = PromiseAsyncResult.create(Promise.resolve(5));
|
|
37
|
-
test.assertNotUndefinedAndNotNull(result);
|
|
38
|
-
|
|
39
|
-
const value: number = await result;
|
|
40
|
-
test.assertEqual(5, value);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
runner.testFunction("then()", () =>
|
|
45
|
-
{
|
|
46
|
-
runner.test("with error parent", async (test: Test) =>
|
|
47
|
-
{
|
|
48
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
49
|
-
const thenResult: PromiseAsyncResult<string> = parentResult.then(() => "hello");
|
|
50
|
-
await test.assertThrowsAsync(thenResult, new Error("abc"));
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
runner.test("with error parent and thenFunction with side-effects", async (test: Test) =>
|
|
54
|
-
{
|
|
55
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
56
|
-
let counter: number = 0;
|
|
57
|
-
const thenResult: PromiseAsyncResult<string> = parentResult.then(() => { counter++; return "hello"; });
|
|
58
|
-
test.assertEqual(0, counter);
|
|
59
|
-
await test.assertThrowsAsync(thenResult, new Error("abc"));
|
|
60
|
-
test.assertEqual(0, counter, "counter should still be 0 because the thenFunction shouldn't be invoked.");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
runner.test("with successful parent and successful thenFunction that ignores parentResult value", async (test: Test) =>
|
|
64
|
-
{
|
|
65
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
66
|
-
const thenResult: PromiseAsyncResult<string> = parentResult.then(() => "hello");
|
|
67
|
-
test.assertEqual("hello", await thenResult);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
runner.test("with successful parent and successful thenFunction that uses parentResult value", async (test: Test) =>
|
|
71
|
-
{
|
|
72
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
73
|
-
const thenResult: PromiseAsyncResult<string> = parentResult.then((argument: number) => (argument + 1).toString());
|
|
74
|
-
test.assertEqual("2", await thenResult);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
runner.test("with successful parent and successful thenFunction that uses parentResult value with side-effects", async (test: Test) =>
|
|
78
|
-
{
|
|
79
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
80
|
-
let counter: number = 0;
|
|
81
|
-
const thenResult: PromiseAsyncResult<string> = parentResult.then((argument: number) => { counter++; return (argument + 1).toString(); });
|
|
82
|
-
test.assertEqual(0, counter);
|
|
83
|
-
|
|
84
|
-
await PromiseAsyncResult.yield();
|
|
85
|
-
test.assertEqual(1, counter);
|
|
86
|
-
|
|
87
|
-
test.assertEqual("2", await thenResult);
|
|
88
|
-
test.assertEqual(1, counter);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
runner.test("with successful parent and thenFunction that throws", async (test: Test) =>
|
|
92
|
-
{
|
|
93
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(10);
|
|
94
|
-
let counter: number = 0;
|
|
95
|
-
const thenResult: PromiseAsyncResult<string> = parentResult.then((argument: number) =>
|
|
96
|
-
{
|
|
97
|
-
counter++;
|
|
98
|
-
throw new Error(`arg: ${argument}, ${counter}`);
|
|
99
|
-
});
|
|
100
|
-
test.assertEqual(counter, 0);
|
|
101
|
-
|
|
102
|
-
await PromiseAsyncResult.yield();
|
|
103
|
-
test.assertEqual(counter, 1);
|
|
104
|
-
|
|
105
|
-
for (let i = 0; i < 3; i++)
|
|
106
|
-
{
|
|
107
|
-
await test.assertThrowsAsync(thenResult, new Error("arg: 10, 1"));
|
|
108
|
-
test.assertEqual(counter, 1);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
runner.testFunction("onValue()", () =>
|
|
114
|
-
{
|
|
115
|
-
runner.test("with error parent", async (test: Test) =>
|
|
116
|
-
{
|
|
117
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
118
|
-
let counter: number = 0;
|
|
119
|
-
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue(() => { counter++; });
|
|
120
|
-
test.assertSame(counter, 0);
|
|
121
|
-
for (let i = 0; i < 3; i++)
|
|
122
|
-
{
|
|
123
|
-
await test.assertThrowsAsync(onValueResult, new Error("abc"));
|
|
124
|
-
test.assertSame(counter, 0);
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
runner.test("with successful parent and successful thenFunction that ignores parentResult value", async (test: Test) =>
|
|
129
|
-
{
|
|
130
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(10);
|
|
131
|
-
let counter: number = 0;
|
|
132
|
-
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue(() => { counter++; });
|
|
133
|
-
test.assertSame(counter, 0);
|
|
134
|
-
for (let i = 0; i < 3; i++)
|
|
135
|
-
{
|
|
136
|
-
test.assertSame(10, await onValueResult);
|
|
137
|
-
test.assertSame(counter, 1);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
runner.test("with successful parent and successful thenFunction that uses parentResult value", async (test: Test) =>
|
|
142
|
-
{
|
|
143
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(2);
|
|
144
|
-
let counter: number = 0;
|
|
145
|
-
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue((argument: number) => { counter += argument; });
|
|
146
|
-
test.assertSame(counter, 0);
|
|
147
|
-
for (let i = 0; i < 3; i++)
|
|
148
|
-
{
|
|
149
|
-
test.assertSame(await onValueResult, 2);
|
|
150
|
-
test.assertSame(counter, 2);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
runner.test("with successful parent and onValueFunction that throws", async (test: Test) =>
|
|
155
|
-
{
|
|
156
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(2);
|
|
157
|
-
let counter: number = 0;
|
|
158
|
-
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue((argument: number) =>
|
|
159
|
-
{
|
|
160
|
-
counter += argument;
|
|
161
|
-
throw new Error(`argument: ${argument}`);
|
|
162
|
-
});
|
|
163
|
-
test.assertSame(counter, 0);
|
|
164
|
-
for (let i = 0; i < 3; i++)
|
|
165
|
-
{
|
|
166
|
-
await test.assertThrowsAsync(onValueResult, new Error("argument: 2"));
|
|
167
|
-
test.assertSame(counter, 2);
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
runner.testFunction("catch()", () =>
|
|
173
|
-
{
|
|
174
|
-
runner.test("with undefined errorType", (test: Test) =>
|
|
175
|
-
{
|
|
176
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
177
|
-
test.assertThrows(() => parentResult.catch(undefined!, () => 6),
|
|
178
|
-
new PreConditionError(
|
|
179
|
-
"Expression: errorType",
|
|
180
|
-
"Expected: not undefined and not null",
|
|
181
|
-
"Actual: undefined"));
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
runner.test("with null errorType", (test: Test) =>
|
|
185
|
-
{
|
|
186
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
187
|
-
test.assertThrows(() => parentResult.catch(null!, () => 6),
|
|
188
|
-
new PreConditionError(
|
|
189
|
-
"Expression: errorType",
|
|
190
|
-
"Expected: not undefined and not null",
|
|
191
|
-
"Actual: null"));
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
runner.test("with error parent", async (test: Test) =>
|
|
195
|
-
{
|
|
196
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
197
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => 20);
|
|
198
|
-
test.assertSame(await catchResult, 20);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
runner.test("with error parent, no errorType, and no error parameter", async (test: Test) =>
|
|
202
|
-
{
|
|
203
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
204
|
-
let counter: number = 0;
|
|
205
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(() => { counter++; return 21; });
|
|
206
|
-
test.assertSame(0, counter);
|
|
207
|
-
for (let i = 0; i < 3; i++)
|
|
208
|
-
{
|
|
209
|
-
test.assertSame(await catchResult, 21);
|
|
210
|
-
test.assertSame(1, counter);
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
runner.test("with error parent, no errorType, and unknown error parameter", async (test: Test) =>
|
|
215
|
-
{
|
|
216
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
217
|
-
let counter: number = 0;
|
|
218
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch((error: unknown) =>
|
|
219
|
-
{
|
|
220
|
-
if (error instanceof Error)
|
|
221
|
-
{
|
|
222
|
-
counter += error.message.length;
|
|
223
|
-
}
|
|
224
|
-
else
|
|
225
|
-
{
|
|
226
|
-
counter -= 1;
|
|
227
|
-
}
|
|
228
|
-
return 21;
|
|
229
|
-
});
|
|
230
|
-
test.assertSame(0, counter);
|
|
231
|
-
for (let i = 0; i < 3; i++)
|
|
232
|
-
{
|
|
233
|
-
test.assertSame(await catchResult, 21);
|
|
234
|
-
test.assertSame(3, counter);
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
runner.test("with error parent and catchFunction with side-effects", async (test: Test) =>
|
|
239
|
-
{
|
|
240
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
241
|
-
let counter: number = 0;
|
|
242
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => { counter++; return 21; });
|
|
243
|
-
test.assertSame(0, counter);
|
|
244
|
-
for (let i = 0; i < 3; i++)
|
|
245
|
-
{
|
|
246
|
-
test.assertSame(await catchResult, 21);
|
|
247
|
-
test.assertSame(1, counter);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
runner.test("with errorType that is a super-type of the actual error without error parameter", async (test: Test) =>
|
|
252
|
-
{
|
|
253
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
254
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => 5);
|
|
255
|
-
test.assertSame(await catchResult, 5);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
runner.test("with errorType that is a super-type of the actual error with error parameter", async (test: Test) =>
|
|
259
|
-
{
|
|
260
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
261
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, (error: Error) => error.message.length);
|
|
262
|
-
test.assertSame(await catchResult, 3);
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
runner.test("with errorType that is a sub-type of the actual error", async (test: Test) =>
|
|
266
|
-
{
|
|
267
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
268
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(PreConditionError, () => 20);
|
|
269
|
-
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
runner.test("with errorType that is unrelated to the actual error", async (test: Test) =>
|
|
273
|
-
{
|
|
274
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
275
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(RangeError, () => 20);
|
|
276
|
-
await test.assertThrowsAsync(catchResult, new PreConditionError("def"));
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
runner.test("with catchFunction that throws", async (test: Test) =>
|
|
280
|
-
{
|
|
281
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
282
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => { throw new TypeError("abc"); });
|
|
283
|
-
await test.assertThrowsAsync(catchResult, new TypeError("abc"));
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
runner.test("with successful parent", async (test: Test) =>
|
|
287
|
-
{
|
|
288
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
289
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => 2);
|
|
290
|
-
test.assertSame(await catchResult, 1);
|
|
291
|
-
});
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
runner.testFunction("onError()", () =>
|
|
295
|
-
{
|
|
296
|
-
runner.test("with undefined errorType", (test: Test) =>
|
|
297
|
-
{
|
|
298
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
299
|
-
test.assertThrows(() => parentResult.onError(undefined!, () => { }),
|
|
300
|
-
new PreConditionError(
|
|
301
|
-
"Expression: errorType",
|
|
302
|
-
"Expected: not undefined and not null",
|
|
303
|
-
"Actual: undefined"));
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
runner.test("with null errorType", (test: Test) =>
|
|
307
|
-
{
|
|
308
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
309
|
-
test.assertThrows(() => parentResult.onError(null!, () => { }),
|
|
310
|
-
new PreConditionError(
|
|
311
|
-
"Expression: errorType",
|
|
312
|
-
"Expected: not undefined and not null",
|
|
313
|
-
"Actual: null"));
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
runner.test("with error parent, no errorType, and no error parameter", async (test: Test) =>
|
|
317
|
-
{
|
|
318
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
319
|
-
let counter: number = 0;
|
|
320
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(() => { counter++; });
|
|
321
|
-
test.assertSame(0, counter);
|
|
322
|
-
for (let i = 0; i < 3; i++)
|
|
323
|
-
{
|
|
324
|
-
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
325
|
-
test.assertSame(1, counter);
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
runner.test("with error parent, no errorType, and unknown error parameter", async (test: Test) =>
|
|
330
|
-
{
|
|
331
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
332
|
-
let counter: number = 0;
|
|
333
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError((error: unknown) =>
|
|
334
|
-
{
|
|
335
|
-
if (error instanceof Error)
|
|
336
|
-
{
|
|
337
|
-
counter += error.message.length;
|
|
338
|
-
}
|
|
339
|
-
else
|
|
340
|
-
{
|
|
341
|
-
counter -= 1;
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
test.assertSame(0, counter);
|
|
345
|
-
for (let i = 0; i < 3; i++)
|
|
346
|
-
{
|
|
347
|
-
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
348
|
-
test.assertSame(3, counter);
|
|
349
|
-
}
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
runner.test("with errorType that is a super-type of the actual error without error parameter", async (test: Test) =>
|
|
353
|
-
{
|
|
354
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
355
|
-
let counter: number = 0;
|
|
356
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, () => { counter++; });
|
|
357
|
-
test.assertSame(counter, 0);
|
|
358
|
-
for (let i = 0; i < 3; i++)
|
|
359
|
-
{
|
|
360
|
-
await test.assertThrowsAsync(catchResult, new PreConditionError("abc"));
|
|
361
|
-
test.assertSame(counter, 1);
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
runner.test("with errorType that is a super-type of the actual error with error parameter", async (test: Test) =>
|
|
366
|
-
{
|
|
367
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
368
|
-
let counter: number = 0;
|
|
369
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, (error: Error) => { counter += error.message.length; });
|
|
370
|
-
test.assertSame(counter, 0);
|
|
371
|
-
for (let i = 0; i < 3; i++)
|
|
372
|
-
{
|
|
373
|
-
await test.assertThrowsAsync(catchResult, new PreConditionError("abc"));
|
|
374
|
-
test.assertSame(counter, 3);
|
|
375
|
-
}
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
runner.test("with errorType that is a sub-type of the actual error", async (test: Test) =>
|
|
379
|
-
{
|
|
380
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
381
|
-
let counter: number = 0;
|
|
382
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(PreConditionError, () => { counter++; });
|
|
383
|
-
test.assertSame(counter, 0);
|
|
384
|
-
for (let i = 0; i < 3; i++)
|
|
385
|
-
{
|
|
386
|
-
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
387
|
-
test.assertSame(counter, 0);
|
|
388
|
-
}
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
runner.test("with errorType that is unrelated to the actual error", async (test: Test) =>
|
|
392
|
-
{
|
|
393
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
394
|
-
let counter: number = 0;
|
|
395
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(RangeError, () => { counter++; });
|
|
396
|
-
test.assertSame(counter, 0);
|
|
397
|
-
for (let i = 0; i < 3; i++)
|
|
398
|
-
{
|
|
399
|
-
await test.assertThrowsAsync(catchResult, new PreConditionError("def"));
|
|
400
|
-
test.assertSame(counter, 0);
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
runner.test("with onErrorFunction that throws", async (test: Test) =>
|
|
405
|
-
{
|
|
406
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
407
|
-
let counter: number = 0;
|
|
408
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, () => { counter++; throw new Error("abc"); });
|
|
409
|
-
test.assertSame(counter, 0);
|
|
410
|
-
for (let i = 0; i < 3; i++)
|
|
411
|
-
{
|
|
412
|
-
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
413
|
-
test.assertSame(counter, 1);
|
|
414
|
-
}
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
runner.test("with successful parent", async (test: Test) =>
|
|
418
|
-
{
|
|
419
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
420
|
-
let counter: number = 0;
|
|
421
|
-
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, () => { counter++; });
|
|
422
|
-
test.assertSame(counter, 0);
|
|
423
|
-
for (let i = 0; i < 3; i++)
|
|
424
|
-
{
|
|
425
|
-
test.assertSame(await catchResult, 1);
|
|
426
|
-
test.assertSame(counter, 0);
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
runner.testFunction("convertError()", () =>
|
|
432
|
-
{
|
|
433
|
-
runner.test("with undefined convertErrorFunction", (test: Test) =>
|
|
434
|
-
{
|
|
435
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
436
|
-
test.assertThrows(() => parentResult.convertError(undefined!),
|
|
437
|
-
new PreConditionError(
|
|
438
|
-
"Expression: convertErrorFunction",
|
|
439
|
-
"Expected: not undefined and not null",
|
|
440
|
-
"Actual: undefined"));
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
runner.test("with null convertErrorFunction", (test: Test) =>
|
|
444
|
-
{
|
|
445
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
446
|
-
test.assertThrows(() => parentResult.convertError(null!),
|
|
447
|
-
new PreConditionError(
|
|
448
|
-
"Expression: convertErrorFunction",
|
|
449
|
-
"Expected: not undefined and not null",
|
|
450
|
-
"Actual: null"));
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
runner.test("with successful parent", async (test: Test) =>
|
|
454
|
-
{
|
|
455
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
456
|
-
let counter: number = 0;
|
|
457
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError((error: unknown) =>
|
|
458
|
-
{
|
|
459
|
-
counter++;
|
|
460
|
-
return new Error(`${error} - def`);
|
|
461
|
-
});
|
|
462
|
-
test.assertSame(counter, 0);
|
|
463
|
-
for (let i = 0; i < 3; i++)
|
|
464
|
-
{
|
|
465
|
-
test.assertSame(await convertErrorResult, 5);
|
|
466
|
-
test.assertSame(counter, 0);
|
|
467
|
-
}
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
runner.test("with error parent and non-throwing convertErrorFunction", async (test: Test) =>
|
|
471
|
-
{
|
|
472
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
473
|
-
let counter: number = 0;
|
|
474
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError((error: unknown) =>
|
|
475
|
-
{
|
|
476
|
-
counter++;
|
|
477
|
-
return new Error(`${(error as Error).message} - def`);
|
|
478
|
-
});
|
|
479
|
-
test.assertSame(counter, 0);
|
|
480
|
-
for (let i = 0; i < 3; i++)
|
|
481
|
-
{
|
|
482
|
-
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
483
|
-
test.assertSame(counter, 1);
|
|
484
|
-
}
|
|
485
|
-
});
|
|
486
|
-
|
|
487
|
-
runner.test("with error parent and throwing convertErrorFunction", async (test: Test) =>
|
|
488
|
-
{
|
|
489
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
490
|
-
let counter: number = 0;
|
|
491
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError((error: unknown) =>
|
|
492
|
-
{
|
|
493
|
-
counter++;
|
|
494
|
-
throw new Error(`${(error as Error).message} - def`);
|
|
495
|
-
});
|
|
496
|
-
test.assertSame(counter, 0);
|
|
497
|
-
for (let i = 0; i < 3; i++)
|
|
498
|
-
{
|
|
499
|
-
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
500
|
-
test.assertSame(counter, 1);
|
|
501
|
-
}
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
runner.test("with successful parent", async (test: Test) =>
|
|
505
|
-
{
|
|
506
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
507
|
-
let counter: number = 0;
|
|
508
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
509
|
-
{
|
|
510
|
-
counter++;
|
|
511
|
-
return new Error(`${error.message} - def`);
|
|
512
|
-
});
|
|
513
|
-
test.assertSame(counter, 0);
|
|
514
|
-
for (let i = 0; i < 3; i++)
|
|
515
|
-
{
|
|
516
|
-
test.assertSame(await convertErrorResult, 5);
|
|
517
|
-
test.assertSame(counter, 0);
|
|
518
|
-
}
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
runner.test("with error parent, exact error match, and non-throwing convertErrorFunction", async (test: Test) =>
|
|
522
|
-
{
|
|
523
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
524
|
-
let counter: number = 0;
|
|
525
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
526
|
-
{
|
|
527
|
-
counter++;
|
|
528
|
-
return new Error(`${error.message} - def`);
|
|
529
|
-
});
|
|
530
|
-
test.assertSame(counter, 0);
|
|
531
|
-
for (let i = 0; i < 3; i++)
|
|
532
|
-
{
|
|
533
|
-
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
534
|
-
test.assertSame(counter, 1);
|
|
535
|
-
}
|
|
536
|
-
});
|
|
537
|
-
|
|
538
|
-
runner.test("with error parent, super error match, and non-throwing convertErrorFunction", async (test: Test) =>
|
|
539
|
-
{
|
|
540
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
541
|
-
let counter: number = 0;
|
|
542
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
543
|
-
{
|
|
544
|
-
counter++;
|
|
545
|
-
return new Error(`${error.message} - def`);
|
|
546
|
-
});
|
|
547
|
-
test.assertSame(counter, 0);
|
|
548
|
-
for (let i = 0; i < 3; i++)
|
|
549
|
-
{
|
|
550
|
-
await test.assertThrowsAsync(convertErrorResult, new Error(`abc - def`));
|
|
551
|
-
test.assertSame(counter, 1);
|
|
552
|
-
}
|
|
553
|
-
});
|
|
554
|
-
|
|
555
|
-
runner.test("with error parent, no error match, and non-throwing convertErrorFunction", async (test: Test) =>
|
|
556
|
-
{
|
|
557
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
558
|
-
let counter: number = 0;
|
|
559
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(TypeError, (error: TypeError) =>
|
|
560
|
-
{
|
|
561
|
-
counter++;
|
|
562
|
-
return new Error(`${error} - def`);
|
|
563
|
-
});
|
|
564
|
-
test.assertSame(counter, 0);
|
|
565
|
-
for (let i = 0; i < 3; i++)
|
|
566
|
-
{
|
|
567
|
-
await test.assertThrowsAsync(convertErrorResult, new PreConditionError("abc"));
|
|
568
|
-
test.assertSame(counter, 0);
|
|
569
|
-
}
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
runner.test("with error parent, exact error match, and throwing convertErrorFunction", async (test: Test) =>
|
|
573
|
-
{
|
|
574
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
575
|
-
let counter: number = 0;
|
|
576
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
577
|
-
{
|
|
578
|
-
counter++;
|
|
579
|
-
throw new Error(`${error.message} - def`);
|
|
580
|
-
});
|
|
581
|
-
test.assertSame(counter, 0);
|
|
582
|
-
for (let i = 0; i < 3; i++)
|
|
583
|
-
{
|
|
584
|
-
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
585
|
-
test.assertSame(counter, 1);
|
|
586
|
-
}
|
|
587
|
-
});
|
|
588
|
-
|
|
589
|
-
runner.test("with error parent, super error match, and throwing convertErrorFunction", async (test: Test) =>
|
|
590
|
-
{
|
|
591
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new TypeError("abc"));
|
|
592
|
-
let counter: number = 0;
|
|
593
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
594
|
-
{
|
|
595
|
-
counter++;
|
|
596
|
-
throw new Error(`${error.message} - def`);
|
|
597
|
-
});
|
|
598
|
-
test.assertSame(counter, 0);
|
|
599
|
-
for (let i = 0; i < 3; i++)
|
|
600
|
-
{
|
|
601
|
-
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
602
|
-
test.assertSame(counter, 1);
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
runner.test("with error parent, no error match, and throwing convertErrorFunction", async (test: Test) =>
|
|
607
|
-
{
|
|
608
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new TypeError("abc"));
|
|
609
|
-
let counter: number = 0;
|
|
610
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(PreConditionError, () =>
|
|
611
|
-
{
|
|
612
|
-
counter++;
|
|
613
|
-
throw new Error("def");
|
|
614
|
-
});
|
|
615
|
-
test.assertSame(counter, 0);
|
|
616
|
-
for (let i = 0; i < 3; i++)
|
|
617
|
-
{
|
|
618
|
-
await test.assertThrowsAsync(convertErrorResult, new TypeError("abc"));
|
|
619
|
-
test.assertSame(counter, 0);
|
|
620
|
-
}
|
|
621
|
-
});
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
runner.testFunction("finally()", () =>
|
|
625
|
-
{
|
|
626
|
-
runner.test("with successful parent and non-throwing onfinally function", async (test: Test) =>
|
|
627
|
-
{
|
|
628
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
629
|
-
let counter: number = 0;
|
|
630
|
-
const finallyResult: PromiseAsyncResult<number> = parentResult.finally(() => { counter++; });
|
|
631
|
-
test.assertSame(counter, 0);
|
|
632
|
-
for (let i = 0; i < 3; i++)
|
|
633
|
-
{
|
|
634
|
-
test.assertSame(await finallyResult, 5);
|
|
635
|
-
test.assertSame(counter, 1);
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
runner.test("with successful parent and throwing onfinally function", async (test: Test) =>
|
|
640
|
-
{
|
|
641
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
642
|
-
let counter: number = 0;
|
|
643
|
-
const finallyResult: PromiseAsyncResult<number> = parentResult.finally(() =>
|
|
644
|
-
{
|
|
645
|
-
counter++;
|
|
646
|
-
throw new RangeError("oops!");
|
|
647
|
-
});
|
|
648
|
-
test.assertSame(counter, 0);
|
|
649
|
-
for (let i = 0; i < 3; i++)
|
|
650
|
-
{
|
|
651
|
-
await test.assertThrowsAsync(finallyResult, new RangeError("oops!"));
|
|
652
|
-
test.assertSame(counter, 1);
|
|
653
|
-
}
|
|
654
|
-
});
|
|
655
|
-
|
|
656
|
-
runner.test("with error parent and non-throwing onfinally function", async (test: Test) =>
|
|
657
|
-
{
|
|
658
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
659
|
-
let counter: number = 0;
|
|
660
|
-
const finallyResult: PromiseAsyncResult<number> = parentResult.finally(() => { counter++; });
|
|
661
|
-
test.assertSame(counter, 0);
|
|
662
|
-
for (let i = 0; i < 3; i++)
|
|
663
|
-
{
|
|
664
|
-
await test.assertThrowsAsync(finallyResult, new Error("abc"));
|
|
665
|
-
test.assertSame(counter, 1);
|
|
666
|
-
}
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
runner.test("with error parent and throwing onfinally function", async (test: Test) =>
|
|
670
|
-
{
|
|
671
|
-
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
672
|
-
let counter: number = 0;
|
|
673
|
-
const convertErrorResult: PromiseAsyncResult<number> = parentResult.finally(() =>
|
|
674
|
-
{
|
|
675
|
-
counter++;
|
|
676
|
-
throw new RangeError("def");
|
|
677
|
-
});
|
|
678
|
-
test.assertSame(counter, 0);
|
|
679
|
-
for (let i = 0; i < 3; i++)
|
|
680
|
-
{
|
|
681
|
-
await test.assertThrowsAsync(convertErrorResult, new RangeError("def"));
|
|
682
|
-
test.assertSame(counter, 1);
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
});
|
|
686
|
-
});
|
|
687
|
-
});
|
|
1
|
+
import { PromiseAsyncResult } from "../sources/promiseAsyncResult";
|
|
2
|
+
import { NotFoundError } from "../sources/notFoundError";
|
|
3
|
+
import { PreConditionError } from "../sources/preConditionError";
|
|
4
|
+
import { Test } from "./test";
|
|
5
|
+
import { TestRunner } from "./testRunner";
|
|
6
|
+
|
|
7
|
+
export function test(runner: TestRunner): void
|
|
8
|
+
{
|
|
9
|
+
runner.testFile("promiseAsyncResult.ts", () =>
|
|
10
|
+
{
|
|
11
|
+
runner.testType("PromiseAsyncResult<T>", () =>
|
|
12
|
+
{
|
|
13
|
+
runner.testFunction("create()", () =>
|
|
14
|
+
{
|
|
15
|
+
function createErrorTest<T>(testName: string, promise: Promise<T>, expected: Error): void
|
|
16
|
+
{
|
|
17
|
+
runner.test(testName, (test: Test) =>
|
|
18
|
+
{
|
|
19
|
+
test.assertThrows(() => PromiseAsyncResult.create(promise), expected);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
createErrorTest("with undefined", undefined!, new PreConditionError(
|
|
24
|
+
"Expression: action or promise",
|
|
25
|
+
"Expected: not undefined and not null",
|
|
26
|
+
"Actual: undefined",
|
|
27
|
+
));
|
|
28
|
+
createErrorTest("with null", null!, new PreConditionError(
|
|
29
|
+
"Expression: action or promise",
|
|
30
|
+
"Expected: not undefined and not null",
|
|
31
|
+
"Actual: null",
|
|
32
|
+
));
|
|
33
|
+
|
|
34
|
+
runner.test("with Promise", async (test: Test) =>
|
|
35
|
+
{
|
|
36
|
+
const result: PromiseAsyncResult<number> = PromiseAsyncResult.create(Promise.resolve(5));
|
|
37
|
+
test.assertNotUndefinedAndNotNull(result);
|
|
38
|
+
|
|
39
|
+
const value: number = await result;
|
|
40
|
+
test.assertEqual(5, value);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
runner.testFunction("then()", () =>
|
|
45
|
+
{
|
|
46
|
+
runner.test("with error parent", async (test: Test) =>
|
|
47
|
+
{
|
|
48
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
49
|
+
const thenResult: PromiseAsyncResult<string> = parentResult.then(() => "hello");
|
|
50
|
+
await test.assertThrowsAsync(thenResult, new Error("abc"));
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
runner.test("with error parent and thenFunction with side-effects", async (test: Test) =>
|
|
54
|
+
{
|
|
55
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
56
|
+
let counter: number = 0;
|
|
57
|
+
const thenResult: PromiseAsyncResult<string> = parentResult.then(() => { counter++; return "hello"; });
|
|
58
|
+
test.assertEqual(0, counter);
|
|
59
|
+
await test.assertThrowsAsync(thenResult, new Error("abc"));
|
|
60
|
+
test.assertEqual(0, counter, "counter should still be 0 because the thenFunction shouldn't be invoked.");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
runner.test("with successful parent and successful thenFunction that ignores parentResult value", async (test: Test) =>
|
|
64
|
+
{
|
|
65
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
66
|
+
const thenResult: PromiseAsyncResult<string> = parentResult.then(() => "hello");
|
|
67
|
+
test.assertEqual("hello", await thenResult);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
runner.test("with successful parent and successful thenFunction that uses parentResult value", async (test: Test) =>
|
|
71
|
+
{
|
|
72
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
73
|
+
const thenResult: PromiseAsyncResult<string> = parentResult.then((argument: number) => (argument + 1).toString());
|
|
74
|
+
test.assertEqual("2", await thenResult);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
runner.test("with successful parent and successful thenFunction that uses parentResult value with side-effects", async (test: Test) =>
|
|
78
|
+
{
|
|
79
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
80
|
+
let counter: number = 0;
|
|
81
|
+
const thenResult: PromiseAsyncResult<string> = parentResult.then((argument: number) => { counter++; return (argument + 1).toString(); });
|
|
82
|
+
test.assertEqual(0, counter);
|
|
83
|
+
|
|
84
|
+
await PromiseAsyncResult.yield();
|
|
85
|
+
test.assertEqual(1, counter);
|
|
86
|
+
|
|
87
|
+
test.assertEqual("2", await thenResult);
|
|
88
|
+
test.assertEqual(1, counter);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
runner.test("with successful parent and thenFunction that throws", async (test: Test) =>
|
|
92
|
+
{
|
|
93
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(10);
|
|
94
|
+
let counter: number = 0;
|
|
95
|
+
const thenResult: PromiseAsyncResult<string> = parentResult.then((argument: number) =>
|
|
96
|
+
{
|
|
97
|
+
counter++;
|
|
98
|
+
throw new Error(`arg: ${argument}, ${counter}`);
|
|
99
|
+
});
|
|
100
|
+
test.assertEqual(counter, 0);
|
|
101
|
+
|
|
102
|
+
await PromiseAsyncResult.yield();
|
|
103
|
+
test.assertEqual(counter, 1);
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < 3; i++)
|
|
106
|
+
{
|
|
107
|
+
await test.assertThrowsAsync(thenResult, new Error("arg: 10, 1"));
|
|
108
|
+
test.assertEqual(counter, 1);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
runner.testFunction("onValue()", () =>
|
|
114
|
+
{
|
|
115
|
+
runner.test("with error parent", async (test: Test) =>
|
|
116
|
+
{
|
|
117
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
118
|
+
let counter: number = 0;
|
|
119
|
+
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue(() => { counter++; });
|
|
120
|
+
test.assertSame(counter, 0);
|
|
121
|
+
for (let i = 0; i < 3; i++)
|
|
122
|
+
{
|
|
123
|
+
await test.assertThrowsAsync(onValueResult, new Error("abc"));
|
|
124
|
+
test.assertSame(counter, 0);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
runner.test("with successful parent and successful thenFunction that ignores parentResult value", async (test: Test) =>
|
|
129
|
+
{
|
|
130
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(10);
|
|
131
|
+
let counter: number = 0;
|
|
132
|
+
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue(() => { counter++; });
|
|
133
|
+
test.assertSame(counter, 0);
|
|
134
|
+
for (let i = 0; i < 3; i++)
|
|
135
|
+
{
|
|
136
|
+
test.assertSame(10, await onValueResult);
|
|
137
|
+
test.assertSame(counter, 1);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
runner.test("with successful parent and successful thenFunction that uses parentResult value", async (test: Test) =>
|
|
142
|
+
{
|
|
143
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(2);
|
|
144
|
+
let counter: number = 0;
|
|
145
|
+
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue((argument: number) => { counter += argument; });
|
|
146
|
+
test.assertSame(counter, 0);
|
|
147
|
+
for (let i = 0; i < 3; i++)
|
|
148
|
+
{
|
|
149
|
+
test.assertSame(await onValueResult, 2);
|
|
150
|
+
test.assertSame(counter, 2);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
runner.test("with successful parent and onValueFunction that throws", async (test: Test) =>
|
|
155
|
+
{
|
|
156
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(2);
|
|
157
|
+
let counter: number = 0;
|
|
158
|
+
const onValueResult: PromiseAsyncResult<number> = parentResult.onValue((argument: number) =>
|
|
159
|
+
{
|
|
160
|
+
counter += argument;
|
|
161
|
+
throw new Error(`argument: ${argument}`);
|
|
162
|
+
});
|
|
163
|
+
test.assertSame(counter, 0);
|
|
164
|
+
for (let i = 0; i < 3; i++)
|
|
165
|
+
{
|
|
166
|
+
await test.assertThrowsAsync(onValueResult, new Error("argument: 2"));
|
|
167
|
+
test.assertSame(counter, 2);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
runner.testFunction("catch()", () =>
|
|
173
|
+
{
|
|
174
|
+
runner.test("with undefined errorType", (test: Test) =>
|
|
175
|
+
{
|
|
176
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
177
|
+
test.assertThrows(() => parentResult.catch(undefined!, () => 6),
|
|
178
|
+
new PreConditionError(
|
|
179
|
+
"Expression: errorType",
|
|
180
|
+
"Expected: not undefined and not null",
|
|
181
|
+
"Actual: undefined"));
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
runner.test("with null errorType", (test: Test) =>
|
|
185
|
+
{
|
|
186
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
187
|
+
test.assertThrows(() => parentResult.catch(null!, () => 6),
|
|
188
|
+
new PreConditionError(
|
|
189
|
+
"Expression: errorType",
|
|
190
|
+
"Expected: not undefined and not null",
|
|
191
|
+
"Actual: null"));
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
runner.test("with error parent", async (test: Test) =>
|
|
195
|
+
{
|
|
196
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
197
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => 20);
|
|
198
|
+
test.assertSame(await catchResult, 20);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
runner.test("with error parent, no errorType, and no error parameter", async (test: Test) =>
|
|
202
|
+
{
|
|
203
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
204
|
+
let counter: number = 0;
|
|
205
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(() => { counter++; return 21; });
|
|
206
|
+
test.assertSame(0, counter);
|
|
207
|
+
for (let i = 0; i < 3; i++)
|
|
208
|
+
{
|
|
209
|
+
test.assertSame(await catchResult, 21);
|
|
210
|
+
test.assertSame(1, counter);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
runner.test("with error parent, no errorType, and unknown error parameter", async (test: Test) =>
|
|
215
|
+
{
|
|
216
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
217
|
+
let counter: number = 0;
|
|
218
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch((error: unknown) =>
|
|
219
|
+
{
|
|
220
|
+
if (error instanceof Error)
|
|
221
|
+
{
|
|
222
|
+
counter += error.message.length;
|
|
223
|
+
}
|
|
224
|
+
else
|
|
225
|
+
{
|
|
226
|
+
counter -= 1;
|
|
227
|
+
}
|
|
228
|
+
return 21;
|
|
229
|
+
});
|
|
230
|
+
test.assertSame(0, counter);
|
|
231
|
+
for (let i = 0; i < 3; i++)
|
|
232
|
+
{
|
|
233
|
+
test.assertSame(await catchResult, 21);
|
|
234
|
+
test.assertSame(3, counter);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
runner.test("with error parent and catchFunction with side-effects", async (test: Test) =>
|
|
239
|
+
{
|
|
240
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
241
|
+
let counter: number = 0;
|
|
242
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => { counter++; return 21; });
|
|
243
|
+
test.assertSame(0, counter);
|
|
244
|
+
for (let i = 0; i < 3; i++)
|
|
245
|
+
{
|
|
246
|
+
test.assertSame(await catchResult, 21);
|
|
247
|
+
test.assertSame(1, counter);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
runner.test("with errorType that is a super-type of the actual error without error parameter", async (test: Test) =>
|
|
252
|
+
{
|
|
253
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
254
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => 5);
|
|
255
|
+
test.assertSame(await catchResult, 5);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
runner.test("with errorType that is a super-type of the actual error with error parameter", async (test: Test) =>
|
|
259
|
+
{
|
|
260
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
261
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, (error: Error) => error.message.length);
|
|
262
|
+
test.assertSame(await catchResult, 3);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
runner.test("with errorType that is a sub-type of the actual error", async (test: Test) =>
|
|
266
|
+
{
|
|
267
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
268
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(PreConditionError, () => 20);
|
|
269
|
+
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
runner.test("with errorType that is unrelated to the actual error", async (test: Test) =>
|
|
273
|
+
{
|
|
274
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
275
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(RangeError, () => 20);
|
|
276
|
+
await test.assertThrowsAsync(catchResult, new PreConditionError("def"));
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
runner.test("with catchFunction that throws", async (test: Test) =>
|
|
280
|
+
{
|
|
281
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
282
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => { throw new TypeError("abc"); });
|
|
283
|
+
await test.assertThrowsAsync(catchResult, new TypeError("abc"));
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
runner.test("with successful parent", async (test: Test) =>
|
|
287
|
+
{
|
|
288
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
289
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.catch(Error, () => 2);
|
|
290
|
+
test.assertSame(await catchResult, 1);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
runner.testFunction("onError()", () =>
|
|
295
|
+
{
|
|
296
|
+
runner.test("with undefined errorType", (test: Test) =>
|
|
297
|
+
{
|
|
298
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
299
|
+
test.assertThrows(() => parentResult.onError(undefined!, () => { }),
|
|
300
|
+
new PreConditionError(
|
|
301
|
+
"Expression: errorType",
|
|
302
|
+
"Expected: not undefined and not null",
|
|
303
|
+
"Actual: undefined"));
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
runner.test("with null errorType", (test: Test) =>
|
|
307
|
+
{
|
|
308
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
309
|
+
test.assertThrows(() => parentResult.onError(null!, () => { }),
|
|
310
|
+
new PreConditionError(
|
|
311
|
+
"Expression: errorType",
|
|
312
|
+
"Expected: not undefined and not null",
|
|
313
|
+
"Actual: null"));
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
runner.test("with error parent, no errorType, and no error parameter", async (test: Test) =>
|
|
317
|
+
{
|
|
318
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
319
|
+
let counter: number = 0;
|
|
320
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(() => { counter++; });
|
|
321
|
+
test.assertSame(0, counter);
|
|
322
|
+
for (let i = 0; i < 3; i++)
|
|
323
|
+
{
|
|
324
|
+
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
325
|
+
test.assertSame(1, counter);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
runner.test("with error parent, no errorType, and unknown error parameter", async (test: Test) =>
|
|
330
|
+
{
|
|
331
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
332
|
+
let counter: number = 0;
|
|
333
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError((error: unknown) =>
|
|
334
|
+
{
|
|
335
|
+
if (error instanceof Error)
|
|
336
|
+
{
|
|
337
|
+
counter += error.message.length;
|
|
338
|
+
}
|
|
339
|
+
else
|
|
340
|
+
{
|
|
341
|
+
counter -= 1;
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
test.assertSame(0, counter);
|
|
345
|
+
for (let i = 0; i < 3; i++)
|
|
346
|
+
{
|
|
347
|
+
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
348
|
+
test.assertSame(3, counter);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
runner.test("with errorType that is a super-type of the actual error without error parameter", async (test: Test) =>
|
|
353
|
+
{
|
|
354
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
355
|
+
let counter: number = 0;
|
|
356
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, () => { counter++; });
|
|
357
|
+
test.assertSame(counter, 0);
|
|
358
|
+
for (let i = 0; i < 3; i++)
|
|
359
|
+
{
|
|
360
|
+
await test.assertThrowsAsync(catchResult, new PreConditionError("abc"));
|
|
361
|
+
test.assertSame(counter, 1);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
runner.test("with errorType that is a super-type of the actual error with error parameter", async (test: Test) =>
|
|
366
|
+
{
|
|
367
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
368
|
+
let counter: number = 0;
|
|
369
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, (error: Error) => { counter += error.message.length; });
|
|
370
|
+
test.assertSame(counter, 0);
|
|
371
|
+
for (let i = 0; i < 3; i++)
|
|
372
|
+
{
|
|
373
|
+
await test.assertThrowsAsync(catchResult, new PreConditionError("abc"));
|
|
374
|
+
test.assertSame(counter, 3);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
runner.test("with errorType that is a sub-type of the actual error", async (test: Test) =>
|
|
379
|
+
{
|
|
380
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
381
|
+
let counter: number = 0;
|
|
382
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(PreConditionError, () => { counter++; });
|
|
383
|
+
test.assertSame(counter, 0);
|
|
384
|
+
for (let i = 0; i < 3; i++)
|
|
385
|
+
{
|
|
386
|
+
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
387
|
+
test.assertSame(counter, 0);
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
runner.test("with errorType that is unrelated to the actual error", async (test: Test) =>
|
|
392
|
+
{
|
|
393
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
394
|
+
let counter: number = 0;
|
|
395
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(RangeError, () => { counter++; });
|
|
396
|
+
test.assertSame(counter, 0);
|
|
397
|
+
for (let i = 0; i < 3; i++)
|
|
398
|
+
{
|
|
399
|
+
await test.assertThrowsAsync(catchResult, new PreConditionError("def"));
|
|
400
|
+
test.assertSame(counter, 0);
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
runner.test("with onErrorFunction that throws", async (test: Test) =>
|
|
405
|
+
{
|
|
406
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("def"));
|
|
407
|
+
let counter: number = 0;
|
|
408
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, () => { counter++; throw new Error("abc"); });
|
|
409
|
+
test.assertSame(counter, 0);
|
|
410
|
+
for (let i = 0; i < 3; i++)
|
|
411
|
+
{
|
|
412
|
+
await test.assertThrowsAsync(catchResult, new Error("abc"));
|
|
413
|
+
test.assertSame(counter, 1);
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
runner.test("with successful parent", async (test: Test) =>
|
|
418
|
+
{
|
|
419
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(1);
|
|
420
|
+
let counter: number = 0;
|
|
421
|
+
const catchResult: PromiseAsyncResult<number> = parentResult.onError(Error, () => { counter++; });
|
|
422
|
+
test.assertSame(counter, 0);
|
|
423
|
+
for (let i = 0; i < 3; i++)
|
|
424
|
+
{
|
|
425
|
+
test.assertSame(await catchResult, 1);
|
|
426
|
+
test.assertSame(counter, 0);
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
runner.testFunction("convertError()", () =>
|
|
432
|
+
{
|
|
433
|
+
runner.test("with undefined convertErrorFunction", (test: Test) =>
|
|
434
|
+
{
|
|
435
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
436
|
+
test.assertThrows(() => parentResult.convertError(undefined!),
|
|
437
|
+
new PreConditionError(
|
|
438
|
+
"Expression: convertErrorFunction",
|
|
439
|
+
"Expected: not undefined and not null",
|
|
440
|
+
"Actual: undefined"));
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
runner.test("with null convertErrorFunction", (test: Test) =>
|
|
444
|
+
{
|
|
445
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
446
|
+
test.assertThrows(() => parentResult.convertError(null!),
|
|
447
|
+
new PreConditionError(
|
|
448
|
+
"Expression: convertErrorFunction",
|
|
449
|
+
"Expected: not undefined and not null",
|
|
450
|
+
"Actual: null"));
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
runner.test("with successful parent", async (test: Test) =>
|
|
454
|
+
{
|
|
455
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
456
|
+
let counter: number = 0;
|
|
457
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError((error: unknown) =>
|
|
458
|
+
{
|
|
459
|
+
counter++;
|
|
460
|
+
return new Error(`${error} - def`);
|
|
461
|
+
});
|
|
462
|
+
test.assertSame(counter, 0);
|
|
463
|
+
for (let i = 0; i < 3; i++)
|
|
464
|
+
{
|
|
465
|
+
test.assertSame(await convertErrorResult, 5);
|
|
466
|
+
test.assertSame(counter, 0);
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
runner.test("with error parent and non-throwing convertErrorFunction", async (test: Test) =>
|
|
471
|
+
{
|
|
472
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
473
|
+
let counter: number = 0;
|
|
474
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError((error: unknown) =>
|
|
475
|
+
{
|
|
476
|
+
counter++;
|
|
477
|
+
return new Error(`${(error as Error).message} - def`);
|
|
478
|
+
});
|
|
479
|
+
test.assertSame(counter, 0);
|
|
480
|
+
for (let i = 0; i < 3; i++)
|
|
481
|
+
{
|
|
482
|
+
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
483
|
+
test.assertSame(counter, 1);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
runner.test("with error parent and throwing convertErrorFunction", async (test: Test) =>
|
|
488
|
+
{
|
|
489
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
490
|
+
let counter: number = 0;
|
|
491
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError((error: unknown) =>
|
|
492
|
+
{
|
|
493
|
+
counter++;
|
|
494
|
+
throw new Error(`${(error as Error).message} - def`);
|
|
495
|
+
});
|
|
496
|
+
test.assertSame(counter, 0);
|
|
497
|
+
for (let i = 0; i < 3; i++)
|
|
498
|
+
{
|
|
499
|
+
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
500
|
+
test.assertSame(counter, 1);
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
runner.test("with successful parent", async (test: Test) =>
|
|
505
|
+
{
|
|
506
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
507
|
+
let counter: number = 0;
|
|
508
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
509
|
+
{
|
|
510
|
+
counter++;
|
|
511
|
+
return new Error(`${error.message} - def`);
|
|
512
|
+
});
|
|
513
|
+
test.assertSame(counter, 0);
|
|
514
|
+
for (let i = 0; i < 3; i++)
|
|
515
|
+
{
|
|
516
|
+
test.assertSame(await convertErrorResult, 5);
|
|
517
|
+
test.assertSame(counter, 0);
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
runner.test("with error parent, exact error match, and non-throwing convertErrorFunction", async (test: Test) =>
|
|
522
|
+
{
|
|
523
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
524
|
+
let counter: number = 0;
|
|
525
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
526
|
+
{
|
|
527
|
+
counter++;
|
|
528
|
+
return new Error(`${error.message} - def`);
|
|
529
|
+
});
|
|
530
|
+
test.assertSame(counter, 0);
|
|
531
|
+
for (let i = 0; i < 3; i++)
|
|
532
|
+
{
|
|
533
|
+
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
534
|
+
test.assertSame(counter, 1);
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
runner.test("with error parent, super error match, and non-throwing convertErrorFunction", async (test: Test) =>
|
|
539
|
+
{
|
|
540
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
541
|
+
let counter: number = 0;
|
|
542
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
543
|
+
{
|
|
544
|
+
counter++;
|
|
545
|
+
return new Error(`${error.message} - def`);
|
|
546
|
+
});
|
|
547
|
+
test.assertSame(counter, 0);
|
|
548
|
+
for (let i = 0; i < 3; i++)
|
|
549
|
+
{
|
|
550
|
+
await test.assertThrowsAsync(convertErrorResult, new Error(`abc - def`));
|
|
551
|
+
test.assertSame(counter, 1);
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
runner.test("with error parent, no error match, and non-throwing convertErrorFunction", async (test: Test) =>
|
|
556
|
+
{
|
|
557
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new PreConditionError("abc"));
|
|
558
|
+
let counter: number = 0;
|
|
559
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(TypeError, (error: TypeError) =>
|
|
560
|
+
{
|
|
561
|
+
counter++;
|
|
562
|
+
return new Error(`${error} - def`);
|
|
563
|
+
});
|
|
564
|
+
test.assertSame(counter, 0);
|
|
565
|
+
for (let i = 0; i < 3; i++)
|
|
566
|
+
{
|
|
567
|
+
await test.assertThrowsAsync(convertErrorResult, new PreConditionError("abc"));
|
|
568
|
+
test.assertSame(counter, 0);
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
runner.test("with error parent, exact error match, and throwing convertErrorFunction", async (test: Test) =>
|
|
573
|
+
{
|
|
574
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
575
|
+
let counter: number = 0;
|
|
576
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
577
|
+
{
|
|
578
|
+
counter++;
|
|
579
|
+
throw new Error(`${error.message} - def`);
|
|
580
|
+
});
|
|
581
|
+
test.assertSame(counter, 0);
|
|
582
|
+
for (let i = 0; i < 3; i++)
|
|
583
|
+
{
|
|
584
|
+
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
585
|
+
test.assertSame(counter, 1);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
runner.test("with error parent, super error match, and throwing convertErrorFunction", async (test: Test) =>
|
|
590
|
+
{
|
|
591
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new TypeError("abc"));
|
|
592
|
+
let counter: number = 0;
|
|
593
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(Error, (error: Error) =>
|
|
594
|
+
{
|
|
595
|
+
counter++;
|
|
596
|
+
throw new Error(`${error.message} - def`);
|
|
597
|
+
});
|
|
598
|
+
test.assertSame(counter, 0);
|
|
599
|
+
for (let i = 0; i < 3; i++)
|
|
600
|
+
{
|
|
601
|
+
await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
|
|
602
|
+
test.assertSame(counter, 1);
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
runner.test("with error parent, no error match, and throwing convertErrorFunction", async (test: Test) =>
|
|
607
|
+
{
|
|
608
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new TypeError("abc"));
|
|
609
|
+
let counter: number = 0;
|
|
610
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.convertError(PreConditionError, () =>
|
|
611
|
+
{
|
|
612
|
+
counter++;
|
|
613
|
+
throw new Error("def");
|
|
614
|
+
});
|
|
615
|
+
test.assertSame(counter, 0);
|
|
616
|
+
for (let i = 0; i < 3; i++)
|
|
617
|
+
{
|
|
618
|
+
await test.assertThrowsAsync(convertErrorResult, new TypeError("abc"));
|
|
619
|
+
test.assertSame(counter, 0);
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
runner.testFunction("finally()", () =>
|
|
625
|
+
{
|
|
626
|
+
runner.test("with successful parent and non-throwing onfinally function", async (test: Test) =>
|
|
627
|
+
{
|
|
628
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
629
|
+
let counter: number = 0;
|
|
630
|
+
const finallyResult: PromiseAsyncResult<number> = parentResult.finally(() => { counter++; });
|
|
631
|
+
test.assertSame(counter, 0);
|
|
632
|
+
for (let i = 0; i < 3; i++)
|
|
633
|
+
{
|
|
634
|
+
test.assertSame(await finallyResult, 5);
|
|
635
|
+
test.assertSame(counter, 1);
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
runner.test("with successful parent and throwing onfinally function", async (test: Test) =>
|
|
640
|
+
{
|
|
641
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.value(5);
|
|
642
|
+
let counter: number = 0;
|
|
643
|
+
const finallyResult: PromiseAsyncResult<number> = parentResult.finally(() =>
|
|
644
|
+
{
|
|
645
|
+
counter++;
|
|
646
|
+
throw new RangeError("oops!");
|
|
647
|
+
});
|
|
648
|
+
test.assertSame(counter, 0);
|
|
649
|
+
for (let i = 0; i < 3; i++)
|
|
650
|
+
{
|
|
651
|
+
await test.assertThrowsAsync(finallyResult, new RangeError("oops!"));
|
|
652
|
+
test.assertSame(counter, 1);
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
runner.test("with error parent and non-throwing onfinally function", async (test: Test) =>
|
|
657
|
+
{
|
|
658
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
659
|
+
let counter: number = 0;
|
|
660
|
+
const finallyResult: PromiseAsyncResult<number> = parentResult.finally(() => { counter++; });
|
|
661
|
+
test.assertSame(counter, 0);
|
|
662
|
+
for (let i = 0; i < 3; i++)
|
|
663
|
+
{
|
|
664
|
+
await test.assertThrowsAsync(finallyResult, new Error("abc"));
|
|
665
|
+
test.assertSame(counter, 1);
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
runner.test("with error parent and throwing onfinally function", async (test: Test) =>
|
|
670
|
+
{
|
|
671
|
+
const parentResult: PromiseAsyncResult<number> = PromiseAsyncResult.error(new Error("abc"));
|
|
672
|
+
let counter: number = 0;
|
|
673
|
+
const convertErrorResult: PromiseAsyncResult<number> = parentResult.finally(() =>
|
|
674
|
+
{
|
|
675
|
+
counter++;
|
|
676
|
+
throw new RangeError("def");
|
|
677
|
+
});
|
|
678
|
+
test.assertSame(counter, 0);
|
|
679
|
+
for (let i = 0; i < 3; i++)
|
|
680
|
+
{
|
|
681
|
+
await test.assertThrowsAsync(convertErrorResult, new RangeError("def"));
|
|
682
|
+
test.assertSame(counter, 1);
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
});
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
688
|
}
|