OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library visitor_test; |
| 6 |
| 7 import 'package:csslib/visitor.dart'; |
| 8 import 'package:test/test.dart'; |
| 9 |
| 10 import 'testing.dart'; |
| 11 |
| 12 class ClassVisitor extends Visitor { |
| 13 final List expectedClasses; |
| 14 final Set<String> foundClasses = new Set(); |
| 15 |
| 16 ClassVisitor(this.expectedClasses); |
| 17 |
| 18 void visitClassSelector(ClassSelector node) { |
| 19 foundClasses.add(node.name); |
| 20 } |
| 21 |
| 22 bool get matches { |
| 23 bool match = true; |
| 24 foundClasses.forEach((value) { |
| 25 match = match && expectedClasses.contains(value); |
| 26 }); |
| 27 expectedClasses.forEach((value) { |
| 28 match = match && foundClasses.contains(value); |
| 29 }); |
| 30 |
| 31 return match; |
| 32 } |
| 33 } |
| 34 |
| 35 void testClassVisitors() { |
| 36 var errors = []; |
| 37 var in1 = '.foobar { }'; |
| 38 |
| 39 var s = parseCss(in1, errors: errors); |
| 40 |
| 41 expect(s != null, true); |
| 42 expect(errors.isEmpty, true, reason: errors.toString()); |
| 43 |
| 44 var clsVisits = new ClassVisitor(['foobar'])..visitTree(s); |
| 45 expect(clsVisits.matches, true); |
| 46 |
| 47 in1 = ''' |
| 48 .foobar1 { } |
| 49 .xyzzy .foo #my-div { color: red; } |
| 50 div.hello { font: arial; } |
| 51 '''; |
| 52 |
| 53 s = parseCss(in1, errors: errors..clear(), opts: simpleOptions); |
| 54 |
| 55 expect(s != null, true); |
| 56 expect(errors.isEmpty, true, reason: errors.toString()); |
| 57 |
| 58 clsVisits = new ClassVisitor(['foobar1', 'xyzzy', 'foo', 'hello']) |
| 59 ..visitTree(s); |
| 60 expect(clsVisits.matches, true); |
| 61 |
| 62 expect(prettyPrint(s), r''' |
| 63 .foobar1 { |
| 64 } |
| 65 .xyzzy .foo #my-div { |
| 66 color: #f00; |
| 67 } |
| 68 div.hello { |
| 69 font: arial; |
| 70 }'''); |
| 71 } |
| 72 |
| 73 class PolyfillEmitter extends CssPrinter { |
| 74 final String _prefix; |
| 75 |
| 76 PolyfillEmitter(this._prefix); |
| 77 |
| 78 void visitClassSelector(ClassSelector node) { |
| 79 emit('.${_prefix}_${node.name}'); |
| 80 } |
| 81 } |
| 82 |
| 83 String polyfillPrint(String prefix, StyleSheet ss) => |
| 84 (new PolyfillEmitter(prefix)..visitTree(ss, pretty: true)).toString(); |
| 85 |
| 86 void testPolyFill() { |
| 87 var errors = []; |
| 88 final input = r''' |
| 89 .foobar { } |
| 90 div.xyzzy { } |
| 91 #foo .foo .bar .foobar { } |
| 92 '''; |
| 93 |
| 94 final generated = r''' |
| 95 .myComponent_foobar { |
| 96 } |
| 97 div.myComponent_xyzzy { |
| 98 } |
| 99 #foo .myComponent_foo .myComponent_bar .myComponent_foobar { |
| 100 }'''; |
| 101 |
| 102 var s = parseCss(input, errors: errors); |
| 103 expect(s != null, true); |
| 104 expect(errors.isEmpty, true, reason: errors.toString()); |
| 105 |
| 106 final emitted = polyfillPrint('myComponent', s); |
| 107 expect(emitted, generated); |
| 108 } |
| 109 |
| 110 main() { |
| 111 test('Class Visitors', testClassVisitors); |
| 112 test('Polyfill', testPolyFill); |
| 113 } |
OLD | NEW |