tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

driver.js (4117B)


      1 var tests = [], filters = [], 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 (!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  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 passed");
     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 }
     97 
     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 eqCharPos(a, b, msg) {
    110  function str(p) { return "{line:" + p.line + ",ch:" + p.ch + ",sticky:" + p.sticky + "}"; }
    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 eqCursorPos(a, b, msg) {
    117  eqCharPos(a, b, msg);
    118  if (a) eq(a.sticky, b.sticky, msg ? msg + ' (sticky)' : 'sticky');
    119 }
    120 function is(a, msg) {
    121  if (!a) throw new Failure(label("assertion failed", msg));
    122 }
    123 
    124 function countTests() {
    125  if (!filters.length) return tests.length;
    126  var sum = 0;
    127  for (var i = 0; i < tests.length; ++i) {
    128    var name = tests[i].name;
    129    for (var j = 0; j < filters.length; j++) {
    130      if (name.match(filters[j])) {
    131        ++sum;
    132        break;
    133      }
    134    }
    135  }
    136  return sum;
    137 }
    138 
    139 function parseTestFilter(s) {
    140  if (/_\*$/.test(s)) return new RegExp("^" + s.slice(0, s.length - 2), "i");
    141  else return new RegExp(s, "i");
    142 }