1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const {
EXPAND_TAB,
TAB_SIZE,
DETECT_INDENT,
getTabPrefs,
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(
getTabPrefs(),
{ indentUnit: 73, indentWithTabs: true },
"getTabPrefs basic test"
);
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 (const test of TESTS) {
const iterFn = function(start, end, callback) {
test.input.slice(start, end).forEach(callback);
};
deepEqual(
getIndentationFromIteration(iterFn),
test.expected,
"test getIndentationFromIteration " + test.desc
);
}
for (const 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);
}
}
|