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