File: test_setup.js

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (62 lines) | stat: -rw-r--r-- 1,478 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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!");
}