Add assert.throws.early

This commit is contained in:
Leo Balter 2017-05-01 12:29:52 -04:00
parent 0ad3d51fd9
commit 666f0e920a
No known key found for this signature in database
GPG Key ID: 2C75F319D398E36B
6 changed files with 63 additions and 1 deletions

View File

@ -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.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.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

View File

@ -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';
$ERROR(message);
};
assert.throws.early = function(err, code) {
let wrappedCode = `function wrapperFn() { ${code} }`;
let ieval = eval;
assert.throws(err, () => { Function(wrappedCode); }, `Function: ${code}`);
};

View 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");

View 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');
});

View 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;');

View 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');