move a few things away in to-be-integrated/
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / test / driver.js
1 var tests = [], debug = null, debugUsed = new Array(), allNames = [];
2
3 function Failure(why) {this.message = why;}
4 Failure.prototype.toString = function() { return this.message; };
5
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;
10   return -1;
11 }
12
13 function test(name, run, expectedFail) {
14   // Force unique names
15   var originalName = name;
16   var i = 2; // Second function would be NAME_2
17   while (indexOf(allNames, name) !== -1){
18     name = originalName + "_" + i;
19     i++;
20   }
21   allNames.push(name);
22   // Add test
23   tests.push({name: name, func: run, expectedFail: expectedFail});
24   return name;
25 }
26 var namespace = "";
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;
31     try {
32       run(cm);
33       successful = true;
34     } finally {
35       if ((debug && !successful) || verbose) {
36         place.style.visibility = "visible";
37       } else {
38         place.removeChild(cm.getWrapperElement());
39       }
40     }
41   }, expectedFail);
42 }
43
44 function runTests(callback) {
45   if (debug) {
46     if (indexOf(debug, "verbose") === 0) {
47       verbose = true;
48       debug.splice(0, 1);
49     }
50     if (debug.length < 1) {
51       debug = null;
52     }
53   }
54   var totalTime = 0;
55   function step(i) {
56     if (i === tests.length){
57       running = false;
58       return callback("done");
59     }
60     var test = tests[i], expFail = test.expectedFail, startTime = +new Date;
61     if (debug !== null) {
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);
66       } else {
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);
73         } else {
74           debugIndex = indexOf(debugUsed, wildcardName);
75           if (debugIndex == -1) return step(i + 1);
76         }
77       }
78     }
79     var threw = false;
80     try {
81       var message = test.func();
82     } catch(e) {
83       threw = true;
84       if (expFail) callback("expected", test.name);
85       else if (e instanceof Failure) callback("fail", test.name, e.message);
86       else {
87         var pos = /\bat .*?([^\/:]+):(\d+):/.exec(e.stack);
88         callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : ""));
89       }
90     }
91     if (!threw) {
92       if (expFail) callback("fail", test.name, message || "expected failure, but succeeded");
93       else callback("ok", test.name, message);
94     }
95     if (!quit) { // Run next test
96       var delay = 0;
97       totalTime += (+new Date) - startTime;
98       if (totalTime > 500){
99         totalTime = 0;
100         delay = 50;
101       }
102       setTimeout(function(){step(i + 1);}, delay);
103     } else { // Quit tests
104       running = false;
105       return null;
106     }
107   }
108   step(0);
109 }
110
111 function label(str, msg) {
112   if (msg) return str + " (" + msg + ")";
113   return str;
114 }
115 function eq(a, b, msg) {
116   if (a != b) throw new Failure(label(a + " != " + b, msg));
117 }
118 function eqPos(a, b, msg) {
119   function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; }
120   if (a == b) return;
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));
124 }
125 function is(a, msg) {
126   if (!a) throw new Failure(label("assertion failed", msg));
127 }
128
129 function countTests() {
130   if (!debug) return tests.length;
131   var sum = 0;
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)
136       ++sum;
137   }
138   return sum;
139 }