-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestglue.js
86 lines (74 loc) · 2.01 KB
/
testglue.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Glue to make XPCShell test harness helpers work in a web page.
*/
function do_throw(text, stack) {
console.error(text);
throw text;
}
function print() {
console.info.apply(console, arguments);
}
function do_execute_soon(func) {
window.setTimeout(func, 0);
}
function do_test_pending() {}
function do_test_finished() {}
function do_check_eq(left, right) {
let pass = left == right;
console.log(pass ? "PASS" : "FAIL", left + " == " + right);
if (!pass) {
do_throw("FAIL");
}
}
function do_check_true(condition) {
do_check_eq(condition, true);
}
const _TEST_FILE = "test.js";
/*** XPCShell test harness helpers ***/
/**
* Add a test function to the list of tests that are to be run asynchronously.
*
* Each test function must call run_next_test() when it's done. Test files
* should call run_next_test() in their run_test function to execute all
* async tests.
*
* @return the test function that was passed in.
*/
let gTests = [];
function add_test(func) {
gTests.push(func);
return func;
}
/**
* Runs the next test function from the list of async tests.
*/
let gRunningTest = null;
let gTestIndex = 0; // The index of the currently running test.
function run_next_test()
{
function _run_next_test()
{
if (gTestIndex < gTests.length) {
do_test_pending();
gRunningTest = gTests[gTestIndex++];
print("TEST-INFO | " + _TEST_FILE + " | Starting " +
gRunningTest.name);
// Exceptions do not kill asynchronous tests, so they'll time out.
try {
gRunningTest();
}
catch (e) {
do_throw(e);
}
}
}
// For sane stacks during failures, we execute this code soon, but not now.
// We do this now, before we call do_test_finished(), to ensure the pending
// counter (_tests_pending) never reaches 0 while we still have tests to run
// (do_execute_soon bumps that counter).
do_execute_soon(_run_next_test);
if (gRunningTest !== null) {
// Close the previous test do_test_pending call.
do_test_finished();
}
}