browser/devtools/sourceeditor/test/cm_driver.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 var tests = [], filters = [], allNames = [];
     3 function Failure(why) {this.message = why;}
     4 Failure.prototype.toString = function() { return this.message; };
     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 }
    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 (!successful || verbose) {
    36         place.style.visibility = "visible";
    37       } else {
    38         place.removeChild(cm.getWrapperElement());
    39       }
    40     }
    41   }, expectedFail);
    42 }
    44 function runTests(callback) {
    45   var totalTime = 0;
    46   function step(i) {
    47     for (;;) {
    48       if (i === tests.length) {
    49         running = false;
    50         return callback("done");
    51       }
    52       var test = tests[i], skip = false;
    53       if (filters.length) {
    54         skip = true;
    55         for (var j = 0; j < filters.length; j++)
    56           if (test.name.match(filters[j])) skip = false;
    57       }
    58       if (skip) {
    59         callback("skipped", test.name, message);
    60         i++;
    61       } else {
    62         break;
    63       }
    64     }
    65     var expFail = test.expectedFail, startTime = +new Date, threw = false;
    66     try {
    67       var message = test.func();
    68     } catch(e) {
    69       threw = true;
    70       if (expFail) callback("expected", test.name);
    71       else if (e instanceof Failure) callback("fail", test.name, e.message);
    72       else {
    73         var pos = /(?:\bat |@).*?([^\/:]+):(\d+)/.exec(e.stack);
    74         if (pos) console["log"](e.stack);
    75         callback("error", test.name, e.toString() + (pos ? " (" + pos[1] + ":" + pos[2] + ")" : ""));
    76       }
    77     }
    78     if (!threw) {
    79       if (expFail) callback("fail", test.name, message || "expected failure, but succeeded");
    80       else callback("ok", test.name, message);
    81     }
    82     if (!quit) { // Run next test
    83       var delay = 0;
    84       totalTime += (+new Date) - startTime;
    85       if (totalTime > 500){
    86         totalTime = 0;
    87         delay = 50;
    88       }
    89       setTimeout(function(){step(i + 1);}, delay);
    90     } else { // Quit tests
    91       running = false;
    92       return null;
    93     }
    94   }
    95   step(0);
    96 }
    98 function label(str, msg) {
    99   if (msg) return str + " (" + msg + ")";
   100   return str;
   101 }
   102 function eq(a, b, msg) {
   103   if (a != b) throw new Failure(label(a + " != " + b, msg));
   104 }
   105 function near(a, b, margin, msg) {
   106   if (Math.abs(a - b) > margin)
   107     throw new Failure(label(a + " is not close to " + b + " (" + margin + ")", msg));
   108 }
   109 function eqPos(a, b, msg) {
   110   function str(p) { return "{line:" + p.line + ",ch:" + p.ch + "}"; }
   111   if (a == b) return;
   112   if (a == null) throw new Failure(label("comparing null to " + str(b), msg));
   113   if (b == null) throw new Failure(label("comparing " + str(a) + " to null", msg));
   114   if (a.line != b.line || a.ch != b.ch) throw new Failure(label(str(a) + " != " + str(b), msg));
   115 }
   116 function is(a, msg) {
   117   if (!a) throw new Failure(label("assertion failed", msg));
   118 }
   120 function countTests() {
   121   if (!filters.length) return tests.length;
   122   var sum = 0;
   123   for (var i = 0; i < tests.length; ++i) {
   124     var name = tests[i].name;
   125     for (var j = 0; j < filters.length; j++) {
   126       if (name.match(filters[j])) {
   127         ++sum;
   128         break;
   129       }
   130     }
   131   }
   132   return sum;
   133 }
   135 function parseTestFilter(s) {
   136   if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i");
   137   else return new RegExp(s, "i");
   138 }

mercurial