| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, 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 directive_parser_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:unittest/vm_config.dart'; |
| 9 import 'package:web_components/src/info.dart' show DartCodeInfo; |
| 10 import 'package:web_components/src/messages.dart' show messages; |
| 11 import 'package:web_components/src/directive_parser.dart'; |
| 12 import 'testing.dart'; |
| 13 |
| 14 main() { |
| 15 useVmConfiguration(); |
| 16 useMockMessages(); |
| 17 |
| 18 test('empty contents', () { |
| 19 var info = _parse(''); |
| 20 expect(info, isNotNull); |
| 21 expect(info.libraryName, isNull); |
| 22 expect(info.partOf, isNull); |
| 23 expect(info.directives, isEmpty); |
| 24 expect(info.code, isEmpty); |
| 25 }); |
| 26 |
| 27 test('no directives, no code', () { |
| 28 var code = '/* a comment */\n\n/** another *///foo\n'; |
| 29 var info = _parse(code); |
| 30 expect(info, isNotNull); |
| 31 expect(info.libraryName, isNull); |
| 32 expect(info.partOf, isNull); |
| 33 expect(info.directives, isEmpty); |
| 34 expect(info.code, isEmpty); |
| 35 }); |
| 36 |
| 37 test('no directives, some code', () { |
| 38 var code = '/* a comment */\n\n/** another *///foo\ncode();'; |
| 39 var info = _parse(code); |
| 40 expect(info, isNotNull); |
| 41 expect(info.libraryName, isNull); |
| 42 expect(info.partOf, isNull); |
| 43 expect(info.directives, isEmpty); |
| 44 expect(info.code, equals('code();')); |
| 45 }); |
| 46 |
| 47 test('library, but no directives', () { |
| 48 var code = '// a comment\n library foo;\n/* \n\n */ code();\n'; |
| 49 var info = _parse(code); |
| 50 expect(info, isNotNull); |
| 51 expect(info.libraryName, equals('foo')); |
| 52 expect(info.partOf, isNull); |
| 53 expect(info.directives, isEmpty); |
| 54 expect(info.code, equals('code();\n')); |
| 55 }); |
| 56 |
| 57 test('directives, but no library', () { |
| 58 var code = 'import "url";\n code();\n'; |
| 59 var info = _parse(code); |
| 60 expect(info, isNotNull); |
| 61 expect(info.libraryName, isNull); |
| 62 expect(info.partOf, isNull); |
| 63 expect(info.directives, hasLength(1)); |
| 64 expect(info.directives[0].uri, equals('url')); |
| 65 expect(info.directives[0].prefix, isNull); |
| 66 expect(info.directives[0].hide, isNull); |
| 67 expect(info.directives[0].show, isNull); |
| 68 expect(info.code, equals('code();\n')); |
| 69 }); |
| 70 |
| 71 test('import with multiple strings', () { |
| 72 var code = 'import "url"\n"foo";'; |
| 73 var info = _parse(code); |
| 74 expect(info.directives, hasLength(1)); |
| 75 expect(info.directives[0].uri, equals('urlfoo')); |
| 76 }); |
| 77 |
| 78 test('directives, no prefix', () { |
| 79 var code = 'library foo.bar; import \'url2\';\n' |
| 80 'export \'foo.dart\';\npart "part.dart";\n code();\n'; |
| 81 var info = _parse(code); |
| 82 expect(info, isNotNull); |
| 83 expect(info.libraryName, equals('foo.bar')); |
| 84 expect(info.partOf, isNull); |
| 85 expect(info.directives, hasLength(3)); |
| 86 expect(info.directives[0].label, equals('import')); |
| 87 expect(info.directives[0].uri, equals('url2')); |
| 88 expect(info.directives[0].prefix, isNull); |
| 89 expect(info.directives[0].hide, isNull); |
| 90 expect(info.directives[0].show, isNull); |
| 91 expect(info.directives[1].label, equals('export')); |
| 92 expect(info.directives[1].uri, equals('foo.dart')); |
| 93 expect(info.directives[1].prefix, isNull); |
| 94 expect(info.directives[1].hide, isNull); |
| 95 expect(info.directives[1].show, isNull); |
| 96 expect(info.directives[2].label, equals('part')); |
| 97 expect(info.directives[2].uri, equals("part.dart")); |
| 98 expect(info.code, equals('code();\n')); |
| 99 }); |
| 100 |
| 101 test('part-of, but no directives', () { |
| 102 var code = '/* ... */ part of foo.bar;\n/* \n\n */ code();\n'; |
| 103 var info = _parse(code); |
| 104 expect(info, isNotNull); |
| 105 expect(info.libraryName, isNull); |
| 106 expect(info.partOf, equals('foo.bar')); |
| 107 expect(info.directives, isEmpty); |
| 108 expect(info.code, equals('code();\n')); |
| 109 }); |
| 110 |
| 111 test('directives with prefix', () { |
| 112 var code = 'library f;' |
| 113 'import "a1.dart" as first;\n' |
| 114 'import "a2.dart" as second;\n' |
| 115 'import "a3.dart" as i3;\n' |
| 116 'code();\n'; |
| 117 var info = _parse(code); |
| 118 expect(info, isNotNull); |
| 119 expect(info.libraryName, equals('f')); |
| 120 expect(info.partOf, isNull); |
| 121 expect(info.directives, hasLength(3)); |
| 122 expect(info.directives[0].label, equals('import')); |
| 123 expect(info.directives[0].uri, equals('a1.dart')); |
| 124 expect(info.directives[0].prefix, equals('first')); |
| 125 expect(info.directives[0].hide, isNull); |
| 126 expect(info.directives[0].show, isNull); |
| 127 expect(info.directives[1].label, equals('import')); |
| 128 expect(info.directives[1].uri, equals('a2.dart')); |
| 129 expect(info.directives[1].prefix, equals('second')); |
| 130 expect(info.directives[1].hide, isNull); |
| 131 expect(info.directives[1].show, isNull); |
| 132 expect(info.directives[2].label, equals('import')); |
| 133 expect(info.directives[2].uri, equals('a3.dart')); |
| 134 expect(info.directives[2].prefix, equals('i3')); |
| 135 expect(info.directives[2].hide, isNull); |
| 136 expect(info.directives[2].show, isNull); |
| 137 expect(info.code, equals('code();\n')); |
| 138 }); |
| 139 |
| 140 test('directives with combinators', () { |
| 141 var code = 'library f;' |
| 142 'import "a1.dart" as a1 show one, two hide bar;\n' |
| 143 'export "a2.dart" show a, b, c hide d, e;\n' |
| 144 'code();\n'; |
| 145 var info = _parse(code); |
| 146 expect(info, isNotNull); |
| 147 expect(info.libraryName, equals('f')); |
| 148 expect(info.partOf, isNull); |
| 149 expect(info.directives, hasLength(2)); |
| 150 expect(info.directives[0].label, equals('import')); |
| 151 expect(info.directives[0].uri, equals('a1.dart')); |
| 152 expect(info.directives[0].prefix, equals('a1')); |
| 153 expect(info.directives[0].hide, equals(['bar'])); |
| 154 expect(info.directives[0].show, equals(['one', 'two'])); |
| 155 expect(info.directives[1].label, equals('export')); |
| 156 expect(info.directives[1].uri, equals('a2.dart')); |
| 157 expect(info.directives[1].prefix, isNull); |
| 158 expect(info.directives[1].hide, equals(['d', 'e'])); |
| 159 expect(info.directives[1].show, equals(['a', 'b', 'c'])); |
| 160 expect(info.code, equals('code();\n')); |
| 161 }); |
| 162 } |
| 163 |
| 164 _parse(String code) => parseDartCode(code, null, messages); |
| OLD | NEW |