|
| 1 | +//------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) Microsoft Corporation and contributors. All rights reserved. |
| 3 | +// Copyright (c) 2021 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 | +WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js"); |
| 8 | + |
| 9 | + |
| 10 | +function makeTestsFor(ErrorConstructor) { |
| 11 | + const name = ErrorConstructor.name; |
| 12 | + const message = "Test message"; |
| 13 | + const o = {}; |
| 14 | + return [ |
| 15 | + { |
| 16 | + name: `"cause" in ${ name }.prototype.cause === false`, |
| 17 | + body: function () { |
| 18 | + assert.isFalse("cause" in ErrorConstructor.prototype, `Cause property must not exist in ${ name }.prototype`); |
| 19 | + } |
| 20 | + }, |
| 21 | + { |
| 22 | + name: `"cause" in ${ name }().cause === false`, |
| 23 | + body: function () { |
| 24 | + assert.isFalse("cause" in ErrorConstructor(), `${ name }().cause should not be defined if not specified by cause property of options parameter in ${ name } constructor`); |
| 25 | + } |
| 26 | + }, |
| 27 | + { |
| 28 | + name: `message property is defined when ${ name } called with one argument, not cause`, |
| 29 | + body: function () { |
| 30 | + assert.isTrue("message" in ErrorConstructor(message), `message property is defined when ${ name } called with one argument, not cause`); |
| 31 | + assert.isTrue(ErrorConstructor(message).message === message, `message property is defined when ${ name } called with one argument, not cause`); |
| 32 | + assert.isFalse("cause" in ErrorConstructor(message), `message property is defined when ${ name } called with one argument, not cause`); |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + name: `${ name }(${ message }, { cause: o })'s descriptor`, |
| 37 | + body: function () { |
| 38 | + const e = ErrorConstructor(message, { cause: o }); |
| 39 | + const desc = Object.getOwnPropertyDescriptor(e, "cause"); |
| 40 | + assert.areEqual(desc.configurable, true, "e.cause should be configurable"); |
| 41 | + assert.areEqual(desc.writable, true, "e.cause should be writable"); |
| 42 | + assert.areEqual(desc.enumerable, false, "e.cause should not be enumerable"); |
| 43 | + } |
| 44 | + }, |
| 45 | + { |
| 46 | + name: `o === ${ name }(${ message }, { cause: o }).cause`, |
| 47 | + body: function () { |
| 48 | + const e = Error(); |
| 49 | + assert.areEqual(ErrorConstructor("", { cause: o }).cause, o, `Cause property value should be kept as-is`); |
| 50 | + assert.areEqual(ErrorConstructor("", { cause: 0 }).cause, 0, `Cause property value should be kept as-is`); |
| 51 | + assert.areEqual(ErrorConstructor("", { cause: e }).cause, e, `Cause property value should be kept as-is`); |
| 52 | + assert.areEqual(ErrorConstructor("", { cause: "A cause" }).cause, "A cause", `Cause property value should be kept as-is`); |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Options with cause property as getter", |
| 57 | + body: function () { |
| 58 | + var getCounter = 0; |
| 59 | + const options = { |
| 60 | + get cause() { |
| 61 | + getCounter++; |
| 62 | + return o; |
| 63 | + } |
| 64 | + } |
| 65 | + ErrorConstructor(message, options); |
| 66 | + assert.areEqual(getCounter, 1, `getCounter should be 1`); |
| 67 | + } |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Options with cause property as getter which throws", |
| 71 | + body: function () { |
| 72 | + const options = { |
| 73 | + get cause() { |
| 74 | + throw ErrorConstructor(); |
| 75 | + } |
| 76 | + } |
| 77 | + assert.throws(() => ErrorConstructor(message, options), ErrorConstructor); |
| 78 | + } |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "Proxy options parameter", |
| 82 | + body: function () { |
| 83 | + const options = new Proxy({ cause: o }, { |
| 84 | + has(target, p) { |
| 85 | + hasCounter++; |
| 86 | + return p in target; |
| 87 | + }, |
| 88 | + get(target, p) { |
| 89 | + getCounter++; |
| 90 | + return target[p]; |
| 91 | + } |
| 92 | + }); |
| 93 | + var hasCounter = 0, getCounter = 0; |
| 94 | + const e = ErrorConstructor("test", options); |
| 95 | + assert.areEqual(hasCounter, 1, `hasCounter should be 1`); |
| 96 | + assert.areEqual(getCounter, 1, `getCounter should be 1`); |
| 97 | + assert.areEqual(e.cause, o, `Cause property value should be kept as-is`); |
| 98 | + assert.areEqual(hasCounter, 1, `hasCounter should be 1`); |
| 99 | + assert.areEqual(getCounter, 1, `getCounter should be 1`); |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Cause property is not added to error if options parameter doesn't have the cause property", |
| 104 | + body: function () { |
| 105 | + assert.isFalse('cause' in ErrorConstructor(message, { }), `Cause property must not be added to error if options parameter doesn't have the cause property`); |
| 106 | + } |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "Cause property is not added to error if options parameter isn't typeof object", |
| 110 | + body: function () { |
| 111 | + Number.prototype.cause = 0; |
| 112 | + String.prototype.cause = 0; |
| 113 | + assert.isFalse('cause' in ErrorConstructor(message, 0), `Cause property must not be added to error if options parameter isn't typeof object`); |
| 114 | + assert.isFalse('cause' in ErrorConstructor(message, ""), `Cause property must not be added to error if options parameter isn't typeof object`); |
| 115 | + } |
| 116 | + } |
| 117 | + ] |
| 118 | +} |
| 119 | +const tests = [ |
| 120 | + ...makeTestsFor(Error), |
| 121 | + ...makeTestsFor(TypeError), |
| 122 | + ...makeTestsFor(ReferenceError), |
| 123 | + ...makeTestsFor(SyntaxError), |
| 124 | + ...makeTestsFor(RangeError), |
| 125 | + ...makeTestsFor(EvalError), |
| 126 | + // TODO: Uncomment when #6301 is landed |
| 127 | + // ...makeTestsFor(AggregateError) |
| 128 | +]; |
| 129 | + |
| 130 | +testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" }); |
0 commit comments