2017-07-14 17:37:24 +02:00
|
|
|
// Copyright (C) 2017 Ecma International. All rights reserved.
|
|
|
|
// This code is governed by the BSD license found in the LICENSE file.
|
|
|
|
/*---
|
|
|
|
description: |
|
|
|
|
Collection of assertion functions used throughout test262
|
2019-09-25 02:22:26 +02:00
|
|
|
defines: [assert]
|
2017-07-14 17:37:24 +02:00
|
|
|
---*/
|
|
|
|
|
2019-09-18 18:39:12 +02:00
|
|
|
|
2014-12-01 21:37:39 +01:00
|
|
|
function assert(mustBeTrue, message) {
|
2017-04-13 16:37:32 +02:00
|
|
|
if (mustBeTrue === true) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-01 21:37:39 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
if (message === undefined) {
|
2019-09-18 18:25:38 +02:00
|
|
|
message = 'Expected true but got ' + assert._toString(mustBeTrue);
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message);
|
2014-12-01 21:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert._isSameValue = function (a, b) {
|
2017-04-13 16:37:32 +02:00
|
|
|
if (a === b) {
|
|
|
|
// Handle +/-0 vs. -/+0
|
|
|
|
return a !== 0 || 1 / a === 1 / b;
|
|
|
|
}
|
2014-12-01 21:37:39 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
// Handle NaN vs. NaN
|
|
|
|
return a !== a && b !== b;
|
2014-12-01 21:37:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.sameValue = function (actual, expected, message) {
|
2018-10-23 23:25:58 +02:00
|
|
|
try {
|
|
|
|
if (assert._isSameValue(actual, expected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message + ' (_isSameValue operation threw) ' + error);
|
2017-04-13 16:37:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-12-01 21:37:39 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
message = '';
|
|
|
|
} else {
|
|
|
|
message += ' ';
|
|
|
|
}
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2019-09-18 18:25:38 +02:00
|
|
|
message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(expected) + '») to be true';
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message);
|
2014-12-01 21:37:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.notSameValue = function (actual, unexpected, message) {
|
2017-04-13 16:37:32 +02:00
|
|
|
if (!assert._isSameValue(actual, unexpected)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-01 21:37:39 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
message = '';
|
|
|
|
} else {
|
|
|
|
message += ' ';
|
|
|
|
}
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2019-09-18 18:25:38 +02:00
|
|
|
message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(unexpected) + '») to be false';
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message);
|
2014-12-01 21:37:39 +01:00
|
|
|
};
|
2014-12-04 00:29:52 +01:00
|
|
|
|
2015-07-08 17:19:59 +02:00
|
|
|
assert.throws = function (expectedErrorConstructor, func, message) {
|
2021-09-21 22:21:37 +02:00
|
|
|
var expectedName, actualName;
|
2017-04-13 16:37:32 +02:00
|
|
|
if (typeof func !== "function") {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error('assert.throws requires two arguments: the error constructor ' +
|
2017-04-13 16:37:32 +02:00
|
|
|
'and a function to run');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (message === undefined) {
|
|
|
|
message = '';
|
|
|
|
} else {
|
|
|
|
message += ' ';
|
|
|
|
}
|
2014-12-04 00:29:52 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
try {
|
|
|
|
func();
|
|
|
|
} catch (thrown) {
|
|
|
|
if (typeof thrown !== 'object' || thrown === null) {
|
|
|
|
message += 'Thrown value was not an object!';
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message);
|
2017-04-13 16:37:32 +02:00
|
|
|
} else if (thrown.constructor !== expectedErrorConstructor) {
|
2021-09-21 22:21:37 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message);
|
2014-12-04 00:29:52 +01:00
|
|
|
}
|
2017-04-13 16:37:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-12-04 00:29:52 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error(message);
|
2014-12-04 00:29:52 +01:00
|
|
|
};
|
2019-09-18 18:25:38 +02:00
|
|
|
|
|
|
|
assert._toString = function (value) {
|
|
|
|
try {
|
2020-03-25 14:05:43 +01:00
|
|
|
if (value === 0 && 1 / value === -Infinity) {
|
|
|
|
return '-0';
|
|
|
|
}
|
|
|
|
|
2019-09-18 18:25:38 +02:00
|
|
|
return String(value);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name === 'TypeError') {
|
|
|
|
return Object.prototype.toString.call(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
};
|