@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.
Files changed (163) hide show
  1. package/.c8rc.json +12 -0
  2. package/.github/workflows/publish.yml +38 -0
  3. package/.mocharc.json +9 -0
  4. package/README.md +9 -0
  5. package/package.json +36 -0
  6. package/sources/assertMessageParameters.ts +22 -0
  7. package/sources/asyncIterator.ts +437 -0
  8. package/sources/asyncIteratorToJavascriptAsyncIteratorAdapter.ts +48 -0
  9. package/sources/asyncResult.ts +95 -0
  10. package/sources/basicDisposable.ts +57 -0
  11. package/sources/byteList.ts +202 -0
  12. package/sources/byteListStream.ts +121 -0
  13. package/sources/byteReadStream.ts +24 -0
  14. package/sources/byteWriteStream.ts +16 -0
  15. package/sources/bytes.ts +25 -0
  16. package/sources/characterList.ts +195 -0
  17. package/sources/characterListStream.ts +151 -0
  18. package/sources/characterReadStream.ts +81 -0
  19. package/sources/characterReadStreamIterator.ts +128 -0
  20. package/sources/characterWriteStream.ts +45 -0
  21. package/sources/commandLineParameter.ts +45 -0
  22. package/sources/commandLineParameters.ts +21 -0
  23. package/sources/comparable.ts +144 -0
  24. package/sources/comparer.ts +133 -0
  25. package/sources/comparison.ts +20 -0
  26. package/sources/concatenateIterable.ts +119 -0
  27. package/sources/concatenateIterator.ts +165 -0
  28. package/sources/condition.ts +329 -0
  29. package/sources/currentProcess.ts +158 -0
  30. package/sources/dateTime.ts +130 -0
  31. package/sources/depthFirstSearch.ts +230 -0
  32. package/sources/disposable.ts +31 -0
  33. package/sources/emptyError.ts +10 -0
  34. package/sources/english.ts +45 -0
  35. package/sources/equalFunctions.ts +123 -0
  36. package/sources/fetchHttpClient.ts +89 -0
  37. package/sources/fetchHttpResponse.ts +106 -0
  38. package/sources/flatMapIterable.ts +104 -0
  39. package/sources/flatMapIterator.ts +152 -0
  40. package/sources/generator.ts +251 -0
  41. package/sources/httpClient.ts +36 -0
  42. package/sources/httpHeader.ts +37 -0
  43. package/sources/httpHeaders.ts +216 -0
  44. package/sources/httpIncomingRequest.ts +30 -0
  45. package/sources/httpIncomingResponse.ts +19 -0
  46. package/sources/httpMethod.ts +164 -0
  47. package/sources/httpOutgoingRequest.ts +119 -0
  48. package/sources/httpOutgoingResponse.ts +113 -0
  49. package/sources/httpServer.ts +34 -0
  50. package/sources/inMemoryCharacterWriteStream.ts +78 -0
  51. package/sources/index.ts +101 -0
  52. package/sources/iterable.ts +345 -0
  53. package/sources/iterator.ts +481 -0
  54. package/sources/iteratorToJavascriptIteratorAdapter.ts +48 -0
  55. package/sources/javascript.ts +59 -0
  56. package/sources/javascriptArrayList.ts +175 -0
  57. package/sources/javascriptAsyncIteratorToAsyncIteratorAdapter.ts +124 -0
  58. package/sources/javascriptIteratorToIteratorAdapter.ts +133 -0
  59. package/sources/javascriptMapMap.ts +143 -0
  60. package/sources/javascriptSetSet.ts +134 -0
  61. package/sources/list.ts +330 -0
  62. package/sources/listQueue.ts +62 -0
  63. package/sources/listStack.ts +62 -0
  64. package/sources/luxonDateTime.ts +109 -0
  65. package/sources/map.ts +302 -0
  66. package/sources/mapAsyncIterator.ts +141 -0
  67. package/sources/mapIterable.ts +105 -0
  68. package/sources/mapIterator.ts +145 -0
  69. package/sources/mutableCondition.ts +451 -0
  70. package/sources/mutableHttpHeaders.ts +204 -0
  71. package/sources/mutableMap.ts +292 -0
  72. package/sources/network.ts +18 -0
  73. package/sources/node.ts +37 -0
  74. package/sources/nodeJSCharacterWriteStream.ts +42 -0
  75. package/sources/nodeJSHttpIncomingRequest.ts +132 -0
  76. package/sources/nodeJSHttpServer.ts +134 -0
  77. package/sources/notFoundError.ts +12 -0
  78. package/sources/postCondition.ts +284 -0
  79. package/sources/postConditionError.ts +12 -0
  80. package/sources/preCondition.ts +284 -0
  81. package/sources/preConditionError.ts +12 -0
  82. package/sources/promiseAsyncResult.ts +174 -0
  83. package/sources/property.ts +63 -0
  84. package/sources/queue.ts +49 -0
  85. package/sources/realNetwork.ts +28 -0
  86. package/sources/recreationDotGovClient.ts +259 -0
  87. package/sources/searchControl.ts +42 -0
  88. package/sources/set.ts +244 -0
  89. package/sources/skipAsyncIterator.ts +145 -0
  90. package/sources/skipIterator.ts +155 -0
  91. package/sources/stack.ts +48 -0
  92. package/sources/stringComparer.ts +33 -0
  93. package/sources/stringIterator.ts +149 -0
  94. package/sources/strings.ts +322 -0
  95. package/sources/syncResult.ts +300 -0
  96. package/sources/takeAsyncIterator.ts +141 -0
  97. package/sources/takeIterator.ts +151 -0
  98. package/sources/toStringFunctions.ts +185 -0
  99. package/sources/types.ts +371 -0
  100. package/sources/whereAsyncIterator.ts +143 -0
  101. package/sources/whereIterable.ts +108 -0
  102. package/sources/whereIterator.ts +157 -0
  103. package/sources/wonderlandTrailClient.ts +1503 -0
  104. package/tests/assertTest.ts +113 -0
  105. package/tests/assertTestTests.ts +75 -0
  106. package/tests/basicTestSkip.ts +51 -0
  107. package/tests/byteListStreamTests.ts +390 -0
  108. package/tests/byteListTests.ts +27 -0
  109. package/tests/bytesTests.ts +43 -0
  110. package/tests/characterListStreamTests.ts +391 -0
  111. package/tests/characterListTests.ts +250 -0
  112. package/tests/characterWriteStreamTests.ts +12 -0
  113. package/tests/comparerTests.ts +92 -0
  114. package/tests/conditionTests.ts +877 -0
  115. package/tests/consoleTestRunner.ts +404 -0
  116. package/tests/consoleTestRunnerTests.ts +651 -0
  117. package/tests/dateTimeTests.ts +30 -0
  118. package/tests/depthFirstSearchTests.ts +106 -0
  119. package/tests/disposableTests.ts +121 -0
  120. package/tests/englishTests.ts +103 -0
  121. package/tests/equalFunctionsTests.ts +223 -0
  122. package/tests/failedTest.ts +43 -0
  123. package/tests/fetchHttpClientTests.ts +33 -0
  124. package/tests/generatorTests.ts +86 -0
  125. package/tests/httpClientTests.ts +18 -0
  126. package/tests/inMemoryCharacterWriteStreamTests.ts +117 -0
  127. package/tests/iterableTests.ts +141 -0
  128. package/tests/iteratorTests.ts +1086 -0
  129. package/tests/javascriptMapMapTests.ts +21 -0
  130. package/tests/listTests.ts +338 -0
  131. package/tests/mapIteratorTests.ts +55 -0
  132. package/tests/mapTests.ts +104 -0
  133. package/tests/mutableConditionTests.ts +273 -0
  134. package/tests/mutableMapTests.ts +154 -0
  135. package/tests/nodeJSHttpServerTests.ts +75 -0
  136. package/tests/notFoundErrorTests.ts +24 -0
  137. package/tests/postConditionErrorTests.ts +24 -0
  138. package/tests/preConditionErrorTests.ts +24 -0
  139. package/tests/promiseAsyncResultTests.ts +688 -0
  140. package/tests/propertyTests.ts +63 -0
  141. package/tests/queueTests.ts +29 -0
  142. package/tests/recreationDotGovClientTests.ts +191 -0
  143. package/tests/setTests.ts +140 -0
  144. package/tests/skippedTest.ts +39 -0
  145. package/tests/stackTests.ts +66 -0
  146. package/tests/stringComparerTests.ts +60 -0
  147. package/tests/stringIteratorTests.ts +156 -0
  148. package/tests/stringsTests.ts +516 -0
  149. package/tests/syncResultTests.ts +1251 -0
  150. package/tests/test.ts +228 -0
  151. package/tests/testAction.ts +75 -0
  152. package/tests/testActionTests.ts +93 -0
  153. package/tests/testFailureTests.ts +12 -0
  154. package/tests/testRunner.ts +267 -0
  155. package/tests/testRunnerTests.ts +895 -0
  156. package/tests/testSkip.ts +34 -0
  157. package/tests/tests.ts +103 -0
  158. package/tests/toStringFunctionsTests.ts +55 -0
  159. package/tests/typesTests.ts +257 -0
  160. package/tests/whereIteratorTests.ts +77 -0
  161. package/tests/wonderlandTrailClientTests.ts +452 -0
  162. package/tsconfig.json +17 -0
  163. package/tsup.config.ts +13 -0
@@ -0,0 +1,1251 @@
1
+ import { NotFoundError } from "../sources/notFoundError";
2
+ import { PreConditionError } from "../sources/preConditionError";
3
+ import { SyncResult } from "../sources/syncResult";
4
+ import { Test } from "./test";
5
+ import { TestRunner } from "./testRunner";
6
+
7
+ export function test(runner: TestRunner): void
8
+ {
9
+ runner.testFile("syncResult.ts", () =>
10
+ {
11
+ runner.testType("SyncResult<T>", () =>
12
+ {
13
+ runner.testFunction("create()", () =>
14
+ {
15
+ function createErrorTest<T>(testName: string, action: () => T, expected: Error): void
16
+ {
17
+ runner.test(testName, (test: Test) =>
18
+ {
19
+ test.assertThrows(() => SyncResult.create(action), expected);
20
+ });
21
+ }
22
+
23
+ createErrorTest("with undefined", undefined!, new PreConditionError(
24
+ "Expression: action",
25
+ "Expected: not undefined and not null",
26
+ "Actual: undefined",
27
+ ));
28
+ createErrorTest("with null", null!, new PreConditionError(
29
+ "Expression: action",
30
+ "Expected: not undefined and not null",
31
+ "Actual: null",
32
+ ));
33
+
34
+ runner.test("with action", (test: Test) =>
35
+ {
36
+ const result: SyncResult<number> = SyncResult.create(() => 5);
37
+ test.assertNotUndefinedAndNotNull(result);
38
+
39
+ const value: number = result.await();
40
+ test.assertEqual(5, value);
41
+ });
42
+
43
+ runner.test("with action with side-effects", (test: Test) =>
44
+ {
45
+ let counter = 0;
46
+ const result: SyncResult<number> = SyncResult.create(() => { return ++counter; });
47
+ test.assertEqual(counter, 1);
48
+
49
+ for (let i = 0; i < 3; i++)
50
+ {
51
+ test.assertEqual(1, result.await());
52
+ test.assertEqual(1, counter);
53
+ }
54
+ });
55
+ });
56
+
57
+ runner.testFunction("value()", () =>
58
+ {
59
+ function valueTest<T>(value: T): void
60
+ {
61
+ runner.test(`with ${runner.toString(value)}`, (test: Test) =>
62
+ {
63
+ const result: SyncResult<T> = SyncResult.value(value);
64
+ test.assertNotUndefinedAndNotNull(result);
65
+
66
+ const awaitResult: T = result.await();
67
+ test.assertSame(awaitResult, value);
68
+ });
69
+ }
70
+
71
+ valueTest(undefined);
72
+ valueTest(null);
73
+ valueTest(50);
74
+ valueTest({});
75
+ });
76
+
77
+ runner.testFunction("error()", () =>
78
+ {
79
+ function errorTest<T>(error: Error): void
80
+ {
81
+ runner.test(`with ${runner.toString(error)}`, (test: Test) =>
82
+ {
83
+ const result: SyncResult<T> = SyncResult.error(error);
84
+ test.assertNotUndefinedAndNotNull(result);
85
+
86
+ test.assertThrows(result, error);
87
+ });
88
+ }
89
+
90
+ errorTest(new Error("oops"));
91
+ errorTest(new NotFoundError("not here"));
92
+ });
93
+
94
+ runner.testGroup("async/await", () =>
95
+ {
96
+ runner.test("with value", async (test: Test) =>
97
+ {
98
+ const result: SyncResult<number> = SyncResult.value(5);
99
+ test.assertEqual(await result, 5);
100
+ });
101
+
102
+ runner.test("with error", async (test: Test) =>
103
+ {
104
+ const result: SyncResult<number> = SyncResult.error(new NotFoundError("abc"));
105
+ await test.assertThrowsAsync(result, new NotFoundError("abc"));
106
+ });
107
+ });
108
+
109
+ runner.testFunction("then()", () =>
110
+ {
111
+ runner.testGroup("with non-PromiseLike return type", () =>
112
+ {
113
+ runner.test("with error parent", (test: Test) =>
114
+ {
115
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
116
+ const thenResult: SyncResult<string> = parentResult.then(() => "hello");
117
+ test.assertThrows(() => thenResult.await(), new Error("abc"));
118
+ });
119
+
120
+ runner.test("with error parent and thenFunction with side-effects", (test: Test) =>
121
+ {
122
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
123
+ let counter: number = 0;
124
+ const thenResult: SyncResult<string> = parentResult.then(() => { counter++; return "hello"; });
125
+ test.assertSame(0, counter);
126
+ test.assertThrows(() => thenResult.await(), new Error("abc"));
127
+ test.assertSame(0, counter);
128
+ });
129
+
130
+ runner.test("with successful parent and successful thenFunction that ignores parentResult value", (test: Test) =>
131
+ {
132
+ const parentResult: SyncResult<number> = SyncResult.value(1);
133
+ const thenResult: SyncResult<string> = parentResult.then(() => "hello");
134
+ test.assertSame("hello", thenResult.await());
135
+ });
136
+
137
+ runner.test("with successful parent and successful thenFunction that uses parentResult value", (test: Test) =>
138
+ {
139
+ const parentResult: SyncResult<number> = SyncResult.value(1);
140
+ const thenResult: SyncResult<string> = parentResult.then((argument: number) => (argument + 1).toString());
141
+ test.assertSame("2", thenResult.await());
142
+ });
143
+
144
+ runner.test("with successful parent and successful thenFunction that uses parentResult value with side-effects", (test: Test) =>
145
+ {
146
+ const parentResult: SyncResult<number> = SyncResult.value(1);
147
+ let counter: number = 0;
148
+ const thenResult: SyncResult<string> = parentResult.then((argument: number) => { counter++; return (argument + 1).toString(); });
149
+ test.assertSame(1, counter);
150
+ for (let i = 0; i < 3; i++)
151
+ {
152
+ test.assertSame("2", thenResult.await());
153
+ test.assertSame(1, counter);
154
+ }
155
+ });
156
+
157
+ runner.test("with successful parent and thenFunction that throws", async (test: Test) =>
158
+ {
159
+ const parentResult: SyncResult<number> = SyncResult.value(10);
160
+ let counter: number = 0;
161
+ const thenResult: SyncResult<string> = parentResult.then((argument: number) =>
162
+ {
163
+ counter++;
164
+ throw new Error(`arg: ${argument}`);
165
+ });
166
+ test.assertSame(counter, 1);
167
+
168
+ for (let i = 0; i < 3; i++)
169
+ {
170
+ test.assertThrows(thenResult, new Error("arg: 10"));
171
+ test.assertSame(counter, 1);
172
+
173
+ await test.assertThrowsAsync(thenResult, new Error("arg: 10"));
174
+ test.assertSame(counter, 1);
175
+ }
176
+ });
177
+ });
178
+ });
179
+
180
+ runner.testFunction("onValue()", () =>
181
+ {
182
+ runner.test("with error parent", async (test: Test) =>
183
+ {
184
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
185
+ let counter: number = 0;
186
+ const onValueResult: SyncResult<number> = parentResult.onValue(() => counter++);
187
+ test.assertSame(counter, 0);
188
+ for (let i = 0; i < 3; i++)
189
+ {
190
+ await test.assertThrowsAsync(onValueResult, new Error("abc"));
191
+ test.assertSame(counter, 0);
192
+ }
193
+ });
194
+
195
+ runner.test("with successful parent and successful thenFunction that ignores parentResult value", async (test: Test) =>
196
+ {
197
+ const parentResult: SyncResult<number> = SyncResult.value(10);
198
+ let counter: number = 0;
199
+ const onValueResult: SyncResult<number> = parentResult.onValue(() => counter++);
200
+ test.assertSame(counter, 1);
201
+ for (let i = 0; i < 3; i++)
202
+ {
203
+ test.assertSame(10, onValueResult.await());
204
+ test.assertSame(counter, 1);
205
+
206
+ test.assertSame(10, await onValueResult);
207
+ test.assertSame(counter, 1);
208
+ }
209
+ });
210
+
211
+ runner.test("with successful parent and successful thenFunction that uses parentResult value", async (test: Test) =>
212
+ {
213
+ const parentResult: SyncResult<number> = SyncResult.value(2);
214
+ let counter: number = 0;
215
+ const onValueResult: SyncResult<number> = parentResult.onValue((argument: number) => counter += argument);
216
+ test.assertSame(counter, 2);
217
+ for (let i = 0; i < 3; i++)
218
+ {
219
+ test.assertSame(onValueResult.await(), 2);
220
+ test.assertSame(counter, 2);
221
+
222
+ test.assertSame(await onValueResult, 2);
223
+ test.assertSame(counter, 2);
224
+ }
225
+ });
226
+
227
+ runner.test("with successful parent and onValueFunction that throws", async (test: Test) =>
228
+ {
229
+ const parentResult: SyncResult<number> = SyncResult.value(2);
230
+ let counter: number = 0;
231
+ const onValueResult: SyncResult<number> = parentResult.onValue((argument: number) =>
232
+ {
233
+ counter += argument;
234
+ throw new Error(`argument: ${argument}`);
235
+ });
236
+ test.assertSame(counter, 2);
237
+ for (let i = 0; i < 3; i++)
238
+ {
239
+ test.assertThrows(onValueResult, new Error("argument: 2"));
240
+ test.assertSame(counter, 2);
241
+
242
+ await test.assertThrowsAsync(onValueResult, new Error("argument: 2"));
243
+ test.assertSame(counter, 2);
244
+ }
245
+ });
246
+ });
247
+
248
+ runner.testFunction("catch()", () =>
249
+ {
250
+ runner.test("with undefined errorType", (test: Test) =>
251
+ {
252
+ const parentResult: SyncResult<number> = SyncResult.value(5);
253
+ test.assertThrows(() => parentResult.catch(undefined!, () => 6),
254
+ new PreConditionError(
255
+ "Expression: errorType",
256
+ "Expected: not undefined and not null",
257
+ "Actual: undefined"));
258
+ });
259
+
260
+ runner.test("with null errorType", (test: Test) =>
261
+ {
262
+ const parentResult: SyncResult<number> = SyncResult.value(5);
263
+ test.assertThrows(() => parentResult.catch(null!, () => 6),
264
+ new PreConditionError(
265
+ "Expression: errorType",
266
+ "Expected: not undefined and not null",
267
+ "Actual: null"));
268
+ });
269
+
270
+ runner.testGroup("sync", () =>
271
+ {
272
+ runner.test("with error parent", (test: Test) =>
273
+ {
274
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
275
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => 20);
276
+ test.assertSame(catchResult.await(), 20);
277
+ });
278
+
279
+ runner.test("with error parent, no errorType, and no error parameter", (test: Test) =>
280
+ {
281
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
282
+ let counter: number = 0;
283
+ const catchResult: SyncResult<number> = parentResult.catch(() => { counter++; return 21; });
284
+ test.assertSame(1, counter);
285
+ for (let i = 0; i < 3; i++)
286
+ {
287
+ test.assertSame(catchResult.await(), 21);
288
+ test.assertSame(1, counter);
289
+ }
290
+ });
291
+
292
+ runner.test("with error parent, no errorType, and unknown error parameter", (test: Test) =>
293
+ {
294
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
295
+ let counter: number = 0;
296
+ const catchResult: SyncResult<number> = parentResult.catch((error: unknown) =>
297
+ {
298
+ if (error instanceof Error)
299
+ {
300
+ counter += error.message.length;
301
+ }
302
+ else
303
+ {
304
+ counter -= 1;
305
+ }
306
+ return 21;
307
+ });
308
+ test.assertSame(3, counter);
309
+ for (let i = 0; i < 3; i++)
310
+ {
311
+ test.assertSame(catchResult.await(), 21);
312
+ test.assertSame(3, counter);
313
+ }
314
+ });
315
+
316
+ runner.test("with error parent and catchFunction with side-effects", (test: Test) =>
317
+ {
318
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
319
+ let counter: number = 0;
320
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => { counter++; return 21; });
321
+ test.assertSame(1, counter);
322
+ for (let i = 0; i < 3; i++)
323
+ {
324
+ test.assertSame(catchResult.await(), 21);
325
+ test.assertSame(1, counter);
326
+ }
327
+ });
328
+
329
+ runner.test("with errorType that is a super-type of the actual error without error parameter", (test: Test) =>
330
+ {
331
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
332
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => 5);
333
+ test.assertSame(catchResult.await(), 5);
334
+ });
335
+
336
+ runner.test("with errorType that is a super-type of the actual error with error parameter", (test: Test) =>
337
+ {
338
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
339
+ const catchResult: SyncResult<number> = parentResult.catch(Error, (error: Error) => error.message.length);
340
+ test.assertSame(catchResult.await(), 3);
341
+ });
342
+
343
+ runner.test("with errorType that is a sub-type of the actual error", (test: Test) =>
344
+ {
345
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
346
+ const catchResult: SyncResult<number> = parentResult.catch(PreConditionError, () => 20);
347
+ test.assertThrows(catchResult, new Error("abc"));
348
+ });
349
+
350
+ runner.test("with errorType that is unrelated to the actual error", (test: Test) =>
351
+ {
352
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
353
+ const catchResult: SyncResult<number> = parentResult.catch(RangeError, () => 20);
354
+ test.assertThrows(catchResult, new PreConditionError("def"));
355
+ });
356
+
357
+ runner.test("with catchFunction that throws", (test: Test) =>
358
+ {
359
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
360
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => { throw new TypeError("abc"); });
361
+ test.assertThrows(catchResult, new TypeError("abc"));
362
+ });
363
+
364
+ runner.test("with successful parent", (test: Test) =>
365
+ {
366
+ const parentResult: SyncResult<number> = SyncResult.value(1);
367
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => 2);
368
+ test.assertSame(catchResult.await(), 1);
369
+ });
370
+ });
371
+
372
+ runner.testGroup("async", () =>
373
+ {
374
+ runner.test("with error parent", async (test: Test) =>
375
+ {
376
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
377
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => 20);
378
+ test.assertSame(await catchResult, 20);
379
+ });
380
+
381
+ runner.test("with error parent, no errorType, and no error parameter", async (test: Test) =>
382
+ {
383
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
384
+ let counter: number = 0;
385
+ const catchResult: SyncResult<number> = parentResult.catch(() => { counter++; return 21; });
386
+ test.assertSame(1, counter);
387
+ for (let i = 0; i < 3; i++)
388
+ {
389
+ test.assertSame(await catchResult, 21);
390
+ test.assertSame(1, counter);
391
+ }
392
+ });
393
+
394
+ runner.test("with error parent, no errorType, and unknown error parameter", async (test: Test) =>
395
+ {
396
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
397
+ let counter: number = 0;
398
+ const catchResult: SyncResult<number> = parentResult.catch((error: unknown) =>
399
+ {
400
+ if (error instanceof Error)
401
+ {
402
+ counter += error.message.length;
403
+ }
404
+ else
405
+ {
406
+ counter -= 1;
407
+ }
408
+ return 21;
409
+ });
410
+ test.assertSame(3, counter);
411
+ for (let i = 0; i < 3; i++)
412
+ {
413
+ test.assertSame(await catchResult, 21);
414
+ test.assertSame(3, counter);
415
+ }
416
+ });
417
+
418
+ runner.test("with error parent and catchFunction with side-effects", async (test: Test) =>
419
+ {
420
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
421
+ let counter: number = 0;
422
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => { counter++; return 21; });
423
+ test.assertSame(1, counter);
424
+ for (let i = 0; i < 3; i++)
425
+ {
426
+ test.assertSame(await catchResult, 21);
427
+ test.assertSame(1, counter);
428
+ }
429
+ });
430
+
431
+ runner.test("with errorType that is a super-type of the actual error without error parameter", async (test: Test) =>
432
+ {
433
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
434
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => 5);
435
+ test.assertSame(await catchResult, 5);
436
+ });
437
+
438
+ runner.test("with errorType that is a super-type of the actual error with error parameter", async (test: Test) =>
439
+ {
440
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
441
+ const catchResult: SyncResult<number> = parentResult.catch(Error, (error: Error) => error.message.length);
442
+ test.assertSame(await catchResult, 3);
443
+ });
444
+
445
+ runner.test("with errorType that is a sub-type of the actual error", async (test: Test) =>
446
+ {
447
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
448
+ const catchResult: SyncResult<number> = parentResult.catch(PreConditionError, () => 20);
449
+ await test.assertThrowsAsync(catchResult, new Error("abc"));
450
+ });
451
+
452
+ runner.test("with errorType that is unrelated to the actual error", async (test: Test) =>
453
+ {
454
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
455
+ const catchResult: SyncResult<number> = parentResult.catch(RangeError, () => 20);
456
+ await test.assertThrowsAsync(catchResult, new PreConditionError("def"));
457
+ });
458
+
459
+ runner.test("with catchFunction that throws", async (test: Test) =>
460
+ {
461
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
462
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => { throw new TypeError("abc"); });
463
+ await test.assertThrowsAsync(catchResult, new TypeError("abc"));
464
+ });
465
+
466
+ runner.test("with successful parent", async (test: Test) =>
467
+ {
468
+ const parentResult: SyncResult<number> = SyncResult.value(1);
469
+ const catchResult: SyncResult<number> = parentResult.catch(Error, () => 2);
470
+ test.assertSame(await catchResult, 1);
471
+ });
472
+ });
473
+ });
474
+
475
+ runner.testFunction("onError()", () =>
476
+ {
477
+ runner.test("with undefined errorType", (test: Test) =>
478
+ {
479
+ const parentResult: SyncResult<number> = SyncResult.value(5);
480
+ test.assertThrows(() => parentResult.onError(undefined!, () => { }),
481
+ new PreConditionError(
482
+ "Expression: errorType",
483
+ "Expected: not undefined and not null",
484
+ "Actual: undefined"));
485
+ });
486
+
487
+ runner.test("with null errorType", (test: Test) =>
488
+ {
489
+ const parentResult: SyncResult<number> = SyncResult.value(5);
490
+ test.assertThrows(() => parentResult.onError(null!, () => { }),
491
+ new PreConditionError(
492
+ "Expression: errorType",
493
+ "Expected: not undefined and not null",
494
+ "Actual: null"));
495
+ });
496
+
497
+ runner.testGroup("sync", () =>
498
+ {
499
+ runner.test("with error parent, no errorType, and no error parameter", (test: Test) =>
500
+ {
501
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
502
+ let counter: number = 0;
503
+ const catchResult: SyncResult<number> = parentResult.onError(() => { counter++; });
504
+ test.assertSame(1, counter);
505
+ for (let i = 0; i < 3; i++)
506
+ {
507
+ test.assertThrows(catchResult, new Error("abc"));
508
+ test.assertSame(1, counter);
509
+ }
510
+ });
511
+
512
+ runner.test("with error parent, no errorType, and unknown error parameter", (test: Test) =>
513
+ {
514
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
515
+ let counter: number = 0;
516
+ const catchResult: SyncResult<number> = parentResult.onError((error: unknown) =>
517
+ {
518
+ if (error instanceof Error)
519
+ {
520
+ counter += error.message.length;
521
+ }
522
+ else
523
+ {
524
+ counter -= 1;
525
+ }
526
+ });
527
+ test.assertSame(3, counter);
528
+ for (let i = 0; i < 3; i++)
529
+ {
530
+ test.assertThrows(catchResult, new Error("abc"));
531
+ test.assertSame(3, counter);
532
+ }
533
+ });
534
+
535
+ runner.test("with errorType that is a super-type of the actual error without error parameter", (test: Test) =>
536
+ {
537
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
538
+ let counter: number = 0;
539
+ const catchResult: SyncResult<number> = parentResult.onError(Error, () => { counter++; });
540
+ test.assertSame(counter, 1);
541
+ for (let i = 0; i < 3; i++)
542
+ {
543
+ test.assertThrows(catchResult, new PreConditionError("abc"));
544
+ test.assertSame(counter, 1);
545
+ }
546
+ });
547
+
548
+ runner.test("with errorType that is a super-type of the actual error with error parameter", (test: Test) =>
549
+ {
550
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
551
+ let counter: number = 0;
552
+ const catchResult: SyncResult<number> = parentResult.onError(Error, (error: Error) => { counter += error.message.length; });
553
+ test.assertSame(counter, 3);
554
+ for (let i = 0; i < 3; i++)
555
+ {
556
+ test.assertThrows(catchResult, new PreConditionError("abc"));
557
+ test.assertSame(counter, 3);
558
+ }
559
+ });
560
+
561
+ runner.test("with errorType that is a sub-type of the actual error", (test: Test) =>
562
+ {
563
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
564
+ let counter: number = 0;
565
+ const catchResult: SyncResult<number> = parentResult.onError(PreConditionError, () => { counter++; });
566
+ test.assertSame(counter, 0);
567
+ for (let i = 0; i < 3; i++)
568
+ {
569
+ test.assertThrows(catchResult, new Error("abc"));
570
+ test.assertSame(counter, 0);
571
+ }
572
+ });
573
+
574
+ runner.test("with errorType that is unrelated to the actual error", (test: Test) =>
575
+ {
576
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
577
+ let counter: number = 0;
578
+ const catchResult: SyncResult<number> = parentResult.onError(RangeError, () => { counter++; });
579
+ test.assertSame(counter, 0);
580
+ for (let i = 0; i < 3; i++)
581
+ {
582
+ test.assertThrows(catchResult, new PreConditionError("def"));
583
+ test.assertSame(counter, 0);
584
+ }
585
+ });
586
+
587
+ runner.test("with onErrorFunction that throws", (test: Test) =>
588
+ {
589
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
590
+ let counter: number = 0;
591
+ const catchResult: SyncResult<number> = parentResult.onError(Error, () => { counter++; throw new Error("abc"); });
592
+ test.assertSame(counter, 1);
593
+ for (let i = 0; i < 3; i++)
594
+ {
595
+ test.assertThrows(catchResult, new Error("abc"));
596
+ test.assertSame(counter, 1);
597
+ }
598
+ });
599
+
600
+ runner.test("with successful parent", (test: Test) =>
601
+ {
602
+ const parentResult: SyncResult<number> = SyncResult.value(1);
603
+ let counter: number = 0;
604
+ const catchResult: SyncResult<number> = parentResult.onError(Error, () => { counter++; });
605
+ test.assertSame(counter, 0);
606
+ for (let i = 0; i < 3; i++)
607
+ {
608
+ test.assertSame(catchResult.await(), 1);
609
+ test.assertSame(counter, 0);
610
+ }
611
+ });
612
+ });
613
+
614
+ runner.testGroup("async", () =>
615
+ {
616
+ runner.test("with error parent, no errorType, and no error parameter", async (test: Test) =>
617
+ {
618
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
619
+ let counter: number = 0;
620
+ const catchResult: SyncResult<number> = parentResult.onError(() => { counter++; });
621
+ test.assertSame(1, counter);
622
+ for (let i = 0; i < 3; i++)
623
+ {
624
+ await test.assertThrowsAsync(catchResult, new Error("abc"));
625
+ test.assertSame(1, counter);
626
+ }
627
+ });
628
+
629
+ runner.test("with error parent, no errorType, and unknown error parameter", async (test: Test) =>
630
+ {
631
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
632
+ let counter: number = 0;
633
+ const catchResult: SyncResult<number> = parentResult.onError((error: unknown) =>
634
+ {
635
+ if (error instanceof Error)
636
+ {
637
+ counter += error.message.length;
638
+ }
639
+ else
640
+ {
641
+ counter -= 1;
642
+ }
643
+ });
644
+ test.assertSame(3, counter);
645
+ for (let i = 0; i < 3; i++)
646
+ {
647
+ await test.assertThrowsAsync(catchResult, new Error("abc"));
648
+ test.assertSame(3, counter);
649
+ }
650
+ });
651
+
652
+ runner.test("with errorType that is a super-type of the actual error without error parameter", async (test: Test) =>
653
+ {
654
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
655
+ let counter: number = 0;
656
+ const catchResult: SyncResult<number> = parentResult.onError(Error, () => { counter++; });
657
+ test.assertSame(counter, 1);
658
+ for (let i = 0; i < 3; i++)
659
+ {
660
+ await test.assertThrowsAsync(catchResult, new PreConditionError("abc"));
661
+ test.assertSame(counter, 1);
662
+ }
663
+ });
664
+
665
+ runner.test("with errorType that is a super-type of the actual error with error parameter", async (test: Test) =>
666
+ {
667
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
668
+ let counter: number = 0;
669
+ const catchResult: SyncResult<number> = parentResult.onError(Error, (error: Error) => { counter += error.message.length; });
670
+ test.assertSame(counter, 3);
671
+ for (let i = 0; i < 3; i++)
672
+ {
673
+ await test.assertThrowsAsync(catchResult, new PreConditionError("abc"));
674
+ test.assertSame(counter, 3);
675
+ }
676
+ });
677
+
678
+ runner.test("with errorType that is a sub-type of the actual error", async (test: Test) =>
679
+ {
680
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
681
+ let counter: number = 0;
682
+ const catchResult: SyncResult<number> = parentResult.onError(PreConditionError, () => { counter++; });
683
+ test.assertSame(counter, 0);
684
+ for (let i = 0; i < 3; i++)
685
+ {
686
+ await test.assertThrowsAsync(catchResult, new Error("abc"));
687
+ test.assertSame(counter, 0);
688
+ }
689
+ });
690
+
691
+ runner.test("with errorType that is unrelated to the actual error", async (test: Test) =>
692
+ {
693
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
694
+ let counter: number = 0;
695
+ const catchResult: SyncResult<number> = parentResult.onError(RangeError, () => { counter++; });
696
+ test.assertSame(counter, 0);
697
+ for (let i = 0; i < 3; i++)
698
+ {
699
+ await test.assertThrowsAsync(catchResult, new PreConditionError("def"));
700
+ test.assertSame(counter, 0);
701
+ }
702
+ });
703
+
704
+ runner.test("with onErrorFunction that throws", async (test: Test) =>
705
+ {
706
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("def"));
707
+ let counter: number = 0;
708
+ const catchResult: SyncResult<number> = parentResult.onError(Error, () => { counter++; throw new Error("abc"); });
709
+ test.assertSame(counter, 1);
710
+ for (let i = 0; i < 3; i++)
711
+ {
712
+ await test.assertThrowsAsync(catchResult, new Error("abc"));
713
+ test.assertSame(counter, 1);
714
+ }
715
+ });
716
+
717
+ runner.test("with successful parent", async (test: Test) =>
718
+ {
719
+ const parentResult: SyncResult<number> = SyncResult.value(1);
720
+ let counter: number = 0;
721
+ const catchResult: SyncResult<number> = parentResult.onError(Error, () => { counter++; });
722
+ test.assertSame(counter, 0);
723
+ for (let i = 0; i < 3; i++)
724
+ {
725
+ test.assertSame(await catchResult, 1);
726
+ test.assertSame(counter, 0);
727
+ }
728
+ });
729
+ });
730
+ });
731
+
732
+ runner.testFunction("convertError()", () =>
733
+ {
734
+ runner.testGroup("async", () =>
735
+ {
736
+ runner.test("with undefined convertErrorFunction", (test: Test) =>
737
+ {
738
+ const parentResult: SyncResult<number> = SyncResult.value(5);
739
+ test.assertThrows(() => parentResult.convertError(undefined!),
740
+ new PreConditionError(
741
+ "Expression: convertErrorFunction",
742
+ "Expected: not undefined and not null",
743
+ "Actual: undefined"));
744
+ });
745
+
746
+ runner.test("with null convertErrorFunction", (test: Test) =>
747
+ {
748
+ const parentResult: SyncResult<number> = SyncResult.value(5);
749
+ test.assertThrows(() => parentResult.convertError(null!),
750
+ new PreConditionError(
751
+ "Expression: convertErrorFunction",
752
+ "Expected: not undefined and not null",
753
+ "Actual: null"));
754
+ });
755
+
756
+ runner.test("with successful parent", async (test: Test) =>
757
+ {
758
+ const parentResult: SyncResult<number> = SyncResult.value(5);
759
+ let counter: number = 0;
760
+ const convertErrorResult: SyncResult<number> = parentResult.convertError((error: unknown) =>
761
+ {
762
+ counter++;
763
+ return new Error(`${error} - def`);
764
+ });
765
+ test.assertSame(counter, 0);
766
+ for (let i = 0; i < 3; i++)
767
+ {
768
+ test.assertSame(await convertErrorResult, 5);
769
+ test.assertSame(counter, 0);
770
+ }
771
+ });
772
+
773
+ runner.test("with error parent and non-throwing convertErrorFunction", async (test: Test) =>
774
+ {
775
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
776
+ let counter: number = 0;
777
+ const convertErrorResult: SyncResult<number> = parentResult.convertError((error: unknown) =>
778
+ {
779
+ counter++;
780
+ return new Error(`${(error as Error).message} - def`);
781
+ });
782
+ test.assertSame(counter, 1);
783
+ for (let i = 0; i < 3; i++)
784
+ {
785
+ await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
786
+ test.assertSame(counter, 1);
787
+ }
788
+ });
789
+
790
+ runner.test("with error parent and throwing convertErrorFunction", async (test: Test) =>
791
+ {
792
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
793
+ let counter: number = 0;
794
+ const convertErrorResult: SyncResult<number> = parentResult.convertError((error: unknown) =>
795
+ {
796
+ counter++;
797
+ throw new Error(`${(error as Error).message} - def`);
798
+ });
799
+ test.assertSame(counter, 1);
800
+ for (let i = 0; i < 3; i++)
801
+ {
802
+ await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
803
+ test.assertSame(counter, 1);
804
+ }
805
+ });
806
+
807
+ runner.test("with successful parent", async (test: Test) =>
808
+ {
809
+ const parentResult: SyncResult<number> = SyncResult.value(5);
810
+ let counter: number = 0;
811
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
812
+ {
813
+ counter++;
814
+ return new Error(`${error.message} - def`);
815
+ });
816
+ test.assertSame(counter, 0);
817
+ for (let i = 0; i < 3; i++)
818
+ {
819
+ test.assertSame(await convertErrorResult, 5);
820
+ test.assertSame(counter, 0);
821
+ }
822
+ });
823
+
824
+ runner.test("with error parent, exact error match, and non-throwing convertErrorFunction", async (test: Test) =>
825
+ {
826
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
827
+ let counter: number = 0;
828
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
829
+ {
830
+ counter++;
831
+ return new Error(`${error.message} - def`);
832
+ });
833
+ test.assertSame(counter, 1);
834
+ for (let i = 0; i < 3; i++)
835
+ {
836
+ await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
837
+ test.assertSame(counter, 1);
838
+ }
839
+ });
840
+
841
+ runner.test("with error parent, super error match, and non-throwing convertErrorFunction", async (test: Test) =>
842
+ {
843
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
844
+ let counter: number = 0;
845
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
846
+ {
847
+ counter++;
848
+ return new Error(`${error.message} - def`);
849
+ });
850
+ test.assertSame(counter, 1);
851
+ for (let i = 0; i < 3; i++)
852
+ {
853
+ await test.assertThrowsAsync(convertErrorResult, new Error(`abc - def`));
854
+ test.assertSame(counter, 1);
855
+ }
856
+ });
857
+
858
+ runner.test("with error parent, no error match, and non-throwing convertErrorFunction", async (test: Test) =>
859
+ {
860
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
861
+ let counter: number = 0;
862
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(TypeError, (error: TypeError) =>
863
+ {
864
+ counter++;
865
+ return new Error(`${error} - def`);
866
+ });
867
+ test.assertSame(counter, 0);
868
+ for (let i = 0; i < 3; i++)
869
+ {
870
+ await test.assertThrowsAsync(convertErrorResult, new PreConditionError("abc"));
871
+ test.assertSame(counter, 0);
872
+ }
873
+ });
874
+
875
+ runner.test("with error parent, exact error match, and throwing convertErrorFunction", async (test: Test) =>
876
+ {
877
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
878
+ let counter: number = 0;
879
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
880
+ {
881
+ counter++;
882
+ throw new Error(`${error.message} - def`);
883
+ });
884
+ test.assertSame(counter, 1);
885
+ for (let i = 0; i < 3; i++)
886
+ {
887
+ await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
888
+ test.assertSame(counter, 1);
889
+ }
890
+ });
891
+
892
+ runner.test("with error parent, super error match, and throwing convertErrorFunction", async (test: Test) =>
893
+ {
894
+ const parentResult: SyncResult<number> = SyncResult.error(new TypeError("abc"));
895
+ let counter: number = 0;
896
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
897
+ {
898
+ counter++;
899
+ throw new Error(`${error.message} - def`);
900
+ });
901
+ test.assertSame(counter, 1);
902
+ for (let i = 0; i < 3; i++)
903
+ {
904
+ await test.assertThrowsAsync(convertErrorResult, new Error("abc - def"));
905
+ test.assertSame(counter, 1);
906
+ }
907
+ });
908
+
909
+ runner.test("with error parent, no error match, and throwing convertErrorFunction", async (test: Test) =>
910
+ {
911
+ const parentResult: SyncResult<number> = SyncResult.error(new TypeError("abc"));
912
+ let counter: number = 0;
913
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(PreConditionError, () =>
914
+ {
915
+ counter++;
916
+ throw new Error("def");
917
+ });
918
+ test.assertSame(counter, 0);
919
+ for (let i = 0; i < 3; i++)
920
+ {
921
+ await test.assertThrowsAsync(convertErrorResult, new TypeError("abc"));
922
+ test.assertSame(counter, 0);
923
+ }
924
+ });
925
+ });
926
+
927
+ runner.testGroup("sync", () =>
928
+ {
929
+ runner.test("with undefined convertErrorFunction", (test: Test) =>
930
+ {
931
+ const parentResult: SyncResult<number> = SyncResult.value(5);
932
+ test.assertThrows(() => parentResult.convertError(undefined!),
933
+ new PreConditionError(
934
+ "Expression: convertErrorFunction",
935
+ "Expected: not undefined and not null",
936
+ "Actual: undefined"));
937
+ });
938
+
939
+ runner.test("with null convertErrorFunction", (test: Test) =>
940
+ {
941
+ const parentResult: SyncResult<number> = SyncResult.value(5);
942
+ test.assertThrows(() => parentResult.convertError(null!),
943
+ new PreConditionError(
944
+ "Expression: convertErrorFunction",
945
+ "Expected: not undefined and not null",
946
+ "Actual: null"));
947
+ });
948
+
949
+ runner.test("with successful parent", (test: Test) =>
950
+ {
951
+ const parentResult: SyncResult<number> = SyncResult.value(5);
952
+ let counter: number = 0;
953
+ const convertErrorResult: SyncResult<number> = parentResult.convertError((error: unknown) =>
954
+ {
955
+ counter++;
956
+ return new Error(`${error} - def`);
957
+ });
958
+ test.assertSame(counter, 0);
959
+ for (let i = 0; i < 3; i++)
960
+ {
961
+ test.assertSame(convertErrorResult.await(), 5);
962
+ test.assertSame(counter, 0);
963
+ }
964
+ });
965
+
966
+ runner.test("with error parent and non-throwing convertErrorFunction", (test: Test) =>
967
+ {
968
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
969
+ let counter: number = 0;
970
+ const convertErrorResult: SyncResult<number> = parentResult.convertError((error: unknown) =>
971
+ {
972
+ counter++;
973
+ return new Error(`${(error as Error).message} - def`);
974
+ });
975
+ test.assertSame(counter, 1);
976
+ for (let i = 0; i < 3; i++)
977
+ {
978
+ test.assertThrows(convertErrorResult, new Error("abc - def"));
979
+ test.assertSame(counter, 1);
980
+ }
981
+ });
982
+
983
+ runner.test("with error parent and throwing convertErrorFunction", (test: Test) =>
984
+ {
985
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
986
+ let counter: number = 0;
987
+ const convertErrorResult: SyncResult<number> = parentResult.convertError((error: unknown) =>
988
+ {
989
+ counter++;
990
+ throw new Error(`${(error as Error).message} - def`);
991
+ });
992
+ test.assertSame(counter, 1);
993
+ for (let i = 0; i < 3; i++)
994
+ {
995
+ test.assertThrows(convertErrorResult, new Error("abc - def"));
996
+ test.assertSame(counter, 1);
997
+ }
998
+ });
999
+
1000
+ runner.test("with successful parent", (test: Test) =>
1001
+ {
1002
+ const parentResult: SyncResult<number> = SyncResult.value(5);
1003
+ let counter: number = 0;
1004
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
1005
+ {
1006
+ counter++;
1007
+ return new Error(`${error.message} - def`);
1008
+ });
1009
+ test.assertSame(counter, 0);
1010
+ for (let i = 0; i < 3; i++)
1011
+ {
1012
+ test.assertSame(convertErrorResult.await(), 5);
1013
+ test.assertSame(counter, 0);
1014
+ }
1015
+ });
1016
+
1017
+ runner.test("with error parent, exact error match, and non-throwing convertErrorFunction", (test: Test) =>
1018
+ {
1019
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
1020
+ let counter: number = 0;
1021
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
1022
+ {
1023
+ counter++;
1024
+ return new Error(`${error.message} - def`);
1025
+ });
1026
+ test.assertSame(counter, 1);
1027
+ for (let i = 0; i < 3; i++)
1028
+ {
1029
+ test.assertThrows(convertErrorResult, new Error("abc - def"));
1030
+ test.assertSame(counter, 1);
1031
+ }
1032
+ });
1033
+
1034
+ runner.test("with error parent, super error match, and non-throwing convertErrorFunction", (test: Test) =>
1035
+ {
1036
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
1037
+ let counter: number = 0;
1038
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
1039
+ {
1040
+ counter++;
1041
+ return new Error(`${error.message} - def`);
1042
+ });
1043
+ test.assertSame(counter, 1);
1044
+ for (let i = 0; i < 3; i++)
1045
+ {
1046
+ test.assertThrows(convertErrorResult, new Error(`abc - def`));
1047
+ test.assertSame(counter, 1);
1048
+ }
1049
+ });
1050
+
1051
+ runner.test("with error parent, no error match, and non-throwing convertErrorFunction", (test: Test) =>
1052
+ {
1053
+ const parentResult: SyncResult<number> = SyncResult.error(new PreConditionError("abc"));
1054
+ let counter: number = 0;
1055
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(TypeError, (error: TypeError) =>
1056
+ {
1057
+ counter++;
1058
+ return new Error(`${error} - def`);
1059
+ });
1060
+ test.assertSame(counter, 0);
1061
+ for (let i = 0; i < 3; i++)
1062
+ {
1063
+ test.assertThrows(convertErrorResult, new PreConditionError("abc"));
1064
+ test.assertSame(counter, 0);
1065
+ }
1066
+ });
1067
+
1068
+ runner.test("with error parent, exact error match, and throwing convertErrorFunction", (test: Test) =>
1069
+ {
1070
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
1071
+ let counter: number = 0;
1072
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
1073
+ {
1074
+ counter++;
1075
+ throw new Error(`${error.message} - def`);
1076
+ });
1077
+ test.assertSame(counter, 1);
1078
+ for (let i = 0; i < 3; i++)
1079
+ {
1080
+ test.assertThrows(convertErrorResult, new Error("abc - def"));
1081
+ test.assertSame(counter, 1);
1082
+ }
1083
+ });
1084
+
1085
+ runner.test("with error parent, super error match, and throwing convertErrorFunction", (test: Test) =>
1086
+ {
1087
+ const parentResult: SyncResult<number> = SyncResult.error(new TypeError("abc"));
1088
+ let counter: number = 0;
1089
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(Error, (error: Error) =>
1090
+ {
1091
+ counter++;
1092
+ throw new Error(`${error.message} - def`);
1093
+ });
1094
+ test.assertSame(counter, 1);
1095
+ for (let i = 0; i < 3; i++)
1096
+ {
1097
+ test.assertThrows(convertErrorResult, new Error("abc - def"));
1098
+ test.assertSame(counter, 1);
1099
+ }
1100
+ });
1101
+
1102
+ runner.test("with error parent, no error match, and throwing convertErrorFunction", (test: Test) =>
1103
+ {
1104
+ const parentResult: SyncResult<number> = SyncResult.error(new TypeError("abc"));
1105
+ let counter: number = 0;
1106
+ const convertErrorResult: SyncResult<number> = parentResult.convertError(PreConditionError, () =>
1107
+ {
1108
+ counter++;
1109
+ throw new Error("def");
1110
+ });
1111
+ test.assertSame(counter, 0);
1112
+ for (let i = 0; i < 3; i++)
1113
+ {
1114
+ test.assertThrows(convertErrorResult, new TypeError("abc"));
1115
+ test.assertSame(counter, 0);
1116
+ }
1117
+ });
1118
+ });
1119
+ });
1120
+
1121
+ runner.testFunction("finally()", () =>
1122
+ {
1123
+ runner.testGroup("async", () =>
1124
+ {
1125
+ runner.test("with successful parent and non-throwing onfinally function", async (test: Test) =>
1126
+ {
1127
+ const parentResult: SyncResult<number> = SyncResult.value(5);
1128
+ let counter: number = 0;
1129
+ const finallyResult: SyncResult<number> = parentResult.finally(() => { counter++; });
1130
+ test.assertSame(counter, 1);
1131
+ for (let i = 0; i < 3; i++)
1132
+ {
1133
+ test.assertSame(await finallyResult, 5);
1134
+ test.assertSame(counter, 1);
1135
+ }
1136
+ });
1137
+
1138
+ runner.test("with successful parent and throwing onfinally function", async (test: Test) =>
1139
+ {
1140
+ const parentResult: SyncResult<number> = SyncResult.value(5);
1141
+ let counter: number = 0;
1142
+ const finallyResult: SyncResult<number> = parentResult.finally(() =>
1143
+ {
1144
+ counter++;
1145
+ throw new RangeError("oops!");
1146
+ });
1147
+ test.assertSame(counter, 1);
1148
+ for (let i = 0; i < 3; i++)
1149
+ {
1150
+ await test.assertThrowsAsync(finallyResult, new RangeError("oops!"));
1151
+ test.assertSame(counter, 1);
1152
+ }
1153
+ });
1154
+
1155
+ runner.test("with error parent and non-throwing onfinally function", async (test: Test) =>
1156
+ {
1157
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
1158
+ let counter: number = 0;
1159
+ const finallyResult: SyncResult<number> = parentResult.finally(() => { counter++; });
1160
+ test.assertSame(counter, 1);
1161
+ for (let i = 0; i < 3; i++)
1162
+ {
1163
+ await test.assertThrowsAsync(finallyResult, new Error("abc"));
1164
+ test.assertSame(counter, 1);
1165
+ }
1166
+ });
1167
+
1168
+ runner.test("with error parent and throwing onfinally function", async (test: Test) =>
1169
+ {
1170
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
1171
+ let counter: number = 0;
1172
+ const convertErrorResult: SyncResult<number> = parentResult.finally(() =>
1173
+ {
1174
+ counter++;
1175
+ throw new RangeError("def");
1176
+ });
1177
+ test.assertSame(counter, 1);
1178
+ for (let i = 0; i < 3; i++)
1179
+ {
1180
+ await test.assertThrowsAsync(convertErrorResult, new RangeError("def"));
1181
+ test.assertSame(counter, 1);
1182
+ }
1183
+ });
1184
+ });
1185
+
1186
+ runner.testGroup("sync", () =>
1187
+ {
1188
+ runner.test("with successful parent and non-throwing onfinally function", (test: Test) =>
1189
+ {
1190
+ const parentResult: SyncResult<number> = SyncResult.value(5);
1191
+ let counter: number = 0;
1192
+ const finallyResult: SyncResult<number> = parentResult.finally(() => { counter++; });
1193
+ test.assertSame(counter, 1);
1194
+ for (let i = 0; i < 3; i++)
1195
+ {
1196
+ test.assertSame(finallyResult.await(), 5);
1197
+ test.assertSame(counter, 1);
1198
+ }
1199
+ });
1200
+
1201
+ runner.test("with successful parent and throwing onfinally function", (test: Test) =>
1202
+ {
1203
+ const parentResult: SyncResult<number> = SyncResult.value(5);
1204
+ let counter: number = 0;
1205
+ const finallyResult: SyncResult<number> = parentResult.finally(() =>
1206
+ {
1207
+ counter++;
1208
+ throw new RangeError("oops!");
1209
+ });
1210
+ test.assertSame(counter, 1);
1211
+ for (let i = 0; i < 3; i++)
1212
+ {
1213
+ test.assertThrows(finallyResult, new RangeError("oops!"));
1214
+ test.assertSame(counter, 1);
1215
+ }
1216
+ });
1217
+
1218
+ runner.test("with error parent and non-throwing onfinally function", (test: Test) =>
1219
+ {
1220
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
1221
+ let counter: number = 0;
1222
+ const finallyResult: SyncResult<number> = parentResult.finally(() => { counter++; });
1223
+ test.assertSame(counter, 1);
1224
+ for (let i = 0; i < 3; i++)
1225
+ {
1226
+ test.assertThrows(finallyResult, new Error("abc"));
1227
+ test.assertSame(counter, 1);
1228
+ }
1229
+ });
1230
+
1231
+ runner.test("with error parent and throwing onfinally function", (test: Test) =>
1232
+ {
1233
+ const parentResult: SyncResult<number> = SyncResult.error(new Error("abc"));
1234
+ let counter: number = 0;
1235
+ const convertErrorResult: SyncResult<number> = parentResult.finally(() =>
1236
+ {
1237
+ counter++;
1238
+ throw new RangeError("def");
1239
+ });
1240
+ test.assertSame(counter, 1);
1241
+ for (let i = 0; i < 3; i++)
1242
+ {
1243
+ test.assertThrows(convertErrorResult, new RangeError("def"));
1244
+ test.assertSame(counter, 1);
1245
+ }
1246
+ });
1247
+ });
1248
+ });
1249
+ });
1250
+ });
1251
+ }