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:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:csslib/visitor.dart'; | 8 import 'package:csslib/visitor.dart'; |
9 import 'testing.dart'; | 9 import 'testing.dart'; |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 var in1 = '.foobar { }'; | 36 var in1 = '.foobar { }'; |
37 | 37 |
38 var s = parseCss(in1, errors: errors); | 38 var s = parseCss(in1, errors: errors); |
39 | 39 |
40 expect(s != null, true); | 40 expect(s != null, true); |
41 expect(errors.isEmpty, true, reason: errors.toString()); | 41 expect(errors.isEmpty, true, reason: errors.toString()); |
42 | 42 |
43 var clsVisits = new ClassVisitor(['foobar'])..visitTree(s); | 43 var clsVisits = new ClassVisitor(['foobar'])..visitTree(s); |
44 expect(clsVisits.matches, true); | 44 expect(clsVisits.matches, true); |
45 | 45 |
46 in1= ''' | 46 in1 = ''' |
47 .foobar1 { } | 47 .foobar1 { } |
48 .xyzzy .foo #my-div { color: red; } | 48 .xyzzy .foo #my-div { color: red; } |
49 div.hello { font: arial; } | 49 div.hello { font: arial; } |
50 '''; | 50 '''; |
51 | 51 |
52 s = parseCss(in1, errors: errors..clear(), opts: ['--no-colors', 'memory']); | 52 s = parseCss(in1, errors: errors..clear(), opts: ['--no-colors', 'memory']); |
53 | 53 |
54 expect(s != null, true); | 54 expect(s != null, true); |
55 expect(errors.isEmpty, true, reason: errors.toString()); | 55 expect(errors.isEmpty, true, reason: errors.toString()); |
56 | 56 |
57 clsVisits = | 57 clsVisits = new ClassVisitor(['foobar1', 'xyzzy', 'foo', 'hello']) |
58 new ClassVisitor(['foobar1', 'xyzzy', 'foo', 'hello'])..visitTree(s); | 58 ..visitTree(s); |
59 expect(clsVisits.matches, true); | 59 expect(clsVisits.matches, true); |
60 | 60 |
61 expect(prettyPrint(s), r''' | 61 expect(prettyPrint(s), r''' |
62 .foobar1 { | 62 .foobar1 { |
63 } | 63 } |
64 .xyzzy .foo #my-div { | 64 .xyzzy .foo #my-div { |
65 color: #f00; | 65 color: #f00; |
66 } | 66 } |
67 div.hello { | 67 div.hello { |
68 font: arial; | 68 font: arial; |
69 }'''); | 69 }'''); |
70 } | 70 } |
71 | 71 |
72 class PolyfillEmitter extends CssPrinter { | 72 class PolyfillEmitter extends CssPrinter { |
73 final String _prefix; | 73 final String _prefix; |
74 | 74 |
75 PolyfillEmitter(this._prefix); | 75 PolyfillEmitter(this._prefix); |
76 | 76 |
77 void visitClassSelector(ClassSelector node) { | 77 void visitClassSelector(ClassSelector node) { |
78 emit('.${_prefix}_${node.name}'); | 78 emit('.${_prefix}_${node.name}'); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 String polyfillPrint(String prefix, StyleSheet ss) => | 82 String polyfillPrint(String prefix, StyleSheet ss) => |
83 (new PolyfillEmitter(prefix)..visitTree(ss, pretty: true)).toString(); | 83 (new PolyfillEmitter(prefix)..visitTree(ss, pretty: true)).toString(); |
84 | 84 |
85 void testPolyFill() { | 85 void testPolyFill() { |
86 var errors = []; | 86 var errors = []; |
87 final input = r''' | 87 final input = r''' |
88 .foobar { } | 88 .foobar { } |
89 div.xyzzy { } | 89 div.xyzzy { } |
90 #foo .foo .bar .foobar { } | 90 #foo .foo .bar .foobar { } |
91 '''; | 91 '''; |
92 | 92 |
93 final generated = r''' | 93 final generated = r''' |
94 .myComponent_foobar { | 94 .myComponent_foobar { |
95 } | 95 } |
96 div.myComponent_xyzzy { | 96 div.myComponent_xyzzy { |
97 } | 97 } |
98 #foo .myComponent_foo .myComponent_bar .myComponent_foobar { | 98 #foo .myComponent_foo .myComponent_bar .myComponent_foobar { |
99 }'''; | 99 }'''; |
100 | 100 |
101 var s = parseCss(input, errors: errors); | 101 var s = parseCss(input, errors: errors); |
102 expect(s != null, true); | 102 expect(s != null, true); |
103 expect(errors.isEmpty, true, reason: errors.toString()); | 103 expect(errors.isEmpty, true, reason: errors.toString()); |
104 | 104 |
105 final emitted = polyfillPrint('myComponent', s); | 105 final emitted = polyfillPrint('myComponent', s); |
106 expect(emitted, generated); | 106 expect(emitted, generated); |
107 } | 107 } |
108 | 108 |
109 main() { | 109 main() { |
110 test('Class Visitors', testClassVisitors); | 110 test('Class Visitors', testClassVisitors); |
111 test('Polyfill', testPolyFill); | 111 test('Polyfill', testPolyFill); |
112 } | 112 } |
OLD | NEW |