@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,404 @@
1
+ import { PromiseAsyncResult } from "../sources/promiseAsyncResult";
2
+ import { CharacterWriteStream } from "../sources/characterWriteStream";
3
+ import { CurrentProcess } from "../sources/currentProcess";
4
+ import { Iterable } from "../sources/iterable";
5
+ import { JavascriptIterable } from "../sources/javascript";
6
+ import { List } from "../sources/list";
7
+ import { NodeJSCharacterWriteStream } from "../sources/nodeJSCharacterWriteStream";
8
+ import { PreCondition } from "../sources/preCondition";
9
+ import { join } from "../sources/strings";
10
+ import { isFunction, isJavascriptIterable, isPromise } from "../sources/types";
11
+ import { AssertTest } from "./assertTest";
12
+ import { FailedTest } from "./failedTest";
13
+ import { SkippedTest } from "./skippedTest";
14
+ import { Test } from "./test";
15
+ import { TestAction } from "./testAction";
16
+ import { TestRunner } from "./testRunner";
17
+ import { TestSkip } from "./testSkip";
18
+
19
+ export type ConsoleTestFunction = (runner: ConsoleTestRunner) => (void | Promise<void>);
20
+ export type ConsoleTestFunctionContainer = { test: ConsoleTestFunction };
21
+
22
+ export class ConsoleTestRunner extends TestRunner
23
+ {
24
+ private writeStream: CharacterWriteStream;
25
+
26
+ private readonly pendingActions: List<TestAction>;
27
+ private pendingActionsInsertIndex: number;
28
+ private currentTestAction: TestAction | undefined;
29
+ private currentTest: Test | undefined;
30
+
31
+ private passedTestCount: number;
32
+ private readonly skippedTests: List<SkippedTest>;
33
+ private readonly testFailures: List<FailedTest>;
34
+
35
+ public constructor()
36
+ {
37
+ super();
38
+
39
+ this.writeStream = NodeJSCharacterWriteStream.create(process.stdout);
40
+
41
+ this.pendingActions = List.create();
42
+ this.pendingActionsInsertIndex = 0;
43
+
44
+ this.passedTestCount = 0;
45
+ this.skippedTests = List.create();
46
+ this.testFailures = List.create();
47
+ }
48
+
49
+ public static create(): ConsoleTestRunner
50
+ {
51
+ return new ConsoleTestRunner();
52
+ }
53
+
54
+ public static run(testFunction: ConsoleTestFunction | ConsoleTestFunctionContainer): Promise<void>;
55
+ public static run(testFunctions: JavascriptIterable<ConsoleTestFunction | ConsoleTestFunctionContainer>): Promise<void>;
56
+ static run(testFunctionOrTestFunctions: ConsoleTestFunction | ConsoleTestFunctionContainer | JavascriptIterable<ConsoleTestFunction | ConsoleTestFunctionContainer>): Promise<void>
57
+ {
58
+ let testFunction: ConsoleTestFunction;
59
+ if (isFunction(testFunctionOrTestFunctions))
60
+ {
61
+ testFunction = testFunctionOrTestFunctions;
62
+ }
63
+ else if (!isJavascriptIterable(testFunctionOrTestFunctions))
64
+ {
65
+ testFunction = testFunctionOrTestFunctions.test;
66
+ }
67
+ else
68
+ {
69
+ const testFunctions: JavascriptIterable<ConsoleTestFunction | ConsoleTestFunctionContainer> = testFunctionOrTestFunctions;
70
+ testFunction = async (runner: ConsoleTestRunner) =>
71
+ {
72
+ for (const testFunction of testFunctions)
73
+ {
74
+ if (isFunction(testFunction))
75
+ {
76
+ await testFunction(runner);
77
+ }
78
+ else
79
+ {
80
+ await testFunction.test(runner);
81
+ }
82
+ }
83
+ };
84
+ }
85
+
86
+ return CurrentProcess.run(async (currentProcess: CurrentProcess) =>
87
+ {
88
+ const runner: ConsoleTestRunner = ConsoleTestRunner.create()
89
+ .setWriteStream(currentProcess.getOutputWriteStream());
90
+
91
+ await testFunction(runner);
92
+
93
+ await runner.runAsync();
94
+
95
+ await runner.printSummary();
96
+ });
97
+ }
98
+
99
+ public setWriteStream(writeStream: CharacterWriteStream): this
100
+ {
101
+ PreCondition.assertNotUndefinedAndNotNull(writeStream, "writeStream");
102
+
103
+ this.writeStream = writeStream;
104
+
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * Get the number of {@link TestAction}s that have yet to be executed.
110
+ */
111
+ public getPendingTestActionsCount(): number
112
+ {
113
+ return this.pendingActions.getCount().await();
114
+ }
115
+
116
+ /**
117
+ * Get the index in the pending-{@link TestAction} stack that new {@link TestAction}s will be
118
+ * inserted at.
119
+ */
120
+ public getPendingTestActionsInsertIndex(): number
121
+ {
122
+ return this.pendingActionsInsertIndex;
123
+ }
124
+
125
+ /**
126
+ * Get the number of tests that have been skipped.
127
+ */
128
+ public getSkippedTestCount(): number
129
+ {
130
+ return this.skippedTests.getCount().await();
131
+ }
132
+
133
+ public getSkippedTests(): Iterable<SkippedTest>
134
+ {
135
+ return this.skippedTests;
136
+ }
137
+
138
+ /**
139
+ * Get the number of tests that have passed.
140
+ */
141
+ public getPassedTestCount(): number
142
+ {
143
+ return this.passedTestCount;
144
+ }
145
+
146
+ public getFailedTestCount(): number
147
+ {
148
+ return this.testFailures.getCount().await();
149
+ }
150
+
151
+ public getFailedTests(): Iterable<FailedTest>
152
+ {
153
+ return this.testFailures;
154
+ }
155
+
156
+ /**
157
+ * Get the {@link TestAction} that is currently executing or undefined if no {@link TestAction}
158
+ * is executing.
159
+ */
160
+ public getCurrentTestAction(): TestAction | undefined
161
+ {
162
+ return this.currentTestAction;
163
+ }
164
+
165
+ public getCurrentTest(): Test | undefined
166
+ {
167
+ return this.currentTest;
168
+ }
169
+
170
+ private assertNoCurrentTest(): void
171
+ {
172
+ if (this.currentTest !== undefined)
173
+ {
174
+ this.currentTest.fail("Can't start a new test group or a new test while running a test.");
175
+ }
176
+ }
177
+
178
+ public beforeTest(fullTestNameParts: JavascriptIterable<string>): PromiseAsyncResult<void>
179
+ {
180
+ return PromiseAsyncResult.create(async () =>
181
+ {
182
+ await this.writeStream.writeString(join(" ", fullTestNameParts));
183
+ });
184
+ }
185
+
186
+ public afterPassedTest(): PromiseAsyncResult<void>
187
+ {
188
+ return PromiseAsyncResult.create(async () =>
189
+ {
190
+ await this.writeStream.writeLine(" - Passed");
191
+ this.passedTestCount++;
192
+ });
193
+ }
194
+
195
+ public afterSkippedTest(fullTestNameParts: JavascriptIterable<string>, skip: TestSkip | undefined): PromiseAsyncResult<void>
196
+ {
197
+ PreCondition.assertNotEmpty(fullTestNameParts, "fullTestNameParts");
198
+ PreCondition.assertNotUndefinedAndNotNull(skip, "skip");
199
+
200
+ return PromiseAsyncResult.create(async () =>
201
+ {
202
+ this.skippedTests.add(SkippedTest.create(skip, fullTestNameParts));
203
+ await this.writeStream.writeLine(" - Skipped");
204
+ });
205
+ }
206
+
207
+ public afterFailedTest(fullTestNameParts: JavascriptIterable<string>, error: unknown): PromiseAsyncResult<void>
208
+ {
209
+ PreCondition.assertNotEmpty(fullTestNameParts, "fullTestNameParts");
210
+ PreCondition.assertNotUndefinedAndNotNull(error, "error");
211
+
212
+ return PromiseAsyncResult.create(async () =>
213
+ {
214
+ this.testFailures.add(FailedTest.create(fullTestNameParts, error));
215
+ await this.writeStream.writeLine(" - Failed");
216
+ });
217
+ }
218
+
219
+ public testGroup(testGroupName: string, testAction: () => (void | Promise<void>)): void;
220
+ public testGroup(testGroupName: string, skip: TestSkip | undefined, testAction: () => (void | Promise<void>)): void;
221
+ testGroup(testGroupName: string, skipOrTestAction: TestSkip | undefined | (() => (void | Promise<void>)), testAction?: () => (void | Promise<void>)): void
222
+ {
223
+ PreCondition.assertNotUndefinedAndNotNull(testGroupName, "testGroupName");
224
+ PreCondition.assertNotEmpty(testGroupName, "testGroupName");
225
+ let skip: TestSkip | undefined;
226
+ if (isFunction(skipOrTestAction))
227
+ {
228
+ PreCondition.assertUndefined(testAction, "testAction");
229
+
230
+ skip = undefined;
231
+ testAction = skipOrTestAction;
232
+ }
233
+ else
234
+ {
235
+ skip = skipOrTestAction;
236
+ }
237
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
238
+
239
+ this.assertNoCurrentTest();
240
+
241
+ this.pendingActions.insert(
242
+ this.pendingActionsInsertIndex,
243
+ TestAction.create(
244
+ this.getCurrentTestAction(),
245
+ testGroupName,
246
+ skip,
247
+ async () =>
248
+ {
249
+ const previousTestActionInsertIndex: number = this.pendingActionsInsertIndex;
250
+ this.pendingActionsInsertIndex = this.pendingActions.getCount().await();
251
+ const currentTestAction: TestAction = this.getCurrentTestAction()!;
252
+ try
253
+ {
254
+ await testAction();
255
+ }
256
+ catch (error)
257
+ {
258
+ await this.afterFailedTest(currentTestAction.getFullNameParts(), error);
259
+ }
260
+ finally
261
+ {
262
+ this.pendingActionsInsertIndex = previousTestActionInsertIndex;
263
+ }
264
+ },
265
+ ),
266
+ );
267
+ }
268
+
269
+ public test(testName: string, testAction: (test: Test) => (void | Promise<void>)): void;
270
+ public test(testName: string, skip: TestSkip | undefined, testAction: (test: Test) => (void | Promise<void>)): void;
271
+ test(testName: string, skipOrTestAction: TestSkip | undefined | ((test: Test) => (void | Promise<void>)), testAction?: (test: Test) => (void | Promise<void>)): void
272
+ {
273
+ PreCondition.assertNotUndefinedAndNotNull(testName, "testName");
274
+ PreCondition.assertNotEmpty(testName, "testName");
275
+ let skip: TestSkip | undefined;
276
+ if (isFunction(skipOrTestAction))
277
+ {
278
+ PreCondition.assertUndefined(testAction, "testAction");
279
+
280
+ skip = undefined;
281
+ testAction = skipOrTestAction;
282
+ }
283
+ else
284
+ {
285
+ skip = skipOrTestAction;
286
+ }
287
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
288
+
289
+ this.assertNoCurrentTest();
290
+
291
+ this.pendingActions.insert(
292
+ this.pendingActionsInsertIndex,
293
+ TestAction.create(
294
+ this.getCurrentTestAction(),
295
+ testName,
296
+ skip,
297
+ async () =>
298
+ {
299
+ const currentTestAction: TestAction = this.getCurrentTestAction()!;
300
+ try
301
+ {
302
+ await this.beforeTest(currentTestAction.getFullNameParts());
303
+ if (currentTestAction.shouldSkip())
304
+ {
305
+ await this.afterSkippedTest(currentTestAction.getFullNameParts(), currentTestAction.getSkip());
306
+ }
307
+ else
308
+ {
309
+ this.currentTest = AssertTest.create(testName);
310
+ try
311
+ {
312
+ await testAction(this.currentTest);
313
+ await this.afterPassedTest();
314
+ }
315
+ finally
316
+ {
317
+ this.currentTest = undefined;
318
+ }
319
+ }
320
+ }
321
+ catch (error)
322
+ {
323
+ await this.afterFailedTest(currentTestAction.getFullNameParts(), error);
324
+ }
325
+ },
326
+ ),
327
+ );
328
+ }
329
+
330
+ public async runAsync(): Promise<void>
331
+ {
332
+ while (this.pendingActions.any().await())
333
+ {
334
+ this.currentTestAction = this.pendingActions.removeLast().await();
335
+ try
336
+ {
337
+ const result: void | Promise<void> = this.currentTestAction.runAsync();
338
+ if (isPromise(result))
339
+ {
340
+ await result;
341
+ }
342
+ }
343
+ finally
344
+ {
345
+ this.currentTestAction = undefined;
346
+ }
347
+ }
348
+ }
349
+
350
+ public printSummary(): PromiseAsyncResult<void>
351
+ {
352
+ return PromiseAsyncResult.create(async () =>
353
+ {
354
+ await this.writeStream.writeLine();
355
+
356
+ const skippedTests: Iterable<SkippedTest> = this.getSkippedTests();
357
+ if (await skippedTests.any())
358
+ {
359
+ await this.writeStream.writeLine(`Skipped Tests:`);
360
+ let counter: number = 0;
361
+ for (const skippedTest of skippedTests)
362
+ {
363
+ await this.writeStream.writeLine(`${++counter}) ${skippedTest.getFullTestName()}`);
364
+ const skipMessage: string = skippedTest.getSkipMessage();
365
+ if (skipMessage)
366
+ {
367
+ await this.writeStream.writeLine(` ${skipMessage}`);
368
+ }
369
+ }
370
+ await this.writeStream.writeLine();
371
+ }
372
+
373
+ const failedTests: Iterable<FailedTest> = this.getFailedTests();
374
+ if (await failedTests.any())
375
+ {
376
+ await this.writeStream.writeLine("Failed Tests:");
377
+
378
+ let counter: number = 0;
379
+ for (const failedTest of failedTests)
380
+ {
381
+ await this.writeStream.writeLine(`${++counter}) ${failedTest.getFullTestName()}`);
382
+ await this.writeStream.writeLine(` ${failedTest.getErrorMessage()}`);
383
+ await this.writeStream.writeLine();
384
+ }
385
+ }
386
+
387
+ const passedTestCount: number = this.getPassedTestCount();
388
+ if (passedTestCount > 0)
389
+ {
390
+ await this.writeStream.writeLine(`Passed: ${passedTestCount}`);
391
+ }
392
+
393
+ if (await skippedTests.any())
394
+ {
395
+ await this.writeStream.writeLine(`Skipped: ${skippedTests.getCount().await()}`);
396
+ }
397
+
398
+ if (await failedTests.any())
399
+ {
400
+ await this.writeStream.writeLine(`Failed: ${failedTests.getCount().await()}`);
401
+ }
402
+ });
403
+ }
404
+ }