0ad/binaries/data/tests/test_setup.js
elexis a2f7dba96c Move simulation test for approximate equality from e18598cd62 to the test setup, so that it can be reused.
Remove unneeded helper variable from e18598cd62 as intended by
7778a7b436 by using the set method.

This was SVN commit r20467.
2017-11-16 23:54:38 +00:00

45 lines
1.1 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)
{
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));
}