1 var tests = [], debug = null, debugUsed = new Array(), allNames = [];
3 function Failure(why) {this.message = why;}
4 Failure.prototype.toString = function() { return this.message; };
6 function indexOf(collection, elt) {
7 if (collection.indexOf) return collection.indexOf(elt);
8 for (var i = 0, e = collection.length; i < e; ++i)
9 if (collection[i] == elt) return i;
13 function test(name, run, expectedFail) {
15 var originalName = name;
16 var i = 2; // Second function would be NAME_2
17 while (indexOf(allNames, name) !== -1){
18 name = originalName + "_" + i;
23 tests.push({name: name, func: run, expectedFail: expectedFail});
27 function testCM(name, run, opts, expectedFail) {
28 return test(namespace + name, function() {
29 var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts);
30 var successful = false;
35 if ((debug && !successful) || verbose) {
36 place.style.visibility = "visible";
38 place.removeChild(cm.getWrapperElement());
44 function runTests(callback) {
46 if (indexOf(debug, "verbose") === 0) {
50 if (debug.length < 1) {
56 if (i === tests.length){
58 return callback("done");
60 var test = tests[i], expFail = test.expectedFail, startTime = +new Date;
62 var debugIndex = indexOf(debug, test.name);
63 if (debugIndex !== -1) {
64 // Remove from array for reporting incorrect tests later
65 debug.splice(debugIndex, 1);
67 var wildcardName = test.name.split("_")[0] + "_*";
68 debugIndex = indexOf(debug, wildcardName);
69 if (debugIndex !== -1) {
70 // Remove from array for reporting incorrect tests later
71 debug.splice(debugIndex, 1);
72 debugUsed.push(wildcardName);
74 debugIndex = indexOf(debugUsed, wildcardName);
75 if (debugIndex == -1) return step(i + 1);
81 var message = test.func();
84 if (expFail) callback("expected", test.name);
85 else if (e instanceof Failure) callback("fail", test.name, e.message);
87 var pos = /\bat .*?([^\/:]+):(\d+):/.exec(e.stack);
88 callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : ""));
92 if (expFail) callback("fail", test.name, message || "expected failure, but succeeded");
93 else callback("ok", test.name, message);
95 if (!quit) { // Run next test
97 totalTime += (+new Date) - startTime;
102 setTimeout(function(){step(i + 1);}, delay);
103 } else { // Quit tests
111 function label(str, msg) {
112 if (msg) return str + " (" + msg + ")";
115 function eq(a, b, msg) {
116 if (a != b) throw new Failure(label(a + " != " + b, msg));
118 function eqPos(a, b, msg) {
119 function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; }
121 if (a == null) throw new Failure(label("comparing null to " + str(b), msg));
122 if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg));
123 if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg));
125 function is(a, msg) {
126 if (!a) throw new Failure(label("assertion failed", msg));
129 function countTests() {
130 if (!debug) return tests.length;
132 for (var i = 0; i < tests.length; ++i) {
133 var name = tests[i].name;
134 if (indexOf(debug, name) != -1 ||
135 indexOf(debug, name.split("_")[0] + "_*") != -1)