]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for ConfigOps | |
3 | * @author Nicholas C. Zakas | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const assert = require("chai").assert, | |
12 | leche = require("leche"), | |
13 | util = require("util"), | |
14 | ConfigOps = require("../../../lib/shared/config-ops"); | |
15 | ||
16 | //------------------------------------------------------------------------------ | |
17 | // Tests | |
18 | //------------------------------------------------------------------------------ | |
19 | ||
20 | describe("ConfigOps", () => { | |
21 | ||
22 | describe("getRuleSeverity()", () => { | |
23 | const EXPECTED_RESULTS = new Map([ | |
24 | [0, 0], | |
25 | [1, 1], | |
26 | [2, 2], | |
27 | [[0], 0], | |
28 | [[1], 1], | |
29 | [[2], 2], | |
30 | ["off", 0], | |
31 | ["warn", 1], | |
32 | ["error", 2], | |
33 | [["off"], 0], | |
34 | [["warn"], 1], | |
35 | [["error"], 2], | |
36 | ["OFF", 0], | |
37 | ["wArN", 1], | |
38 | [["ErRoR"], 2], | |
39 | ["invalid config", 0], | |
40 | [["invalid config"], 0], | |
41 | [3, 0], | |
42 | [[3], 0], | |
43 | [1.5, 0], | |
44 | [[1.5], 0] | |
45 | ]); | |
46 | ||
47 | for (const key of EXPECTED_RESULTS.keys()) { | |
48 | it(`returns ${util.inspect(EXPECTED_RESULTS.get(key))} for ${util.inspect(key)}`, () => { | |
49 | assert.strictEqual(ConfigOps.getRuleSeverity(key), EXPECTED_RESULTS.get(key)); | |
50 | }); | |
51 | } | |
52 | }); | |
53 | ||
54 | describe("normalizeToStrings()", () => { | |
55 | it("should convert 2 rule setting to error when rule has just a severity", () => { | |
56 | const config = { | |
57 | rules: { | |
58 | foo: 2, | |
59 | bar: 2 | |
60 | } | |
61 | }; | |
62 | ||
63 | ConfigOps.normalizeToStrings(config); | |
64 | ||
65 | assert.deepStrictEqual(config, { | |
66 | rules: { | |
67 | foo: "error", | |
68 | bar: "error" | |
69 | } | |
70 | }); | |
71 | }); | |
72 | ||
73 | it("should convert 2 rule setting to error when rule has array with severity", () => { | |
74 | const config = { | |
75 | rules: { | |
76 | foo: [2, "something"], | |
77 | bar: 2 | |
78 | } | |
79 | }; | |
80 | ||
81 | ConfigOps.normalizeToStrings(config); | |
82 | ||
83 | assert.deepStrictEqual(config, { | |
84 | rules: { | |
85 | foo: ["error", "something"], | |
86 | bar: "error" | |
87 | } | |
88 | }); | |
89 | }); | |
90 | ||
91 | it("should convert 1 rule setting to warn when rule has just a severity", () => { | |
92 | const config = { | |
93 | rules: { | |
94 | foo: 1, | |
95 | bar: 1 | |
96 | } | |
97 | }; | |
98 | ||
99 | ConfigOps.normalizeToStrings(config); | |
100 | ||
101 | assert.deepStrictEqual(config, { | |
102 | rules: { | |
103 | foo: "warn", | |
104 | bar: "warn" | |
105 | } | |
106 | }); | |
107 | }); | |
108 | ||
109 | it("should convert 1 rule setting to warn when rule has array with severity", () => { | |
110 | const config = { | |
111 | rules: { | |
112 | foo: [1, "something"], | |
113 | bar: 1 | |
114 | } | |
115 | }; | |
116 | ||
117 | ConfigOps.normalizeToStrings(config); | |
118 | ||
119 | assert.deepStrictEqual(config, { | |
120 | rules: { | |
121 | foo: ["warn", "something"], | |
122 | bar: "warn" | |
123 | } | |
124 | }); | |
125 | }); | |
126 | ||
127 | it("should convert 0 rule setting to off when rule has just a severity", () => { | |
128 | const config = { | |
129 | rules: { | |
130 | foo: 0, | |
131 | bar: 0 | |
132 | } | |
133 | }; | |
134 | ||
135 | ConfigOps.normalizeToStrings(config); | |
136 | ||
137 | assert.deepStrictEqual(config, { | |
138 | rules: { | |
139 | foo: "off", | |
140 | bar: "off" | |
141 | } | |
142 | }); | |
143 | }); | |
144 | ||
145 | it("should convert 0 rule setting to off when rule has array with severity", () => { | |
146 | const config = { | |
147 | rules: { | |
148 | foo: [0, "something"], | |
149 | bar: 0 | |
150 | } | |
151 | }; | |
152 | ||
153 | ConfigOps.normalizeToStrings(config); | |
154 | ||
155 | assert.deepStrictEqual(config, { | |
156 | rules: { | |
157 | foo: ["off", "something"], | |
158 | bar: "off" | |
159 | } | |
160 | }); | |
161 | }); | |
162 | ||
163 | it("should convert 256 rule setting to off when rule has just a severity", () => { | |
164 | const config = { | |
165 | rules: { | |
166 | foo: 256, | |
167 | bar: 256 | |
168 | } | |
169 | }; | |
170 | ||
171 | ConfigOps.normalizeToStrings(config); | |
172 | ||
173 | assert.deepStrictEqual(config, { | |
174 | rules: { | |
175 | foo: "off", | |
176 | bar: "off" | |
177 | } | |
178 | }); | |
179 | }); | |
180 | ||
181 | it("should convert 256 rule setting to off when rule has array with severity", () => { | |
182 | const config = { | |
183 | rules: { | |
184 | foo: [256, "something"], | |
185 | bar: 256 | |
186 | } | |
187 | }; | |
188 | ||
189 | ConfigOps.normalizeToStrings(config); | |
190 | ||
191 | assert.deepStrictEqual(config, { | |
192 | rules: { | |
193 | foo: ["off", "something"], | |
194 | bar: "off" | |
195 | } | |
196 | }); | |
197 | }); | |
198 | }); | |
199 | ||
200 | describe("isError()", () => { | |
201 | ||
202 | leche.withData([ | |
203 | ["error", true], | |
204 | ["Error", true], | |
205 | [2, true], | |
206 | [["error"], true], | |
207 | [["erRor"], true], | |
208 | [[2], true], | |
209 | [["error", "foo"], true], | |
210 | [["eRror", "bar"], true], | |
211 | [[2, "baz"], true] | |
212 | ], (input, expected) => { | |
213 | ||
214 | it(`should return ${expected}when passed ${input}`, () => { | |
215 | const result = ConfigOps.isErrorSeverity(input); | |
216 | ||
217 | assert.strictEqual(result, expected); | |
218 | }); | |
219 | ||
220 | }); | |
221 | ||
222 | }); | |
223 | ||
224 | describe("normalizeConfigGlobal", () => { | |
225 | [ | |
226 | ["off", "off"], | |
227 | [true, "writable"], | |
228 | ["true", "writable"], | |
229 | [false, "readonly"], | |
230 | ["false", "readonly"], | |
231 | [null, "readonly"], | |
232 | ["writeable", "writable"], | |
233 | ["writable", "writable"], | |
234 | ["readable", "readonly"], | |
235 | ["readonly", "readonly"], | |
236 | ["writable", "writable"] | |
237 | ].forEach(([input, output]) => { | |
238 | it(util.inspect(input), () => { | |
239 | assert.strictEqual(ConfigOps.normalizeConfigGlobal(input), output); | |
240 | }); | |
241 | }); | |
242 | ||
243 | it("throws on other inputs", () => { | |
244 | assert.throws( | |
245 | () => ConfigOps.normalizeConfigGlobal("something else"), | |
246 | /^'something else' is not a valid configuration for a global \(use 'readonly', 'writable', or 'off'\)$/u | |
247 | ); | |
248 | }); | |
249 | }); | |
250 | }); |