tor-browser

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

asserts.js (5479B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 
      6 load(libdir + "non262.js");
      7 
      8 if (typeof assertWarning === 'undefined') {
      9    var assertWarning = function assertWarning(f, pattern) {
     10        enableLastWarning();
     11 
     12        // Verify that a warning is issued.
     13        clearLastWarning();
     14        f();
     15        var warning = getLastWarning();
     16        clearLastWarning();
     17 
     18        disableLastWarning();
     19 
     20        if (warning) {
     21            if (!warning.message.match(pattern)) {
     22                throw new Error(`assertWarning failed: "${warning.message}" does not match "${pattern}"`);
     23            }
     24            return;
     25        }
     26 
     27        throw new Error("assertWarning failed: no warning");
     28    };
     29 }
     30 
     31 if (typeof assertNoWarning === 'undefined') {
     32    var assertNoWarning = function assertNoWarning(f, msg) {
     33        enableLastWarning();
     34 
     35        // Verify that no warning is issued.
     36        clearLastWarning();
     37        f();
     38        var warning = getLastWarning();
     39        clearLastWarning();
     40 
     41        disableLastWarning();
     42 
     43        if (warning) {
     44            if (msg) {
     45                print("assertNoWarning: " + msg);
     46            }
     47 
     48            throw Error(`assertNoWarning: Unexpected warning "${warning.message}" when calling: ` + f);
     49        }
     50    };
     51 }
     52 
     53 if (typeof assertErrorMessage === 'undefined') {
     54  var assertErrorMessage = function assertErrorMessage(f, ctor, test, message) {
     55    try {
     56      f();
     57    } catch (e) {
     58      // Propagate non-specific OOM errors, we never test for these with
     59      // assertErrorMessage, as there is no meaningful ctor.
     60      if (e === "out of memory") {
     61        throw e;
     62      }
     63      if (!(e instanceof ctor)) {
     64        throw new Error((message ? `${message}: ` : "") + `assertion failed: expected exception ${ctor.name}, got ${e}`);
     65      }
     66      if (typeof test == "string") {
     67        if (test == e.message) {
     68          return;
     69        }
     70        throw new Error((message ? `${message}: ` : "") + `assertion failed: expected message "${test}", got "${e.message}"`);
     71      }
     72      if (test instanceof RegExp) {
     73        if (test.test(e.message)) {
     74          return;
     75        }
     76        throw new Error((message ? `${message}: ` : "") + `assertion failed: expected message ${test.toString()}, got "${e.message}"`);
     77      }
     78      if (!test) {
     79        throw new Error((message ? `${message}: ` : "") + `assertErrorMessage requires an error message`);
     80      }
     81      throw new Error((message ? `${message}: ` : "") + `unknown failure in assertErrorMessage: ${e}`);
     82    }
     83    throw new Error((message ? `${message}: ` : "") + `assertion failed: expected exception ${ctor.name}, no exception thrown`);
     84  };
     85 }
     86 
     87 if (typeof assertTypeErrorMessage === 'undefined') {
     88  var assertTypeErrorMessage = function assertTypeErrorMessage(f, test) {
     89    assertErrorMessage(f, TypeError, test);
     90  };
     91 }
     92 
     93 if (typeof assertRangeErrorMessage === 'undefined') {
     94  var assertRangeErrorMessage = function assertRangeErrorMessage(f, test) {
     95    assertErrorMessage(f, RangeError, test);
     96  };
     97 }
     98 
     99 if (typeof assertArrayEq === 'undefined') {
    100  var assertArrayEq = function assertArrayEq(a,b) {
    101    assertEq(a.length, b.length);
    102    for (var i = 0; i < a.length; i++) {
    103      assertEq(a[i], b[i]);
    104    }
    105  };
    106 }
    107 
    108 if (typeof assertSuppressionChain === 'undefined' && typeof globalThis.SuppressedError !== 'undefined') {
    109 
    110  function errorChainVerificationHelper(err, suppressions, verifier) {
    111    let i = 0;
    112    while (err instanceof SuppressedError) {
    113      assertEq(verifier(err.error, suppressions[i]), true);
    114      err = err.suppressed;
    115      i++;
    116    }
    117    assertEq(verifier(err, suppressions[i]), true);
    118    assertEq(i, suppressions.length - 1);
    119  }
    120 
    121  var assertSuppressionChain = function assertSuppressionChain(fn, suppressions) {
    122    let caught = false;
    123    try {
    124      fn();
    125    } catch (err) {
    126      caught = true;
    127      errorChainVerificationHelper(err, suppressions, function(err, suppression) {
    128        return err === suppression;
    129      });
    130    } finally {
    131      assertEq(caught, true);
    132    }
    133  }
    134 
    135  var assertSuppressionChainAsync = function assertSuppressionChainAsync(f, suppressions) {
    136    let thenCalled = false;
    137    let catchCalled = false;
    138    let e = null;
    139    f().then(() => { thenCalled = true; }, err => { catchCalled = true; e = err; });
    140    drainJobQueue();
    141    assertEq(thenCalled, false);
    142    assertEq(catchCalled, true);
    143    assertSuppressionChain(() => { throw e; }, suppressions);
    144  };
    145 
    146  var assertSuppressionChainErrorMessages = function assertSuppressionChainErrorMessages(fn, suppressions) {
    147    let caught = false;
    148    try {
    149      fn();
    150    } catch (err) {
    151      caught = true;
    152      errorChainVerificationHelper(err, suppressions, function(err, suppression) {
    153        return err instanceof suppression.ctor && err.message === suppression.message;
    154      });
    155    } finally {
    156      assertEq(caught, true);
    157    }
    158  }
    159 }
    160 
    161 if (typeof assertThrowsInstanceOfAsync === 'undefined') {
    162  var assertThrowsInstanceOfAsync = function assertThrowsInstanceOfAsync(f, ctor, message) {
    163    let thenCalled = false;
    164    let catchCalled = false;
    165    let e = null;
    166    f().then(() => { thenCalled = true; }, err => { catchCalled = true; e = err; });
    167    drainJobQueue();
    168    assertEq(thenCalled, false);
    169    assertEq(catchCalled, true);
    170    assertThrowsInstanceOf(() => { throw e; }, ctor, message);
    171  };
    172 }