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