2014-12-01 21:37:39 +01:00
|
|
|
function assert(mustBeTrue, message) {
|
|
|
|
if (mustBeTrue === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message === undefined) {
|
2015-02-14 23:02:49 +01:00
|
|
|
message = 'Expected true but got ' + String(mustBeTrue);
|
2014-12-01 21:37:39 +01:00
|
|
|
}
|
|
|
|
$ERROR(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert._isSameValue = function (a, b) {
|
|
|
|
if (a === b) {
|
|
|
|
// Handle +/-0 vs. -/+0
|
|
|
|
return a !== 0 || 1 / a === 1 / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle NaN vs. NaN
|
|
|
|
return a !== a && b !== b;
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.sameValue = function (actual, expected, message) {
|
|
|
|
if (assert._isSameValue(actual, expected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message === undefined) {
|
2015-04-21 19:15:19 +02:00
|
|
|
message = '';
|
|
|
|
} else {
|
|
|
|
message += ' ';
|
2014-12-01 21:37:39 +01:00
|
|
|
}
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2015-07-31 04:13:49 +02:00
|
|
|
message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2014-12-01 21:37:39 +01:00
|
|
|
$ERROR(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.notSameValue = function (actual, unexpected, message) {
|
|
|
|
if (!assert._isSameValue(actual, unexpected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message === undefined) {
|
2015-04-21 19:15:19 +02:00
|
|
|
message = '';
|
|
|
|
} else {
|
|
|
|
message += ' ';
|
2014-12-01 21:37:39 +01:00
|
|
|
}
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2015-07-31 04:13:49 +02:00
|
|
|
message += 'Expected SameValue(«' + String(actual) + '», «' + String(unexpected) + '») to be false';
|
2015-04-21 19:15:19 +02:00
|
|
|
|
2014-12-01 21:37:39 +01:00
|
|
|
$ERROR(message);
|
|
|
|
};
|
2014-12-04 00:29:52 +01:00
|
|
|
|
2015-07-08 17:19:59 +02:00
|
|
|
assert.throws = function (expectedErrorConstructor, func, message) {
|
2014-12-04 00:29:52 +01:00
|
|
|
if (func === undefined) {
|
|
|
|
$ERROR('assert.throws requires two arguments: the error constructor and a function to run');
|
|
|
|
return;
|
|
|
|
}
|
2015-07-08 17:19:59 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
message = '';
|
|
|
|
} else {
|
|
|
|
message += ' ';
|
|
|
|
}
|
2014-12-04 00:29:52 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
func();
|
|
|
|
} catch (thrown) {
|
|
|
|
if (typeof thrown !== 'object' || thrown === null) {
|
2015-07-08 17:19:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-08 17:19:59 +02: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
|
|
|
};
|