| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ const { | |||
| 4 | 4 | ArrayPrototypePush, | |
| 5 | 5 | Error, | |
| 6 | 6 | FunctionPrototype, | |
| 7 | + Proxy, | ||
| 7 | 8 | ReflectApply, | |
| 8 | 9 | SafeSet, | |
| 9 | 10 | } = primordials; | |
@@ -46,20 +47,23 @@ class CallTracker { | |||
| 46 | 47 | const callChecks = this.#callChecks; | |
| 47 | 48 | callChecks.add(context); | |
| 48 | 49 | ||
| 49 | - return function() { | ||
| 50 | - context.actual++; | ||
| 51 | - if (context.actual === context.exact) { | ||
| 52 | - // Once function has reached its call count remove it from | ||
| 53 | - // callChecks set to prevent memory leaks. | ||
| 54 | - callChecks.delete(context); | ||
| 55 | - } | ||
| 56 | - // If function has been called more than expected times, add back into | ||
| 57 | - // callchecks. | ||
| 58 | - if (context.actual === context.exact + 1) { | ||
| 59 | - callChecks.add(context); | ||
| 60 | - } | ||
| 61 | - return ReflectApply(fn, this, arguments); | ||
| 62 | - }; | ||
| 50 | + return new Proxy(fn, { | ||
| 51 | + __proto__: null, | ||
| 52 | + apply(fn, thisArg, argList) { | ||
| 53 | + context.actual++; | ||
| 54 | + if (context.actual === context.exact) { | ||
| 55 | + // Once function has reached its call count remove it from | ||
| 56 | + // callChecks set to prevent memory leaks. | ||
| 57 | + callChecks.delete(context); | ||
| 58 | + } | ||
| 59 | + // If function has been called more than expected times, add back into | ||
| 60 | + // callchecks. | ||
| 61 | + if (context.actual === context.exact + 1) { | ||
| 62 | + callChecks.add(context); | ||
| 63 | + } | ||
| 64 | + return ReflectApply(fn, thisArg, argList); | ||
| 65 | + }, | ||
| 66 | + }); | ||
| 63 | 67 | } | |
| 64 | 68 | ||
| 65 | 69 | report() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | - require('../common'); | ||
| 2 | + const common = require('../common'); | ||
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | ||
| 5 | 5 | // This test ensures that assert.CallTracker.calls() works as intended. | |
@@ -78,3 +78,51 @@ assert.throws( | |||
| 78 | 78 | callsNoop(); | |
| 79 | 79 | tracker.verify(); | |
| 80 | 80 | } | |
| 81 | + | ||
| 82 | + { | ||
| 83 | + function func() {} | ||
| 84 | + const tracker = new assert.CallTracker(); | ||
| 85 | + const callsfunc = tracker.calls(func); | ||
| 86 | + assert.strictEqual(callsfunc.length, 0); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + { | ||
| 90 | + function func(a, b, c = 2) {} | ||
| 91 | + const tracker = new assert.CallTracker(); | ||
| 92 | + const callsfunc = tracker.calls(func); | ||
| 93 | + assert.strictEqual(callsfunc.length, 2); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + { | ||
| 97 | + function func(a, b, c = 2) {} | ||
| 98 | + delete func.length; | ||
| 99 | + const tracker = new assert.CallTracker(); | ||
| 100 | + const callsfunc = tracker.calls(func); | ||
| 101 | + assert.strictEqual(Object.hasOwn(callsfunc, 'length'), false); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + { | ||
| 105 | + const ArrayIteratorPrototype = Reflect.getPrototypeOf( | ||
| 106 | + Array.prototype.values() | ||
| 107 | + ); | ||
| 108 | + const { next } = ArrayIteratorPrototype; | ||
| 109 | + ArrayIteratorPrototype.next = common.mustNotCall( | ||
| 110 | + '%ArrayIteratorPrototype%.next' | ||
| 111 | + ); | ||
| 112 | + Object.prototype.get = common.mustNotCall('%Object.prototype%.get'); | ||
| 113 | + | ||
| 114 | + const customPropertyValue = Symbol(); | ||
| 115 | + function func(a, b, c = 2) { | ||
| 116 | + return a + b + c; | ||
| 117 | + } | ||
| 118 | + func.customProperty = customPropertyValue; | ||
| 119 | + Object.defineProperty(func, 'length', { get: common.mustNotCall() }); | ||
| 120 | + const tracker = new assert.CallTracker(); | ||
| 121 | + const callsfunc = tracker.calls(func); | ||
| 122 | + assert.strictEqual(Object.hasOwn(callsfunc, 'length'), true); | ||
| 123 | + assert.strictEqual(callsfunc.customProperty, customPropertyValue); | ||
| 124 | + assert.strictEqual(callsfunc(1, 2, 3), 6); | ||
| 125 | + | ||
| 126 | + ArrayIteratorPrototype.next = next; | ||
| 127 | + delete Object.prototype.get; | ||
| 128 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments