Add identity tests for the assert.throws

This commit is contained in:
Leo Balter 2021-09-21 13:21:37 -07:00 committed by Rick Waldron
parent 16dae73171
commit 61bd4e9453
3 changed files with 96 additions and 1 deletions

View File

@ -66,6 +66,7 @@ assert.notSameValue = function (actual, unexpected, message) {
};
assert.throws = function (expectedErrorConstructor, func, message) {
var expectedName, actualName;
if (typeof func !== "function") {
throw new Test262Error('assert.throws requires two arguments: the error constructor ' +
'and a function to run');
@ -84,7 +85,13 @@ assert.throws = function (expectedErrorConstructor, func, message) {
message += 'Thrown value was not an object!';
throw new Test262Error(message);
} 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);
}
return;

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

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