Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | var tests = [], filters = [], allNames = []; |
michael@0 | 2 | |
michael@0 | 3 | function Failure(why) {this.message = why;} |
michael@0 | 4 | Failure.prototype.toString = function() { return this.message; }; |
michael@0 | 5 | |
michael@0 | 6 | function indexOf(collection, elt) { |
michael@0 | 7 | if (collection.indexOf) return collection.indexOf(elt); |
michael@0 | 8 | for (var i = 0, e = collection.length; i < e; ++i) |
michael@0 | 9 | if (collection[i] == elt) return i; |
michael@0 | 10 | return -1; |
michael@0 | 11 | } |
michael@0 | 12 | |
michael@0 | 13 | function test(name, run, expectedFail) { |
michael@0 | 14 | // Force unique names |
michael@0 | 15 | var originalName = name; |
michael@0 | 16 | var i = 2; // Second function would be NAME_2 |
michael@0 | 17 | while (indexOf(allNames, name) !== -1){ |
michael@0 | 18 | name = originalName + "_" + i; |
michael@0 | 19 | i++; |
michael@0 | 20 | } |
michael@0 | 21 | allNames.push(name); |
michael@0 | 22 | // Add test |
michael@0 | 23 | tests.push({name: name, func: run, expectedFail: expectedFail}); |
michael@0 | 24 | return name; |
michael@0 | 25 | } |
michael@0 | 26 | var namespace = ""; |
michael@0 | 27 | function testCM(name, run, opts, expectedFail) { |
michael@0 | 28 | return test(namespace + name, function() { |
michael@0 | 29 | var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts); |
michael@0 | 30 | var successful = false; |
michael@0 | 31 | try { |
michael@0 | 32 | run(cm); |
michael@0 | 33 | successful = true; |
michael@0 | 34 | } finally { |
michael@0 | 35 | if (!successful || verbose) { |
michael@0 | 36 | place.style.visibility = "visible"; |
michael@0 | 37 | } else { |
michael@0 | 38 | place.removeChild(cm.getWrapperElement()); |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | }, expectedFail); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function runTests(callback) { |
michael@0 | 45 | var totalTime = 0; |
michael@0 | 46 | function step(i) { |
michael@0 | 47 | for (;;) { |
michael@0 | 48 | if (i === tests.length) { |
michael@0 | 49 | running = false; |
michael@0 | 50 | return callback("done"); |
michael@0 | 51 | } |
michael@0 | 52 | var test = tests[i], skip = false; |
michael@0 | 53 | if (filters.length) { |
michael@0 | 54 | skip = true; |
michael@0 | 55 | for (var j = 0; j < filters.length; j++) |
michael@0 | 56 | if (test.name.match(filters[j])) skip = false; |
michael@0 | 57 | } |
michael@0 | 58 | if (skip) { |
michael@0 | 59 | callback("skipped", test.name, message); |
michael@0 | 60 | i++; |
michael@0 | 61 | } else { |
michael@0 | 62 | break; |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | var expFail = test.expectedFail, startTime = +new Date, threw = false; |
michael@0 | 66 | try { |
michael@0 | 67 | var message = test.func(); |
michael@0 | 68 | } catch(e) { |
michael@0 | 69 | threw = true; |
michael@0 | 70 | if (expFail) callback("expected", test.name); |
michael@0 | 71 | else if (e instanceof Failure) callback("fail", test.name, e.message); |
michael@0 | 72 | else { |
michael@0 | 73 | var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack); |
michael@0 | 74 | if (pos) console["log"](e.stack); |
michael@0 | 75 | callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : "")); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | if (!threw) { |
michael@0 | 79 | if (expFail) callback("fail", test.name, message || "expected failure, but succeeded"); |
michael@0 | 80 | else callback("ok", test.name, message); |
michael@0 | 81 | } |
michael@0 | 82 | if (!quit) { // Run next test |
michael@0 | 83 | var delay = 0; |
michael@0 | 84 | totalTime += (+new Date) - startTime; |
michael@0 | 85 | if (totalTime > 500){ |
michael@0 | 86 | totalTime = 0; |
michael@0 | 87 | delay = 50; |
michael@0 | 88 | } |
michael@0 | 89 | setTimeout(function(){step(i + 1);}, delay); |
michael@0 | 90 | } else { // Quit tests |
michael@0 | 91 | running = false; |
michael@0 | 92 | return null; |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | step(0); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function label(str, msg) { |
michael@0 | 99 | if (msg) return str + " (" + msg + ")"; |
michael@0 | 100 | return str; |
michael@0 | 101 | } |
michael@0 | 102 | function eq(a, b, msg) { |
michael@0 | 103 | if (a != b) throw new Failure(label(a + " != " + b, msg)); |
michael@0 | 104 | } |
michael@0 | 105 | function near(a, b, margin, msg) { |
michael@0 | 106 | if (Math.abs(a - b) > margin) |
michael@0 | 107 | throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg)); |
michael@0 | 108 | } |
michael@0 | 109 | function eqPos(a, b, msg) { |
michael@0 | 110 | function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; } |
michael@0 | 111 | if (a == b) return; |
michael@0 | 112 | if (a == null) throw new Failure(label("comparing null to " + str(b), msg)); |
michael@0 | 113 | if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg)); |
michael@0 | 114 | if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg)); |
michael@0 | 115 | } |
michael@0 | 116 | function is(a, msg) { |
michael@0 | 117 | if (!a) throw new Failure(label("assertion failed", msg)); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | function countTests() { |
michael@0 | 121 | if (!filters.length) return tests.length; |
michael@0 | 122 | var sum = 0; |
michael@0 | 123 | for (var i = 0; i < tests.length; ++i) { |
michael@0 | 124 | var name = tests[i].name; |
michael@0 | 125 | for (var j = 0; j < filters.length; j++) { |
michael@0 | 126 | if (name.match(filters[j])) { |
michael@0 | 127 | ++sum; |
michael@0 | 128 | break; |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | return sum; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | function parseTestFilter(s) { |
michael@0 | 136 | if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i"); |
michael@0 | 137 | else return new RegExp(s, "i"); |
michael@0 | 138 | } |