OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library visitor_test; | 5 library visitor_test; |
6 | 6 |
| 7 import 'package:csslib/src/messages.dart'; |
7 import 'package:csslib/visitor.dart'; | 8 import 'package:csslib/visitor.dart'; |
8 import 'package:test/test.dart'; | 9 import 'package:test/test.dart'; |
9 | 10 |
10 import 'testing.dart'; | 11 import 'testing.dart'; |
11 | 12 |
12 class ClassVisitor extends Visitor { | 13 class ClassVisitor extends Visitor { |
13 final List expectedClasses; | 14 final List expectedClasses; |
14 final Set<String> foundClasses = new Set(); | 15 final Set<String> foundClasses = new Set(); |
15 | 16 |
16 ClassVisitor(this.expectedClasses); | 17 ClassVisitor(this.expectedClasses); |
17 | 18 |
18 void visitClassSelector(ClassSelector node) { | 19 void visitClassSelector(ClassSelector node) { |
19 foundClasses.add(node.name); | 20 foundClasses.add(node.name); |
20 } | 21 } |
21 | 22 |
22 bool get matches { | 23 bool get matches { |
23 bool match = true; | 24 bool match = true; |
24 foundClasses.forEach((value) { | 25 foundClasses.forEach((value) { |
25 match = match && expectedClasses.contains(value); | 26 match = match && expectedClasses.contains(value); |
26 }); | 27 }); |
27 expectedClasses.forEach((value) { | 28 expectedClasses.forEach((value) { |
28 match = match && foundClasses.contains(value); | 29 match = match && foundClasses.contains(value); |
29 }); | 30 }); |
30 | 31 |
31 return match; | 32 return match; |
32 } | 33 } |
33 } | 34 } |
34 | 35 |
35 void testClassVisitors() { | 36 void testClassVisitors() { |
36 var errors = []; | 37 var errors = <Message>[]; |
37 var in1 = '.foobar { }'; | 38 var in1 = '.foobar { }'; |
38 | 39 |
39 var s = parseCss(in1, errors: errors); | 40 var s = parseCss(in1, errors: errors); |
40 | 41 |
41 expect(s != null, true); | 42 expect(s != null, true); |
42 expect(errors.isEmpty, true, reason: errors.toString()); | 43 expect(errors.isEmpty, true, reason: errors.toString()); |
43 | 44 |
44 var clsVisits = new ClassVisitor(['foobar'])..visitTree(s); | 45 var clsVisits = new ClassVisitor(['foobar'])..visitTree(s); |
45 expect(clsVisits.matches, true); | 46 expect(clsVisits.matches, true); |
46 | 47 |
(...skipping 30 matching lines...) Expand all Loading... |
77 | 78 |
78 void visitClassSelector(ClassSelector node) { | 79 void visitClassSelector(ClassSelector node) { |
79 emit('.${_prefix}_${node.name}'); | 80 emit('.${_prefix}_${node.name}'); |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 String polyfillPrint(String prefix, StyleSheet ss) => | 84 String polyfillPrint(String prefix, StyleSheet ss) => |
84 (new PolyfillEmitter(prefix)..visitTree(ss, pretty: true)).toString(); | 85 (new PolyfillEmitter(prefix)..visitTree(ss, pretty: true)).toString(); |
85 | 86 |
86 void testPolyFill() { | 87 void testPolyFill() { |
87 var errors = []; | 88 var errors = <Message>[]; |
88 final input = r''' | 89 final input = r''' |
89 .foobar { } | 90 .foobar { } |
90 div.xyzzy { } | 91 div.xyzzy { } |
91 #foo .foo .bar .foobar { } | 92 #foo .foo .bar .foobar { } |
92 '''; | 93 '''; |
93 | 94 |
94 final generated = r''' | 95 final generated = r''' |
95 .myComponent_foobar { | 96 .myComponent_foobar { |
96 } | 97 } |
97 div.myComponent_xyzzy { | 98 div.myComponent_xyzzy { |
98 } | 99 } |
99 #foo .myComponent_foo .myComponent_bar .myComponent_foobar { | 100 #foo .myComponent_foo .myComponent_bar .myComponent_foobar { |
100 }'''; | 101 }'''; |
101 | 102 |
102 var s = parseCss(input, errors: errors); | 103 var s = parseCss(input, errors: errors); |
103 expect(s != null, true); | 104 expect(s != null, true); |
104 expect(errors.isEmpty, true, reason: errors.toString()); | 105 expect(errors.isEmpty, true, reason: errors.toString()); |
105 | 106 |
106 final emitted = polyfillPrint('myComponent', s); | 107 final emitted = polyfillPrint('myComponent', s); |
107 expect(emitted, generated); | 108 expect(emitted, generated); |
108 } | 109 } |
109 | 110 |
110 main() { | 111 main() { |
111 test('Class Visitors', testClassVisitors); | 112 test('Class Visitors', testClassVisitors); |
112 test('Polyfill', testPolyFill); | 113 test('Polyfill', testPolyFill); |
113 } | 114 } |
OLD | NEW |