tor-browser

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

test_attribute-parsing-01.js (1731B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test splitBy from node-attribute-parser.js
      7 
      8 const {
      9  splitBy,
     10 } = require("resource://devtools/client/shared/node-attribute-parser.js");
     11 
     12 const TEST_DATA = [
     13  {
     14    value: "this is a test",
     15    splitChar: " ",
     16    expected: [
     17      { value: "this" },
     18      { value: " ", type: "string" },
     19      { value: "is" },
     20      { value: " ", type: "string" },
     21      { value: "a" },
     22      { value: " ", type: "string" },
     23      { value: "test" },
     24    ],
     25  },
     26  {
     27    value: "/path/to/handler",
     28    splitChar: " ",
     29    expected: [{ value: "/path/to/handler" }],
     30  },
     31  {
     32    value: "test",
     33    splitChar: " ",
     34    expected: [{ value: "test" }],
     35  },
     36  {
     37    value: " test ",
     38    splitChar: " ",
     39    expected: [
     40      { value: " ", type: "string" },
     41      { value: "test" },
     42      { value: " ", type: "string" },
     43    ],
     44  },
     45  {
     46    value: "",
     47    splitChar: " ",
     48    expected: [],
     49  },
     50  {
     51    value: "   ",
     52    splitChar: " ",
     53    expected: [
     54      { value: " ", type: "string" },
     55      { value: " ", type: "string" },
     56      { value: " ", type: "string" },
     57    ],
     58  },
     59 ];
     60 
     61 function run_test() {
     62  for (const { value, splitChar, expected } of TEST_DATA) {
     63    info("Splitting string: " + value);
     64    const tokens = splitBy(value, splitChar);
     65 
     66    info("Checking that the number of parsed tokens is correct");
     67    Assert.equal(tokens.length, expected.length);
     68 
     69    for (let i = 0; i < tokens.length; i++) {
     70      info("Checking the data in token " + i);
     71      Assert.equal(tokens[i].value, expected[i].value);
     72      if (expected[i].type) {
     73        Assert.equal(tokens[i].type, expected[i].type);
     74      }
     75    }
     76  }
     77 }