0ad/binaries/data/tests/test_setup.js
elexis e185820e10 Implement TS_ASSERT_NUMBER as a future precaution of the integer epsilon slip in 30fe33bb0b.
Add another vector rotation test.

This was SVN commit r21064.
2018-01-29 11:55:43 +00:00

62 lines
1.4 KiB
JavaScript

// The engine provides:
// Engine.TS_FAIL = function(msg) { ... }
//
// This file define global helper functions for common test assertions.
// (Functions should be defined with local names too, so they show up properly
// in stack traces.)
function fail(msg)
{
// Get a list of callers
let trace = (new Error).stack.split("\n");
// Remove the Error ctor and this function from the stack
trace = trace.splice(2);
trace = "Stack trace:\n" + trace.join("\n");
Engine.TS_FAIL(trace + msg);
}
global.TS_FAIL = function TS_FAIL(msg)
{
fail(msg);
}
global.TS_ASSERT = function TS_ASSERT(e)
{
if (!e)
fail("Assert failed");
}
global.TS_ASSERT_EQUALS = function TS_ASSERT_EQUALS(x, y)
{
if (!(x === y))
fail("Expected equal, got "+uneval(x)+" !== "+uneval(y));
}
global.TS_ASSERT_EQUALS_APPROX = function TS_ASSERT_EQUALS_APPROX(x, y, maxDifference)
{
TS_ASSERT_NUMBER(maxDifference);
if (Math.abs(x - y) > maxDifference)
fail("Expected almost equal, got " + uneval(x) + " !== " + uneval(y));
}
global.TS_ASSERT_UNEVAL_EQUALS = function TS_ASSERT_UNEVAL_EQUALS(x, y)
{
if (!(uneval(x) === uneval(y)))
fail("Expected equal, got "+uneval(x)+" !== "+uneval(y));
}
global.TS_ASSERT_EXCEPTION = function(func)
{
try {
func();
Engine.TS_FAIL("Missed exception at:\n" + new Error().stack);
} catch (e) {
}
}
global.TS_ASSERT_NUMBER = function(value)
{
if (typeof value != "number" || !isFinite(value))
fail("The given value must be a real number!");
}