mirror of
https://github.com/tc39/test262.git
synced 2025-07-27 07:54:41 +02:00
Add assert.throws.early
This commit is contained in:
parent
0ad3d51fd9
commit
666f0e920a
@ -159,7 +159,8 @@ $DONE(arg) | see Writing Asynchronous Tests, below
|
|||||||
assert(value, message) | throw a new Test262Error instance if the specified value is not strictly equal to the JavaScript `true` value; accepts an optional string message for use in creating the error
|
assert(value, message) | throw a new Test262Error instance if the specified value is not strictly equal to the JavaScript `true` value; accepts an optional string message for use in creating the error
|
||||||
assert.sameValue(actual, expected, message) | throw a new Test262Error instance if the first two arguments are not [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message for use in creating the error
|
assert.sameValue(actual, expected, message) | throw a new Test262Error instance if the first two arguments are not [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message for use in creating the error
|
||||||
assert.notSameValue(actual, unexpected, message) | throw a new Test262Error instance if the first two arguments are [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message for use in creating the error
|
assert.notSameValue(actual, unexpected, message) | throw a new Test262Error instance if the first two arguments are [the same value](https://tc39.github.io/ecma262/#sec-samevalue); accepts an optional string message for use in creating the error
|
||||||
assert.throws(expectedErrorConstructor, fn) | throw a new Test262Error instance if the provided function does not throw an error, or if the constructor of the value thrown does not match the provided constructor
|
assert.throws(expectedErrorConstructor, fn, message) | throw a new Test262Error instance if the provided function does not throw an error, or if the constructor of the value thrown does not match the provided constructor
|
||||||
|
assert.throws.early(expectedErrorConstructor, fn) | throw a new Test262Error instance if the provided function does not throw an early error, or if the constructor of the value thrown does not match the provided constructor. This assertion catches only errors that will be parsed through `Function(code)`.
|
||||||
|
|
||||||
```
|
```
|
||||||
/// error class
|
/// error class
|
||||||
|
@ -79,3 +79,10 @@ assert.throws = function (expectedErrorConstructor, func, message) {
|
|||||||
message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
|
message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
|
||||||
$ERROR(message);
|
$ERROR(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert.throws.early = function(err, code) {
|
||||||
|
let wrappedCode = `function wrapperFn() { ${code} }`;
|
||||||
|
let ieval = eval;
|
||||||
|
|
||||||
|
assert.throws(err, () => { Function(wrappedCode); }, `Function: ${code}`);
|
||||||
|
};
|
||||||
|
20
test/harness/assert-throws-early-incorrect-ctor.js
Normal file
20
test/harness/assert-throws-early-incorrect-ctor.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2017 Mozilla Corporation. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
Functions that throw values whose constructor does not match the specified
|
||||||
|
constructor do not satisfy the assertion.
|
||||||
|
---*/
|
||||||
|
|
||||||
|
// monkeypatch the API
|
||||||
|
$ERROR = function $ERROR(message) {
|
||||||
|
throw new Test262Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
assert.throws.early(SyntaxError, "1 = 1;");
|
||||||
|
}, "'1=1' is a ReferenceError");
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
assert.throws.early(ReferenceError, "var;");
|
||||||
|
}, "'var;' is a SyntaxError");
|
16
test/harness/assert-throws-early-not-early.js
Normal file
16
test/harness/assert-throws-early-not-early.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright (C) 2017 Mozilla Corporation. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
The assertion fails when the code does not parse with an early error
|
||||||
|
---*/
|
||||||
|
|
||||||
|
// monkeypatch the API
|
||||||
|
$ERROR = function $ERROR(message) {
|
||||||
|
throw new Test262Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
assert.throws.early(ReferenceError, 'x = 1');
|
||||||
|
});
|
9
test/harness/assert-throws-early-referenceerror.js
Normal file
9
test/harness/assert-throws-early-referenceerror.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// Copyright (C) 2017 Mozilla Corporation. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
The assertion pass when the code parses with an early ReferenceError
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.throws.early(ReferenceError, '1 = 1;');
|
9
test/harness/assert-throws-early-syntaxerror.js
Normal file
9
test/harness/assert-throws-early-syntaxerror.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// Copyright (C) 2017 Mozilla Corporation. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
The assertion pass when the code parses with an early SyntaxError
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.throws.early(SyntaxError, 'let let');
|
Loading…
x
Reference in New Issue
Block a user