forked from ariya/phantomjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assert.js
126 lines (108 loc) · 3.02 KB
/
assert.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// After the given timeout (in seconds), the script automatically terminates.
var timeout = 0.25;
var total = 0;
function resetExitTimer() {
window.clearTimeout(window.autoExitTimer);
window.autoExitTimer = window.setTimeout(function () {
console.log(total, 'tests passed.');
phantom.exit(0);
}, timeout * 1000);
}
function showError() {
// Get the stack trace by throwing an exception and catching it.
try {
throw new Error();
} catch (e) {
var traces = e.stack.split('\n');
// The first one is this showError() function, not useful.
// The second is the assertion function, also superfluous.
traces = traces.slice(3);
console.log(traces.join('\n'));
} finally {
phantom.exit(1);
}
}
function showErrorEqual(actual, expected) {
console.error('AssertionError: expected', actual, 'to be', expected);
showError();
}
function showErrorNotEqual(actual, expected) {
console.error('AssertionError: expected', actual,
'to be different than', expected);
showError();
}
function isTrue(value) {
resetExitTimer();
if (!value) {
showErrorEqual(value, true);
}
++total;
}
function equal(actual, expected) {
resetExitTimer();
if (actual != expected) {
showErrorEqual(actual, expected);
}
++total;
}
function jsonEqual(actual, expected) {
resetExitTimer();
var actualJson = JSON.stringify(actual);
var expectedJson = JSON.stringify(expected);
if (actualJson != expectedJson) {
showErrorEqual(actualJson, expectedJson);
}
++total;
}
function notEqual(actual, expected) {
resetExitTimer();
if (actual == expected) {
showErrorNotEqual(actual, expected);
}
++total;
}
function strictEqual(actual, expected) {
resetExitTimer();
if (actual !== expected) {
showErrorEqual(actual, expected);
}
++total;
}
function typeOf(object, expected) {
resetExitTimer();
if (typeof object !== expected) {
showErrorEqual(typeof object, expected);
}
++total;
}
function waitFor(condition, callback, options) {
callback = callback || function() {};
options = options || {};
var poolMs = options.poolMs || 10;
var timeoutMs = options.timeoutMs || 1000;
var cutoffMs = timeoutMs + new Date().getTime();
var waitForInternal = function () {
if (condition()) {
++total;
callback();
} else {
var now = new Date().getTime();
if (now < cutoffMs) {
window.setTimeout(waitForInternal, poolMs);
} else {
console.error("Timeout while waiting for ''",
condition.toString(), "'");
showError();
}
}
}
waitForInternal();
}
exports.timeout = timeout;
exports.isTrue = isTrue;
exports.equal = equal;
exports.jsonEqual = jsonEqual;
exports.notEqual = notEqual;
exports.strictEqual = strictEqual;
exports.typeOf = typeOf;
exports.waitFor = waitFor;