summaryrefslogtreecommitdiffstats
path: root/devtools/shared/tests/unit/test_indentation.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /devtools/shared/tests/unit/test_indentation.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/shared/tests/unit/test_indentation.js')
-rw-r--r--devtools/shared/tests/unit/test_indentation.js133
1 files changed, 133 insertions, 0 deletions
diff --git a/devtools/shared/tests/unit/test_indentation.js b/devtools/shared/tests/unit/test_indentation.js
new file mode 100644
index 000000000..f7e4403d4
--- /dev/null
+++ b/devtools/shared/tests/unit/test_indentation.js
@@ -0,0 +1,133 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const Services = require("Services");
+const {
+ EXPAND_TAB,
+ TAB_SIZE,
+ DETECT_INDENT,
+ getIndentationFromPrefs,
+ getIndentationFromIteration,
+ getIndentationFromString,
+} = require("devtools/shared/indentation");
+
+function test_indent_from_prefs() {
+ Services.prefs.setBoolPref(DETECT_INDENT, true);
+ equal(getIndentationFromPrefs(), false,
+ "getIndentationFromPrefs returning false");
+
+ Services.prefs.setIntPref(TAB_SIZE, 73);
+ Services.prefs.setBoolPref(EXPAND_TAB, false);
+ Services.prefs.setBoolPref(DETECT_INDENT, false);
+ deepEqual(getIndentationFromPrefs(), {indentUnit: 73, indentWithTabs: true},
+ "getIndentationFromPrefs basic test");
+}
+
+const TESTS = [
+ {
+ desc: "two spaces",
+ input: [
+ "/*",
+ " * tricky comment block",
+ " */",
+ "div {",
+ " color: red;",
+ " background: blue;",
+ "}",
+ " ",
+ "span {",
+ " padding-left: 10px;",
+ "}"
+ ],
+ expected: {indentUnit: 2, indentWithTabs: false}
+ },
+ {
+ desc: "four spaces",
+ input: [
+ "var obj = {",
+ " addNumbers: function() {",
+ " var x = 5;",
+ " var y = 18;",
+ " return x + y;",
+ " },",
+ " ",
+ " /*",
+ " * Do some stuff to two numbers",
+ " * ",
+ " * @param x",
+ " * @param y",
+ " * ",
+ " * @return the result of doing stuff",
+ " */",
+ " subtractNumbers: function(x, y) {",
+ " var x += 7;",
+ " var y += 18;",
+ " var result = x - y;",
+ " result %= 2;",
+ " }",
+ "}"
+ ],
+ expected: {indentUnit: 4, indentWithTabs: false}
+ },
+ {
+ desc: "tabs",
+ input: [
+ "/*",
+ " * tricky comment block",
+ " */",
+ "div {",
+ "\tcolor: red;",
+ "\tbackground: blue;",
+ "}",
+ "",
+ "span {",
+ "\tpadding-left: 10px;",
+ "}"
+ ],
+ expected: {indentUnit: 2, indentWithTabs: true}
+ },
+ {
+ desc: "no indent",
+ input: [
+ "var x = 0;",
+ " // stray thing",
+ "var y = 9;",
+ " ",
+ ""
+ ],
+ expected: {indentUnit: 2, indentWithTabs: false}
+ },
+];
+
+function test_indent_detection() {
+ Services.prefs.setIntPref(TAB_SIZE, 2);
+ Services.prefs.setBoolPref(EXPAND_TAB, true);
+ Services.prefs.setBoolPref(DETECT_INDENT, true);
+
+ for (let test of TESTS) {
+ let iterFn = function (start, end, callback) {
+ test.input.slice(start, end).forEach(callback);
+ };
+
+ deepEqual(getIndentationFromIteration(iterFn), test.expected,
+ "test getIndentationFromIteration " + test.desc);
+ }
+
+ for (let test of TESTS) {
+ deepEqual(getIndentationFromString(test.input.join("\n")), test.expected,
+ "test getIndentationFromString " + test.desc);
+ }
+}
+
+function run_test() {
+ try {
+ test_indent_from_prefs();
+ test_indent_detection();
+ } finally {
+ Services.prefs.clearUserPref(TAB_SIZE);
+ Services.prefs.clearUserPref(EXPAND_TAB);
+ Services.prefs.clearUserPref(DETECT_INDENT);
+ }
+}