|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft. All rights reserved. |
| 3 | +// Copyright (c) ChakraCore Project Contributors. All rights reserved. |
| 4 | +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. |
| 5 | +//------------------------------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +// @ts-check |
| 8 | +/// <reference path="../UnitTestFramework/UnitTestFramework.js" /> |
| 9 | + |
| 10 | +WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js"); |
| 11 | + |
| 12 | +const simpleObj = { "null": null, "undefined": undefined, something: 42 }; |
| 13 | +Object.freeze(simpleObj); |
| 14 | + |
| 15 | +const tests = [ |
| 16 | + { |
| 17 | + name: "Simple method call on property", |
| 18 | + body() { |
| 19 | + // Verify normal behavior |
| 20 | + assert.throws(() => simpleObj.nothing(), TypeError); |
| 21 | + assert.throws(() => simpleObj.null(), TypeError); |
| 22 | + assert.throws(() => simpleObj.undefined(), TypeError); |
| 23 | + |
| 24 | + // With optional-chains |
| 25 | + assert.isUndefined(simpleObj.nothing?.(), "OptChain should evaluated to 'undefined'"); |
| 26 | + assert.isUndefined(simpleObj.null?.(), "OptChain should evaluated to 'undefined'"); |
| 27 | + assert.isUndefined(simpleObj.undefined?.(), "OptChain should evaluated to 'undefined'"); |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "Simple method call on indexer", |
| 32 | + body() { |
| 33 | + // Verify normal behavior |
| 34 | + assert.throws(() => simpleObj["nothing"](), TypeError); |
| 35 | + assert.throws(() => simpleObj["null"](), TypeError); |
| 36 | + assert.throws(() => simpleObj["undefined"](), TypeError); |
| 37 | + |
| 38 | + // With optional-chains |
| 39 | + assert.isUndefined(simpleObj["nothing"]?.(), "OptChain should evaluated to 'undefined'"); |
| 40 | + assert.isUndefined(simpleObj["null"]?.(), "OptChain should evaluated to 'undefined'"); |
| 41 | + assert.isUndefined(simpleObj["undefined"]?.(), "OptChain should evaluated to 'undefined'"); |
| 42 | + } |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Simple method call on non-callable property", |
| 46 | + body() { |
| 47 | + // Verify normal behavior |
| 48 | + assert.throws(() => simpleObj.something(), TypeError, "Non-callable prop", "Function expected"); |
| 49 | + |
| 50 | + // With optional-chains |
| 51 | + assert.throws(() => simpleObj.something?.(), TypeError, "Non-callable prop", "Function expected"); |
| 52 | + assert.throws(() => simpleObj?.something(), TypeError, "Non-callable prop", "Function expected"); |
| 53 | + assert.throws(() => simpleObj?.something?.(), TypeError, "Non-callable prop", "Function expected"); |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Simple method call on non-callable indexer", |
| 58 | + body() { |
| 59 | + // Verify normal behavior |
| 60 | + assert.throws(() => simpleObj["something"](), TypeError, "Non-callable prop", "Function expected"); |
| 61 | + |
| 62 | + // With optional-chains |
| 63 | + assert.throws(() => simpleObj["something"]?.(), TypeError, "Non-callable prop", "Function expected"); |
| 64 | + assert.throws(() => simpleObj?.["something"](), TypeError, "Non-callable prop", "Function expected"); |
| 65 | + assert.throws(() => simpleObj?.["something"]?.(), TypeError, "Non-callable prop", "Function expected"); |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Optional properties before call", |
| 70 | + body() { |
| 71 | + // Verify normal behavior |
| 72 | + assert.throws(() => simpleObj.nothing.something(), TypeError); |
| 73 | + assert.throws(() => simpleObj.null.something(), TypeError); |
| 74 | + assert.throws(() => simpleObj.undefined.something(), TypeError); |
| 75 | + |
| 76 | + // With optional-chains |
| 77 | + assert.isUndefined(simpleObj.nothing?.something(), "OptChain should evaluated to 'undefined'"); |
| 78 | + assert.isUndefined(simpleObj.null?.something(), "OptChain should evaluated to 'undefined'"); |
| 79 | + assert.isUndefined(simpleObj.undefined?.something(), "OptChain should evaluated to 'undefined'"); |
| 80 | + } |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Optional indexers before call", |
| 84 | + body() { |
| 85 | + // Verify normal behavior |
| 86 | + assert.throws(() => simpleObj.nothing["something"](), TypeError); |
| 87 | + assert.throws(() => simpleObj.null["something"](), TypeError); |
| 88 | + assert.throws(() => simpleObj.undefined["something"](), TypeError); |
| 89 | + |
| 90 | + // With optional-chains |
| 91 | + assert.isUndefined(simpleObj.nothing?.["something"](), "OptChain should evaluated to 'undefined'"); |
| 92 | + assert.isUndefined(simpleObj.null?.["something"](), "OptChain should evaluated to 'undefined'"); |
| 93 | + assert.isUndefined(simpleObj.undefined?.["something"](), "OptChain should evaluated to 'undefined'"); |
| 94 | + } |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Propagate 'this' correctly", |
| 98 | + body() { |
| 99 | + const specialObj = { |
| 100 | + b() { return this._b; }, |
| 101 | + _b: { c: 42 } |
| 102 | + }; |
| 103 | + |
| 104 | + assert.areEqual(42, specialObj.b().c); |
| 105 | + assert.areEqual(42, specialObj?.b().c); |
| 106 | + assert.areEqual(42, specialObj.b?.().c); |
| 107 | + assert.areEqual(42, specialObj?.b?.().c); |
| 108 | + assert.areEqual(42, (specialObj?.b)().c); |
| 109 | + assert.areEqual(42, (specialObj.b)?.().c); |
| 110 | + assert.areEqual(42, (specialObj?.b)?.().c); |
| 111 | + } |
| 112 | + } |
| 113 | +]; |
| 114 | + |
| 115 | +testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" }); |
0 commit comments