Debian Codegraph @

test/com/google/javascript/jscomp/ReplaceCssNamesTest.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import static com.google.javascript.jscomp.ReplaceCssNames.UNEXPECTED_STRING_LITERAL_ERROR;
20 import static com.google.javascript.jscomp.ReplaceCssNames.UNKNOWN_SYMBOL_WARNING;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import com.google.common.collect.Maps;
25 import com.google.javascript.rhino.Node;
26
27 import java.util.Map;
28 import java.util.Set;
29
30
31 /**
32 * Tests for ReplaceCssNames.java.
33 *
34 */
35 public class ReplaceCssNamesTest extends CompilerTestCase {
36 /** Whether to pass the map of replacements as opposed to null */
37 boolean useReplacementMap;
38
39 /** Map of replacements to use during the test. */
40 Map<String, String> replacementMap =
41 new ImmutableMap.Builder<String, String>()
42 .put("active", "a")
43 .put("buttonbar", "b")
44 .put("colorswatch", "c")
45 .put("disabled", "d")
46 .put("elephant", "e")
47 .put("footer", "f")
48 .put("goog", "g")
49 .build();
50
51 Map<String, String> replacementMapFull =
52 new ImmutableMap.Builder<String, String>()
53 .put("long-prefix", "h")
54 .put("suffix1", "i")
55 .put("unrelated-word", "k")
56 .put("unrelated", "l")
57 .put("long-suffix", "m")
58 .put("long-prefix-suffix1", "h-i")
59 .build();
60
61 CssRenamingMap renamingMap;
62 Set<String> whitelist;
63
64 Map<String, Integer> cssNames;
65
66 public ReplaceCssNamesTest() {
67 }
68
69 @Override protected CompilerPass getProcessor(Compiler compiler) {
70 return new ReplaceCssNames(compiler, cssNames, whitelist) {
71 @Override
72 protected CssRenamingMap getCssRenamingMap() {
73 return useReplacementMap ? renamingMap : null;
74 }
75 };
76 }
77
78 protected CssRenamingMap getPartialMap() {
79 CssRenamingMap map = new CssRenamingMap.ByPart() {
80 @Override public String get(String value) {
81 return replacementMap.get(value);
82 }
83 };
84 return map;
85 }
86
87 protected CssRenamingMap getFullMap() {
88 return new CssRenamingMap.ByWhole() {
89 @Override public String get(String value) {
90 return replacementMapFull.get(value);
91 }
92 };
93 }
94
95 @Override
96 protected void setUp() throws Exception {
97 super.setUp();
98 super.enableLineNumberCheck(true);
99 cssNames = Maps.newHashMap();
100 useReplacementMap = true;
101 renamingMap = getPartialMap();
102 }
103
104 @Override
105 protected int getNumRepetitions() {
106 // The first pass strips the goog.getCssName even if a warning is issued,
107 // such that a subsequent pass won't issue a warning.
108 return 1;
109 }
110
111 public void testDoNotUseReplacementMap() {
112 useReplacementMap = false;
113 test("var x = goog.getCssName('goog-footer-active')",
114 "var x = 'goog-footer-active'");
115 test("el.className = goog.getCssName('goog-colorswatch-disabled')",
116 "el.className = 'goog-colorswatch-disabled'");
117 test("setClass(goog.getCssName('active-buttonbar'))",
118 "setClass('active-buttonbar')");
119 Map<String, Integer> expected =
120 new ImmutableMap.Builder<String, Integer>()
121 .put("goog", 2)
122 .put("footer", 1)
123 .put("active", 2)
124 .put("colorswatch", 1)
125 .put("disabled", 1)
126 .put("buttonbar", 1)
127 .build();
128 assertEquals(expected, cssNames);
129 }
130
131 public void testOneArgWithUnknownStringLiterals() {
132 test("var x = goog.getCssName('unknown')",
133 "var x = 'unknown'", null, UNKNOWN_SYMBOL_WARNING);
134 test("el.className = goog.getCssName('ooo')",
135 "el.className = 'ooo'", null, UNKNOWN_SYMBOL_WARNING);
136 test("setClass(goog.getCssName('ab'))",
137 "setClass('ab')", null, UNKNOWN_SYMBOL_WARNING);
138 }
139
140 public void testOneArgWithSimpleStringLiterals() {
141 test("var x = goog.getCssName('buttonbar')",
142 "var x = 'b'");
143 test("el.className = goog.getCssName('colorswatch')",
144 "el.className = 'c'");
145 test("setClass(goog.getCssName('elephant'))",
146 "setClass('e')");
147 Map<String, Integer> expected =
148 new ImmutableMap.Builder<String, Integer>()
149 .put("buttonbar", 1)
150 .put("colorswatch", 1)
151 .put("elephant", 1)
152 .build();
153 assertEquals(expected, cssNames);
154 }
155
156 public void testOneArgWithCompositeClassNames() {
157 test("var x = goog.getCssName('goog-footer-active')",
158 "var x = 'g-f-a'");
159 test("el.className = goog.getCssName('goog-colorswatch-disabled')",
160 "el.className = 'g-c-d'");
161 test("setClass(goog.getCssName('active-buttonbar'))",
162 "setClass('a-b')");
163 Map<String, Integer> expected =
164 new ImmutableMap.Builder<String, Integer>()
165 .put("goog", 2)
166 .put("footer", 1)
167 .put("active", 2)
168 .put("colorswatch", 1)
169 .put("disabled", 1)
170 .put("buttonbar", 1)
171 .build();
172 assertEquals(expected, cssNames);
173 }
174
175 public void testOneArgWithCompositeClassNamesFull() {
176 renamingMap = getFullMap();
177
178 test("var x = goog.getCssName('long-prefix')",
179 "var x = 'h'");
180 test("var x = goog.getCssName('long-prefix-suffix1')",
181 "var x = 'h-i'");
182 test("var x = goog.getCssName('unrelated')",
183 "var x = 'l'");
184 test("var x = goog.getCssName('unrelated-word')",
185 "var x = 'k'");
186 }
187
188 public void testOneArgWithCompositeClassNamesWithUnknownParts() {
189 test("var x = goog.getCssName('goog-header-active')",
190 "var x = 'goog-header-active'", null, UNKNOWN_SYMBOL_WARNING);
191 test("el.className = goog.getCssName('goog-colorswatch-focussed')",
192 "el.className = 'goog-colorswatch-focussed'",
193 null, UNKNOWN_SYMBOL_WARNING);
194 test("setClass(goog.getCssName('inactive-buttonbar'))",
195 "setClass('inactive-buttonbar')", null, UNKNOWN_SYMBOL_WARNING);
196 }
197
198 public void testTwoArgsWithStringLiterals() {
199 test("var x = goog.getCssName('header', 'active')",
200 null, UNEXPECTED_STRING_LITERAL_ERROR);
201 test("el.className = goog.getCssName('footer', window)",
202 null, ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
203 test("setClass(goog.getCssName('buttonbar', 'disabled'))",
204 null, UNEXPECTED_STRING_LITERAL_ERROR);
205 test("setClass(goog.getCssName(goog.getCssName('buttonbar'), 'active'))",
206 null, UNEXPECTED_STRING_LITERAL_ERROR);
207 }
208
209 public void testTwoArsWithVariableFirstArg() {
210 test("var x = goog.getCssName(baseClass, 'active')",
211 "var x = baseClass + '-a'");
212 test("el.className = goog.getCssName(this.getClass(), 'disabled')",
213 "el.className = this.getClass() + '-d'");
214 test("setClass(goog.getCssName(BASE_CLASS, 'disabled'))",
215 "setClass(BASE_CLASS + '-d')");
216 }
217
218 public void testTwoArgsWithVariableFirstArgFull() {
219 renamingMap = getFullMap();
220
221 test("var x = goog.getCssName(baseClass, 'long-suffix')",
222 "var x = baseClass + '-m'");
223 }
224
225 public void testZeroArguments() {
226 test("goog.getCssName()", null,
227 ReplaceCssNames.INVALID_NUM_ARGUMENTS_ERROR);
228 }
229
230 public void testManyArguments() {
231 test("goog.getCssName('a', 'b', 'c')", null,
232 ReplaceCssNames.INVALID_NUM_ARGUMENTS_ERROR);
233 test("goog.getCssName('a', 'b', 'c', 'd')", null,
234 ReplaceCssNames.INVALID_NUM_ARGUMENTS_ERROR);
235 test("goog.getCssName('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')", null,
236 ReplaceCssNames.INVALID_NUM_ARGUMENTS_ERROR);
237 }
238
239 public void testNonStringArgument() {
240 test("goog.getCssName(window);", null,
241 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
242 test("goog.getCssName(555);", null,
243 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
244 test("goog.getCssName([]);", null,
245 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
246 test("goog.getCssName({});", null,
247 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
248 test("goog.getCssName(null);", null,
249 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
250 test("goog.getCssName(undefined);", null,
251 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
252
253 test("goog.getCssName(baseClass, window);", null,
254 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
255 test("goog.getCssName(baseClass, 555);", null,
256 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
257 test("goog.getCssName(baseClass, []);", null,
258 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
259 test("goog.getCssName(baseClass, {});", null,
260 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
261 test("goog.getCssName(baseClass, null);", null,
262 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
263 test("goog.getCssName(baseClass, undefined);", null,
264 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
265 test("goog.getCssName('foo', 3);", null,
266 ReplaceCssNames.STRING_LITERAL_EXPECTED_ERROR);
267 }
268
269 public void testNoSymbolMapStripsCallAndDoesntIssueWarnings() {
270 String input = "[goog.getCssName('test'), goog.getCssName(base, 'active')]";
271 Compiler compiler = new Compiler();
272 ErrorManager errorMan = new BasicErrorManager() {
273 @Override protected void printSummary() {}
274 @Override public void println(CheckLevel level, JSError error) {}
275 };
276 compiler.setErrorManager(errorMan);
277 Node root = compiler.parseTestCode(input);
278 useReplacementMap = false;
279 ReplaceCssNames replacer = new ReplaceCssNames(compiler, null, null);
280 replacer.process(null, root);
281 assertEquals("[\"test\",base+\"-active\"]", compiler.toSource(root));
282 assertEquals("There should be no errors", 0, errorMan.getErrorCount());
283 assertEquals("There should be no warnings", 0, errorMan.getWarningCount());
284 }
285
286 public void testWhitelistByPart() {
287 whitelist = ImmutableSet.<String>of("goog", "elephant");
288 test("var x = goog.getCssName('goog')",
289 "var x = 'goog'");
290 test("var x = goog.getCssName('elephant')",
291 "var x = 'elephant'");
292 // Whitelisting happens before splitting, not after.
293 test("var x = goog.getCssName('goog-elephant')",
294 "var x = 'g-e'");
295 }
296
297 public void testWhitelistByWhole() {
298 whitelist = ImmutableSet.<String>of("long-prefix");
299 renamingMap = getFullMap();
300 test("var x = goog.getCssName('long-prefix')",
301 "var x = 'long-prefix'");
302 }
303
304 public void testWhitelistWithDashes() {
305 whitelist = ImmutableSet.<String>of("goog-elephant");
306 test("var x = goog.getCssName('goog')",
307 "var x = 'g'");
308 test("var x = goog.getCssName('elephant')",
309 "var x = 'e'");
310 test("var x = goog.getCssName('goog-elephant')",
311 "var x = 'goog-elephant'");
312 }
313
314 }