|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 4 | +//------------------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js"); |
| 7 | + |
| 8 | +//setup code |
| 9 | +let constructionCount = 0; |
| 10 | +let functionCallCount = 0; |
| 11 | +function a(arg1, arg2) { |
| 12 | + this[arg1] = arg2; |
| 13 | + ++functionCallCount; |
| 14 | +} |
| 15 | + |
| 16 | +const trapped = new Proxy(a, { |
| 17 | + construct: function (x, y, z) { |
| 18 | + ++constructionCount; |
| 19 | + return Reflect.construct(x, y, z); |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +const noTrap = new Proxy(a, {}); |
| 24 | + |
| 25 | +const boundObject = {}; |
| 26 | + |
| 27 | +const boundFunc = a.bind(boundObject, "prop-name"); |
| 28 | + |
| 29 | +const boundTrapped = trapped.bind(boundObject, "prop-name"); |
| 30 | +const boundUnTrapped = noTrap.bind(boundObject, "prop-name"); |
| 31 | + |
| 32 | +const tests = [ |
| 33 | + { |
| 34 | + name : "Construct trapped bound proxy with new", |
| 35 | + body : function() { |
| 36 | + constructionCount = 0; |
| 37 | + functionCallCount = 0; |
| 38 | + const obj = new boundTrapped("prop-value"); |
| 39 | + assert.areNotEqual(boundObject, obj, "bound function should ignore bound this when constructing"); |
| 40 | + assert.areEqual("prop-value", obj["prop-name"], "bound function should keep bound arguments when constructing"); |
| 41 | + assert.areEqual(1, constructionCount, "bound proxy should be constructed once"); |
| 42 | + assert.areEqual(1, functionCallCount, "bound proxy function should be called once"); |
| 43 | + assert.strictEqual(a.prototype, obj.__proto__, "constructed object should be instance of original function"); |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + name : "Construct trapped bound proxy with Reflect", |
| 48 | + body : function() { |
| 49 | + class newTarget {} |
| 50 | + constructionCount = 0; |
| 51 | + functionCallCount = 0; |
| 52 | + const obj = Reflect.construct(boundTrapped, ["prop-value-2"], newTarget); |
| 53 | + assert.areNotEqual(boundObject, obj, "bound function should ignore bound this when constructing"); |
| 54 | + assert.strictEqual(newTarget.prototype, obj.__proto__, "bound function should use explicit newTarget if provided"); |
| 55 | + assert.areEqual("prop-value-2", obj["prop-name"], "bound function should keep bound arguments when constructing"); |
| 56 | + assert.areEqual(1, constructionCount, "bound proxy should be constructed once"); |
| 57 | + assert.areEqual(1, functionCallCount, "bound proxy function should be called once"); |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + name : "Construct bound proxy with new", |
| 62 | + body : function() { |
| 63 | + functionCallCount = 0; |
| 64 | + const obj = new boundUnTrapped("prop-value"); |
| 65 | + assert.areNotEqual(boundObject, obj, "bound function should ignore bound this when constructing"); |
| 66 | + assert.areEqual("prop-value", obj["prop-name"], "bound function should keep bound arguments when constructing"); |
| 67 | + assert.areEqual(1, functionCallCount, "bound proxy function should be called once"); |
| 68 | + assert.strictEqual(a.prototype, obj.__proto__, "constructed object should be instance of original function"); |
| 69 | + } |
| 70 | + }, |
| 71 | + { |
| 72 | + name : "Construct bound proxy with Reflect", |
| 73 | + body : function() { |
| 74 | + class newTarget {} |
| 75 | + functionCallCount = 0; |
| 76 | + const obj = Reflect.construct(boundUnTrapped, ["prop-value-2"], newTarget); |
| 77 | + assert.areNotEqual(boundObject, obj, "bound function should ignore bound this when constructing"); |
| 78 | + assert.strictEqual(newTarget.prototype, obj.__proto__, "bound function should use explicit newTarget if provided"); |
| 79 | + assert.areEqual("prop-value-2", obj["prop-name"], "bound function should keep bound arguments when constructing"); |
| 80 | + assert.areEqual(1, functionCallCount, "bound proxy function should be called once"); |
| 81 | + } |
| 82 | + }, |
| 83 | + { |
| 84 | + name : "Construct bound function with Reflect", |
| 85 | + body : function() { |
| 86 | + class newTarget {} |
| 87 | + functionCallCount = 0; |
| 88 | + const obj = Reflect.construct(boundFunc, ["prop-value-2"], newTarget); |
| 89 | + assert.areNotEqual(boundObject, obj, "bound function should ignore bound this when constructing"); |
| 90 | + assert.strictEqual(newTarget.prototype, obj.__proto__, "bound function should use explicit newTarget if provided"); |
| 91 | + assert.areEqual("prop-value-2", obj["prop-name"], "bound function should keep bound arguments when constructing"); |
| 92 | + assert.areEqual(1, functionCallCount, "bound function should be called once"); |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + name : "Construct bound function with new", |
| 97 | + body : function() { |
| 98 | + functionCallCount = 0; |
| 99 | + const obj = new boundFunc("prop-value-2"); |
| 100 | + assert.areNotEqual(boundObject, obj, "bound function should ignore bound this when constructing"); |
| 101 | + assert.strictEqual(a.prototype, obj.__proto__, "bound function should use explicit newTarget if provided"); |
| 102 | + assert.areEqual("prop-value-2", obj["prop-name"], "bound function should keep bound arguments when constructing"); |
| 103 | + assert.areEqual(1, functionCallCount, "bound function should be called once"); |
| 104 | + } |
| 105 | + } |
| 106 | +]; |
| 107 | + |
| 108 | +testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" }); |
0 commit comments