@gjsify/unit 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test.node.mjs DELETED
@@ -1,1219 +0,0 @@
1
- // ../../../node_modules/@girs/gjs/gjs.js
2
- var imports = globalThis.imports || {};
3
-
4
- // lib/esm/spy.js
5
- function spy(f) {
6
- const calls = [];
7
- function spy2(...args) {
8
- let retv;
9
- try {
10
- retv = f ? f.apply(this, args) : void 0;
11
- } catch (error) {
12
- calls.push({
13
- type: "throw",
14
- this: this,
15
- arguments: args,
16
- throw: error
17
- });
18
- throw error;
19
- }
20
- calls.push({
21
- type: "return",
22
- this: this,
23
- arguments: args,
24
- return: retv
25
- });
26
- return retv;
27
- }
28
- Object.defineProperties(spy2, {
29
- calls: descriptors.calls(calls),
30
- returnedCalls: descriptors.returnedCalls,
31
- thrownCalls: descriptors.thrownCalls,
32
- firstCall: descriptors.firstCall,
33
- lastCall: descriptors.lastCall,
34
- firstReturnedCall: descriptors.firstReturnedCall,
35
- lastReturnedCall: descriptors.lastReturnedCall,
36
- firstThrownCall: descriptors.firstThrownCall,
37
- lastThrownCall: descriptors.lastThrownCall,
38
- reset: descriptors.reset,
39
- toString: descriptors.toString(f)
40
- });
41
- return spy2;
42
- }
43
- var descriptors = {
44
- calls(value) {
45
- return { value, configurable: true };
46
- },
47
- returnedCalls: {
48
- get: function returnedCalls() {
49
- return this.calls.filter(isReturned);
50
- },
51
- configurable: true
52
- },
53
- thrownCalls: {
54
- get: function thrownCalls() {
55
- return this.calls.filter(isThrown);
56
- },
57
- configurable: true
58
- },
59
- firstCall: {
60
- get: function firstCall() {
61
- return this.calls[0] || null;
62
- },
63
- configurable: true
64
- },
65
- lastCall: {
66
- get: function lastCall() {
67
- return this.calls[this.calls.length - 1] || null;
68
- },
69
- configurable: true
70
- },
71
- firstReturnedCall: {
72
- get: function firstReturnedCall() {
73
- for (let i = 0; i < this.calls.length; ++i) {
74
- const call = this.calls[i];
75
- if (isReturned(call)) {
76
- return call;
77
- }
78
- }
79
- return null;
80
- },
81
- configurable: true
82
- },
83
- lastReturnedCall: {
84
- get: function lastReturnedCall() {
85
- for (let i = this.calls.length - 1; i >= 0; --i) {
86
- const call = this.calls[i];
87
- if (isReturned(call)) {
88
- return call;
89
- }
90
- }
91
- return null;
92
- },
93
- configurable: true
94
- },
95
- firstThrownCall: {
96
- get: function firstThrownCall() {
97
- for (let i = 0; i < this.calls.length; ++i) {
98
- const call = this.calls[i];
99
- if (isThrown(call)) {
100
- return call;
101
- }
102
- }
103
- return null;
104
- },
105
- configurable: true
106
- },
107
- lastThrownCall: {
108
- get: function lastThrownCall() {
109
- for (let i = this.calls.length - 1; i >= 0; --i) {
110
- const call = this.calls[i];
111
- if (isThrown(call)) {
112
- return call;
113
- }
114
- }
115
- return null;
116
- },
117
- configurable: true
118
- },
119
- reset: {
120
- value: function reset() {
121
- ;
122
- this.calls.length = 0;
123
- },
124
- configurable: true
125
- },
126
- toString(f) {
127
- return {
128
- value: function toString() {
129
- return `/* The spy of */ ${f ? f.toString() : "function(){}"}`;
130
- },
131
- configurable: true
132
- };
133
- }
134
- };
135
- function isReturned(call) {
136
- return call.type === "return";
137
- }
138
- function isThrown(call) {
139
- return call.type === "throw";
140
- }
141
-
142
- // lib/esm/index.js
143
- import nodeAssert from "assert";
144
- var mainloop = globalThis?.imports?.mainloop;
145
- var countTestsOverall = 0;
146
- var countTestsFailed = 0;
147
- var countTestsIgnored = 0;
148
- var runtime = "";
149
- var RED = "\x1B[31m";
150
- var GREEN = "\x1B[32m";
151
- var BLUE = "\x1B[34m";
152
- var GRAY = "\x1B[90m";
153
- var RESET = "\x1B[39m";
154
- var print = globalThis.print || console.log;
155
- var MatcherFactory = class _MatcherFactory {
156
- constructor(actualValue, positive, negated) {
157
- this.actualValue = actualValue;
158
- this.positive = positive;
159
- if (negated) {
160
- this.not = negated;
161
- } else {
162
- this.not = new _MatcherFactory(actualValue, !positive, this);
163
- }
164
- }
165
- not;
166
- triggerResult(success, msg) {
167
- if (success && !this.positive || !success && this.positive) {
168
- ++countTestsFailed;
169
- throw new Error(msg);
170
- }
171
- }
172
- to(callback) {
173
- this.triggerResult(
174
- callback(this.actualValue),
175
- ` Expected callback to validate`
176
- );
177
- }
178
- toBe(expectedValue) {
179
- this.triggerResult(
180
- this.actualValue === expectedValue,
181
- ` Expected values to match using ===
182
- Expected: ${expectedValue} (${typeof expectedValue})
183
- Actual: ${this.actualValue} (${typeof this.actualValue})`
184
- );
185
- }
186
- toEqual(expectedValue) {
187
- this.triggerResult(
188
- this.actualValue == expectedValue,
189
- ` Expected values to match using ==
190
- Expected: ${expectedValue} (${typeof expectedValue})
191
- Actual: ${this.actualValue} (${typeof this.actualValue})`
192
- );
193
- }
194
- toEqualArray(expectedValue) {
195
- let success = Array.isArray(this.actualValue) && Array.isArray(expectedValue) && this.actualValue.length === expectedValue.length;
196
- for (let i = 0; i < this.actualValue.length; i++) {
197
- const actualVal = this.actualValue[i];
198
- const expectedVal = expectedValue[i];
199
- success = actualVal == expectedVal;
200
- if (!success)
201
- break;
202
- }
203
- this.triggerResult(
204
- success,
205
- ` Expected array items to match using ==
206
- Expected: ${expectedValue} (${typeof expectedValue})
207
- Actual: ${this.actualValue} (${typeof this.actualValue})`
208
- );
209
- }
210
- toMatch(expectedValue) {
211
- if (typeof this.actualValue.match !== "function") {
212
- throw new Error(`You can not use toMatch on type ${typeof this.actualValue}`);
213
- }
214
- this.triggerResult(
215
- !!this.actualValue.match(expectedValue),
216
- " Expected values to match using regular expression\n Expression: " + expectedValue + "\n Actual: " + this.actualValue
217
- );
218
- }
219
- toBeDefined() {
220
- this.triggerResult(
221
- typeof this.actualValue !== "undefined",
222
- ` Expected value to be defined`
223
- );
224
- }
225
- toBeUndefined() {
226
- this.triggerResult(
227
- typeof this.actualValue === "undefined",
228
- ` Expected value to be undefined`
229
- );
230
- }
231
- toBeNull() {
232
- this.triggerResult(
233
- this.actualValue === null,
234
- ` Expected value to be null`
235
- );
236
- }
237
- toBeTruthy() {
238
- this.triggerResult(
239
- this.actualValue,
240
- ` Expected value to be truthy`
241
- );
242
- }
243
- toBeFalsy() {
244
- this.triggerResult(
245
- !this.actualValue,
246
- ` Expected value to be falsy`
247
- );
248
- }
249
- toContain(needle) {
250
- this.triggerResult(
251
- this.actualValue instanceof Array && this.actualValue.indexOf(needle) !== -1,
252
- ` Expected ` + this.actualValue + ` to contain ` + needle
253
- );
254
- }
255
- toBeLessThan(greaterValue) {
256
- this.triggerResult(
257
- this.actualValue < greaterValue,
258
- ` Expected ` + this.actualValue + ` to be less than ` + greaterValue
259
- );
260
- }
261
- toBeGreaterThan(smallerValue) {
262
- this.triggerResult(
263
- this.actualValue > smallerValue,
264
- ` Expected ` + this.actualValue + ` to be greater than ` + smallerValue
265
- );
266
- }
267
- toBeCloseTo(expectedValue, precision) {
268
- const shiftHelper = Math.pow(10, precision);
269
- this.triggerResult(
270
- Math.round(this.actualValue * shiftHelper) / shiftHelper === Math.round(expectedValue * shiftHelper) / shiftHelper,
271
- ` Expected ` + this.actualValue + ` with precision ` + precision + ` to be close to ` + expectedValue
272
- );
273
- }
274
- toThrow(ErrorType) {
275
- let errorMessage = "";
276
- let didThrow = false;
277
- let typeMatch = true;
278
- try {
279
- this.actualValue();
280
- didThrow = false;
281
- } catch (e) {
282
- errorMessage = e.message || "";
283
- didThrow = true;
284
- if (ErrorType) {
285
- typeMatch = e instanceof ErrorType;
286
- }
287
- }
288
- const functionName = this.actualValue.name || typeof this.actualValue === "function" ? "[anonymous function]" : this.actualValue.toString();
289
- this.triggerResult(
290
- didThrow,
291
- ` Expected ${functionName} to ${this.positive ? "throw" : "not throw"} an exception ${!this.positive && errorMessage ? `, but an error with the message "${errorMessage}" was thrown` : ""}`
292
- );
293
- if (ErrorType) {
294
- this.triggerResult(
295
- typeMatch,
296
- ` Expected Error type '${ErrorType.name}', but the error is not an instance of it`
297
- );
298
- }
299
- }
300
- };
301
- var describe = async function(moduleName, callback) {
302
- print("\n" + moduleName);
303
- await callback();
304
- beforeEachCb = null;
305
- afterEachCb = null;
306
- };
307
- var beforeEachCb;
308
- var afterEachCb;
309
- var beforeEach = function(callback) {
310
- beforeEachCb = callback;
311
- };
312
- var afterEach = function(callback) {
313
- afterEachCb = callback;
314
- };
315
- var it = async function(expectation, callback) {
316
- try {
317
- if (typeof beforeEachCb === "function") {
318
- await beforeEachCb();
319
- }
320
- await callback();
321
- if (typeof afterEachCb === "function") {
322
- await afterEachCb();
323
- }
324
- print(` ${GREEN}\u2714${RESET} ${GRAY}${expectation}${RESET}`);
325
- } catch (e) {
326
- print(` ${RED}\u274C${RESET} ${GRAY}${expectation}${RESET}`);
327
- print(`${RED}${e.message}${RESET}`);
328
- if (e.stack)
329
- print(e.stack);
330
- }
331
- };
332
- var expect = function(actualValue) {
333
- ++countTestsOverall;
334
- const expecter = new MatcherFactory(actualValue, true);
335
- return expecter;
336
- };
337
- var assert = function(success, message) {
338
- ++countTestsOverall;
339
- if (!success) {
340
- ++countTestsFailed;
341
- }
342
- nodeAssert(success, message);
343
- };
344
- assert.strictEqual = function(actual, expected, message) {
345
- ++countTestsOverall;
346
- try {
347
- nodeAssert.strictEqual(actual, expected, message);
348
- } catch (error) {
349
- ++countTestsFailed;
350
- throw error;
351
- }
352
- };
353
- assert.throws = function(promiseFn, ...args) {
354
- ++countTestsOverall;
355
- let error;
356
- try {
357
- promiseFn();
358
- } catch (e) {
359
- error = e;
360
- }
361
- if (!error)
362
- ++countTestsFailed;
363
- nodeAssert.throws(() => {
364
- if (error)
365
- throw error;
366
- }, args[0], args[1]);
367
- };
368
- assert.deepStrictEqual = function(actual, expected, message) {
369
- ++countTestsOverall;
370
- try {
371
- nodeAssert.deepStrictEqual(actual, expected, message);
372
- } catch (error) {
373
- ++countTestsFailed;
374
- throw error;
375
- }
376
- };
377
- var runTests = async function(namespaces) {
378
- for (const subNamespace in namespaces) {
379
- const namespace = namespaces[subNamespace];
380
- if (typeof namespace === "function") {
381
- await namespace();
382
- } else if (typeof namespace === "object") {
383
- await runTests(namespace);
384
- }
385
- }
386
- };
387
- var printResult = () => {
388
- if (countTestsIgnored) {
389
- print(`
390
- ${BLUE}\u2714 ${countTestsIgnored} ignored test${countTestsIgnored > 1 ? "s" : ""}${RESET}`);
391
- }
392
- if (countTestsFailed) {
393
- print(`
394
- ${RED}\u274C ${countTestsFailed} of ${countTestsOverall} tests failed${RESET}`);
395
- } else {
396
- print(`
397
- ${GREEN}\u2714 ${countTestsOverall} completed${RESET}`);
398
- }
399
- };
400
- var getRuntime = async () => {
401
- if (runtime && runtime !== "Unknown") {
402
- return runtime;
403
- }
404
- if (globalThis.Deno?.version?.deno) {
405
- return "Deno " + globalThis.Deno?.version?.deno;
406
- } else {
407
- let process = globalThis.process;
408
- if (!process) {
409
- try {
410
- process = await import("process");
411
- } catch (error) {
412
- console.error(error);
413
- console.warn(error.message);
414
- runtime = "Unknown";
415
- }
416
- }
417
- if (process?.versions?.gjs) {
418
- runtime = "Gjs " + process.versions.gjs;
419
- } else if (process?.versions?.node) {
420
- runtime = "Node.js " + process.versions.node;
421
- }
422
- }
423
- return runtime || "Unknown";
424
- };
425
- var printRuntime = async () => {
426
- const runtime2 = await getRuntime();
427
- print(`
428
- Running on ${runtime2}`);
429
- };
430
- var run = async (namespaces) => {
431
- printRuntime().then(async () => {
432
- return runTests(namespaces).then(() => {
433
- printResult();
434
- print();
435
- mainloop?.quit();
436
- });
437
- });
438
- mainloop?.run();
439
- };
440
-
441
- // src/index.spec.ts
442
- var index_spec_default = async () => {
443
- await describe("assert", async () => {
444
- await it("should consider truthy values as valid", async () => {
445
- assert(true);
446
- assert(1);
447
- assert({});
448
- assert([]);
449
- });
450
- });
451
- await describe("beforeEach", async () => {
452
- let foo = "";
453
- let count = 0;
454
- let countAfter = 0;
455
- beforeEach(async () => {
456
- console.log("beforeEach");
457
- foo = "bar";
458
- ++count;
459
- });
460
- afterEach(async () => {
461
- console.log("afterEach");
462
- --countAfter;
463
- });
464
- await it('foo should be "bar"', async () => {
465
- expect(foo).toBe("bar");
466
- foo = "override me again";
467
- });
468
- await it('foo should be "bar" again', async () => {
469
- expect(foo).toBe("bar");
470
- });
471
- await it("count should be 3 again", async () => {
472
- expect(count).toBe(3);
473
- });
474
- await it("countAfter should be -3", async () => {
475
- expect(countAfter).toBe(-3);
476
- });
477
- });
478
- await describe("expect::to", async () => {
479
- await it("should be possible to validate expectations by callback", async () => {
480
- expect(3).to(function(actualValue) {
481
- return actualValue === 3;
482
- });
483
- });
484
- await it("should be possible to invalidate expectations by callback", async () => {
485
- expect(3).not.to(function(actualValue) {
486
- return actualValue === 5;
487
- });
488
- });
489
- });
490
- await describe("expect::toBe", async () => {
491
- var obj = {};
492
- var obj2 = {};
493
- await it("should compare using ===", async () => {
494
- expect(true).toBe(true);
495
- expect(false).toBe(false);
496
- expect("test").toBe("test");
497
- expect(obj).toBe(obj);
498
- });
499
- await it("should compare using !==", async () => {
500
- expect(true).not.toBe(false);
501
- expect(true).not.toBe(1);
502
- expect(false).not.toBe(true);
503
- expect(false).not.toBe(0);
504
- expect("test").not.toBe("test2");
505
- expect(obj).not.toBe(obj2);
506
- });
507
- });
508
- await describe("expect::toEqual", async () => {
509
- var obj = {};
510
- var obj2 = {};
511
- await it("should compare using ==", async () => {
512
- expect(true).toEqual(true);
513
- expect(true).toEqual(1);
514
- expect(false).toEqual(false);
515
- expect(false).toEqual(0);
516
- expect("test").toEqual("test");
517
- expect(obj).toEqual(obj);
518
- });
519
- await it("should compare using !=", async () => {
520
- expect(true).not.toEqual(false);
521
- expect(false).not.toEqual(true);
522
- expect("test").not.toEqual("test2");
523
- expect(obj).not.toEqual(obj2);
524
- });
525
- });
526
- await describe("expect::toMatch", async () => {
527
- await it("should consider matching regular expressions as valid", async () => {
528
- expect("test").toMatch(/test/);
529
- expect("test").toMatch(/est/);
530
- expect("test").toMatch("test");
531
- });
532
- await it("should consider non matching regular expressions as invalid", async () => {
533
- expect("test").not.toMatch(/tester/);
534
- });
535
- });
536
- await describe("expect::toBeDefined", async () => {
537
- var obj = { key: "value" };
538
- await it("should consider defined values as valid", async () => {
539
- expect(obj.key).toBeDefined();
540
- });
541
- await it("should consider undefined values as invalid", async () => {
542
- expect(obj.invalidKey).not.toBeDefined();
543
- });
544
- });
545
- await describe("expect::toBeUndefined", async () => {
546
- var obj = { key: "value" };
547
- await it("should consider undefined values as valid", async () => {
548
- expect(obj.invalidKey).toBeUndefined();
549
- });
550
- await it("should consider defined values as invalid", async () => {
551
- expect(obj.key).not.toBeUndefined();
552
- });
553
- });
554
- await describe("expect::toBeNull", async () => {
555
- await it("should consider null values as valid", async () => {
556
- expect(null).toBeNull();
557
- });
558
- await it("should consider non null values as invalid", async () => {
559
- expect(0).not.toBeNull();
560
- expect(false).not.toBeNull();
561
- expect("").not.toBeNull();
562
- expect("null").not.toBeNull();
563
- expect(void 0).not.toBeNull();
564
- expect({}).not.toBeNull();
565
- });
566
- });
567
- await describe("expect::toBeTruthy", async () => {
568
- await it("should consider truthy values as valid", async () => {
569
- expect(true).toBeTruthy();
570
- expect(1).toBeTruthy();
571
- expect({}).toBeTruthy();
572
- expect([]).toBeTruthy();
573
- });
574
- await it("should consider non truthy values as invalid", async () => {
575
- expect(false).not.toBeTruthy();
576
- expect(0).not.toBeTruthy();
577
- expect("").not.toBeTruthy();
578
- expect(null).not.toBeTruthy();
579
- expect(void 0).not.toBeTruthy();
580
- });
581
- });
582
- await describe("expect::toBeFalsy", async () => {
583
- await it("should consider truthy values as valid", async () => {
584
- expect(false).toBeFalsy();
585
- expect(0).toBeFalsy();
586
- expect("").toBeFalsy();
587
- expect(null).toBeFalsy();
588
- expect(void 0).toBeFalsy();
589
- });
590
- await it("should consider non truthy values as invalid", async () => {
591
- expect(true).not.toBeFalsy();
592
- expect(1).not.toBeFalsy();
593
- expect({}).not.toBeFalsy();
594
- expect([]).not.toBeFalsy();
595
- });
596
- });
597
- await describe("expect::toContain", async () => {
598
- var testArray = [1, "a"];
599
- await it("should consider array containing a value as valid", async () => {
600
- expect(testArray).toContain(1);
601
- expect(testArray).toContain("a");
602
- });
603
- await it("should consider arrays not containing a value as invalid", async () => {
604
- expect(testArray).not.toContain(0);
605
- expect(testArray).not.toContain("b");
606
- });
607
- });
608
- await describe("expect::toBeLessThan", async () => {
609
- await it("should consider greater values as valid", async () => {
610
- expect(1).toBeLessThan(2);
611
- expect(1).toBeLessThan(200);
612
- });
613
- await it("should consider equal values as invalid", async () => {
614
- expect(1).not.toBeLessThan(1);
615
- });
616
- await it("should consider smaller values as invalid", async () => {
617
- expect(1).not.toBeLessThan(0);
618
- expect(1).not.toBeLessThan(-5);
619
- });
620
- });
621
- await describe("expect::toBeGreaterThan", async () => {
622
- await it("should consider smaller values as valid", async () => {
623
- expect(2).toBeGreaterThan(1);
624
- expect(2).toBeGreaterThan(0);
625
- expect(2).toBeGreaterThan(-5);
626
- });
627
- await it("should consider equal values as invalid", async () => {
628
- expect(1).not.toBeGreaterThan(1);
629
- });
630
- await it("should consider greater values as invalid", async () => {
631
- expect(1).not.toBeGreaterThan(2);
632
- expect(1).not.toBeGreaterThan(200);
633
- });
634
- });
635
- await describe("expect::toBeCloseTo", async () => {
636
- var pi = 3.1415926, e = 2.78;
637
- await it("should consider close numbers as valid", async () => {
638
- expect(pi).toBeCloseTo(e, 0);
639
- });
640
- await it("should consider non close numbers as invalid", async () => {
641
- expect(pi).not.toBeCloseTo(e, 2);
642
- });
643
- });
644
- await describe("expect::toBeCloseTo", async () => {
645
- function throwException() {
646
- throw {};
647
- }
648
- function dontThrowException() {
649
- }
650
- await it("should consider functions throwing an exception as valid", async () => {
651
- expect(throwException).toThrow();
652
- });
653
- await it("should consider functions not throwing an exception as invalid", async () => {
654
- expect(dontThrowException).not.toThrow();
655
- });
656
- });
657
- };
658
-
659
- // src/spy.spec.ts
660
- var spy_spec_default = async () => {
661
- await describe("'spy' function", async () => {
662
- await it("should return a function.", async () => {
663
- const f = spy();
664
- f();
665
- assert.strictEqual(typeof f, "function");
666
- });
667
- await it("should return a function which calls the given function.", async () => {
668
- let called = false;
669
- const f = spy(() => {
670
- called = true;
671
- });
672
- f();
673
- assert.strictEqual(called, true);
674
- });
675
- await it("should return a function which calls the given method.", async () => {
676
- const box = {
677
- value: 0,
678
- set(value) {
679
- this.value = value;
680
- }
681
- };
682
- box.set = spy(box.set);
683
- box.set(1);
684
- assert.strictEqual(box.value, 1);
685
- });
686
- await it("should return a function which return the return value of the given function.", async () => {
687
- const f = spy(() => 777);
688
- const retv = f();
689
- assert.strictEqual(retv, 777);
690
- });
691
- await it("should return a function which throw the thrown error of the given function.", async () => {
692
- const f = spy(() => {
693
- throw 666;
694
- });
695
- let error = void 0;
696
- try {
697
- f();
698
- } catch (e) {
699
- error = e;
700
- }
701
- assert.strictEqual(error, 666);
702
- });
703
- });
704
- await describe("Spy.calls property", async () => {
705
- await it("should be an array.", async () => {
706
- const f = spy();
707
- assert(Array.isArray(f.calls));
708
- });
709
- await it("should be empty before calling the spy.", async () => {
710
- const f = spy();
711
- assert.strictEqual(f.calls.length, 0);
712
- });
713
- await it("should contain call information after called one time.", async () => {
714
- const f = spy();
715
- f();
716
- assert.strictEqual(f.calls.length, 1);
717
- assert.deepStrictEqual(f.calls[0], {
718
- type: "return",
719
- this: void 0,
720
- arguments: [],
721
- return: void 0
722
- });
723
- });
724
- await it("should contain call information after called one time -- with args.", async () => {
725
- const f = spy();
726
- f.call(1, 2, 3);
727
- assert.strictEqual(f.calls.length, 1);
728
- assert.deepStrictEqual(f.calls[0], {
729
- type: "return",
730
- this: 1,
731
- arguments: [2, 3],
732
- return: void 0
733
- });
734
- });
735
- await it("should contain call information after called one time -- with args and return value.", async () => {
736
- const f = spy(() => -1);
737
- f.call(1, 2, 3);
738
- assert.strictEqual(f.calls.length, 1);
739
- assert.deepStrictEqual(f.calls[0], {
740
- type: "return",
741
- this: 1,
742
- arguments: [2, 3],
743
- return: -1
744
- });
745
- });
746
- await it("should contain call information after called two times -- with args and return value.", async () => {
747
- const f = spy(() => -1);
748
- f.call(1, 2, 3);
749
- f.call(4, 5);
750
- assert.strictEqual(f.calls.length, 2);
751
- assert.deepStrictEqual(f.calls[0], {
752
- type: "return",
753
- this: 1,
754
- arguments: [2, 3],
755
- return: -1
756
- });
757
- assert.deepStrictEqual(f.calls[1], {
758
- type: "return",
759
- this: 4,
760
- arguments: [5],
761
- return: -1
762
- });
763
- });
764
- await it("should contain call information regardless returned or thrown.", async () => {
765
- const f = spy((toThrow) => {
766
- if (toThrow) {
767
- throw -1;
768
- }
769
- return 1;
770
- });
771
- f(false);
772
- try {
773
- f(true);
774
- } catch {
775
- }
776
- assert.strictEqual(f.calls.length, 2);
777
- assert.deepStrictEqual(f.calls[0], {
778
- type: "return",
779
- this: void 0,
780
- arguments: [false],
781
- return: 1
782
- });
783
- assert.deepStrictEqual(f.calls[1], {
784
- type: "throw",
785
- this: void 0,
786
- arguments: [true],
787
- throw: -1
788
- });
789
- });
790
- });
791
- await describe("Spy.firstCall property", async () => {
792
- await it("should be null before calling the spy.", async () => {
793
- const f = spy();
794
- assert.strictEqual(f.firstCall, null);
795
- });
796
- await it("should be 'f.calls[0]' after calling the spy one time.", async () => {
797
- const f = spy();
798
- f();
799
- assert.strictEqual(f.firstCall, f.calls[0]);
800
- });
801
- await it("should be 'f.calls[0]' after calling the spy two times.", async () => {
802
- const f = spy();
803
- f();
804
- f();
805
- assert.strictEqual(f.firstCall, f.calls[0]);
806
- });
807
- });
808
- await describe("Spy.lastCall property", async () => {
809
- await it("should be null before calling the spy.", async () => {
810
- const f = spy();
811
- assert.strictEqual(f.lastCall, null);
812
- });
813
- await it("should be 'f.calls[f.calls.length - 1]' after calling the spy one time.", async () => {
814
- const f = spy();
815
- f();
816
- assert.strictEqual(f.lastCall, f.calls[f.calls.length - 1]);
817
- });
818
- await it("should be 'f.calls[f.calls.length - 1]' after calling the spy two times.", async () => {
819
- const f = spy();
820
- f();
821
- f();
822
- assert.strictEqual(f.lastCall, f.calls[f.calls.length - 1]);
823
- });
824
- });
825
- await describe("Spy.returnedCalls property", async () => {
826
- await it("should be an array.", async () => {
827
- const f = spy();
828
- assert(Array.isArray(f.returnedCalls));
829
- });
830
- await it("should be empty before calling the spy.", async () => {
831
- const f = spy();
832
- assert.strictEqual(f.returnedCalls.length, 0);
833
- });
834
- await it("should contain call information that `call.type === 'return'` in `f.calls` after calling the spy one time.", async () => {
835
- const f = spy();
836
- f();
837
- assert.strictEqual(f.returnedCalls.length, 1);
838
- assert.strictEqual(f.returnedCalls[0], f.calls[0]);
839
- });
840
- await it("should contain call information that `call.type === 'return'` in `f.calls` after calling the spy two times.", async () => {
841
- const f = spy();
842
- f();
843
- f();
844
- assert.strictEqual(f.returnedCalls.length, 2);
845
- assert.strictEqual(f.returnedCalls[0], f.calls[0]);
846
- assert.strictEqual(f.returnedCalls[1], f.calls[1]);
847
- });
848
- await it("should not contain call information that `call.type === 'throw'`.", async () => {
849
- const f = spy((toThrow) => {
850
- if (toThrow) {
851
- throw -1;
852
- }
853
- return 1;
854
- });
855
- f(false);
856
- try {
857
- f(true);
858
- } catch {
859
- }
860
- f(false);
861
- assert.strictEqual(f.calls.length, 3);
862
- assert.strictEqual(f.returnedCalls.length, 2);
863
- assert.strictEqual(f.returnedCalls[0], f.calls[0]);
864
- assert.strictEqual(f.returnedCalls[1], f.calls[2]);
865
- });
866
- await it("should not contain call information that `call.type === 'throw'`. (2)", async () => {
867
- const f = spy((toThrow) => {
868
- if (toThrow) {
869
- throw -1;
870
- }
871
- return 1;
872
- });
873
- try {
874
- f(true);
875
- } catch {
876
- }
877
- f(false);
878
- try {
879
- f(true);
880
- } catch {
881
- }
882
- assert.strictEqual(f.calls.length, 3);
883
- assert.strictEqual(f.returnedCalls.length, 1);
884
- assert.strictEqual(f.returnedCalls[0], f.calls[1]);
885
- });
886
- });
887
- await describe("Spy.firstReturnedCall property", async () => {
888
- await it("should be null before calling the spy.", async () => {
889
- const f = spy();
890
- assert.strictEqual(f.firstReturnedCall, null);
891
- });
892
- await it("should be 'f.returnedCalls[0]' after calling the spy one time.", async () => {
893
- const f = spy();
894
- f();
895
- assert.strictEqual(f.firstReturnedCall, f.returnedCalls[0]);
896
- });
897
- await it("should be 'f.returnedCalls[0]' after calling the spy two times.", async () => {
898
- const f = spy();
899
- f();
900
- f();
901
- assert.strictEqual(f.firstReturnedCall, f.returnedCalls[0]);
902
- });
903
- await it("should be 'f.returnedCalls[0]' after calling the spy even if `f.calls[0]` was thrown.", async () => {
904
- const f = spy((toThrow) => {
905
- if (toThrow) {
906
- throw -1;
907
- }
908
- return 1;
909
- });
910
- try {
911
- f(true);
912
- } catch {
913
- }
914
- f(false);
915
- assert.strictEqual(f.firstReturnedCall, f.returnedCalls[0]);
916
- });
917
- await it("should be null after calling the spy even if all calls were thrown.", async () => {
918
- const f = spy((toThrow) => {
919
- if (toThrow) {
920
- throw -1;
921
- }
922
- return 1;
923
- });
924
- try {
925
- f(true);
926
- } catch {
927
- }
928
- try {
929
- f(true);
930
- } catch {
931
- }
932
- assert.strictEqual(f.firstReturnedCall, null);
933
- });
934
- });
935
- await describe("Spy.lastReturnedCall property", async () => {
936
- await it("should be null before calling the spy.", async () => {
937
- const f = spy();
938
- assert.strictEqual(f.lastReturnedCall, null);
939
- });
940
- await it("should be 'f.returnedCalls[f.returnedCalls.length - 1]' after calling the spy one time.", async () => {
941
- const f = spy();
942
- f();
943
- assert.strictEqual(
944
- f.lastReturnedCall,
945
- f.returnedCalls[f.returnedCalls.length - 1]
946
- );
947
- });
948
- await it("should be 'f.returnedCalls[f.returnedCalls.length - 1]' after calling the spy two times.", async () => {
949
- const f = spy();
950
- f();
951
- f();
952
- assert.strictEqual(
953
- f.lastReturnedCall,
954
- f.returnedCalls[f.returnedCalls.length - 1]
955
- );
956
- });
957
- await it("should be 'f.returnedCalls[f.returnedCalls.length - 1]' after calling the spy even if `f.calls[f.calls.length - 1]` was thrown.", async () => {
958
- const f = spy((toThrow) => {
959
- if (toThrow) {
960
- throw -1;
961
- }
962
- return 1;
963
- });
964
- f(false);
965
- try {
966
- f(true);
967
- } catch {
968
- }
969
- assert.strictEqual(
970
- f.lastReturnedCall,
971
- f.returnedCalls[f.returnedCalls.length - 1]
972
- );
973
- });
974
- await it("should be null after calling the spy even if all calls were thrown.", async () => {
975
- const f = spy((toThrow) => {
976
- if (toThrow) {
977
- throw -1;
978
- }
979
- return 1;
980
- });
981
- try {
982
- f(true);
983
- } catch {
984
- }
985
- try {
986
- f(true);
987
- } catch {
988
- }
989
- assert.strictEqual(f.lastReturnedCall, null);
990
- });
991
- });
992
- await describe("Spy.thrownCalls property", async () => {
993
- await it("should be an array.", async () => {
994
- const f = spy();
995
- assert(Array.isArray(f.thrownCalls));
996
- });
997
- await it("should be empty before calling the spy.", async () => {
998
- const f = spy();
999
- assert.strictEqual(f.thrownCalls.length, 0);
1000
- });
1001
- await it("should contain call information that `call.type === 'throw'` in `f.calls` after calling the spy one time.", async () => {
1002
- const f = spy(() => {
1003
- throw new Error();
1004
- });
1005
- try {
1006
- f();
1007
- } catch {
1008
- }
1009
- assert.strictEqual(f.thrownCalls.length, 1);
1010
- assert.strictEqual(f.thrownCalls[0], f.calls[0]);
1011
- });
1012
- await it("should contain call information that `call.type === 'throw'` in `f.calls` after calling the spy two times.", async () => {
1013
- const f = spy(() => {
1014
- throw new Error();
1015
- });
1016
- try {
1017
- f();
1018
- } catch {
1019
- }
1020
- try {
1021
- f();
1022
- } catch {
1023
- }
1024
- assert.strictEqual(f.thrownCalls.length, 2);
1025
- assert.strictEqual(f.thrownCalls[0], f.calls[0]);
1026
- assert.strictEqual(f.thrownCalls[1], f.calls[1]);
1027
- });
1028
- await it("should not contain call information that `call.type === 'return'`.", async () => {
1029
- const f = spy((toThrow) => {
1030
- if (toThrow) {
1031
- throw -1;
1032
- }
1033
- return 1;
1034
- });
1035
- try {
1036
- f(true);
1037
- } catch {
1038
- }
1039
- f(false);
1040
- try {
1041
- f(true);
1042
- } catch {
1043
- }
1044
- assert.strictEqual(f.calls.length, 3);
1045
- assert.strictEqual(f.thrownCalls.length, 2);
1046
- assert.strictEqual(f.thrownCalls[0], f.calls[0]);
1047
- assert.strictEqual(f.thrownCalls[1], f.calls[2]);
1048
- });
1049
- await it("should not contain call information that `call.type === 'return'`. (2)", async () => {
1050
- const f = spy((toThrow) => {
1051
- if (toThrow) {
1052
- throw -1;
1053
- }
1054
- return 1;
1055
- });
1056
- f(false);
1057
- try {
1058
- f(true);
1059
- } catch {
1060
- }
1061
- f(false);
1062
- assert.strictEqual(f.calls.length, 3);
1063
- assert.strictEqual(f.thrownCalls.length, 1);
1064
- assert.strictEqual(f.thrownCalls[0], f.calls[1]);
1065
- });
1066
- });
1067
- await describe("Spy.firstThrownCall property", async () => {
1068
- await it("should be null before calling the spy.", async () => {
1069
- const f = spy(() => {
1070
- throw new Error();
1071
- });
1072
- assert.strictEqual(f.firstThrownCall, null);
1073
- });
1074
- await it("should be 'f.thrownCalls[0]' after calling the spy one time.", async () => {
1075
- const f = spy(() => {
1076
- throw new Error();
1077
- });
1078
- try {
1079
- f();
1080
- } catch {
1081
- }
1082
- assert.strictEqual(f.firstThrownCall, f.thrownCalls[0]);
1083
- });
1084
- await it("should be 'f.thrownCalls[0]' after calling the spy two times.", async () => {
1085
- const f = spy(() => {
1086
- throw new Error();
1087
- });
1088
- try {
1089
- f();
1090
- } catch {
1091
- }
1092
- try {
1093
- f();
1094
- } catch {
1095
- }
1096
- assert.strictEqual(f.firstThrownCall, f.thrownCalls[0]);
1097
- });
1098
- await it("should be 'f.thrownCalls[0]' after calling the spy even if `f.calls[0]` was returned.", async () => {
1099
- const f = spy((toThrow) => {
1100
- if (toThrow) {
1101
- throw -1;
1102
- }
1103
- return 1;
1104
- });
1105
- f(false);
1106
- try {
1107
- f(true);
1108
- } catch {
1109
- }
1110
- assert.strictEqual(f.firstThrownCall, f.thrownCalls[0]);
1111
- });
1112
- await it("should be null after calling the spy even if all calls were returned.", async () => {
1113
- const f = spy();
1114
- f();
1115
- f();
1116
- assert.strictEqual(f.firstThrownCall, null);
1117
- });
1118
- });
1119
- await describe("Spy.lastThrownCall property", async () => {
1120
- await it("should be null before calling the spy.", async () => {
1121
- const f = spy(() => {
1122
- throw new Error();
1123
- });
1124
- assert.strictEqual(f.lastThrownCall, null);
1125
- });
1126
- await it("should be 'f.thrownCalls[f.thrownCalls.length - 1]' after calling the spy one time.", async () => {
1127
- const f = spy(() => {
1128
- throw new Error();
1129
- });
1130
- try {
1131
- f();
1132
- } catch {
1133
- }
1134
- assert.strictEqual(
1135
- f.lastThrownCall,
1136
- f.thrownCalls[f.thrownCalls.length - 1]
1137
- );
1138
- });
1139
- await it("should be 'f.thrownCalls[f.thrownCalls.length - 1]' after calling the spy two times.", async () => {
1140
- const f = spy(() => {
1141
- throw new Error();
1142
- });
1143
- try {
1144
- f();
1145
- } catch {
1146
- }
1147
- try {
1148
- f();
1149
- } catch {
1150
- }
1151
- assert.strictEqual(
1152
- f.lastThrownCall,
1153
- f.thrownCalls[f.thrownCalls.length - 1]
1154
- );
1155
- });
1156
- await it("should be 'f.thrownCalls[f.thrownCalls.length - 1]' after calling the spy even if `f.calls[f.calls.length - 1]` was returned.", async () => {
1157
- const f = spy((toThrow) => {
1158
- if (toThrow) {
1159
- throw -1;
1160
- }
1161
- return 1;
1162
- });
1163
- try {
1164
- f(true);
1165
- } catch {
1166
- }
1167
- f(false);
1168
- assert.strictEqual(
1169
- f.lastThrownCall,
1170
- f.thrownCalls[f.thrownCalls.length - 1]
1171
- );
1172
- });
1173
- await it("should be null after calling the spy even if all calls were returned.", async () => {
1174
- const f = spy();
1175
- f();
1176
- f();
1177
- assert.strictEqual(f.lastThrownCall, null);
1178
- });
1179
- });
1180
- await describe("Spy.reset method", async () => {
1181
- await it("should be a function.", async () => {
1182
- const f = spy();
1183
- assert.strictEqual(typeof f.reset, "function");
1184
- });
1185
- await it("should do nothing before calling the spy.", async () => {
1186
- const f = spy();
1187
- f.reset();
1188
- assert.strictEqual(f.calls.length, 0);
1189
- });
1190
- await it("should clear `f.calls`.", async () => {
1191
- const f = spy();
1192
- f();
1193
- assert.strictEqual(f.calls.length, 1);
1194
- f.reset();
1195
- assert.strictEqual(f.calls.length, 0);
1196
- assert.strictEqual(f.calls[0], void 0);
1197
- });
1198
- });
1199
- await describe("Spy.toString method", async () => {
1200
- await it("should be a function.", async () => {
1201
- const f = spy();
1202
- assert.strictEqual(typeof f.toString, "function");
1203
- });
1204
- await it("should return the original function with a comment. (noop)", async () => {
1205
- const f = spy();
1206
- assert.strictEqual(f.toString(), "/* The spy of */ function(){}");
1207
- });
1208
- await it("should return the original function with a comment. (with f)", async () => {
1209
- const f0 = function original() {
1210
- return 777;
1211
- };
1212
- const f = spy(f0);
1213
- assert.strictEqual(f.toString(), `/* The spy of */ ${f0}`);
1214
- });
1215
- });
1216
- };
1217
-
1218
- // src/test.mts
1219
- run({ indexTestSuite: index_spec_default, spyTestSuite: spy_spec_default });