| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "dart:uri"; | 5 import "dart:uri"; |
| 6 | 6 |
| 7 import 'compiler_helper.dart'; | 7 import 'compiler_helper.dart'; |
| 8 import 'parser_helper.dart'; | 8 import 'parser_helper.dart'; |
| 9 | 9 |
| 10 import '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t'; | 10 import '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t'; |
| 11 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart'; | 11 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart'; |
| 12 import '../../../sdk/lib/_internal/compiler/implementation/util/util.dart' | 12 import '../../../sdk/lib/_internal/compiler/implementation/util/util.dart' |
| 13 show Spannable; | 13 show Spannable, Link; |
| 14 import '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' | 14 import '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' |
| 15 show Node; | 15 show Node, LibraryTag; |
| 16 | 16 |
| 17 void checkPosition(Spannable spannable, Node node, String source, compiler) { | 17 void checkPosition(Spannable spannable, Node node, String source, compiler) { |
| 18 SourceSpan span = compiler.spanFromSpannable(spannable); | 18 SourceSpan span = compiler.spanFromSpannable(spannable); |
| 19 Expect.isTrue(span.begin < span.end, | 19 Expect.isTrue(span.begin < span.end, |
| 20 'begin = ${span.begin}; end = ${span.end}'); | 20 'begin = ${span.begin}; end = ${span.end}'); |
| 21 Expect.isTrue(span.end < source.length, | 21 Expect.isTrue(span.end < source.length, |
| 22 'end = ${span.end}; length = ${source.length}'); | 22 'end = ${span.end}; length = ${source.length}'); |
| 23 String yield = source.substring(span.begin, span.end); | 23 String yield = source.substring(span.begin, span.end); |
| 24 | 24 |
| 25 // TODO(ahe): The node does not include "@". Fix that. | 25 // TODO(ahe): The node does not include "@". Fix that. |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 140 } |
| 141 | 141 |
| 142 void testTopLevelMethodMetadata() { | 142 void testTopLevelMethodMetadata() { |
| 143 checkAnnotation('foo', 'foo() {}'); | 143 checkAnnotation('foo', 'foo() {}'); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void testTopLevelFieldMetadata() { | 146 void testTopLevelFieldMetadata() { |
| 147 checkAnnotation('foo', 'var foo;'); | 147 checkAnnotation('foo', 'var foo;'); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void testLibraryTags() { |
| 151 void compileAndCheckLibrary( |
| 152 String source, |
| 153 Link<MetadataAnnotation> extractMetadata(LibraryElement element)) { |
| 154 Uri partUri = new Uri.fromComponents(scheme: 'source', path: 'part.dart'); |
| 155 String partSource = '@native part of foo;'; |
| 156 |
| 157 Uri libUri = new Uri.fromComponents(scheme: 'source', path: 'lib.dart'); |
| 158 String libSource = 'library lib;'; |
| 159 |
| 160 Uri uri = new Uri.fromComponents(scheme: 'source', path: 'main.dart'); |
| 161 |
| 162 var compiler = compilerFor(source, uri) |
| 163 ..registerSource(partUri, partSource) |
| 164 ..registerSource(libUri, libSource) |
| 165 ..runCompiler(uri); |
| 166 compiler.enqueuer.resolution.queueIsClosed = false; |
| 167 LibraryElement element = compiler.libraries['$uri']; |
| 168 Expect.isNotNull(element, 'Cannot find $uri'); |
| 169 |
| 170 Link<MetadataAnnotation> metadata = extractMetadata(element); |
| 171 Expect.equals(1, length(metadata)); |
| 172 |
| 173 PartialMetadataAnnotation annotation = metadata.head; |
| 174 annotation.ensureResolved(compiler); |
| 175 Constant value = annotation.value; |
| 176 Expect.stringEquals('xyz', value.value.slowToString()); |
| 177 |
| 178 checkPosition(annotation, annotation.cachedNode, source, compiler); |
| 179 } |
| 180 |
| 181 var source; |
| 182 |
| 183 source = """@native |
| 184 library foo; |
| 185 const native = 'xyz'; |
| 186 main() {}"""; |
| 187 compileAndCheckLibrary(source, (e) => e.libraryTag.metadata); |
| 188 |
| 189 source = """@native |
| 190 import 'lib.dart'; |
| 191 const native = 'xyz'; |
| 192 main() {}"""; |
| 193 compileAndCheckLibrary(source, (e) => e.tags.single.metadata); |
| 194 |
| 195 source = """@native |
| 196 export 'lib.dart'; |
| 197 const native = 'xyz'; |
| 198 main() {}"""; |
| 199 compileAndCheckLibrary(source, (e) => e.tags.single.metadata); |
| 200 |
| 201 source = """@native |
| 202 part 'part.dart'; |
| 203 const native = 'xyz'; |
| 204 main() {}"""; |
| 205 compileAndCheckLibrary(source, (e) => e.tags.single.metadata); |
| 206 |
| 207 source = """@native |
| 208 part 'part.dart'; |
| 209 const native = 'xyz'; |
| 210 main() {}"""; |
| 211 compileAndCheckLibrary(source, |
| 212 (e) => e.compilationUnits.first.partTag.metadata); |
| 213 } |
| 214 |
| 150 void main() { | 215 void main() { |
| 151 testClassMetadata(); | 216 testClassMetadata(); |
| 152 testTopLevelMethodMetadata(); | 217 testTopLevelMethodMetadata(); |
| 153 testTopLevelFieldMetadata(); | 218 testTopLevelFieldMetadata(); |
| 219 testLibraryTags(); |
| 154 } | 220 } |
| OLD | NEW |