test262/harness/assert.js

112 lines
2.5 KiB
JavaScript
Raw Normal View History

// 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
defines: [assert]
---*/
2014-12-01 21:37:39 +01:00
function assert(mustBeTrue, message) {
if (mustBeTrue === true) {
return;
}
2014-12-01 21:37:39 +01:00
if (message === undefined) {
message = 'Expected true but got ' + assert._toString(mustBeTrue);
}
$ERROR(message);
2014-12-01 21:37:39 +01:00
}
assert._isSameValue = function (a, b) {
if (a === b) {
// Handle +/-0 vs. -/+0
return a !== 0 || 1 / a === 1 / b;
}
2014-12-01 21:37:39 +01:00
// Handle NaN vs. NaN
return a !== a && b !== b;
2014-12-01 21:37:39 +01:00
};
assert.sameValue = function (actual, expected, message) {
try {
if (assert._isSameValue(actual, expected)) {
return;
}
} catch (error) {
$ERROR(message + ' (_isSameValue operation threw) ' + error);
return;
}
2014-12-01 21:37:39 +01:00
if (message === undefined) {
message = '';
} else {
message += ' ';
}
message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(expected) + '») to be true';
$ERROR(message);
2014-12-01 21:37:39 +01:00
};
assert.notSameValue = function (actual, unexpected, message) {
if (!assert._isSameValue(actual, unexpected)) {
return;
}
2014-12-01 21:37:39 +01:00
if (message === undefined) {
message = '';
} else {
message += ' ';
}
message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(unexpected) + '») to be false';
$ERROR(message);
2014-12-01 21:37:39 +01:00
};
2014-12-04 00:29:52 +01:00
assert.throws = function (expectedErrorConstructor, func, message) {
if (typeof func !== "function") {
$ERROR('assert.throws requires two arguments: the error constructor ' +
'and a function to run');
return;
}
if (message === undefined) {
message = '';
} else {
message += ' ';
}
2014-12-04 00:29:52 +01:00
try {
func();
} catch (thrown) {
if (typeof thrown !== 'object' || thrown === null) {
message += 'Thrown value was not an object!';
$ERROR(message);
} else if (thrown.constructor !== expectedErrorConstructor) {
message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
$ERROR(message);
2014-12-04 00:29:52 +01:00
}
return;
}
2014-12-04 00:29:52 +01:00
message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
$ERROR(message);
2014-12-04 00:29:52 +01:00
};
assert._toString = function (value) {
try {
2020-03-25 14:05:43 +01:00
if (value === 0 && 1 / value === -Infinity) {
return '-0';
}
return String(value);
} catch (err) {
if (err.name === 'TypeError') {
return Object.prototype.toString.call(value);
}
throw err;
}
};