forked from as-pect/as-pect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestCollector.ts
1664 lines (1500 loc) · 54.2 KB
/
TestCollector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { IAspectExports } from "../util/IAspectExports";
import { LogValue } from "../util/LogValue";
import { ActualValue } from "../util/ActualValue";
import { TestGroup } from "./TestGroup";
import { ILogTarget } from "../util/ILogTarget";
import { IWarning } from "./IWarning";
import {
IPerformanceConfiguration,
createDefaultPerformanceConfiguration,
} from "../util/IPerformanceConfiguration";
import { TestResult } from "./TestResult";
import { PerformanceLimits } from "./PerformanceLimits";
// @ts-ignore: Constructor is new Long(low, high, signed);
import Long from "long";
/**
* @ignore
* This function is a filter for stack trace lines.
*
* @param {string} input - The stack trace line.
*/
const wasmFilter = (input: string): boolean => /wasm-function/i.test(input);
/**
* @ignore
* This is a collection of all the parameters required for intantiating a TestCollector.
*/
export interface ITestCollectorParameters {
/** A provided performance configuration. */
performanceConfiguration?: Partial<IPerformanceConfiguration>;
/** A regular expression that filters what tests can be run. Must be set before calling `testContext.run(wasm);` */
testRegex?: RegExp;
/** A regular expression that filters what test groups can be run. Must be set before calling `testContext.run(wasm);` */
groupRegex?: RegExp;
/**
* Put the AssemblyScript test filename here.
*
* @example
* ```ts
* const ctx = new TestContext({
* fileName: "example.spec.ts",
* });
* ```
*/
fileName?: string;
/** Disable RTrace when set to `true`. */
nortrace?: boolean;
}
/**
* @ignore
* This is an internal class that is responsible for collecting all the tests in a test binary.
*/
export class TestCollector {
protected wasm: IAspectExports | null = null;
// test group values
private groupStack: TestGroup[] = [new TestGroup()];
/** A collection of `TestGroup` objects that ran tests after `testContext.run(wasm)` was called. */
public testGroups: TestGroup[] = [];
/** The root `TestGroup` object. */
public topLevelGroup: TestGroup | null = null;
protected logTarget: ILogTarget = this.groupStack[0];
/** A set of errors that were collected during the testing process. */
public errors: IWarning[] = [];
/** A set of warnings that were collected during the testing process. */
public warnings: IWarning[] = [];
/** The name of the AssemblyScript test file. */
public fileName: string = "";
// test state machine values
protected stack: string = "";
protected message: string = "";
protected actual: ActualValue | null = null;
protected expected: ActualValue | null = null;
// performance collection values
private performanceEnabledValue: boolean | undefined;
private maxSamplesValue: number | undefined;
private maxTestRunTimeValue: number | undefined;
private roundDecimalPlacesValue: number | undefined;
private recordAverageValue: boolean | undefined;
private recordMedianValue: boolean | undefined;
private recordStdDevValue: boolean | undefined;
private recordMaxValue: boolean | undefined;
private recordMinValue: boolean | undefined;
private recordVariance: boolean | undefined;
// partial performance configuration
private performanceConfiguration: Partial<
IPerformanceConfiguration
> = createDefaultPerformanceConfiguration();
/**
* This value is used to detect if an `expect()` function call was used outside of a test
* function. If a reportExpected or reportActual function is called before the `context.run()`
* method is called, it should prevent the `run()` method from running the tests and report a
* failure.
*/
protected ready: boolean = false;
/**
* These are the test and group filters for the binary. They must be provided before collection
* begins.
*/
protected testRegex: RegExp = new RegExp("");
protected groupRegex: RegExp = new RegExp("");
/**
* RTrace is a funciton that helps with debugging reference counting and can be used to find
* leaks. If it is enabled, it will be included automatically by the bootstrap in the
* assemblyscript imports.
*/
protected rtraceEnabled: boolean = true;
// This map collects the starting values for the labels created by `RTrace.start()`
private rtraceLabels: Map<number, number> = new Map();
constructor(props?: ITestCollectorParameters) {
/* istanbul ignore next */
if (props) {
/* istanbul ignore next */
if (props.fileName) this.fileName = props.fileName;
/* istanbul ignore next */
if (props.testRegex) this.testRegex = props.testRegex;
/* istanbul ignore next */
if (props.groupRegex) this.groupRegex = props.groupRegex;
/* istanbul ignore next */
if (props.performanceConfiguration)
this.performanceConfiguration = props.performanceConfiguration;
if (this.performanceConfiguration.maxSamples != null) {
if (
this.performanceConfiguration.maxSamples >
PerformanceLimits.MaxSamples
) {
/* istanbul ignore next */
this.pushWarning({
message:
"Invalid Performance Configuration: maxSamples exceeds " +
PerformanceLimits.MaxSamples,
stackTrace: new Error().stack || "",
type: "PerformanceConfigurationWarning",
});
}
if (this.performanceConfiguration.maxSamples < 0) {
/* istanbul ignore next */
this.pushWarning({
message:
"Invalid Performance Configuration: maxSamples less than 0.",
stackTrace: new Error().stack || "",
type: "PerformanceConfigurationWarning",
});
}
}
if (this.performanceConfiguration.maxTestRunTime != null) {
if (
this.performanceConfiguration.maxTestRunTime >
PerformanceLimits.MaxTestRuntime
) {
/* istanbul ignore next */
this.pushWarning({
message:
"Invalid Performance Configuration: maxTestRunTime exceeds " +
PerformanceLimits.MaxTestRuntime,
stackTrace: new Error().stack || "",
type: "PerformanceConfigurationWarning",
});
}
if (this.performanceConfiguration.maxTestRunTime < 0) {
/* istanbul ignore next */
this.pushWarning({
message:
"Invalid Performance Configuration: maxTestRunTime less than 0.",
stackTrace: new Error().stack || "",
type: "PerformanceConfigurationWarning",
});
}
}
if (this.performanceConfiguration.roundDecimalPlaces != null) {
if (
this.performanceConfiguration.roundDecimalPlaces >
PerformanceLimits.MaximumDecimalPlaces
) {
/* istanbul ignore next */
this.pushWarning({
message:
"Invalid Performance Configuration: roundDecimalPlaces exceeds " +
PerformanceLimits.MaximumDecimalPlaces,
stackTrace: new Error().stack || "",
type: "PerformanceConfigurationWarning",
});
}
if (
this.performanceConfiguration.roundDecimalPlaces <
PerformanceLimits.MinimumDecimalPlaces
) {
/* istanbul ignore next */
this.pushWarning({
message:
"Invalid Performance Configuration: roundDecimalPlaces less than " +
PerformanceLimits.MinimumDecimalPlaces,
stackTrace: new Error().stack || "",
type: "PerformanceConfigurationWarning",
});
}
}
/* istanbul ignore next */
if (props.nortrace) this.rtraceEnabled = false;
}
}
/**
* Call this method to start the `__main()` method provided by the `as-pect` exports to start the
* process of test collection.
*/
protected collectTests(): void {
// reset the performance values first, then collect the tests by calling `__main()`
this.resetPerformanceValues();
this.wasm!.__start();
this.wasm!.__ready();
let topLevelGroup = this.groupStack[0];
topLevelGroup.willRun = this.groupRegex.test(topLevelGroup.name);
this.reportEndDescribe();
this.topLevelGroup = topLevelGroup!;
/* istanbul ignore next */
if (!topLevelGroup || this.groupStack.length > 0) {
/* istanbul ignore next */
this.errors.push({
message: "Invalid TestContext state after test collection.",
stackTrace: this.getLogStackTrace(),
type: "InvalidTestContextState",
});
}
}
/**
* This method creates a WebAssembly imports object with all the TestContext functions
* bound to the TestContext.
*
* @param {any[]} imports - Every import item specified.
*/
public createImports(...imports: any[]): any {
const result = Object.assign(
{},
...imports, // get all the user defined imports
{
__aspect: {
debug: this.debug.bind(this),
endRTrace: this.endRTrace.bind(this),
getRTraceAllocations: this.getRTraceAllocations.bind(this),
getRTraceBlocks: this.getRTraceBlocks.bind(this),
getRTraceCount: this.getRTraceCount.bind(this),
getRTraceDecrements: this.getRTraceDecrements.bind(this),
getRTraceFrees: this.getRTraceFrees.bind(this),
getRTraceGroupAllocations: this.getRTraceGroupAllocations.bind(this),
getRTraceGroupBlocks: this.getRTraceGroupBlocks.bind(this),
getRTraceGroupDecrements: this.getRTraceGroupDecrements.bind(this),
getRTraceGroupFrees: this.getRTraceGroupFrees.bind(this),
getRTraceGroupIncrements: this.getRTraceGroupIncrements.bind(this),
getRTraceIncrements: this.getRTraceIncrements.bind(this),
getRTraceTestAllocations: this.getRTraceTestAllocations.bind(this),
getRTraceTestBlocks: this.getRTraceTestBlocks.bind(this),
getRTraceTestDecrements: this.getRTraceTestDecrements.bind(this),
getRTraceTestFrees: this.getRTraceTestFrees.bind(this),
getRTraceTestIncrements: this.getRTraceTestIncrements.bind(this),
getStackTrace: this.getStackTrace.bind(this),
logArray: this.logArray.bind(this),
logBool: this.logBool.bind(this),
logLong: this.logLong.bind(this),
logNull: this.logNull.bind(this),
logReference: this.logReference.bind(this),
logString: this.logString.bind(this),
logValue: this.logValue.bind(this),
maxSamples: this.maxSamples.bind(this),
maxTestRunTime: this.maxTestRunTime.bind(this),
performanceEnabled: this.performanceEnabled.bind(this),
reportActualArray: this.reportActualArray.bind(this),
reportActualBool: this.reportActualBool.bind(this),
reportActualLong: this.reportActualLong.bind(this),
reportActualNull: this.reportActualNull.bind(this),
reportActualReference: this.reportActualReference.bind(this),
reportActualString: this.reportActualString.bind(this),
reportActualValue: this.reportActualValue.bind(this),
reportAfterAll: this.reportAfterAll.bind(this),
reportAfterEach: this.reportAfterEach.bind(this),
reportAverage: this.reportAverage.bind(this),
reportBeforeAll: this.reportBeforeAll.bind(this),
reportBeforeEach: this.reportBeforeEach.bind(this),
reportDescribe: this.reportDescribe.bind(this),
reportEndDescribe: this.reportEndDescribe.bind(this),
reportExpectedArray: this.reportExpectedArray.bind(this),
reportExpectedBool: this.reportExpectedBool.bind(this),
reportExpectedFalsy: this.reportExpectedFalsy.bind(this),
reportExpectedFinite: this.reportExpectedFinite.bind(this),
reportExpectedLong: this.reportExpectedLong.bind(this),
reportExpectedNull: this.reportExpectedNull.bind(this),
reportExpectedReference: this.reportExpectedReference.bind(this),
reportExpectedString: this.reportExpectedString.bind(this),
reportExpectedTruthy: this.reportExpectedTruthy.bind(this),
reportExpectedValue: this.reportExpectedValue.bind(this),
reportInvalidExpectCall: this.reportInvalidExpectCall.bind(this),
reportMax: this.reportMax.bind(this),
reportMedian: this.reportMedian.bind(this),
reportMin: this.reportMin.bind(this),
reportNegatedTest: this.reportNegatedTest.bind(this),
reportStdDev: this.reportStdDev.bind(this),
reportTest: this.reportTest.bind(this),
reportTodo: this.reportTodo.bind(this),
reportVariance: this.reportVariance.bind(this),
roundDecimalPlaces: this.roundDecimalPlaces.bind(this),
startRTrace: this.startRTrace.bind(this),
tryCall: this.tryCall.bind(this),
},
},
);
/** If RTrace is enabled, add it to the imports. */
if (this.rtraceEnabled)
result.rtrace = {
onalloc: this.onalloc.bind(this),
onfree: this.onfree.bind(this),
onincrement: this.onincrement.bind(this),
ondecrement: this.ondecrement.bind(this),
};
/** add an env object */
result.env = result.env || {};
/** Override the abort function */
const previousAbort = result.env.abort || (() => {});
result.env.abort = (...args: any[]) => {
previousAbort(...args);
// @ts-ignore
this.abort(...args);
};
return result;
}
/**
* This is called to stop the debugger. e.g. `node --inspect-brk asp`.
*/
/* istanbul ignore next */
private debug(): void {
/* istanbul ignore next */
debugger;
}
/**
* This is a web assembly utility function that wraps a function call in a try catch block to
* report success or failure.
*
* @param {number} pointer - The function pointer to call. It must accept no parameters and return
* void.
* @returns {1 | 0} - If the callback was run successfully without error, it returns 1, else it
* returns 0.
*/
protected tryCall(pointer: number): 1 | 0 {
/** This is a safety net conditional, no reason to test it. */
/* istanbul ignore next */
if (pointer < 0) return 1;
try {
this.wasm!.__call(pointer);
} catch (ex) {
this.stack = this.getErrorStackTrace(ex);
return 0;
}
return 1;
}
/**
* Log a null value to the reporter.
*/
private logBool(boolValue: number): void {
const value = new LogValue();
const target = this.logTarget;
value.stack = this.getLogStackTrace();
value.message = `Value ${!!boolValue}`;
value.value = boolValue;
value.target = target;
}
/**
* Log a null value to the reporter.
*/
private logNull(): void {
// create a new log value
const value = new LogValue();
const target = this.logTarget;
// collect log metadata
value.stack = this.getLogStackTrace();
value.message = "null";
value.target = target;
// push the log value to the logs
target.logs.push(value);
}
/**
* Log an array to the reporter.
*
* @param arrayPointer - The array pointer.
*/
private logArray(arrayPointer: number): void {
const value = new LogValue();
const target = this.logTarget;
value.stack = this.getLogStackTrace();
value.values = this.wasm!.__getArray(arrayPointer);
value.message = JSON.stringify(value.values);
value.target = target;
target.logs.push(value);
}
/**
* Log a reference to the reporter.
*
* @param {number} referencePointer - The pointer to the reference.
* @param {number} offset - The offset of the reference.
*/
private logReference(referencePointer: number, offset: number): void {
const value = new LogValue();
const target = this.logTarget;
value.bytes = Array.from(
this.wasm!.U8.slice(referencePointer, referencePointer + offset),
);
value.message = "Reference Type";
value.offset = offset;
value.pointer = referencePointer;
value.stack = this.getLogStackTrace();
value.target = target;
value.value = referencePointer;
// push the log value to the logs
target.logs.push(value);
}
/**
* This adds a logged string to the current test.
*
* @param {number} pointer - The pointer to the logged string reference.
*/
private logString(pointer: number): void {
const value = new LogValue();
const target = this.logTarget;
value.message = this.getString(pointer, "");
value.offset = 0;
value.pointer = pointer;
value.stack = this.getLogStackTrace();
value.target = target;
value.value = pointer;
// push the log value to the logs
target.logs.push(value);
}
/**
* Log a numevalueric value to the reporter.
*
* @param {number} value - The value to be logged.
* @param {1 | 0} signed - The value indicating if the number is signed.
*/
private logValue(numericValue: number, signed: 1 | 0): void {
const value = new LogValue();
const target = this.logTarget;
// convert to unsigned value if the integer is not signed
numericValue = signed === 1 ? numericValue : numericValue >>> 0;
value.stack = this.getLogStackTrace();
value.message = `Value ${numericValue.toString()}`;
value.value = numericValue;
value.target = target;
// push the log value to the logs
target.logs.push(value);
}
/**
* Log a long value.
*
* @param suiteNamePointer - The boxed long value's pointer.
*/
private logLong(boxPointer: number, signed: 1 | 0): void {
const value = new LogValue();
const target = this.logTarget;
const long = new Long.fromBytesLE(
this.wasm!.U8.slice(boxPointer, boxPointer + 8),
!signed,
);
value.stack = this.getLogStackTrace();
value.message = `Value ${long.toString()}`;
value.target = target;
// push the log value to the logs
target.logs.push(value);
}
/**
* This web assembly linked function creates a test group. It's called when the test suite calls
* the describe("test", callback) function from within AssemblyScript. It receives a pointer to
* the description of the tests, forks the top level test group, pushes the suiteName to a list,
* then pushes the forked group to the top of the test context stack.
*
* @param {number} suiteNamePointer
*/
private reportDescribe(suiteNamePointer: number): void {
const group = this.groupStack[this.groupStack.length - 1];
const nextGroup = group.fork();
nextGroup.name =
group.name +
this.getString(suiteNamePointer, "No describe() name provided.");
nextGroup.willRun = this.groupRegex.test(nextGroup.name);
this.groupStack.push(nextGroup);
this.logTarget = nextGroup;
}
/**
* This web assembly linked function finishes a test group. It's called when the test suite calls
* the describe("test", callback) function from within AssemblyScript. It pops the current
* test group from the test context stack and pushes it to the final test group list.
*/
private reportEndDescribe(): void {
const next = this.groupStack.pop()!;
const testCount = next.tests.length;
/**
* If a describe finishes first, it happens BEFORE other describes. This means
* inner describe blocks run at lower priority than outer describe blocks. It also should be
* pushed if it willRun.
*/
if (next.willRun && testCount > 0) {
this.testGroups.unshift(next);
}
this.logTarget = this.groupStack[this.groupStack.length - 1];
}
/**
* This web assembly linked function sets the group's "beforeEach" callback pointer to
* the current groupStackItem.
*
* @param {number} callbackPointer - The callback that should run before each test.
*/
private reportBeforeEach(callbackPointer: number): void {
var group = this.groupStack[this.groupStack.length - 1];
group.beforeEachPointers.push(callbackPointer);
}
/**
* This web assembly linked function adds the group's "beforeAll" callback pointer to
* the current groupStackItem.
*
* @param {number} callbackPointer - The callback that should run before each test in the
* current context.
*/
private reportBeforeAll(callbackPointer: number): void {
var group = this.groupStack[this.groupStack.length - 1];
group.beforeAllPointers.push(callbackPointer);
}
/**
* This web assembly linked function sets the group's "afterEach" callback pointer.
*
* @param {number} callbackPointer - The callback that should run before each test group.
*/
private reportAfterEach(callbackPointer: number): void {
var group = this.groupStack[this.groupStack.length - 1];
group.afterEachPointers.push(callbackPointer);
}
/**
* This web assembly linked function adds the group's "afterAll" callback pointer to
* the current groupStackItem.
*
* @param {number} callbackPointer - The callback that should run before each test in the
* current context.
*/
private reportAfterAll(callbackPointer: number): void {
var group = this.groupStack[this.groupStack.length - 1];
group.afterAllPointers.push(callbackPointer);
}
/**
* This web assembly linked function creates a test from the callback and the testNamePointer in
* the current group. It assumes that the group has already been created with the describe
* function. It is called when `it("description", callback)` or `test("description", callback)`
* is called.
*
* @param {number} testNamePointer - The test's name pointer.
* @param {number} callback - The test's function.
*/
private reportTest(testNamePointer: number, callback: number): void {
const group = this.groupStack[this.groupStack.length - 1];
if (!group.willRun) return;
const name = this.getString(testNamePointer, "No test() name provided.");
if (!this.testRegex.test(name)) return;
const test = new TestResult();
test.functionPointer = callback;
test.name = name;
test.message = "";
test.negated = false;
test.performance = this.performanceEnabledValue || false;
if (test.performance) {
test.maxSamples = !isFinite(this.maxSamplesValue!)
? PerformanceLimits.MaxSamples
: Math.min(this.maxSamplesValue!, PerformanceLimits.MaxSamples);
test.maxRuntime = !isFinite(this.maxTestRunTimeValue!)
? PerformanceLimits.MaxTestRuntime
: Math.min(this.maxTestRunTimeValue!, PerformanceLimits.MaxTestRuntime);
test.decimalPlaces = !isFinite(this.roundDecimalPlacesValue!)
? 3
: Math.max(
Math.round(this.roundDecimalPlacesValue!),
PerformanceLimits.MinimumDecimalPlaces,
);
if (test.decimalPlaces > PerformanceLimits.MaximumDecimalPlaces) {
test.decimalPlaces = PerformanceLimits.MaximumDecimalPlaces;
}
test.calculateAverageValue = this.recordAverageValue || false;
test.calculateMedianValue = this.recordMedianValue || false;
test.calculateStandardDeviationValue = this.recordStdDevValue || false;
test.calculateMaxValue = this.recordMaxValue || false;
test.calculateMinValue = this.recordMinValue || false;
test.calculateVarianceValue = this.recordVariance || false;
}
group.tests.push(test);
this.resetPerformanceValues();
}
/**
* This web assembly linked function is responsible for reporting tests that are expected
* to fail. This is useful for verifying that specific application states will throw.
*
* @param {number} testNamePointer - The test's name pointer.
* @param {number} callback - The test's function.
* @param {number} message - The message associated with this test if it does not throw.
*/
private reportNegatedTest(
testNamePointer: number,
callback: number,
message: number,
): void {
const group = this.groupStack[this.groupStack.length - 1];
if (!group.willRun) return;
const name = this.getString(testNamePointer, "No test() name provided.");
if (!this.testRegex.test(name)) return;
const test = new TestResult();
test.functionPointer = callback;
test.name = name;
test.message = this.getString(message, "");
test.negated = true;
test.performance = this.performanceEnabledValue || false;
/* istanbul ignore next */
if (test.performance) {
/* istanbul ignore next */
test.maxSamples = !isFinite(this.maxSamplesValue!)
? PerformanceLimits.MaxSamples
: Math.min(this.maxSamplesValue!, PerformanceLimits.MaxSamples);
/* istanbul ignore next */
test.maxRuntime = !isFinite(this.maxTestRunTimeValue!)
? PerformanceLimits.MaxTestRuntime
: Math.min(this.maxTestRunTimeValue!, PerformanceLimits.MaxTestRuntime);
/* istanbul ignore next */
test.decimalPlaces = !isFinite(this.roundDecimalPlacesValue!)
? 3
: Math.max(
Math.round(this.roundDecimalPlacesValue!),
PerformanceLimits.MinimumDecimalPlaces,
);
/* istanbul ignore next */
test.calculateAverageValue = this.recordAverageValue || false;
/* istanbul ignore next */
test.calculateMedianValue = this.recordMedianValue || false;
/* istanbul ignore next */
test.calculateStandardDeviationValue = this.recordStdDevValue || false;
/* istanbul ignore next */
test.calculateMaxValue = this.recordMaxValue || false;
/* istanbul ignore next */
test.calculateMinValue = this.recordMaxValue || false;
/* istanbul ignore next */
test.calculateVarianceValue = this.recordVariance || false;
}
group.tests.push(test);
this.resetPerformanceValues();
}
/**
* This function reports a single "todo" item in a test suite.
*
* @param {number} todoPointer - The todo description string pointer.
*/
private reportTodo(todoPointer: number): void {
var group = this.groupStack[this.groupStack.length - 1];
group.todos.push(this.getString(todoPointer, "No todo() value provided."));
}
/**
* This function reports an actual null value.
*/
private reportActualNull(stackTrace: number): void {
const value = new ActualValue();
value.message = `null`;
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.value = null;
this.actual = value;
}
/**
* This function reports an expected null value.
*
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedNull(negated: 1 | 0, stackTrace: number): void {
const value = new ActualValue();
value.message = `null`;
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
value.value = null;
this.expected = value;
}
/**
* This function reports an actual numeric value.
*
* @param {number} numericValue - The value to be expected.
* @param {1 | 0} signed - The value indicating if the value is signed.
*/
private reportActualValue(
numericValue: number,
signed: 1 | 0,
stackTrace: number,
): void {
// flip the sign bits if it's unsigned
numericValue = signed === 1 ? numericValue : numericValue >>> 0;
const value = new ActualValue();
value.message = numericValue.toString();
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.value = numericValue;
this.actual = value;
}
/**
* This function reports an actual numeric value.
*
* @param {number} numericValue - The value to be expected.
* @param {1 | 0} signed - The value indicating if the value is signed.
*/
private reportActualBool(boolValue: 1 | 0, stackTrace: number): void {
const value = new ActualValue();
value.message = (!!boolValue).toString();
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.value = boolValue;
this.actual = value;
}
/**
* This function reports an expected numeric value.
*
* @param {number} numericValue - The expected value.
* @param {1 | 0} signed - The value indicating if the value is signed.
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedValue(
numericValue: number,
signed: 0 | 1,
negated: 0 | 1,
stackTrace: number,
): void {
// convert to unsigned if the value is unsigned
numericValue = signed === 1 ? numericValue : numericValue >>> 0;
const value = new ActualValue();
value.message = numericValue.toString();
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
value.value = numericValue;
this.expected = value;
}
/**
* This function reports an expected numeric value.
*
* @param {number} numericValue - The expected value.
* @param {1 | 0} signed - The value indicating if the value is signed.
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedBool(
boolValue: 0 | 1,
negated: 0 | 1,
stackTrace: number,
): void {
const value = new ActualValue();
value.message = (!!boolValue).toString();
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
value.value = boolValue;
this.expected = value;
}
/**
* This function reports an actual long value.
*
* @param {number} boxPointer - The expected box pointer.
* @param {1 | 0} signed - An indicator if the long value is signed.
*/
private reportActualLong(
boxPointer: number,
signed: 1 | 0,
stackTrace: number,
): void {
const value = new ActualValue();
const long = new Long.fromBytesLE(
this.wasm!.U8.slice(boxPointer, boxPointer + 8),
!signed,
);
value.message = "Long Value: " + long.toString();
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
this.actual = value;
}
/**
* This function reports an actual reference value.
*
* @param {number} referencePointer - The actual reference pointer.
* @param {number} offset - The size of the reference in bytes.
*/
private reportActualReference(
referencePointer: number,
offset: number,
stackTrace: number,
): void {
const value = new ActualValue();
value.message = "Reference Value";
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.pointer = referencePointer;
value.offset = offset;
value.bytes = Array.from(
this.wasm!.U8.slice(referencePointer, referencePointer + offset),
);
value.value = referencePointer;
this.actual = value;
}
/**
* This function reports an expected reference value.
*
* @param {number} referencePointer - The expected reference pointer.
* @param {number} offset - The size of the reference in bytes.
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedReference(
referencePointer: number,
offset: number,
negated: 1 | 0,
stackTrace: number,
): void {
const value = new ActualValue();
value.message = "Reference Value";
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.pointer = referencePointer;
value.offset = offset;
value.bytes = Array.from(
this.wasm!.U8.slice(referencePointer, referencePointer + offset),
);
value.negated = negated === 1;
value.value = referencePointer;
this.expected = value;
}
/**
* This function reports an expected long value.
*
* @param {number} boxPointer - The expected box pointer.
* @param {1 | 0} signed - An indicator if the long value is signed.
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedLong(
boxPointer: number,
signed: 1 | 0,
negated: 1 | 0,
stackTrace: number,
): void {
const value = new ActualValue();
const long = new Long.fromBytesLE(
this.wasm!.U8.slice(boxPointer, boxPointer + 8),
!signed,
);
value.message = "Long Value: " + long.toString();
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
this.expected = value;
}
/**
* This function reports an expected truthy value.
*
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedTruthy(negated: 1 | 0, stackTrace: number): void {
const value = new ActualValue();
value.message = "Truthy Value";
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
this.expected = value;
}
/**
* This function reports an expected falsy value.
*
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedFalsy(negated: 1 | 0, stackTrace: number): void {
const value = new ActualValue();
value.message = "Falsy Value";
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
this.expected = value;
}
/**
* This function reports an expected finite value.
*
* @param {1 | 0} negated - An indicator if the expectation is negated.
*/
private reportExpectedFinite(negated: 1 | 0, stackTrace: number): void {
const value = new ActualValue();
value.message = "Finite Value";
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.negated = negated === 1;
this.expected = value;
}
/**
* This function reports an actual string value.
*
* @param {number} stringPointer - A pointer that points to the actual string.
*/
private reportActualString(stringPointer: number, stackTrace: number): void {
const value = new ActualValue();
value.message = this.getString(stringPointer, "Null actual string.");
value.pointer = stringPointer;
value.stack = this.stackTraces.get(stackTrace)!;
value.target = this.logTarget;
value.value = stringPointer;
this.actual = value;
}
/**
* This function reports an expected string value.
*