comparison .cms/lib/codemirror/test/driver.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 var tests = [], filters = [], nameCounts = {};
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 if (nameCounts[name] == undefined){
16 nameCounts[name] = 2;
17 } else {
18 // Append number if not first test with this name.
19 name = name + '_' + (nameCounts[name]++);
20 }
21 // Add test
22 tests.push({name: name, func: run, expectedFail: expectedFail});
23 return name;
24 }
25 var namespace = "";
26 function testCM(name, run, opts, expectedFail) {
27 return test(namespace + name, function() {
28 var place = document.getElementById("testground"), cm = window.cm = CodeMirror(place, opts);
29 var successful = false;
30 try {
31 run(cm);
32 successful = true;
33 } finally {
34 if (!successful || verbose) {
35 place.style.visibility = "visible";
36 } else {
37 place.removeChild(cm.getWrapperElement());
38 }
39 }
40 }, expectedFail);
41 }
42
43 function runTests(callback) {
44 var totalTime = 0;
45 function step(i) {
46 for (;;) {
47 if (i === tests.length) {
48 running = false;
49 return callback("done");
50 }
51 var test = tests[i], skip = false;
52 if (filters.length) {
53 skip = true;
54 for (var j = 0; j < filters.length; j++)
55 if (test.name.match(filters[j])) skip = false;
56 }
57 if (skip) {
58 callback("skipped", test.name, message);
59 i++;
60 } else {
61 break;
62 }
63 }
64 var expFail = test.expectedFail, startTime = +new Date, threw = false;
65 try {
66 var message = test.func();
67 } catch(e) {
68 threw = true;
69 if (expFail) callback("expected", test.name);
70 else if (e instanceof Failure) callback("fail", test.name, e.message);
71 else {
72 var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack);
73 if (pos) console["log"](e.stack);
74 callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : ""));
75 }
76 }
77 if (!threw) {
78 if (expFail) callback("fail", test.name, message || "expected failure, but passed");
79 else callback("ok", test.name, message);
80 }
81 if (!quit) { // Run next test
82 var delay = 0;
83 totalTime += (+new Date) - startTime;
84 if (totalTime > 500){
85 totalTime = 0;
86 delay = 50;
87 }
88 setTimeout(function(){step(i + 1);}, delay);
89 } else { // Quit tests
90 running = false;
91 return null;
92 }
93 }
94 step(0);
95 }
96
97 function label(str, msg) {
98 if (msg) return str + " (" + msg + ")";
99 return str;
100 }
101 function eq(a, b, msg) {
102 if (a != b) throw new Failure(label(a + " != " + b, msg));
103 }
104 function notEq(a, b, msg) {
105 if (a == b) throw new Failure(label(a + " == " + b, msg));
106 }
107 function near(a, b, margin, msg) {
108 if (Math.abs(a - b) > margin)
109 throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg));
110 }
111 function eqCharPos(a, b, msg) {
112 function str(p) { return "{line:" + p.line + ",ch:" + p.ch + ",sticky:" + p.sticky + "}"; }
113 if (a == b) return;
114 if (a == null) throw new Failure(label("comparing null to " + str(b), msg));
115 if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg));
116 if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg));
117 }
118 function eqCursorPos(a, b, msg) {
119 eqCharPos(a, b, msg);
120 if (a) eq(a.sticky, b.sticky, msg ? msg + ' (sticky)' : 'sticky');
121 }
122 function is(a, msg) {
123 if (!a) throw new Failure(label("assertion failed", msg));
124 }
125
126 function countTests() {
127 if (!filters.length) return tests.length;
128 var sum = 0;
129 for (var i = 0; i < tests.length; ++i) {
130 var name = tests[i].name;
131 for (var j = 0; j < filters.length; j++) {
132 if (name.match(filters[j])) {
133 ++sum;
134 break;
135 }
136 }
137 }
138 return sum;
139 }
140
141 function parseTestFilter(s) {
142 if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i");
143 else return new RegExp(s, "i");
144 }