-
Notifications
You must be signed in to change notification settings - Fork 63
/
unittests.html
125 lines (115 loc) · 3.83 KB
/
unittests.html
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
<!DOCTYPE html>
<!--
Test Harness for CodeMirror
This HTML file provides a minimal environment within which one can execute
JSUnit-style tests.
-->
<html>
<head>
<title>Test Harness for CodeMirror</title>
<script type="text/javascript" src="js/unittests.js"></script>
<script type="text/javascript">
// Counters for unit test results.
var test_good = 0;
var test_bad = 0;
// If expected and actual are the identical, print 'Ok', otherwise 'Fail!'
function assertEquals(msg, expected, actual) {
if (typeof actual == 'undefined') {
// msg is optional.
actual = expected;
expected = msg;
msg = 'Expected: \'' + expected + '\' Actual: \'' + actual + '\'';
}
var html;
if (expected === actual) {
html = '<FONT COLOR="#009900">Ok</FONT>';
test_good++;
} else {
html = '<FONT COLOR="#990000"><BIG>Fail!</BIG></FONT><BR>';
msg += ' Expected: \'' + expected + '\' Actual: \'' + actual + '\'';
msg = msg.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
msg = msg.replace(/\r\n/g, '¶');
msg = msg.replace(/\n/g, '¶');
msg = msg.replace(/\r/g, '¶');
msg = msg.replace(/\u00A0/g, '·');
html += '<code>' + msg + '</code>';
test_bad++;
}
printDiv(html);
}
// If expected and actual are the equivalent, pass the test.
function assertEquivalent(msg, expected, actual) {
if (typeof actual == 'undefined') {
// msg is optional.
actual = expected;
expected = msg;
msg = 'Expected: \'' + expected + '\' Actual: \'' + actual + '\'';
}
if (_equivalent(expected, actual)) {
assertEquals(msg, String.toString(expected), String.toString(actual));
} else {
assertEquals(msg, expected, actual);
}
}
// Are a and b the equivalent? -- Recursive.
function _equivalent(a, b) {
if (a == b) {
return true;
}
if (typeof a == 'object' && typeof b == 'object' && a !== null && b !== null) {
if (a.toString() != b.toString()) {
return false;
}
for (var p in a) {
if (!_equivalent(a[p], b[p])) {
return false;
}
}
for (var p in b) {
if (!_equivalent(a[p], b[p])) {
return false;
}
}
return true;
}
return false;
}
function runTests() {
for (var x = 0; x < tests.length; x++) {
printDiv('<H3>' + tests[x] + ':</H3>');
eval(tests[x] + '()');
}
}
function initTests() {
var startTime = (new Date()).getTime();
runTests();
var endTime = (new Date()).getTime();
var html = '<H3>Done.</H3>';
html += '<P>Tests passed: ' + test_good + '<BR>Tests failed: ' + test_bad + '</P>';
html += '<P>Total time: ' + (endTime - startTime) + ' ms</P>';
printDiv(html);
}
function printDiv(html) {
var div = document.createElement('div');
div.innerHTML = html;
document.getElementById('testoutput').appendChild(div);
}
</script>
</head>
<body>
<h1>Test Harness for CodeMirror</h1>
<div id="testoutput"></div>
<hr>
<div style="border: 1px solid black;">
<textarea id="inputfield"></textarea>
</div>
<script type="text/javascript" src="js/codemirror.js"></script>
<script>
var editor = new CodeMirror(CodeMirror.replace(document.getElementById('inputfield')), {
parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
path: "js/",
initCallback: initTests
});
</script>
</body>
</html>