tor-browser

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

test_indentation.js (3369B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const {
      7  EXPAND_TAB,
      8  TAB_SIZE,
      9  DETECT_INDENT,
     10  getTabPrefs,
     11  getIndentationFromPrefs,
     12  getIndentationFromIteration,
     13  getIndentationFromString,
     14 } = require("resource://devtools/shared/indentation.js");
     15 
     16 function test_indent_from_prefs() {
     17  Services.prefs.setBoolPref(DETECT_INDENT, true);
     18  equal(
     19    getIndentationFromPrefs(),
     20    false,
     21    "getIndentationFromPrefs returning false"
     22  );
     23 
     24  Services.prefs.setIntPref(TAB_SIZE, 73);
     25  Services.prefs.setBoolPref(EXPAND_TAB, false);
     26  Services.prefs.setBoolPref(DETECT_INDENT, false);
     27  deepEqual(
     28    getTabPrefs(),
     29    { indentUnit: 73, indentWithTabs: true },
     30    "getTabPrefs basic test"
     31  );
     32  deepEqual(
     33    getIndentationFromPrefs(),
     34    { indentUnit: 73, indentWithTabs: true },
     35    "getIndentationFromPrefs basic test"
     36  );
     37 }
     38 
     39 const TESTS = [
     40  {
     41    desc: "two spaces",
     42    input: [
     43      "/*",
     44      " * tricky comment block",
     45      " */",
     46      "div {",
     47      "  color: red;",
     48      "  background: blue;",
     49      "}",
     50      "     ",
     51      "span {",
     52      "  padding-left: 10px;",
     53      "}",
     54    ],
     55    expected: { indentUnit: 2, indentWithTabs: false },
     56  },
     57  {
     58    desc: "four spaces",
     59    input: [
     60      "var obj = {",
     61      "    addNumbers: function() {",
     62      "        var x = 5;",
     63      "        var y = 18;",
     64      "        return x + y;",
     65      "    },",
     66      "   ",
     67      "    /*",
     68      "     * Do some stuff to two numbers",
     69      "     * ",
     70      "     * @param x",
     71      "     * @param y",
     72      "     * ",
     73      "     * @return the result of doing stuff",
     74      "     */",
     75      "    subtractNumbers: function(x, y) {",
     76      "        var x += 7;",
     77      "        var y += 18;",
     78      "        var result = x - y;",
     79      "        result %= 2;",
     80      "    }",
     81      "}",
     82    ],
     83    expected: { indentUnit: 4, indentWithTabs: false },
     84  },
     85  {
     86    desc: "tabs",
     87    input: [
     88      "/*",
     89      " * tricky comment block",
     90      " */",
     91      "div {",
     92      "\tcolor: red;",
     93      "\tbackground: blue;",
     94      "}",
     95      "",
     96      "span {",
     97      "\tpadding-left: 10px;",
     98      "}",
     99    ],
    100    expected: { indentUnit: 2, indentWithTabs: true },
    101  },
    102  {
    103    desc: "no indent",
    104    input: [
    105      "var x = 0;",
    106      "           // stray thing",
    107      "var y = 9;",
    108      "    ",
    109      "",
    110    ],
    111    expected: { indentUnit: 2, indentWithTabs: false },
    112  },
    113 ];
    114 
    115 function test_indent_detection() {
    116  Services.prefs.setIntPref(TAB_SIZE, 2);
    117  Services.prefs.setBoolPref(EXPAND_TAB, true);
    118  Services.prefs.setBoolPref(DETECT_INDENT, true);
    119 
    120  for (const test of TESTS) {
    121    const iterFn = function (start, end, callback) {
    122      test.input.slice(start, end).forEach(callback);
    123    };
    124 
    125    deepEqual(
    126      getIndentationFromIteration(iterFn),
    127      test.expected,
    128      "test getIndentationFromIteration " + test.desc
    129    );
    130  }
    131 
    132  for (const test of TESTS) {
    133    deepEqual(
    134      getIndentationFromString(test.input.join("\n")),
    135      test.expected,
    136      "test getIndentationFromString " + test.desc
    137    );
    138  }
    139 }
    140 
    141 function run_test() {
    142  try {
    143    test_indent_from_prefs();
    144    test_indent_detection();
    145  } finally {
    146    Services.prefs.clearUserPref(TAB_SIZE);
    147    Services.prefs.clearUserPref(EXPAND_TAB);
    148    Services.prefs.clearUserPref(DETECT_INDENT);
    149  }
    150 }