tor-browser

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

subset-tests-by-key.js (2362B)


      1 (function() {
      2  var subTestKeyPattern = null;
      3  var match;
      4  var collectKeys = false;
      5  var collectCounts = false;
      6  var keys = {};
      7  var exclude = false;
      8  if (location.search) {
      9    match = /(?:^\?|&)(include|exclude)=([^&]+)?/.exec(location.search);
     10    if (match) {
     11      subTestKeyPattern = new RegExp(`^${match[2]}$`);
     12      if (match[1] === 'exclude') {
     13        exclude = true;
     14      }
     15    }
     16    // Below is utility code to generate <meta> for copy/paste into tests.
     17    // Sample usage:
     18    // test.html?get-keys
     19    match = /(?:^\?|&)get-keys(&get-counts)?(?:&|$)/.exec(location.search);
     20    if (match) {
     21      collectKeys = true;
     22      if (match[1]) {
     23        collectCounts = true;
     24      }
     25      add_completion_callback(() => {
     26        var metas = [];
     27        var template = '<meta name="variant" content="?include=%s">';
     28        if (collectCounts) {
     29          template += ' <!--%s-->';
     30        }
     31        for (var key in keys) {
     32          var meta = template.replace("%s", key);
     33          if (collectCounts) {
     34            meta = meta.replace("%s", keys[key]);
     35          }
     36          metas.push(meta);
     37        }
     38        var pre = document.createElement('pre');
     39        pre.textContent = metas.join('\n') + '\n';
     40        document.body.insertBefore(pre, document.body.firstChild);
     41        document.getSelection().selectAllChildren(pre);
     42      });
     43    }
     44  }
     45  /**
     46   * Check if `key` is in the subset specified in the URL.
     47   * @param {string} key
     48   * @returns {boolean}
     49   */
     50  function shouldRunSubTest(key) {
     51    if (key && subTestKeyPattern) {
     52      var found = subTestKeyPattern.test(key);
     53      if (exclude) {
     54        return !found;
     55      }
     56      return found;
     57    }
     58    return true;
     59  }
     60  /**
     61   * Only test a subset of tests with `?include=Foo` or `?exclude=Foo` in the URL.
     62   * Can be used together with `<meta name="variant" content="...">`
     63   * Sample usage:
     64   * for (const test of tests) {
     65   *   subsetTestByKey("Foo", async_test, test.fn, test.name);
     66   * }
     67   */
     68   function subsetTestByKey(key, testFunc, ...args) {
     69    if (collectKeys) {
     70      if (collectCounts && key in keys) {
     71        keys[key]++;
     72      } else {
     73        keys[key] = 1;
     74      }
     75    }
     76    if (shouldRunSubTest(key)) {
     77      return testFunc(...args);
     78    }
     79    return null;
     80  }
     81  self.shouldRunSubTest = shouldRunSubTest;
     82  self.subsetTestByKey = subsetTestByKey;
     83 })();