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

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

Issue 9956037: Simplify tests under utils/tests to follow StandardTestSuite convention. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 months 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #import("../../../css/css.dart");
6
7 class ExpressionTest {
8
9 static testMain() {
10 initCssWorld();
11
12 testClass();
13 testId();
14 testElement();
15 testNamespace();
16 testSelectorGroups();
17 testCombinator();
18 testWildcard();
19 testPseudo();
20 testAttribute();
21 testNegation();
22 }
23
24 static void testClass() {
25 Parser parser = new Parser(new SourceFile(
26 SourceFile.IN_MEMORY_FILE, ".foobar {}"));
27
28 Stylesheet stylesheet = parser.parse();
29 Expect.isNotNull(stylesheet);
30 Expect.equals(stylesheet.topLevels.length, 1);
31
32 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
33 RuleSet ruleset = stylesheet.topLevels[0];
34 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
35 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
36
37 List<SimpleSelectorSequence> simpleSeqs =
38 ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
39 Expect.equals(simpleSeqs.length, 1);
40 for (final selector in simpleSeqs) {
41 final simpSelector = selector.simpleSelector;
42 Expect.isTrue(simpSelector is ClassSelector);
43 Expect.isTrue(selector.isCombinatorNone());
44 Expect.equals(simpSelector.name, "foobar");
45 }
46
47 parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
48 ".foobar .bar .no-story {}"));
49
50 stylesheet = parser.parse();
51 Expect.isNotNull(stylesheet);
52 Expect.equals(stylesheet.topLevels.length, 1);
53
54 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
55 ruleset = stylesheet.topLevels[0];
56 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
57 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
58
59 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
60
61 var idx = 0;
62 for (final selector in simpleSeqs) {
63 final simpSelector = selector.simpleSelector;
64 if (idx == 0) {
65 Expect.isTrue(simpSelector is ClassSelector);
66 Expect.isTrue(selector.isCombinatorNone());
67 Expect.equals(simpSelector.name, "foobar");
68 } else if (idx == 1) {
69 Expect.isTrue(simpSelector is ClassSelector);
70 Expect.isTrue(selector.isCombinatorDescendant());
71 Expect.equals(simpSelector.name, "bar");
72 } else if (idx == 2) {
73 Expect.isTrue(simpSelector is ClassSelector);
74 Expect.isTrue(selector.isCombinatorDescendant());
75 Expect.equals(simpSelector.name, "no-story");
76 } else {
77 Expect.fail("unexpected expression");
78 }
79
80 idx++;
81 }
82
83 Expect.equals(simpleSeqs.length, idx);
84 }
85
86 static void testId() {
87 Parser parser = new Parser(new SourceFile(
88 SourceFile.IN_MEMORY_FILE, "#elemId {}"));
89
90 Stylesheet stylesheet = parser.parse();
91 Expect.isNotNull(stylesheet);
92 Expect.equals(stylesheet.topLevels.length, 1);
93
94 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
95 RuleSet ruleset = stylesheet.topLevels[0];
96 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
97 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
98
99 List<SimpleSelectorSequence> simpleSeqs =
100 ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
101
102 for (final selector in simpleSeqs) {
103 final simpSelector = selector.simpleSelector;
104 Expect.isTrue(simpSelector is IdSelector);
105 Expect.isTrue(selector.isCombinatorNone());
106 Expect.equals(simpSelector.name, "elemId");
107 }
108 }
109
110 static void testElement() {
111 Parser parser = new Parser(new SourceFile(
112 SourceFile.IN_MEMORY_FILE, "div {}"));
113 Stylesheet stylesheet = parser.parse();
114 Expect.isNotNull(stylesheet);
115 Expect.equals(stylesheet.topLevels.length, 1);
116
117 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
118 RuleSet ruleset = stylesheet.topLevels[0];
119 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
120 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
121
122 List<SimpleSelectorSequence> simpleSeqs =
123 ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
124
125 for (final selector in simpleSeqs) {
126 final simpSelector = selector.simpleSelector;
127 Expect.isTrue(simpSelector is ElementSelector);
128 Expect.isTrue(selector.isCombinatorNone());
129 Expect.equals(simpSelector.name, "div");
130 }
131
132 parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
133 "div div span {}"));
134 stylesheet = parser.parse();
135 Expect.isNotNull(stylesheet);
136 Expect.equals(stylesheet.topLevels.length, 1);
137
138 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
139 ruleset = stylesheet.topLevels[0];
140 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
141 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
142
143 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
144
145 var idx = 0;
146 for (final selector in simpleSeqs) {
147 final simpSelector = selector.simpleSelector;
148 if (idx == 0) {
149 Expect.isTrue(simpSelector is ElementSelector);
150 Expect.isTrue(selector.isCombinatorNone());
151 Expect.equals(simpSelector.name, "div");
152 } else if (idx == 1) {
153 Expect.isTrue(simpSelector is ElementSelector);
154 Expect.isTrue(selector.isCombinatorDescendant());
155 Expect.equals(simpSelector.name, "div");
156 } else if (idx == 2) {
157 Expect.isTrue(simpSelector is ElementSelector);
158 Expect.isTrue(selector.isCombinatorDescendant());
159 Expect.equals(simpSelector.name, "span");
160 } else {
161 Expect.fail("unexpected expression");
162 }
163
164 idx++;
165 }
166 Expect.equals(simpleSeqs.length, idx);
167 }
168
169 static void testNamespace() {
170 Parser parser = new Parser(new SourceFile(
171 SourceFile.IN_MEMORY_FILE, "ns1|div {}"));
172 Stylesheet stylesheet = parser.parse();
173 Expect.isNotNull(stylesheet);
174 Expect.equals(stylesheet.topLevels.length, 1);
175
176 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
177 RuleSet ruleset = stylesheet.topLevels[0];
178 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
179 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
180
181 List<SimpleSelectorSequence> simpleSeqs =
182 ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
183
184 for (final selector in simpleSeqs) {
185 final simpSelector = selector.simpleSelector;
186 Expect.isTrue(simpSelector is NamespaceSelector);
187 Expect.isTrue(selector.isCombinatorNone());
188 Expect.isFalse(simpSelector.isNamespaceWildcard());
189 Expect.equals(simpSelector.namespace, "ns1");
190 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector;
191 Expect.isTrue(elementSelector is ElementSelector);
192 Expect.isFalse(elementSelector.isWildcard());
193 Expect.equals(elementSelector.name, "div");
194 }
195
196 parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
197 "ns1|div div ns2|span .foobar {}"));
198 stylesheet = parser.parse();
199 Expect.isNotNull(stylesheet);
200
201 Expect.equals(stylesheet.topLevels.length, 1);
202
203 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
204 ruleset = stylesheet.topLevels[0];
205 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
206 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
207
208 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
209
210 var idx = 0;
211 for (final selector in simpleSeqs) {
212 final simpSelector = selector.simpleSelector;
213 if (idx == 0) {
214 Expect.isTrue(simpSelector is NamespaceSelector);
215 Expect.isTrue(selector.isCombinatorNone());
216 Expect.equals(simpSelector.namespace, "ns1");
217 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector;
218 Expect.isTrue(elementSelector is ElementSelector);
219 Expect.isFalse(elementSelector.isWildcard());
220 Expect.equals(elementSelector.name, "div");
221 } else if (idx == 1) {
222 Expect.isTrue(simpSelector is ElementSelector);
223 Expect.isTrue(selector.isCombinatorDescendant());
224 Expect.equals(simpSelector.name, "div");
225 } else if (idx == 2) {
226 Expect.isTrue(simpSelector is NamespaceSelector);
227 Expect.isTrue(selector.isCombinatorDescendant());
228 Expect.equals(simpSelector.namespace, "ns2");
229 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector;
230 Expect.isTrue(elementSelector is ElementSelector);
231 Expect.isFalse(elementSelector.isWildcard());
232 Expect.equals(elementSelector.name, "span");
233 } else if (idx == 3) {
234 Expect.isTrue(simpSelector is ClassSelector);
235 Expect.isTrue(selector.isCombinatorDescendant());
236 Expect.equals(simpSelector.name, "foobar");
237 } else {
238 Expect.fail("unexpected expression");
239 }
240
241 idx++;
242 }
243
244 Expect.equals(simpleSeqs.length, idx);
245 }
246
247 static void testSelectorGroups() {
248 Parser parser = new Parser(new SourceFile(
249 SourceFile.IN_MEMORY_FILE,
250 "div, .foobar ,#elemId, .xyzzy .test, ns1|div div #elemId .foobar {}"));
251 Stylesheet stylesheet = parser.parse();
252 Expect.isNotNull(stylesheet);
253
254 Expect.equals(stylesheet.topLevels.length, 1);
255
256 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
257 RuleSet ruleset = stylesheet.topLevels[0];
258 Expect.equals(ruleset.selectorGroup.selectors.length, 5);
259 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
260
261 var groupIdx = 0;
262 for (final selectorGroup in ruleset.selectorGroup.selectors) {
263 var idx = 0;
264 for (final selector in selectorGroup.simpleSelectorSequences) {
265 final simpSelector = selector.simpleSelector;
266 switch (groupIdx) {
267 case 0: // First selector group.
268 Expect.isTrue(simpSelector is ElementSelector);
269 Expect.isTrue(selector.isCombinatorNone());
270 Expect.equals(simpSelector.name, "div");
271 break;
272 case 1: // Second selector group.
273 Expect.isTrue(simpSelector is ClassSelector);
274 Expect.isTrue(selector.isCombinatorNone());
275 Expect.equals(simpSelector.name, "foobar");
276 break;
277 case 2: // Third selector group.
278 Expect.isTrue(simpSelector is IdSelector);
279 Expect.isTrue(selector.isCombinatorNone());
280 Expect.equals(simpSelector.name, "elemId");
281 break;
282 case 3: // Fourth selector group.
283 Expect.equals(selectorGroup.simpleSelectorSequences.length, 2);
284 if (idx == 0) {
285 Expect.isTrue(simpSelector is ClassSelector);
286 Expect.isTrue(selector.isCombinatorNone());
287 Expect.equals(simpSelector.name, "xyzzy");
288 } else if (idx == 1) {
289 Expect.isTrue(simpSelector is ClassSelector);
290 Expect.isTrue(selector.isCombinatorDescendant());
291 Expect.equals(simpSelector.name, "test");
292 } else {
293 Expect.fail("unexpected expression");
294 }
295 break;
296 case 4: // Fifth selector group.
297 Expect.equals(selectorGroup.simpleSelectorSequences.length, 4);
298 if (idx == 0) {
299 Expect.isTrue(simpSelector is NamespaceSelector);
300 Expect.isTrue(selector.isCombinatorNone());
301 Expect.equals(simpSelector.namespace, "ns1");
302 ElementSelector elementSelector = simpSelector.nameAsSimpleSelecto r;
303 Expect.isTrue(elementSelector is ElementSelector);
304 Expect.isFalse(elementSelector.isWildcard());
305 Expect.equals(elementSelector.name, "div");
306 } else if (idx == 1) {
307 Expect.isTrue(simpSelector is ElementSelector);
308 Expect.isTrue(selector.isCombinatorDescendant());
309 Expect.equals(simpSelector.name, "div");
310 } else if (idx == 2) {
311 Expect.isTrue(simpSelector is IdSelector);
312 Expect.isTrue(selector.isCombinatorDescendant());
313 Expect.equals(simpSelector.name, "elemId");
314 } else if (idx == 3) {
315 Expect.isTrue(simpSelector is ClassSelector);
316 Expect.isTrue(selector.isCombinatorDescendant());
317 Expect.equals(simpSelector.name, "foobar");
318 } else {
319 Expect.fail("unexpected expression");
320 }
321 break;
322 }
323 idx++;
324 }
325 groupIdx++;
326 }
327 }
328
329 static void testCombinator() {
330 Parser parser = new Parser(new SourceFile(
331 SourceFile.IN_MEMORY_FILE,
332 ".foobar > .bar + .no-story ~ myNs|div #elemId {}"));
333
334 Stylesheet stylesheet = parser.parse();
335 Expect.isNotNull(stylesheet);
336 Expect.equals(stylesheet.topLevels.length, 1);
337
338 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
339 RuleSet ruleset = stylesheet.topLevels[0];
340 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
341 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
342
343 List<SimpleSelectorSequence> simpleSeqs =
344 ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
345
346 Expect.equals(simpleSeqs.length, 5);
347 var idx = 0;
348 for (final selector in simpleSeqs) {
349 final simpSelector = selector.simpleSelector;
350 if (idx == 0) {
351 Expect.isTrue(simpSelector is ClassSelector);
352 Expect.isTrue(selector.isCombinatorNone());
353 Expect.equals(simpSelector.name, "foobar");
354 } else if (idx == 1) {
355 Expect.isTrue(simpSelector is ClassSelector);
356 Expect.isTrue(selector.isCombinatorGreater());
357 Expect.equals(simpSelector.name, "bar");
358 } else if (idx == 2) {
359 Expect.isTrue(simpSelector is ClassSelector);
360 Expect.isTrue(selector.isCombinatorPlus());
361 Expect.equals(simpSelector.name, "no-story");
362 } else if (idx == 3) {
363 Expect.isTrue(simpSelector is NamespaceSelector);
364 Expect.isTrue(selector.isCombinatorTilde());
365 Expect.equals(simpSelector.namespace, "myNs");
366 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector;
367 Expect.isTrue(elementSelector is ElementSelector);
368 Expect.isFalse(elementSelector.isWildcard());
369 Expect.equals(elementSelector.name, "div");
370 } else if (idx == 4) {
371 Expect.isTrue(simpSelector is IdSelector);
372 Expect.isTrue(selector.isCombinatorDescendant());
373 Expect.equals(simpSelector.name, "elemId");
374 } else {
375 Expect.fail("unexpected expression");
376 }
377
378 idx++;
379 }
380 }
381
382 static void testWildcard() {
383 Parser parser = new Parser(new SourceFile(
384 SourceFile.IN_MEMORY_FILE, "* {}"));
385
386 Stylesheet stylesheet = parser.parse();
387 Expect.isNotNull(stylesheet);
388 Expect.equals(stylesheet.topLevels.length, 1);
389
390 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
391 RuleSet ruleset = stylesheet.topLevels[0];
392 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
393 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
394
395 List<SimpleSelectorSequence> simpleSeqs =
396 ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
397
398 for (final selector in simpleSeqs) {
399 final simpSelector = selector.simpleSelector;
400 Expect.isTrue(simpSelector is ElementSelector);
401 Expect.isTrue(selector.isCombinatorNone());
402 Expect.isTrue(simpSelector.isWildcard());
403 Expect.equals(simpSelector.name, "*");
404 }
405
406 parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
407 "*.foobar {}"));
408
409 stylesheet = parser.parse();
410 Expect.isNotNull(stylesheet);
411 Expect.equals(stylesheet.topLevels.length, 1);
412
413 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
414 ruleset = stylesheet.topLevels[0];
415 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
416 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
417
418 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
419
420 Expect.equals(simpleSeqs.length, 2);
421 var idx = 0;
422 for (final selector in simpleSeqs) {
423 final simpSelector = selector.simpleSelector;
424 if (idx == 0) {
425 Expect.isTrue(simpSelector is ElementSelector);
426 Expect.isTrue(selector.isCombinatorNone());
427 Expect.isTrue(simpSelector.isWildcard());
428 Expect.equals(simpSelector.name, "*");
429 } else if (idx == 1) {
430 Expect.isTrue(simpSelector is ClassSelector);
431 Expect.isTrue(selector.isCombinatorNone());
432 Expect.equals(simpSelector.name, "foobar");
433 } else {
434 Expect.fail("unexpected expression");
435 }
436
437 idx++;
438 }
439
440 parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
441 "myNs|*.foobar {}"));
442
443 stylesheet = parser.parse();
444 Expect.isNotNull(stylesheet);
445 Expect.equals(stylesheet.topLevels.length, 1);
446
447 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
448 ruleset = stylesheet.topLevels[0];
449 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
450 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
451
452 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
453
454 Expect.equals(simpleSeqs.length, 2);
455 idx = 0;
456 for (final selector in simpleSeqs) {
457 final simpSelector = selector.simpleSelector;
458 if (idx == 0) {
459 Expect.isTrue(simpSelector is NamespaceSelector);
460 Expect.isTrue(selector.isCombinatorNone());
461 Expect.isFalse(simpSelector.isNamespaceWildcard());
462 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector;
463 Expect.equals("myNs", simpSelector.namespace);
464 Expect.isTrue(elementSelector.isWildcard());
465 Expect.equals("*", elementSelector.name);
466 } else if (idx == 1) {
467 Expect.isTrue(simpSelector is ClassSelector);
468 Expect.isTrue(selector.isCombinatorNone());
469 Expect.equals("foobar", simpSelector.name);
470 } else {
471 Expect.fail("unexpected expression");
472 }
473
474 idx++;
475 }
476
477 parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
478 "*|*.foobar {}"));
479
480 stylesheet = parser.parse();
481 Expect.isNotNull(stylesheet);
482
483 Expect.isTrue(stylesheet.topLevels[0] is RuleSet);
484 ruleset = stylesheet.topLevels[0];
485 Expect.equals(ruleset.selectorGroup.selectors.length, 1);
486 Expect.equals(ruleset.declarationGroup.declarations.length, 0);
487
488 simpleSeqs = ruleset.selectorGroup.selectors[0].simpleSelectorSequences;
489
490 Expect.equals(simpleSeqs.length, 2);
491 idx = 0;
492 for (final selector in simpleSeqs) {
493 final simpSelector = selector.simpleSelector;
494 if (idx == 0) {
495 Expect.isTrue(simpSelector is NamespaceSelector);
496 Expect.isTrue(selector.isCombinatorNone());
497 Expect.isTrue(simpSelector.isNamespaceWildcard());
498 Expect.equals("*", simpSelector.namespace);
499 ElementSelector elementSelector = simpSelector.nameAsSimpleSelector;
500 Expect.isTrue(elementSelector.isWildcard());
501 Expect.equals("*", elementSelector.name);
502 } else if (idx == 1) {
503 Expect.isTrue(simpSelector is ClassSelector);
504 Expect.isTrue(selector.isCombinatorNone());
505 Expect.equals("foobar", simpSelector.name);
506 } else {
507 Expect.fail("unexpected expression");
508 }
509
510 idx++;
511 }
512
513 }
514
515 static void testPseudo() {
516 // TODO(terry): Implement
517 }
518
519 static void testAttribute() {
520 // TODO(terry): Implement
521 }
522
523 static void testNegation() {
524 // TODO(terry): Implement
525 }
526
527 }
528
529
530 main() {
531 ExpressionTest.testMain();
532 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698