michael@0: var tests = [], filters = [], allNames = []; michael@0: michael@0: function Failure(why) {this.message = why;} michael@0: Failure.prototype.toString = function() { return this.message; }; michael@0: michael@0: function indexOf(collection, elt) { michael@0: if (collection.indexOf) return collection.indexOf(elt); michael@0: for (var i = 0, e = collection.length; i < e; ++i) michael@0: if (collection[i] == elt) return i; michael@0: return -1; michael@0: } michael@0: michael@0: function test(name, run, expectedFail) { michael@0: // Force unique names michael@0: var originalName = name; michael@0: var i = 2; // Second function would be NAME_2 michael@0: while (indexOf(allNames, name) !== -1){ michael@0: name = originalName + "_" + i; michael@0: i++; michael@0: } michael@0: allNames.push(name); michael@0: // Add test michael@0: tests.push({name: name, func: run, expectedFail: expectedFail}); michael@0: return name; michael@0: } michael@0: var namespace = ""; michael@0: function testCM(name, run, opts, expectedFail) { michael@0: return test(namespace + name, function() { michael@0: var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts); michael@0: var successful = false; michael@0: try { michael@0: run(cm); michael@0: successful = true; michael@0: } finally { michael@0: if (!successful || verbose) { michael@0: place.style.visibility = "visible"; michael@0: } else { michael@0: place.removeChild(cm.getWrapperElement()); michael@0: } michael@0: } michael@0: }, expectedFail); michael@0: } michael@0: michael@0: function runTests(callback) { michael@0: var totalTime = 0; michael@0: function step(i) { michael@0: for (;;) { michael@0: if (i === tests.length) { michael@0: running = false; michael@0: return callback("done"); michael@0: } michael@0: var test = tests[i], skip = false; michael@0: if (filters.length) { michael@0: skip = true; michael@0: for (var j = 0; j < filters.length; j++) michael@0: if (test.name.match(filters[j])) skip = false; michael@0: } michael@0: if (skip) { michael@0: callback("skipped", test.name, message); michael@0: i++; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: var expFail = test.expectedFail, startTime = +new Date, threw = false; michael@0: try { michael@0: var message = test.func(); michael@0: } catch(e) { michael@0: threw = true; michael@0: if (expFail) callback("expected", test.name); michael@0: else if (e instanceof Failure) callback("fail", test.name, e.message); michael@0: else { michael@0: var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack); michael@0: if (pos) console["log"](e.stack); michael@0: callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : "")); michael@0: } michael@0: } michael@0: if (!threw) { michael@0: if (expFail) callback("fail", test.name, message || "expected failure, but succeeded"); michael@0: else callback("ok", test.name, message); michael@0: } michael@0: if (!quit) { // Run next test michael@0: var delay = 0; michael@0: totalTime += (+new Date) - startTime; michael@0: if (totalTime > 500){ michael@0: totalTime = 0; michael@0: delay = 50; michael@0: } michael@0: setTimeout(function(){step(i + 1);}, delay); michael@0: } else { // Quit tests michael@0: running = false; michael@0: return null; michael@0: } michael@0: } michael@0: step(0); michael@0: } michael@0: michael@0: function label(str, msg) { michael@0: if (msg) return str + " (" + msg + ")"; michael@0: return str; michael@0: } michael@0: function eq(a, b, msg) { michael@0: if (a != b) throw new Failure(label(a + " != " + b, msg)); michael@0: } michael@0: function near(a, b, margin, msg) { michael@0: if (Math.abs(a - b) > margin) michael@0: throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg)); michael@0: } michael@0: function eqPos(a, b, msg) { michael@0: function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; } michael@0: if (a == b) return; michael@0: if (a == null) throw new Failure(label("comparing null to " + str(b), msg)); michael@0: if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg)); michael@0: if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg)); michael@0: } michael@0: function is(a, msg) { michael@0: if (!a) throw new Failure(label("assertion failed", msg)); michael@0: } michael@0: michael@0: function countTests() { michael@0: if (!filters.length) return tests.length; michael@0: var sum = 0; michael@0: for (var i = 0; i < tests.length; ++i) { michael@0: var name = tests[i].name; michael@0: for (var j = 0; j < filters.length; j++) { michael@0: if (name.match(filters[j])) { michael@0: ++sum; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: return sum; michael@0: } michael@0: michael@0: function parseTestFilter(s) { michael@0: if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i"); michael@0: else return new RegExp(s, "i"); michael@0: }