Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: utils/tests/css/src/ExpressionTest.dart

Issue 8937017: New CSS parser written in Dart to replace pyparser (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Put back for DartC Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« utils/css/tree.dart ('K') | « utils/css/validate.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #import('../../../../frog/lang.dart', prefix:'lang'); 5 #import('../../../../frog/lang.dart', prefix:'lang');
6 #import("../../../css/css.dart"); 6 #import("../../../css/css.dart");
7 7
8 class SelectorLiteralTest { 8 class SelectorLiteralTest {
9 9
10 static testMain() { 10 static testMain() {
11 initCssWorld(); 11 initCssWorld();
12 12
13 testClass(); 13 testClass();
14 testId(); 14 testId();
15 testElement(); 15 testElement();
16 testNamespace(); 16 testNamespace();
17 testSelectorGroups(); 17 testSelectorGroups();
18 testCombinator(); 18 testCombinator();
19 testWildcard(); 19 testWildcard();
20 testPseudo(); 20 testPseudo();
21 testAttribute(); 21 testAttribute();
22 testNegation(); 22 testNegation();
23 } 23 }
24 24
25 static void testClass() { 25 static void testClass() {
26 Parser parser = new Parser(new lang.SourceFile( 26 Parser parser = new Parser(new lang.SourceFile(
27 lang.SourceFile.IN_MEMORY_FILE, ".foobar")); 27 lang.SourceFile.IN_MEMORY_FILE, ".foobar"));
28 28
29 List<SelectorGroup> exprTree = parser.preprocess(); 29 List<SelectorGroup> exprTree = parser.parse();
30 Expect.isNotNull(exprTree); 30 Expect.isNotNull(exprTree);
31 Expect.equals(exprTree.length, 1); 31 Expect.equals(exprTree.length, 1);
32 for (selectorGroup in exprTree) { 32 for (selectorGroup in exprTree) {
33 Expect.equals(selectorGroup.selectors.length, 1); 33 Expect.equals(selectorGroup.selectors.length, 1);
34 for (selector in selectorGroup.selectors) { 34 for (selector in selectorGroup.selectors) {
35 Expect.isTrue(selector is ClassSelector); 35 Expect.isTrue(selector is ClassSelector);
36 Expect.isTrue(selector.isCombinatorNone()); 36 Expect.isTrue(selector.isCombinatorNone());
37 Expect.equals(selector.name, "foobar"); 37 Expect.equals(selector.name, "foobar");
38 } 38 }
39 } 39 }
40 40
41 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, 41 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE,
42 ".foobar .bar .no-story")); 42 ".foobar .bar .no-story"));
43 43
44 exprTree = parser.preprocess(); 44 exprTree = parser.parse();
45 Expect.isNotNull(exprTree); 45 Expect.isNotNull(exprTree);
46 Expect.equals(exprTree.length, 1); 46 Expect.equals(exprTree.length, 1);
47 for (selectorGroup in exprTree) { 47 for (selectorGroup in exprTree) {
48 var idx = 0; 48 var idx = 0;
49 for (selector in selectorGroup.selectors) { 49 for (selector in selectorGroup.selectors) {
50 if (idx == 0) { 50 if (idx == 0) {
51 Expect.isTrue(selector is ClassSelector); 51 Expect.isTrue(selector is ClassSelector);
52 Expect.isTrue(selector.isCombinatorNone()); 52 Expect.isTrue(selector.isCombinatorNone());
53 Expect.equals(selector.name, "foobar"); 53 Expect.equals(selector.name, "foobar");
54 } else if (idx == 1) { 54 } else if (idx == 1) {
(...skipping 10 matching lines...) Expand all
65 65
66 idx++; 66 idx++;
67 } 67 }
68 } 68 }
69 } 69 }
70 70
71 static void testId() { 71 static void testId() {
72 Parser parser = new Parser(new lang.SourceFile( 72 Parser parser = new Parser(new lang.SourceFile(
73 lang.SourceFile.IN_MEMORY_FILE, "#elemId")); 73 lang.SourceFile.IN_MEMORY_FILE, "#elemId"));
74 74
75 List<SelectorGroup> exprTree = parser.preprocess(); 75 List<SelectorGroup> exprTree = parser.parse();
76 Expect.isNotNull(exprTree); 76 Expect.isNotNull(exprTree);
77 Expect.equals(exprTree.length, 1); 77 Expect.equals(exprTree.length, 1);
78 Expect.isNotNull(exprTree); 78 Expect.isNotNull(exprTree);
79 for (selectorGroup in exprTree) { 79 for (selectorGroup in exprTree) {
80 Expect.equals(selectorGroup.selectors.length, 1); 80 Expect.equals(selectorGroup.selectors.length, 1);
81 for (selector in selectorGroup.selectors) { 81 for (selector in selectorGroup.selectors) {
82 Expect.isTrue(selector is IdSelector); 82 Expect.isTrue(selector is IdSelector);
83 Expect.isTrue(selector.isCombinatorNone()); 83 Expect.isTrue(selector.isCombinatorNone());
84 Expect.equals(selector.name, "elemId"); 84 Expect.equals(selector.name, "elemId");
85 } 85 }
86 } 86 }
87 } 87 }
88 88
89 static void testElement() { 89 static void testElement() {
90 Parser parser = new Parser(new lang.SourceFile( 90 Parser parser = new Parser(new lang.SourceFile(
91 lang.SourceFile.IN_MEMORY_FILE, "div")); 91 lang.SourceFile.IN_MEMORY_FILE, "div"));
92 List<SelectorGroup> exprTree = parser.preprocess(); 92 List<SelectorGroup> exprTree = parser.parse();
93 Expect.isNotNull(exprTree); 93 Expect.isNotNull(exprTree);
94 Expect.equals(exprTree.length, 1); 94 Expect.equals(exprTree.length, 1);
95 for (selectorGroup in exprTree) { 95 for (selectorGroup in exprTree) {
96 Expect.equals(selectorGroup.selectors.length, 1); 96 Expect.equals(selectorGroup.selectors.length, 1);
97 for (selector in selectorGroup.selectors) { 97 for (selector in selectorGroup.selectors) {
98 Expect.isTrue(selector is ElementSelector); 98 Expect.isTrue(selector is ElementSelector);
99 Expect.isTrue(selector.isCombinatorNone()); 99 Expect.isTrue(selector.isCombinatorNone());
100 Expect.equals(selector.name, "div"); 100 Expect.equals(selector.name, "div");
101 } 101 }
102 } 102 }
103 103
104 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, 104 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE,
105 "div div span")); 105 "div div span"));
106 exprTree = parser.preprocess(); 106 exprTree = parser.parse();
107 Expect.isNotNull(exprTree); 107 Expect.isNotNull(exprTree);
108 Expect.equals(exprTree.length, 1); 108 Expect.equals(exprTree.length, 1);
109 for (selectorGroup in exprTree) { 109 for (selectorGroup in exprTree) {
110 var idx = 0; 110 var idx = 0;
111 for (selector in selectorGroup.selectors) { 111 for (selector in selectorGroup.selectors) {
112 if (idx == 0) { 112 if (idx == 0) {
113 Expect.isTrue(selector is ElementSelector); 113 Expect.isTrue(selector is ElementSelector);
114 Expect.isTrue(selector.isCombinatorNone()); 114 Expect.isTrue(selector.isCombinatorNone());
115 Expect.equals(selector.name, "div"); 115 Expect.equals(selector.name, "div");
116 } else if (idx == 1) { 116 } else if (idx == 1) {
117 Expect.isTrue(selector is ElementSelector); 117 Expect.isTrue(selector is ElementSelector);
118 Expect.isTrue(selector.isCombinatorDescendant()); 118 Expect.isTrue(selector.isCombinatorDescendant());
119 Expect.equals(selector.name, "div"); 119 Expect.equals(selector.name, "div");
120 } else if (idx == 2) { 120 } else if (idx == 2) {
121 Expect.isTrue(selector is ElementSelector); 121 Expect.isTrue(selector is ElementSelector);
122 Expect.isTrue(selector.isCombinatorDescendant()); 122 Expect.isTrue(selector.isCombinatorDescendant());
123 Expect.equals(selector.name, "span"); 123 Expect.equals(selector.name, "span");
124 } else { 124 } else {
125 Expect.fail("unexpected expression"); 125 Expect.fail("unexpected expression");
126 } 126 }
127 127
128 idx++; 128 idx++;
129 } 129 }
130 } 130 }
131 } 131 }
132 132
133 static void testNamespace() { 133 static void testNamespace() {
134 Parser parser = new Parser(new lang.SourceFile( 134 Parser parser = new Parser(new lang.SourceFile(
135 lang.SourceFile.IN_MEMORY_FILE, "ns1|div")); 135 lang.SourceFile.IN_MEMORY_FILE, "ns1|div"));
136 List<SelectorGroup> exprTree = parser.preprocess(); 136 List<SelectorGroup> exprTree = parser.parse();
137 Expect.isNotNull(exprTree); 137 Expect.isNotNull(exprTree);
138 Expect.equals(exprTree.length, 1); 138 Expect.equals(exprTree.length, 1);
139 for (selectorGroup in exprTree) { 139 for (selectorGroup in exprTree) {
140 Expect.equals(selectorGroup.selectors.length, 1); 140 Expect.equals(selectorGroup.selectors.length, 1);
141 for (selector in selectorGroup.selectors) { 141 for (selector in selectorGroup.selectors) {
142 Expect.isTrue(selector is NamespaceSelector); 142 Expect.isTrue(selector is NamespaceSelector);
143 Expect.isTrue(selector.isCombinatorNone()); 143 Expect.isTrue(selector.isCombinatorNone());
144 Expect.isFalse(selector.isNamespaceWildcard()); 144 Expect.isFalse(selector.isNamespaceWildcard());
145 Expect.equals(selector.namespace, "ns1"); 145 Expect.equals(selector.namespace, "ns1");
146 ElementSelector elementSelector = selector.nameAsSimpleSelector; 146 ElementSelector elementSelector = selector.nameAsSimpleSelector;
147 Expect.isTrue(elementSelector is ElementSelector); 147 Expect.isTrue(elementSelector is ElementSelector);
148 Expect.isFalse(elementSelector.isWildcard()); 148 Expect.isFalse(elementSelector.isWildcard());
149 Expect.equals(elementSelector.name, "div"); 149 Expect.equals(elementSelector.name, "div");
150 } 150 }
151 } 151 }
152 152
153 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, 153 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE,
154 "ns1|div div ns2|span .foobar")); 154 "ns1|div div ns2|span .foobar"));
155 exprTree = parser.preprocess(); 155 exprTree = parser.parse();
156 Expect.isNotNull(exprTree); 156 Expect.isNotNull(exprTree);
157 Expect.equals(exprTree.length, 1); 157 Expect.equals(exprTree.length, 1);
158 for (selectorGroup in exprTree) { 158 for (selectorGroup in exprTree) {
159 var idx = 0; 159 var idx = 0;
160 for (selector in selectorGroup.selectors) { 160 for (selector in selectorGroup.selectors) {
161 if (idx == 0) { 161 if (idx == 0) {
162 Expect.isTrue(selector is NamespaceSelector); 162 Expect.isTrue(selector is NamespaceSelector);
163 Expect.isTrue(selector.isCombinatorNone()); 163 Expect.isTrue(selector.isCombinatorNone());
164 Expect.equals(selector.namespace, "ns1"); 164 Expect.equals(selector.namespace, "ns1");
165 ElementSelector elementSelector = selector.nameAsSimpleSelector; 165 ElementSelector elementSelector = selector.nameAsSimpleSelector;
(...skipping 22 matching lines...) Expand all
188 188
189 idx++; 189 idx++;
190 } 190 }
191 } 191 }
192 } 192 }
193 193
194 static void testSelectorGroups() { 194 static void testSelectorGroups() {
195 Parser parser = new Parser(new lang.SourceFile( 195 Parser parser = new Parser(new lang.SourceFile(
196 lang.SourceFile.IN_MEMORY_FILE, 196 lang.SourceFile.IN_MEMORY_FILE,
197 "div, .foobar ,#elemId, .xyzzy .test, ns1|div div #elemId .foobar")); 197 "div, .foobar ,#elemId, .xyzzy .test, ns1|div div #elemId .foobar"));
198 List<SelectorGroup> exprTree = parser.preprocess(); 198 List<SelectorGroup> exprTree = parser.parse();
199 Expect.isNotNull(exprTree); 199 Expect.isNotNull(exprTree);
200 Expect.equals(exprTree.length, 5); 200 Expect.equals(exprTree.length, 5);
201 var groupIdx = 0; 201 var groupIdx = 0;
202 for (selectorGroup in exprTree) { 202 for (selectorGroup in exprTree) {
203 var idx = 0; 203 var idx = 0;
204 for (selector in selectorGroup.selectors) { 204 for (selector in selectorGroup.selectors) {
205 switch (groupIdx) { 205 switch (groupIdx) {
206 case 0: // First selector group. 206 case 0: // First selector group.
207 Expect.equals(selectorGroup.selectors.length, 1); 207 Expect.equals(selectorGroup.selectors.length, 1);
208 Expect.isTrue(selector is ElementSelector); 208 Expect.isTrue(selector is ElementSelector);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 } 266 }
267 groupIdx++; 267 groupIdx++;
268 } 268 }
269 } 269 }
270 270
271 static void testCombinator() { 271 static void testCombinator() {
272 Parser parser = new Parser(new lang.SourceFile( 272 Parser parser = new Parser(new lang.SourceFile(
273 lang.SourceFile.IN_MEMORY_FILE, 273 lang.SourceFile.IN_MEMORY_FILE,
274 ".foobar > .bar + .no-story ~ myNs|div #elemId")); 274 ".foobar > .bar + .no-story ~ myNs|div #elemId"));
275 275
276 List<SelectorGroup> exprTree = parser.preprocess(); 276 List<SelectorGroup> exprTree = parser.parse();
277 Expect.isNotNull(exprTree); 277 Expect.isNotNull(exprTree);
278 Expect.equals(exprTree.length, 1); 278 Expect.equals(exprTree.length, 1);
279 for (selectorGroup in exprTree) { 279 for (selectorGroup in exprTree) {
280 var idx = 0; 280 var idx = 0;
281 Expect.equals(selectorGroup.selectors.length, 5); 281 Expect.equals(selectorGroup.selectors.length, 5);
282 for (selector in selectorGroup.selectors) { 282 for (selector in selectorGroup.selectors) {
283 if (idx == 0) { 283 if (idx == 0) {
284 Expect.isTrue(selector is ClassSelector); 284 Expect.isTrue(selector is ClassSelector);
285 Expect.isTrue(selector.isCombinatorNone()); 285 Expect.isTrue(selector.isCombinatorNone());
286 Expect.equals(selector.name, "foobar"); 286 Expect.equals(selector.name, "foobar");
(...skipping 23 matching lines...) Expand all
310 310
311 idx++; 311 idx++;
312 } 312 }
313 } 313 }
314 } 314 }
315 315
316 static void testWildcard() { 316 static void testWildcard() {
317 Parser parser = new Parser(new lang.SourceFile( 317 Parser parser = new Parser(new lang.SourceFile(
318 lang.SourceFile.IN_MEMORY_FILE, "*")); 318 lang.SourceFile.IN_MEMORY_FILE, "*"));
319 319
320 List<SelectorGroup> exprTree = parser.preprocess(); 320 List<SelectorGroup> exprTree = parser.parse();
321 Expect.isNotNull(exprTree); 321 Expect.isNotNull(exprTree);
322 Expect.equals(exprTree.length, 1); 322 Expect.equals(exprTree.length, 1);
323 for (selectorGroup in exprTree) { 323 for (selectorGroup in exprTree) {
324 Expect.equals(selectorGroup.selectors.length, 1); 324 Expect.equals(selectorGroup.selectors.length, 1);
325 for (selector in selectorGroup.selectors) { 325 for (selector in selectorGroup.selectors) {
326 Expect.isTrue(selector is ElementSelector); 326 Expect.isTrue(selector is ElementSelector);
327 Expect.isTrue(selector.isCombinatorNone()); 327 Expect.isTrue(selector.isCombinatorNone());
328 Expect.isTrue(selector.isWildcard()); 328 Expect.isTrue(selector.isWildcard());
329 Expect.equals(selector.name, "*"); 329 Expect.equals(selector.name, "*");
330 } 330 }
331 } 331 }
332 332
333 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, 333 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE,
334 "*.foobar")); 334 "*.foobar"));
335 335
336 exprTree = parser.preprocess(); 336 exprTree = parser.parse();
337 Expect.isNotNull(exprTree); 337 Expect.isNotNull(exprTree);
338 Expect.equals(exprTree.length, 1); 338 Expect.equals(exprTree.length, 1);
339 for (selectorGroup in exprTree) { 339 for (selectorGroup in exprTree) {
340 var idx = 0; 340 var idx = 0;
341 for (selector in selectorGroup.selectors) { 341 for (selector in selectorGroup.selectors) {
342 Expect.equals(selectorGroup.selectors.length, 2); 342 Expect.equals(selectorGroup.selectors.length, 2);
343 if (idx == 0) { 343 if (idx == 0) {
344 Expect.isTrue(selector is ElementSelector); 344 Expect.isTrue(selector is ElementSelector);
345 Expect.isTrue(selector.isCombinatorNone()); 345 Expect.isTrue(selector.isCombinatorNone());
346 Expect.isTrue(selector.isWildcard()); 346 Expect.isTrue(selector.isWildcard());
347 Expect.equals(selector.name, "*"); 347 Expect.equals(selector.name, "*");
348 } else if (idx == 1) { 348 } else if (idx == 1) {
349 Expect.isTrue(selector is ClassSelector); 349 Expect.isTrue(selector is ClassSelector);
350 Expect.isTrue(selector.isCombinatorNone()); 350 Expect.isTrue(selector.isCombinatorNone());
351 Expect.equals(selector.name, "foobar"); 351 Expect.equals(selector.name, "foobar");
352 } else { 352 } else {
353 Expect.fail("unexpected expression"); 353 Expect.fail("unexpected expression");
354 } 354 }
355 355
356 idx++; 356 idx++;
357 } 357 }
358 } 358 }
359 359
360 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, 360 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE,
361 "myNs|*.foobar")); 361 "myNs|*.foobar"));
362 362
363 exprTree = parser.preprocess(); 363 exprTree = parser.parse();
364 Expect.isNotNull(exprTree); 364 Expect.isNotNull(exprTree);
365 Expect.equals(exprTree.length, 1); 365 Expect.equals(exprTree.length, 1);
366 for (selectorGroup in exprTree) { 366 for (selectorGroup in exprTree) {
367 var idx = 0; 367 var idx = 0;
368 for (selector in selectorGroup.selectors) { 368 for (selector in selectorGroup.selectors) {
369 Expect.equals(selectorGroup.selectors.length, 2); 369 Expect.equals(selectorGroup.selectors.length, 2);
370 if (idx == 0) { 370 if (idx == 0) {
371 Expect.isTrue(selector is NamespaceSelector); 371 Expect.isTrue(selector is NamespaceSelector);
372 Expect.isTrue(selector.isCombinatorNone()); 372 Expect.isTrue(selector.isCombinatorNone());
373 Expect.isFalse(selector.isNamespaceWildcard()); 373 Expect.isFalse(selector.isNamespaceWildcard());
374 ElementSelector elementSelector = selector.nameAsSimpleSelector; 374 ElementSelector elementSelector = selector.nameAsSimpleSelector;
375 Expect.equals("myNs", selector.namespace); 375 Expect.equals("myNs", selector.namespace);
376 Expect.isTrue(elementSelector.isWildcard()); 376 Expect.isTrue(elementSelector.isWildcard());
377 Expect.equals("*", elementSelector.name); 377 Expect.equals("*", elementSelector.name);
378 } else if (idx == 1) { 378 } else if (idx == 1) {
379 Expect.isTrue(selector is ClassSelector); 379 Expect.isTrue(selector is ClassSelector);
380 Expect.isTrue(selector.isCombinatorNone()); 380 Expect.isTrue(selector.isCombinatorNone());
381 Expect.equals("foobar", selector.name); 381 Expect.equals("foobar", selector.name);
382 } else { 382 } else {
383 Expect.fail("unexpected expression"); 383 Expect.fail("unexpected expression");
384 } 384 }
385 385
386 idx++; 386 idx++;
387 } 387 }
388 } 388 }
389 389
390 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, 390 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE,
391 "*|*.foobar")); 391 "*|*.foobar"));
392 392
393 exprTree = parser.preprocess(); 393 exprTree = parser.parse();
394 Expect.isNotNull(exprTree); 394 Expect.isNotNull(exprTree);
395 Expect.equals(exprTree.length, 1); 395 Expect.equals(exprTree.length, 1);
396 for (selectorGroup in exprTree) { 396 for (selectorGroup in exprTree) {
397 var idx = 0; 397 var idx = 0;
398 for (selector in selectorGroup.selectors) { 398 for (selector in selectorGroup.selectors) {
399 Expect.equals(selectorGroup.selectors.length, 2); 399 Expect.equals(selectorGroup.selectors.length, 2);
400 if (idx == 0) { 400 if (idx == 0) {
401 Expect.isTrue(selector is NamespaceSelector); 401 Expect.isTrue(selector is NamespaceSelector);
402 Expect.isTrue(selector.isCombinatorNone()); 402 Expect.isTrue(selector.isCombinatorNone());
403 Expect.isTrue(selector.isNamespaceWildcard()); 403 Expect.isTrue(selector.isNamespaceWildcard());
(...skipping 26 matching lines...) Expand all
430 static void testNegation() { 430 static void testNegation() {
431 // TODO(terry): Implement 431 // TODO(terry): Implement
432 } 432 }
433 433
434 } 434 }
435 435
436 436
437 main() { 437 main() {
438 SelectorLiteralTest.testMain(); 438 SelectorLiteralTest.testMain();
439 } 439 }
OLDNEW
« utils/css/tree.dart ('K') | « utils/css/validate.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698