tor-browser

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

basic-longest.js (2953B)


      1 // Copyright (C) 2025 André Bargull. All rights reserved.
      2 // This code is governed by the BSD license found in the LICENSE file.
      3 
      4 /*---
      5 esid: sec-iterator.zipkeyed
      6 description: >
      7  Basic Iterator.zipKeyed test with "longest" mode.
      8 includes: [compareArray.js, propertyHelper.js, iteratorZipUtils.js]
      9 features: [joint-iteration]
     10 ---*/
     11 
     12 function testSequence(inputs, inputsLabel, minLength, maxLength) {
     13  function test(options, optionsLabel, getPaddingForInput) {
     14    var label = optionsLabel + ", " + inputsLabel;
     15    var it = Iterator.zipKeyed(inputs, options);
     16    assertZippedKeyed(it, inputs, minLength, label);
     17 
     18    var expectedKeys = Object.keys(inputs);
     19 
     20    for (var i = minLength; i < maxLength; i++) {
     21      var itemLabel = label + ", step " + i;
     22 
     23      var result = it.next();
     24      var value = result.value;
     25 
     26      // Test IteratorResult structure.
     27      assertIteratorResult(result, value, false, itemLabel);
     28 
     29      // Ensure resulting object has the expected keys and values.
     30      assert.compareArray(Object.keys(value), expectedKeys, itemLabel + ": result object keys");
     31 
     32      var expectedValues = Object.values(inputs).map(function (input, j) {
     33        return i < input.length ? input[i] : getPaddingForInput(j);
     34      });
     35      assert.compareArray(Object.values(value), expectedValues, itemLabel + ": result object values");
     36    }
     37    assertIteratorResult(it.next(), undefined, true, label + ": after completion");
     38  }
     39 
     40  test(
     41    { mode: "longest" },
     42    "options = { mode: 'longest' }",
     43    function () {
     44      return undefined;
     45    },
     46  );
     47 
     48  test(
     49    { mode: "longest", padding: {} },
     50    "options = { mode: 'longest', padding: {} }",
     51    function () {
     52      return undefined;
     53    },
     54  );
     55 
     56  test(
     57    { mode: "longest", padding: { prop_0: "pad" } },
     58    "options = { mode: 'longest', padding: { prop_0: 'pad' } }",
     59    function (idx) {
     60      return idx === 0 ? "pad" : undefined;
     61    },
     62  );
     63 
     64  test(
     65    { mode: "longest", padding: { prop_1: "pad" } },
     66    "options = { mode: 'longest', padding: { prop_1: 'pad' } }",
     67    function (idx) {
     68      return idx === 1 ? "pad" : undefined;
     69    },
     70  );
     71 
     72  var padding = {};
     73  for (var key in inputs) {
     74    padding[key] = "pad";
     75  }
     76  test(
     77    { mode: "longest", padding: padding },
     78    "options = { mode: 'longest', padding: { prop_0: 'pad', ..., prop_N: 'pad' } }",
     79    function (idx) {
     80      return "pad";
     81    },
     82  );
     83 
     84  // Object with many properties.
     85  padding = new Proxy({}, {
     86    has(target, key) {
     87      return key.indexOf('_') !== -1;
     88    },
     89    get(target, key, receiver) {
     90      var split = key.split('_');
     91      if (split.length !== 2) return undefined;
     92      return 'pad_' + split[1];
     93    }
     94  });
     95  test(
     96    { mode: "longest", padding: padding },
     97    "options = { mode: 'longest', padding: { prop_0: 'pad_1', ..., prop_N: 'pad_N' } }",
     98    function (idx) {
     99      return 'pad_' + idx;
    100    },
    101  );
    102 
    103 }
    104 
    105 forEachSequenceCombinationKeyed(testSequence);
    106 
    107 reportCompare(0, 0);