tor-browser

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

head.js (3223B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 *
      5 * This is a shim for the W3C testharness.js, mapping those of
      6 * its functions that we need to the testing/xpcshell/head.js API.
      7 * See <http://www.w3.org/2008/webapps/wiki/Harness> for documentation.
      8 * This shim does some tests a little differently than the W3C test
      9 * harness; equality comparisons, especially, are less precise.
     10 * The difference does not presently affect any test results.
     11 *
     12 * We use the lower-level do_report_result throughout this file,
     13 * rather than the high-level xpcshell/head.js API that has near
     14 * equivalents for the W3C assert_* functions, because only
     15 * do_report_result allows us to provide Components.stack.caller.
     16 */
     17 
     18 function assert_equals(a, b, msg) {
     19  let text =
     20    msg +
     21    ": " +
     22    _wrap_with_quotes_if_necessary(a) +
     23    " == " +
     24    _wrap_with_quotes_if_necessary(b);
     25  do_report_result(a == b, text, Components.stack.caller, false);
     26 }
     27 
     28 function assert_not_equals(a, b, msg) {
     29  let text =
     30    msg +
     31    ": " +
     32    _wrap_with_quotes_if_necessary(a) +
     33    " != " +
     34    _wrap_with_quotes_if_necessary(b);
     35  do_report_result(a != b, text, Components.stack.caller, false);
     36 }
     37 
     38 function assert_array_equals(a, b, msg) {
     39  do_report_result(
     40    a.length == b.length,
     41    msg + ": (length) " + a.length + " == " + b.length,
     42    Components.stack.caller,
     43    false
     44  );
     45  for (let i = 0; i < a.length; ++i) {
     46    if (a[i] !== b[i]) {
     47      do_report_result(
     48        false,
     49        msg +
     50          ": [" +
     51          i +
     52          "] " +
     53          _wrap_with_quotes_if_necessary(a[i]) +
     54          " === " +
     55          _wrap_with_quotes_if_necessary(b[i]),
     56        Components.stack.caller,
     57        false
     58      );
     59    }
     60  }
     61  // If we get here, all array elements are equal.
     62  do_report_result(
     63    true,
     64    msg + ": all array elements equal",
     65    Components.stack.caller,
     66    false
     67  );
     68 }
     69 
     70 function assert_true(cond, msg) {
     71  do_report_result(
     72    !!cond,
     73    msg + ": " + uneval(cond),
     74    Components.stack.caller,
     75    false
     76  );
     77 }
     78 
     79 function assert_throws(ex, func) {
     80  if (!("name" in ex)) {
     81    do_throw(
     82      "first argument to assert_throws must be of the form " +
     83        "{'name': something}"
     84    );
     85  }
     86 
     87  let msg = "expected to catch an exception named " + ex.name;
     88 
     89  try {
     90    func();
     91  } catch (e) {
     92    if ("name" in e) {
     93      do_report_result(
     94        e.name == ex.name,
     95        msg + ", got " + e.name,
     96        Components.stack.caller,
     97        false
     98      );
     99    } else {
    100      do_report_result(
    101        false,
    102        msg + ", got " + legible_exception(ex),
    103        Components.stack.caller,
    104        false
    105      );
    106    }
    107 
    108    return;
    109  }
    110 
    111  // Call this here, not in the 'try' clause, so do_report_result's own
    112  // throw doesn't get caught by our 'catch' clause.
    113  do_report_result(
    114    false,
    115    msg + ", but returned normally",
    116    Components.stack.caller,
    117    false
    118  );
    119 }
    120 
    121 var tests = [];
    122 
    123 function test(func, msg) {
    124  tests.push({
    125    msg,
    126    func,
    127    filename: Components.stack.caller.filename,
    128  });
    129 }
    130 
    131 function run_test() {
    132  tests.forEach(function (t) {
    133    info(`test group: ${t.msg}, {source_file: ${t.filename}}`);
    134    t.func();
    135  });
    136 }