1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/sourceeditor/test/cm_driver.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +var tests = [], filters = [], allNames = []; 1.5 + 1.6 +function Failure(why) {this.message = why;} 1.7 +Failure.prototype.toString = function() { return this.message; }; 1.8 + 1.9 +function indexOf(collection, elt) { 1.10 + if (collection.indexOf) return collection.indexOf(elt); 1.11 + for (var i = 0, e = collection.length; i < e; ++i) 1.12 + if (collection[i] == elt) return i; 1.13 + return -1; 1.14 +} 1.15 + 1.16 +function test(name, run, expectedFail) { 1.17 + // Force unique names 1.18 + var originalName = name; 1.19 + var i = 2; // Second function would be NAME_2 1.20 + while (indexOf(allNames, name) !== -1){ 1.21 + name = originalName + "_" + i; 1.22 + i++; 1.23 + } 1.24 + allNames.push(name); 1.25 + // Add test 1.26 + tests.push({name: name, func: run, expectedFail: expectedFail}); 1.27 + return name; 1.28 +} 1.29 +var namespace = ""; 1.30 +function testCM(name, run, opts, expectedFail) { 1.31 + return test(namespace + name, function() { 1.32 + var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts); 1.33 + var successful = false; 1.34 + try { 1.35 + run(cm); 1.36 + successful = true; 1.37 + } finally { 1.38 + if (!successful || verbose) { 1.39 + place.style.visibility = "visible"; 1.40 + } else { 1.41 + place.removeChild(cm.getWrapperElement()); 1.42 + } 1.43 + } 1.44 + }, expectedFail); 1.45 +} 1.46 + 1.47 +function runTests(callback) { 1.48 + var totalTime = 0; 1.49 + function step(i) { 1.50 + for (;;) { 1.51 + if (i === tests.length) { 1.52 + running = false; 1.53 + return callback("done"); 1.54 + } 1.55 + var test = tests[i], skip = false; 1.56 + if (filters.length) { 1.57 + skip = true; 1.58 + for (var j = 0; j < filters.length; j++) 1.59 + if (test.name.match(filters[j])) skip = false; 1.60 + } 1.61 + if (skip) { 1.62 + callback("skipped", test.name, message); 1.63 + i++; 1.64 + } else { 1.65 + break; 1.66 + } 1.67 + } 1.68 + var expFail = test.expectedFail, startTime = +new Date, threw = false; 1.69 + try { 1.70 + var message = test.func(); 1.71 + } catch(e) { 1.72 + threw = true; 1.73 + if (expFail) callback("expected", test.name); 1.74 + else if (e instanceof Failure) callback("fail", test.name, e.message); 1.75 + else { 1.76 + var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack); 1.77 + if (pos) console["log"](e.stack); 1.78 + callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : "")); 1.79 + } 1.80 + } 1.81 + if (!threw) { 1.82 + if (expFail) callback("fail", test.name, message || "expected failure, but succeeded"); 1.83 + else callback("ok", test.name, message); 1.84 + } 1.85 + if (!quit) { // Run next test 1.86 + var delay = 0; 1.87 + totalTime += (+new Date) - startTime; 1.88 + if (totalTime > 500){ 1.89 + totalTime = 0; 1.90 + delay = 50; 1.91 + } 1.92 + setTimeout(function(){step(i + 1);}, delay); 1.93 + } else { // Quit tests 1.94 + running = false; 1.95 + return null; 1.96 + } 1.97 + } 1.98 + step(0); 1.99 +} 1.100 + 1.101 +function label(str, msg) { 1.102 + if (msg) return str + " (" + msg + ")"; 1.103 + return str; 1.104 +} 1.105 +function eq(a, b, msg) { 1.106 + if (a != b) throw new Failure(label(a + " != " + b, msg)); 1.107 +} 1.108 +function near(a, b, margin, msg) { 1.109 + if (Math.abs(a - b) > margin) 1.110 + throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg)); 1.111 +} 1.112 +function eqPos(a, b, msg) { 1.113 + function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; } 1.114 + if (a == b) return; 1.115 + if (a == null) throw new Failure(label("comparing null to " + str(b), msg)); 1.116 + if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg)); 1.117 + if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg)); 1.118 +} 1.119 +function is(a, msg) { 1.120 + if (!a) throw new Failure(label("assertion failed", msg)); 1.121 +} 1.122 + 1.123 +function countTests() { 1.124 + if (!filters.length) return tests.length; 1.125 + var sum = 0; 1.126 + for (var i = 0; i < tests.length; ++i) { 1.127 + var name = tests[i].name; 1.128 + for (var j = 0; j < filters.length; j++) { 1.129 + if (name.match(filters[j])) { 1.130 + ++sum; 1.131 + break; 1.132 + } 1.133 + } 1.134 + } 1.135 + return sum; 1.136 +} 1.137 + 1.138 +function parseTestFilter(s) { 1.139 + if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i"); 1.140 + else return new RegExp(s, "i"); 1.141 +}