@everyonesoftware/common 5.0.0 → 7.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.
@@ -0,0 +1,1062 @@
1
+ import {
2
+ ANSIStyles,
3
+ AsyncResult,
4
+ CurrentProcess,
5
+ IndentedCharacterWriteStream,
6
+ Iterable,
7
+ List,
8
+ Map,
9
+ NodeJSCharacterWriteStream,
10
+ NotFoundError,
11
+ PreCondition,
12
+ PromiseAsyncResult,
13
+ Stack,
14
+ ToStringFunctions,
15
+ __export,
16
+ andList,
17
+ getName,
18
+ getParameterCount,
19
+ isBoolean,
20
+ isFunction,
21
+ isJavascriptIterable,
22
+ isString,
23
+ isUndefinedOrNull,
24
+ join
25
+ } from "./chunk-MMAX3IZF.js";
26
+
27
+ // tests/test.ts
28
+ var Test = class _Test {
29
+ /**
30
+ * Assert that the provided value is undefined;
31
+ * @param value The value to check.
32
+ */
33
+ assertUndefined(value) {
34
+ _Test.assertUndefined(this, value);
35
+ }
36
+ /**
37
+ * Assert that the provided value is undefined.
38
+ * @param test The current {@link Test}.
39
+ * @param value The value to check.
40
+ */
41
+ static assertUndefined(test2, value) {
42
+ test2.assertSame(value, void 0);
43
+ }
44
+ /**
45
+ * Assert that the provided value is not undefined;
46
+ * @param value The value to check.
47
+ */
48
+ assertNotUndefined(value) {
49
+ _Test.assertNotUndefined(this, value);
50
+ }
51
+ /**
52
+ * Assert that the provided value is not undefined.
53
+ * @param test The current {@link Test}.
54
+ * @param value The value to check.
55
+ */
56
+ static assertNotUndefined(test2, value) {
57
+ test2.assertNotSame(value, void 0);
58
+ }
59
+ /**
60
+ * Assert that the provided value is null.
61
+ * @param value The value to check.
62
+ */
63
+ assertNull(value) {
64
+ _Test.assertNull(this, value);
65
+ }
66
+ /**
67
+ * Assert that the provided value is null.
68
+ * @param value The value to check.
69
+ */
70
+ static assertNull(test2, value) {
71
+ test2.assertSame(value, null);
72
+ }
73
+ /**
74
+ * Assert that the provided value is not null.
75
+ * @param value The value to check.
76
+ */
77
+ assertNotNull(value) {
78
+ _Test.assertNull(this, value);
79
+ }
80
+ /**
81
+ * Assert that the provided value is not null.
82
+ * @param value The value to check.
83
+ */
84
+ static assertNotNull(test2, value) {
85
+ test2.assertNotSame(value, null);
86
+ }
87
+ /**
88
+ * Assert that the provided value is not undefined and not null.
89
+ * @param value The value to check.
90
+ */
91
+ assertNotUndefinedAndNotNull(value) {
92
+ _Test.assertNotUndefinedAndNotNull(this, value);
93
+ }
94
+ /**
95
+ * Assert that the provided value is not undefined and not null.
96
+ * @param value The value to check.
97
+ */
98
+ static assertNotUndefinedAndNotNull(test2, value) {
99
+ test2.assertNotSame(value, null);
100
+ test2.assertNotSame(value, void 0);
101
+ }
102
+ /**
103
+ * Assert that the provided collection is not undefined, not null, and not empty.
104
+ * @param value The value to check.
105
+ */
106
+ assertNotEmpty(value) {
107
+ _Test.assertNotEmpty(this, value);
108
+ }
109
+ static assertNotEmpty(test2, value) {
110
+ test2.assertNotUndefinedAndNotNull(value);
111
+ test2.assertTrue(Iterable.any(value).await());
112
+ }
113
+ /**
114
+ * Assert that the provided value is false.
115
+ * @param value The value to check.
116
+ */
117
+ assertFalse(value) {
118
+ _Test.assertFalse(this, value);
119
+ }
120
+ static assertFalse(test2, value) {
121
+ PreCondition.assertNotUndefinedAndNotNull(test2, "test");
122
+ test2.assertSame(value, false);
123
+ }
124
+ /**
125
+ * Assert that the provided value is true.
126
+ * @param value The value to check.
127
+ */
128
+ assertTrue(value) {
129
+ _Test.assertTrue(this, value);
130
+ }
131
+ /**
132
+ * Assert that the provided value is true.
133
+ * @param value The value to check.
134
+ */
135
+ static assertTrue(test2, value) {
136
+ PreCondition.assertNotUndefinedAndNotNull(test2, "test");
137
+ test2.assertSame(value, true);
138
+ }
139
+ /**
140
+ * Assert that the provided value is an instance of the provided {@link Type}.
141
+ * @param value The value to check.
142
+ * @param type The {@link Type} to check.
143
+ * @param expression The expression that produced the value.
144
+ * @param message An optional error message.
145
+ */
146
+ assertInstanceOf(value, type, typeCheck) {
147
+ return _Test.assertInstanceOf(this, value, type, typeCheck);
148
+ }
149
+ /**
150
+ * Assert that the provided value is an instance of the provided {@link Type}.
151
+ * @param value The value to check.
152
+ * @param type The {@link Type} to check.
153
+ * @param expression The expression that produced the value.
154
+ * @param message An optional error message.
155
+ */
156
+ static assertInstanceOf(test2, value, type, typeCheck) {
157
+ PreCondition.assertNotUndefinedAndNotNull(type, "type");
158
+ if (isUndefinedOrNull(typeCheck)) {
159
+ typeCheck = ((value2) => value2 instanceof type);
160
+ }
161
+ if (!typeCheck(value)) {
162
+ test2.fail(`Expected value to be of type ${type.name} but found ${value} instead.`);
163
+ }
164
+ }
165
+ };
166
+
167
+ // tests/assertTest.ts
168
+ import * as assert from "assert";
169
+ var AssertTest = class _AssertTest {
170
+ name;
171
+ constructor(name) {
172
+ this.name = name;
173
+ }
174
+ /**
175
+ * Create a new {@link AssertTest} object.
176
+ */
177
+ static create(name) {
178
+ return new _AssertTest(name);
179
+ }
180
+ fail(message) {
181
+ PreCondition.assertNotEmpty(message, "message");
182
+ assert.fail(message);
183
+ }
184
+ assertUndefined(value) {
185
+ Test.assertUndefined(this, value);
186
+ }
187
+ assertNotUndefined(value) {
188
+ Test.assertNotUndefined(this, value);
189
+ }
190
+ assertNull(value) {
191
+ Test.assertNull(this, value);
192
+ }
193
+ assertNotNull(value) {
194
+ Test.assertNotNull(this, value);
195
+ }
196
+ assertNotUndefinedAndNotNull(value) {
197
+ Test.assertNotUndefinedAndNotNull(this, value);
198
+ }
199
+ assertNotEmpty(value) {
200
+ Test.assertNotEmpty(this, value);
201
+ }
202
+ assertSame(left, right) {
203
+ assert.strictEqual(left, right);
204
+ }
205
+ assertNotSame(left, right) {
206
+ assert.notStrictEqual(left, right);
207
+ }
208
+ assertEqual(left, right, message) {
209
+ assert.deepStrictEqual(left, right, message);
210
+ }
211
+ assertNotEqual(left, right) {
212
+ assert.notDeepStrictEqual(left, right);
213
+ }
214
+ assertFalse(value) {
215
+ Test.assertFalse(this, value);
216
+ }
217
+ assertTrue(value) {
218
+ Test.assertTrue(this, value);
219
+ }
220
+ assertThrows(action, expectedError) {
221
+ if (!isFunction(action)) {
222
+ const syncResult = action;
223
+ action = () => {
224
+ syncResult.await();
225
+ };
226
+ }
227
+ assert.throws(action, expectedError);
228
+ }
229
+ assertThrowsAsync(action, expectedError) {
230
+ const promiseOrAsyncAction = isFunction(action) ? async () => await action() : action;
231
+ return PromiseAsyncResult.create(assert.rejects(promiseOrAsyncAction, expectedError));
232
+ }
233
+ assertInstanceOf(value, type, typeCheck) {
234
+ Test.assertInstanceOf(this, value, type, typeCheck);
235
+ }
236
+ };
237
+
238
+ // tests/assertTestTests.ts
239
+ var assertTestTests_exports = {};
240
+ __export(assertTestTests_exports, {
241
+ test: () => test
242
+ });
243
+ import { AssertionError } from "assert";
244
+ function test(runner) {
245
+ runner.testFile("assertTest.ts", () => {
246
+ runner.testType("AssertTest", () => {
247
+ runner.testFunction("assertThrows()", () => {
248
+ runner.test("with throwing action", (test2) => {
249
+ const at = AssertTest.create("fake-test-name");
250
+ at.assertThrows(() => {
251
+ throw new Error("abc");
252
+ }, new Error("abc"));
253
+ });
254
+ runner.test("with non-throwing action", (test2) => {
255
+ const at = AssertTest.create("fake-test-name");
256
+ test2.assertThrows(
257
+ () => at.assertThrows(() => {
258
+ }, new Error("oops")),
259
+ new AssertionError({
260
+ message: "Missing expected exception (Error).",
261
+ operator: "throws",
262
+ expected: new Error("oops")
263
+ })
264
+ );
265
+ });
266
+ });
267
+ runner.testFunction("assertThrowsAsync()", () => {
268
+ runner.test("with throwing sync action", async (test2) => {
269
+ const at = AssertTest.create("fake-test-name");
270
+ await at.assertThrowsAsync(() => {
271
+ throw new Error("abc");
272
+ }, new Error("abc"));
273
+ });
274
+ runner.test("with throwing async action", async (test2) => {
275
+ const at = AssertTest.create("fake-test-name");
276
+ await at.assertThrowsAsync(async () => {
277
+ throw new Error("abc");
278
+ }, new Error("abc"));
279
+ });
280
+ runner.test("with rejected Promise", async (test2) => {
281
+ const at = AssertTest.create("fake-test-name");
282
+ await at.assertThrowsAsync(Promise.reject(new Error("abc")), new Error("abc"));
283
+ });
284
+ runner.test("with throwing action that returns a rejected Promise", async (test2) => {
285
+ const at = AssertTest.create("fake-test-name");
286
+ await at.assertThrowsAsync(() => Promise.reject(new Error("abc")), new Error("abc"));
287
+ });
288
+ runner.test("with non-throwing async action", async (test2) => {
289
+ const at = AssertTest.create("fake-test-name");
290
+ await test2.assertThrowsAsync(
291
+ async () => await at.assertThrowsAsync(async () => {
292
+ }, new Error("oops")),
293
+ new AssertionError({
294
+ message: "Missing expected rejection (Error).",
295
+ operator: "rejects",
296
+ expected: new Error("oops")
297
+ })
298
+ );
299
+ });
300
+ });
301
+ });
302
+ });
303
+ }
304
+
305
+ // tests/basicTestSkip.ts
306
+ var BasicTestSkip = class _BasicTestSkip {
307
+ shouldSkip;
308
+ message;
309
+ constructor(shouldSkip, message) {
310
+ PreCondition.assertNotUndefinedAndNotNull(message, "message");
311
+ this.shouldSkip = shouldSkip;
312
+ this.message = message;
313
+ }
314
+ /**
315
+ * Create a new {@link TestSkip} with the provided properties.
316
+ * @param shouldSkip Whether the tests associated with the new {@link TestSkip}
317
+ * should be skipped.
318
+ * @param message The message that explains why the tests associated with this {@link TestSkip}
319
+ * should be skipped.
320
+ */
321
+ static create(shouldSkip, message) {
322
+ if (shouldSkip === void 0 || shouldSkip === null) {
323
+ shouldSkip = true;
324
+ }
325
+ if (message === void 0 || message === null) {
326
+ message = "";
327
+ }
328
+ return new _BasicTestSkip(shouldSkip, message);
329
+ }
330
+ getShouldSkip() {
331
+ return this.shouldSkip;
332
+ }
333
+ getMessage() {
334
+ return this.message;
335
+ }
336
+ };
337
+
338
+ // tests/failedTest.ts
339
+ var FailedTest = class _FailedTest {
340
+ testAction;
341
+ error;
342
+ constructor(testAction, error) {
343
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
344
+ PreCondition.assertNotUndefinedAndNotNull(error, "error");
345
+ this.testAction = testAction;
346
+ this.error = error;
347
+ }
348
+ static create(testAction, error) {
349
+ return new _FailedTest(testAction, error);
350
+ }
351
+ getTestAction() {
352
+ return this.testAction;
353
+ }
354
+ getError() {
355
+ return this.error;
356
+ }
357
+ getErrorMessage() {
358
+ return this.error instanceof Error && this.error.stack ? this.error.stack : `${this.error}`;
359
+ }
360
+ };
361
+
362
+ // tests/skippedTest.ts
363
+ var SkippedTest = class _SkippedTest {
364
+ skip;
365
+ testAction;
366
+ constructor(skip, testAction) {
367
+ PreCondition.assertNotUndefinedAndNotNull(skip, "skip");
368
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
369
+ this.skip = skip;
370
+ this.testAction = testAction;
371
+ }
372
+ static create(skip, testAction) {
373
+ return new _SkippedTest(skip, testAction);
374
+ }
375
+ getSkipMessage() {
376
+ return this.skip.getMessage();
377
+ }
378
+ getTestAction() {
379
+ return this.testAction;
380
+ }
381
+ };
382
+
383
+ // tests/testAction.ts
384
+ var TestAction = class _TestAction {
385
+ parent;
386
+ name;
387
+ type;
388
+ skip;
389
+ action;
390
+ constructor(parent, name, type, skip, action) {
391
+ PreCondition.assertNotUndefinedAndNotNull(name, "name");
392
+ PreCondition.assertNotEmpty(type, "type");
393
+ PreCondition.assertNotUndefinedAndNotNull(action, "action");
394
+ this.parent = parent;
395
+ this.name = name;
396
+ this.type = type;
397
+ this.skip = skip;
398
+ this.action = action;
399
+ }
400
+ static create(parent, name, type, skip, action) {
401
+ return new _TestAction(parent, name, type, skip, action);
402
+ }
403
+ getParent() {
404
+ return this.parent;
405
+ }
406
+ getName() {
407
+ return this.name;
408
+ }
409
+ getFullNameParts() {
410
+ const result = List.create();
411
+ if (this.parent) {
412
+ result.addAll(this.parent.getFullNameParts());
413
+ }
414
+ result.add(this.getName());
415
+ return result;
416
+ }
417
+ getFullName() {
418
+ return join(" ", this.getFullNameParts());
419
+ }
420
+ getType() {
421
+ return this.type;
422
+ }
423
+ getSkip() {
424
+ return this.skip || this.parent?.getSkip();
425
+ }
426
+ shouldSkip() {
427
+ return !!this.getSkip()?.getShouldSkip();
428
+ }
429
+ getAction() {
430
+ return this.action;
431
+ }
432
+ runAsync() {
433
+ return this.action();
434
+ }
435
+ };
436
+
437
+ // tests/testSkip.ts
438
+ var TestSkip = class {
439
+ constructor() {
440
+ }
441
+ /**
442
+ * Create a new {@link TestSkip} with the provided properties.
443
+ * @param shouldSkip Whether the tests associated with the new {@link TestSkip}
444
+ * should be skipped.
445
+ * @param message The message that explains why the tests associated with this {@link TestSkip}
446
+ * should be skipped.
447
+ */
448
+ static create(shouldSkip, message) {
449
+ return BasicTestSkip.create(shouldSkip, message);
450
+ }
451
+ };
452
+
453
+ // tests/testRunner.ts
454
+ var TestRunner = class _TestRunner {
455
+ /**
456
+ * Get a {@link string} that concatenates the {@link string} representation of each
457
+ * of the provided values into an "and-list".
458
+ * @param values The values to concatenate.
459
+ */
460
+ andList(values) {
461
+ return _TestRunner.andList(this, values);
462
+ }
463
+ /**
464
+ * Get a {@link string} that concatenates the {@link string} representation of each
465
+ * of the provided values into an "and-list".
466
+ * @param values The values to concatenate.
467
+ */
468
+ static andList(runner, values) {
469
+ PreCondition.assertNotUndefinedAndNotNull(runner, "runner");
470
+ PreCondition.assertNotUndefinedAndNotNull(values, "values");
471
+ return andList(values.map((value) => runner.toString(value)));
472
+ }
473
+ /**
474
+ * Get the {@link string} representation of the provided value.
475
+ * @param value The value to get the {@link string} representation of.
476
+ */
477
+ toString(value) {
478
+ return _TestRunner.toString(this, value);
479
+ }
480
+ /**
481
+ * Get the {@link string} representation of the provided value.
482
+ * @param value The value to get the {@link string} representation of.
483
+ */
484
+ static toString(_runner, value) {
485
+ const toStringFunctions = ToStringFunctions.create();
486
+ return toStringFunctions.toString(value);
487
+ }
488
+ skip(messageOrShouldSkip, message) {
489
+ let shouldSkip;
490
+ if (!isBoolean(messageOrShouldSkip)) {
491
+ shouldSkip = true;
492
+ message = messageOrShouldSkip;
493
+ } else {
494
+ shouldSkip = messageOrShouldSkip;
495
+ }
496
+ return _TestRunner.skip(this, shouldSkip, message);
497
+ }
498
+ /**
499
+ * Create a {@link TestSkip} object that will prevent tests from being run.
500
+ * @param shouldSkip Whether these tests should be skipped.
501
+ * @param message The message that explains why the tests are being skipped.
502
+ */
503
+ static skip(_runner, shouldSkip, message) {
504
+ return TestSkip.create(shouldSkip, message);
505
+ }
506
+ };
507
+
508
+ // tests/ConsoleTestRunnerUI.ts
509
+ var ConsoleTestRunnerUI = class {
510
+ writeStream;
511
+ styles;
512
+ constructor() {
513
+ this.styles = Map.create();
514
+ }
515
+ static flat() {
516
+ return FlatConsoleTestRunnerUI.create();
517
+ }
518
+ static tree() {
519
+ return TreeConsoleTestRunnerUI.create();
520
+ }
521
+ setWriteStream(writeStream) {
522
+ PreCondition.assertNotUndefinedAndNotNull(writeStream, "writeStream");
523
+ this.writeStream = IndentedCharacterWriteStream.create(writeStream);
524
+ return this;
525
+ }
526
+ setStyle(style, styleFunction) {
527
+ PreCondition.assertNotEmpty(style, "style");
528
+ PreCondition.assertNotUndefinedAndNotNull(styleFunction, "styleFunction");
529
+ this.styles.set(style, styleFunction);
530
+ return this;
531
+ }
532
+ setStyles(styles) {
533
+ PreCondition.assertNotUndefinedAndNotNull(styles, "styles");
534
+ for (const entry of Object.entries(styles)) {
535
+ this.setStyle(entry[0], entry[1]);
536
+ }
537
+ return this;
538
+ }
539
+ applyStyle(style, text) {
540
+ PreCondition.assertNotEmpty(style, "style");
541
+ PreCondition.assertNotUndefinedAndNotNull(text, "text");
542
+ const styleFunction = this.styles.get(style).catch(NotFoundError, () => {
543
+ return (text2) => text2;
544
+ }).await();
545
+ return styleFunction(text);
546
+ }
547
+ addIndentation() {
548
+ this.writeStream?.addIndentation();
549
+ }
550
+ removeIndentation() {
551
+ this.writeStream?.removeIndentation();
552
+ }
553
+ indent(action) {
554
+ return this.writeStream?.indent(action) ?? AsyncResult.value(0);
555
+ }
556
+ writeString(text) {
557
+ return this.writeStream?.writeString(text) ?? AsyncResult.value(0);
558
+ }
559
+ writeLine(text) {
560
+ return this.writeStream?.writeLine(text) ?? AsyncResult.value(0);
561
+ }
562
+ writeTestActionName(testAction) {
563
+ return this.writeString(this.applyStyle(testAction.getType(), testAction.getName()));
564
+ }
565
+ writeFullTestActionName(testAction) {
566
+ return AsyncResult.create(async () => {
567
+ let result = 0;
568
+ const testActionStack = Stack.create();
569
+ let currentTestAction = testAction;
570
+ while (currentTestAction) {
571
+ testActionStack.add(currentTestAction);
572
+ currentTestAction = currentTestAction.getParent();
573
+ }
574
+ while (await testActionStack.any()) {
575
+ currentTestAction = await testActionStack.remove();
576
+ if (currentTestAction.getParent()) {
577
+ result += await this.writeString(" ");
578
+ }
579
+ result += await this.writeTestActionName(currentTestAction);
580
+ }
581
+ return result;
582
+ });
583
+ }
584
+ beforeTestGroup(testGroup) {
585
+ PreCondition.assertNotUndefinedAndNotNull(testGroup, "testGroup");
586
+ return AsyncResult.empty();
587
+ }
588
+ afterTestGroup(testGroup) {
589
+ PreCondition.assertNotUndefinedAndNotNull(testGroup, "testGroup");
590
+ return AsyncResult.empty();
591
+ }
592
+ beforeTest(testAction) {
593
+ return AsyncResult.empty();
594
+ }
595
+ afterPassedTest(testAction) {
596
+ return AsyncResult.create(async () => {
597
+ await this.writeLine(` - ${this.applyStyle("passed", "Passed")}`);
598
+ });
599
+ }
600
+ afterSkippedTest(testAction, skip) {
601
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
602
+ PreCondition.assertNotUndefinedAndNotNull(skip, "skip");
603
+ return AsyncResult.create(async () => {
604
+ await this.writeLine(` - ${this.applyStyle("skipped", "Skipped")}`);
605
+ });
606
+ }
607
+ afterFailedTest(currentTestAction, error) {
608
+ PreCondition.assertNotUndefinedAndNotNull(currentTestAction, "currentTestAction");
609
+ PreCondition.assertNotUndefinedAndNotNull(error, "error");
610
+ return AsyncResult.create(async () => {
611
+ await this.writeLine(` - ${this.applyStyle("failed", "Failed")}`);
612
+ });
613
+ }
614
+ writeSummary(passedTestCount, skippedTests, failedTests) {
615
+ PreCondition.assertGreaterThanOrEqualTo(passedTestCount, 0, "passedTestCount");
616
+ PreCondition.assertNotUndefinedAndNotNull(skippedTests, "skippedTests");
617
+ PreCondition.assertNotUndefinedAndNotNull(failedTests, "failedTests");
618
+ return AsyncResult.create(async () => {
619
+ await this.writeLine();
620
+ if (await skippedTests.any()) {
621
+ await this.writeLine(`${this.applyStyle("skipped", "Skipped Tests")}:`);
622
+ let counter = 0;
623
+ for (const skippedTest of skippedTests) {
624
+ await this.writeString(`${++counter}) `);
625
+ await this.writeFullTestActionName(skippedTest.getTestAction());
626
+ await this.writeLine();
627
+ const skipMessage = skippedTest.getSkipMessage();
628
+ if (skipMessage) {
629
+ await this.indent(() => this.writeLine(skipMessage));
630
+ }
631
+ }
632
+ await this.writeLine();
633
+ }
634
+ if (await failedTests.any()) {
635
+ await this.writeLine(`${this.applyStyle("failed", "Failed Tests")}:`);
636
+ let counter = 0;
637
+ for (const failedTest of failedTests) {
638
+ await this.writeString(`${++counter}) `);
639
+ await this.writeFullTestActionName(failedTest.getTestAction());
640
+ await this.writeLine();
641
+ await this.indent(() => this.writeLine(failedTest.getErrorMessage()));
642
+ await this.writeLine();
643
+ }
644
+ }
645
+ if (passedTestCount > 0) {
646
+ await this.writeLine(`${this.applyStyle("passed", "Passed")}: ${passedTestCount}`);
647
+ }
648
+ if (await skippedTests.any()) {
649
+ await this.writeLine(`${this.applyStyle("skipped", "Skipped")}: ${skippedTests.getCount().await()}`);
650
+ }
651
+ if (await failedTests.any()) {
652
+ await this.writeLine(`${this.applyStyle("failed", "Failed")}: ${failedTests.getCount().await()}`);
653
+ }
654
+ });
655
+ }
656
+ };
657
+ var FlatConsoleTestRunnerUI = class _FlatConsoleTestRunnerUI extends ConsoleTestRunnerUI {
658
+ constructor() {
659
+ super();
660
+ }
661
+ static create() {
662
+ return new _FlatConsoleTestRunnerUI();
663
+ }
664
+ beforeTest(testAction) {
665
+ return AsyncResult.create(async () => {
666
+ await this.writeFullTestActionName(testAction);
667
+ });
668
+ }
669
+ };
670
+ var TreeConsoleTestRunnerUI = class _TreeConsoleTestRunnerUI extends ConsoleTestRunnerUI {
671
+ testActions;
672
+ testActionWrittenDepth;
673
+ constructor() {
674
+ super();
675
+ this.testActions = List.create();
676
+ this.testActionWrittenDepth = 0;
677
+ }
678
+ static create() {
679
+ return new _TreeConsoleTestRunnerUI();
680
+ }
681
+ beforeTestGroup(testGroup) {
682
+ return AsyncResult.create(() => {
683
+ this.testActions.add(testGroup);
684
+ });
685
+ }
686
+ afterTestGroup(testGroup) {
687
+ PreCondition.assertNotUndefinedAndNotNull(testGroup, "testGroup");
688
+ PreCondition.assertTrue(this.testActions.any().await(), "this.testActions.any().await()");
689
+ return AsyncResult.create(() => {
690
+ this.testActions.removeLast().await();
691
+ const testActionCount = this.testActions.getCount().await();
692
+ if (this.testActionWrittenDepth > testActionCount) {
693
+ this.testActionWrittenDepth--;
694
+ this.removeIndentation();
695
+ }
696
+ });
697
+ }
698
+ beforeTest(testAction) {
699
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
700
+ return AsyncResult.create(async () => {
701
+ const testActionCount = this.testActions.getCount().await();
702
+ while (this.testActionWrittenDepth < testActionCount) {
703
+ const testGroup = this.testActions.get(this.testActionWrittenDepth).await();
704
+ await this.writeTestActionName(testGroup);
705
+ await this.writeLine();
706
+ this.addIndentation();
707
+ this.testActionWrittenDepth++;
708
+ }
709
+ await this.writeTestActionName(testAction);
710
+ });
711
+ }
712
+ };
713
+
714
+ // tests/consoleTestRunner.ts
715
+ var ConsoleTestRunner = class _ConsoleTestRunner {
716
+ writeStream;
717
+ testActions;
718
+ testActionInsertIndex;
719
+ currentTestAction;
720
+ currentTest;
721
+ passedTestCount;
722
+ skippedTests;
723
+ testFailures;
724
+ ui;
725
+ constructor(ui) {
726
+ this.writeStream = IndentedCharacterWriteStream.create(NodeJSCharacterWriteStream.create(process.stdout));
727
+ this.testActions = List.create();
728
+ this.testActionInsertIndex = 0;
729
+ this.passedTestCount = 0;
730
+ this.skippedTests = List.create();
731
+ this.testFailures = List.create();
732
+ this.ui = ui || ConsoleTestRunnerUI.tree();
733
+ this.ui.setWriteStream(this.writeStream);
734
+ }
735
+ static create(ui) {
736
+ return new _ConsoleTestRunner(ui);
737
+ }
738
+ static run(testFunctionOrTestFunctions) {
739
+ let testFunction;
740
+ if (isFunction(testFunctionOrTestFunctions)) {
741
+ testFunction = testFunctionOrTestFunctions;
742
+ } else if (!isJavascriptIterable(testFunctionOrTestFunctions)) {
743
+ testFunction = testFunctionOrTestFunctions.test;
744
+ } else {
745
+ const testFunctions = testFunctionOrTestFunctions;
746
+ testFunction = async (runner) => {
747
+ for (const testFunction2 of testFunctions) {
748
+ if (isFunction(testFunction2)) {
749
+ await testFunction2(runner);
750
+ } else {
751
+ await testFunction2.test(runner);
752
+ }
753
+ }
754
+ };
755
+ }
756
+ return CurrentProcess.run(async (currentProcess) => {
757
+ const runner = _ConsoleTestRunner.create().setWriteStream(currentProcess.getOutputWriteStream()).setStyles({
758
+ file: (t) => ANSIStyles.blue(t),
759
+ function: (t) => ANSIStyles.blue(t),
760
+ type: (t) => ANSIStyles.blue(t),
761
+ group: (t) => ANSIStyles.blue(t),
762
+ passed: (t) => ANSIStyles.green(`\u2713 ${t}`),
763
+ skipped: (t) => ANSIStyles.yellow(`\u25CC ${t}`),
764
+ failed: (t) => ANSIStyles.red(`\u2717 ${t}`)
765
+ });
766
+ await testFunction(runner);
767
+ await runner.runAsync();
768
+ await runner.printSummary();
769
+ });
770
+ }
771
+ setWriteStream(writeStream) {
772
+ PreCondition.assertNotUndefinedAndNotNull(writeStream, "writeStream");
773
+ this.writeStream = IndentedCharacterWriteStream.create(writeStream);
774
+ this.ui.setWriteStream(this.writeStream);
775
+ return this;
776
+ }
777
+ setStyle(style, styleFunction) {
778
+ this.ui.setStyle(style, styleFunction);
779
+ return this;
780
+ }
781
+ setStyles(styles) {
782
+ this.ui.setStyles(styles);
783
+ return this;
784
+ }
785
+ /**
786
+ * Get the number of {@link TestAction}s that have yet to be executed.
787
+ */
788
+ getTestActionCount() {
789
+ return this.testActions.getCount().await();
790
+ }
791
+ /**
792
+ * Get the index in the {@link TestAction} list that new {@link TestAction}s will be
793
+ * inserted at.
794
+ */
795
+ getTestActionInsertIndex() {
796
+ return this.testActionInsertIndex;
797
+ }
798
+ resetTestActionInsertIndex() {
799
+ this.testActionInsertIndex = 0;
800
+ }
801
+ insertTestAction(testActionName, testActionType, skip, action) {
802
+ const parentTestAction = this.getCurrentTestAction();
803
+ const testAction = TestAction.create(parentTestAction, testActionName, testActionType, skip, action);
804
+ this.testActions.insert(this.testActionInsertIndex, testAction);
805
+ this.testActionInsertIndex++;
806
+ }
807
+ /**
808
+ * Get the number of tests that have been skipped.
809
+ */
810
+ getSkippedTestCount() {
811
+ return this.skippedTests.getCount().await();
812
+ }
813
+ getSkippedTests() {
814
+ return this.skippedTests;
815
+ }
816
+ /**
817
+ * Get the number of tests that have passed.
818
+ */
819
+ getPassedTestCount() {
820
+ return this.passedTestCount;
821
+ }
822
+ getFailedTestCount() {
823
+ return this.testFailures.getCount().await();
824
+ }
825
+ getFailedTests() {
826
+ return this.testFailures;
827
+ }
828
+ /**
829
+ * Get the {@link TestAction} that is currently executing or undefined if no {@link TestAction}
830
+ * is executing.
831
+ */
832
+ getCurrentTestAction() {
833
+ return this.currentTestAction;
834
+ }
835
+ getCurrentTest() {
836
+ return this.currentTest;
837
+ }
838
+ assertNoCurrentTest() {
839
+ if (this.currentTest) {
840
+ this.currentTest.fail("Can't start a new test group or a new test while running a test.");
841
+ }
842
+ }
843
+ beforeTestGroup(testAction) {
844
+ return this.ui.beforeTestGroup(testAction);
845
+ }
846
+ afterTestGroup(testAction) {
847
+ return this.ui.afterTestGroup(testAction);
848
+ }
849
+ beforeTest(testAction) {
850
+ return this.ui.beforeTest(testAction);
851
+ }
852
+ afterPassedTest(testAction) {
853
+ return AsyncResult.create(async () => {
854
+ await this.ui.afterPassedTest(testAction);
855
+ this.passedTestCount++;
856
+ });
857
+ }
858
+ afterSkippedTest(testAction, skip) {
859
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
860
+ return AsyncResult.create(async () => {
861
+ await this.ui.afterSkippedTest(testAction, skip);
862
+ this.skippedTests.add(SkippedTest.create(skip, testAction));
863
+ });
864
+ }
865
+ afterFailedTest(testAction, error) {
866
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
867
+ PreCondition.assertNotUndefinedAndNotNull(error, "error");
868
+ return AsyncResult.create(async () => {
869
+ await this.ui.afterFailedTest(testAction, error);
870
+ this.testFailures.add(FailedTest.create(testAction, error));
871
+ });
872
+ }
873
+ andList(values) {
874
+ return TestRunner.andList(this, values);
875
+ }
876
+ toString(value) {
877
+ return TestRunner.toString(this, value);
878
+ }
879
+ skip(messageOrShouldSkip, message) {
880
+ let shouldSkip;
881
+ if (!isBoolean(messageOrShouldSkip)) {
882
+ shouldSkip = true;
883
+ message = messageOrShouldSkip;
884
+ } else {
885
+ shouldSkip = messageOrShouldSkip;
886
+ }
887
+ return TestRunner.skip(this, shouldSkip, message);
888
+ }
889
+ testFile(fileName, skipOrTestAction, testAction) {
890
+ PreCondition.assertNotUndefinedAndNotNull(fileName, "fileName");
891
+ PreCondition.assertNotEmpty(fileName, "fileName");
892
+ let skip;
893
+ if (isFunction(skipOrTestAction)) {
894
+ PreCondition.assertUndefined(testAction, "testAction");
895
+ skip = void 0;
896
+ testAction = skipOrTestAction;
897
+ } else {
898
+ skip = skipOrTestAction;
899
+ }
900
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
901
+ this.testGroupOrTest(fileName, "file", skip, testAction);
902
+ }
903
+ testType(typeNameOrType, skipOrTestAction, testAction) {
904
+ PreCondition.assertNotUndefinedAndNotNull(typeNameOrType, "typeNameOrType");
905
+ let typeName;
906
+ if (isString(typeNameOrType)) {
907
+ typeName = typeNameOrType;
908
+ } else {
909
+ typeName = getName(typeNameOrType);
910
+ }
911
+ PreCondition.assertNotEmpty(typeName, "typeName");
912
+ let skip;
913
+ if (isFunction(skipOrTestAction)) {
914
+ PreCondition.assertUndefined(testAction, "testAction");
915
+ skip = void 0;
916
+ testAction = skipOrTestAction;
917
+ } else {
918
+ skip = skipOrTestAction;
919
+ }
920
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
921
+ this.testGroupOrTest(typeName, "type", skip, testAction);
922
+ }
923
+ testFunction(functionSignature, skipOrTestAction, testAction) {
924
+ PreCondition.assertNotEmpty(functionSignature, "functionSignature");
925
+ let skip;
926
+ if (isFunction(skipOrTestAction)) {
927
+ PreCondition.assertUndefined(testAction, "testAction");
928
+ skip = void 0;
929
+ testAction = skipOrTestAction;
930
+ } else {
931
+ skip = skipOrTestAction;
932
+ }
933
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
934
+ this.testGroupOrTest(functionSignature, "function", skip, testAction);
935
+ }
936
+ testGroup(testGroupName, skipOrTestAction, testAction) {
937
+ PreCondition.assertNotUndefinedAndNotNull(testGroupName, "testGroupName");
938
+ PreCondition.assertNotEmpty(testGroupName, "testGroupName");
939
+ let skip;
940
+ if (isFunction(skipOrTestAction)) {
941
+ PreCondition.assertUndefined(testAction, "testAction");
942
+ skip = void 0;
943
+ testAction = skipOrTestAction;
944
+ } else {
945
+ skip = skipOrTestAction;
946
+ }
947
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
948
+ this.innerTestGroup(testGroupName, "group", skip, testAction);
949
+ }
950
+ test(testName, skipOrTestAction, testAction) {
951
+ PreCondition.assertNotUndefinedAndNotNull(testName, "testName");
952
+ PreCondition.assertNotEmpty(testName, "testName");
953
+ let skip;
954
+ if (isFunction(skipOrTestAction)) {
955
+ PreCondition.assertUndefined(testAction, "testAction");
956
+ skip = void 0;
957
+ testAction = skipOrTestAction;
958
+ } else {
959
+ skip = skipOrTestAction;
960
+ }
961
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
962
+ this.innerTest(testName, "test", skip, testAction);
963
+ }
964
+ innerTestGroup(testGroupName, type, skip, testGroupAction) {
965
+ PreCondition.assertNotEmpty(testGroupName, "testGroupName");
966
+ PreCondition.assertNotUndefinedAndNotNull(testGroupAction, "testGroupAction");
967
+ this.assertNoCurrentTest();
968
+ this.insertTestAction(
969
+ testGroupName,
970
+ type,
971
+ skip,
972
+ () => this.beforeTestGroup(this.getCurrentTestAction())
973
+ );
974
+ this.insertTestAction(
975
+ testGroupName,
976
+ type,
977
+ skip,
978
+ async () => {
979
+ this.resetTestActionInsertIndex();
980
+ try {
981
+ await testGroupAction();
982
+ } catch (error) {
983
+ const currentTestGroupAction = this.getCurrentTestAction();
984
+ await this.afterFailedTest(currentTestGroupAction, error);
985
+ }
986
+ }
987
+ );
988
+ this.insertTestAction(
989
+ testGroupName,
990
+ type,
991
+ skip,
992
+ () => this.afterTestGroup(this.getCurrentTestAction())
993
+ );
994
+ }
995
+ innerTest(testName, testType, skip, testAction) {
996
+ PreCondition.assertNotEmpty(testName, "testName");
997
+ PreCondition.assertNotUndefinedAndNotNull(testAction, "testAction");
998
+ this.assertNoCurrentTest();
999
+ this.insertTestAction(
1000
+ testName,
1001
+ testType,
1002
+ skip,
1003
+ async () => {
1004
+ const currentTestAction = this.getCurrentTestAction();
1005
+ try {
1006
+ await this.beforeTest(currentTestAction);
1007
+ const skip2 = currentTestAction.getSkip();
1008
+ if (skip2?.getShouldSkip()) {
1009
+ await this.afterSkippedTest(currentTestAction, skip2);
1010
+ } else {
1011
+ this.currentTest = AssertTest.create(testName);
1012
+ try {
1013
+ await testAction(this.currentTest);
1014
+ } finally {
1015
+ this.currentTest = void 0;
1016
+ }
1017
+ await this.afterPassedTest(currentTestAction);
1018
+ }
1019
+ } catch (error) {
1020
+ await this.afterFailedTest(currentTestAction, error);
1021
+ }
1022
+ }
1023
+ );
1024
+ }
1025
+ testGroupOrTest(name, type, skip, testAction) {
1026
+ if (getParameterCount(testAction) === 0) {
1027
+ this.innerTestGroup(name, type, skip, testAction);
1028
+ } else {
1029
+ this.innerTest(name, type, skip, testAction);
1030
+ }
1031
+ }
1032
+ async runAsync() {
1033
+ while (this.testActions.any().await()) {
1034
+ this.resetTestActionInsertIndex();
1035
+ this.currentTestAction = this.testActions.removeFirst().await();
1036
+ try {
1037
+ await this.currentTestAction.runAsync();
1038
+ } finally {
1039
+ this.currentTestAction = void 0;
1040
+ }
1041
+ }
1042
+ this.resetTestActionInsertIndex();
1043
+ }
1044
+ printSummary() {
1045
+ return this.ui.writeSummary(this.passedTestCount, this.getSkippedTests(), this.getFailedTests());
1046
+ }
1047
+ };
1048
+
1049
+ export {
1050
+ Test,
1051
+ AssertTest,
1052
+ test,
1053
+ assertTestTests_exports,
1054
+ BasicTestSkip,
1055
+ FailedTest,
1056
+ SkippedTest,
1057
+ TestAction,
1058
+ TestSkip,
1059
+ TestRunner,
1060
+ ConsoleTestRunner
1061
+ };
1062
+ //# sourceMappingURL=chunk-VF6FBNAU.js.map