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