mirror of
https://github.com/tc39/test262.git
synced 2025-05-31 04:00:34 +02:00
Add identity tests for the assert.throws
This commit is contained in:
parent
16dae73171
commit
61bd4e9453
@ -66,6 +66,7 @@ assert.notSameValue = function (actual, unexpected, message) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
assert.throws = function (expectedErrorConstructor, func, message) {
|
assert.throws = function (expectedErrorConstructor, func, message) {
|
||||||
|
var expectedName, actualName;
|
||||||
if (typeof func !== "function") {
|
if (typeof func !== "function") {
|
||||||
throw new Test262Error('assert.throws requires two arguments: the error constructor ' +
|
throw new Test262Error('assert.throws requires two arguments: the error constructor ' +
|
||||||
'and a function to run');
|
'and a function to run');
|
||||||
@ -84,7 +85,13 @@ assert.throws = function (expectedErrorConstructor, func, message) {
|
|||||||
message += 'Thrown value was not an object!';
|
message += 'Thrown value was not an object!';
|
||||||
throw new Test262Error(message);
|
throw new Test262Error(message);
|
||||||
} else if (thrown.constructor !== expectedErrorConstructor) {
|
} else if (thrown.constructor !== expectedErrorConstructor) {
|
||||||
message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
|
expectedName = expectedErrorConstructor.name;
|
||||||
|
actualName = thrown.constructor.name;
|
||||||
|
if (expectedName === actualName) {
|
||||||
|
message += 'Expected a ' + expectedName + ' but got a different error constructor with the same name';
|
||||||
|
} else {
|
||||||
|
message += 'Expected a ' + expectedName + ' but got a ' + actualName;
|
||||||
|
}
|
||||||
throw new Test262Error(message);
|
throw new Test262Error(message);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
58
test/harness/assert-throws-custom-typeerror.js
Normal file
58
test/harness/assert-throws-custom-typeerror.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// Copyright (C) 2021 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
Functions that throw instances of the specified constructor function
|
||||||
|
satisfy the assertion, without collision with error constructors of the
|
||||||
|
same name.
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var intrinsicTypeError = TypeError;
|
||||||
|
var threw = false;
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
function TypeError() {}
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
throw new TypeError();
|
||||||
|
}, 'Throws an instance of the matching custom TypeError');
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.throws(intrinsicTypeError, function() {
|
||||||
|
throw new TypeError();
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
threw = true;
|
||||||
|
if (err.constructor !== Test262Error) {
|
||||||
|
throw new Error(
|
||||||
|
'Expected a Test262Error but a "' + err.constructor.name +
|
||||||
|
'" was thrown.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threw === false) {
|
||||||
|
throw new Error('Expected a Test262Error, but no error was thrown.');
|
||||||
|
}
|
||||||
|
|
||||||
|
threw = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
throw new intrinsicTypeError();
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
threw = true;
|
||||||
|
if (err.constructor !== Test262Error) {
|
||||||
|
throw new Error(
|
||||||
|
'Expected a Test262Error but a "' + err.constructor.name +
|
||||||
|
'" was thrown.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threw === false) {
|
||||||
|
throw new Error('Expected a Test262Error, but no error was thrown.');
|
||||||
|
}
|
||||||
|
})();
|
30
test/harness/assert-throws-same-realm.js
Normal file
30
test/harness/assert-throws-same-realm.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (C) 2021 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
Functions that throw instances of the realm specified constructor function
|
||||||
|
satisfy the assertion, without cross realms collisions.
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var intrinsicTypeError = TypeError;
|
||||||
|
var threw = false;
|
||||||
|
var realmGlobal = $262.createRealm().global;
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
throw new realmGlobal.TypeError();
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
threw = true;
|
||||||
|
if (err.constructor !== Test262Error) {
|
||||||
|
throw new Error(
|
||||||
|
'Expected a Test262Error but a "' + err.constructor.name +
|
||||||
|
'" was thrown.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threw === false) {
|
||||||
|
throw new Error('Expected a Test262Error, but no error was thrown.');
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user