| 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 'package:async_helper/async_helper.dart'; | 5 import 'package:async_helper/async_helper.dart'; |
| 6 import 'package:compiler/src/constants/values.dart' | 6 import 'package:compiler/src/constants/values.dart' show PrimitiveConstantValue; |
| 7 show PrimitiveConstantValue; | |
| 8 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
| 9 import 'compiler_helper.dart'; | 8 import 'compiler_helper.dart'; |
| 10 import 'package:compiler/src/parser/partial_elements.dart' show | 9 import 'package:compiler/src/parser/partial_elements.dart' |
| 11 PartialMetadataAnnotation; | 10 show PartialMetadataAnnotation; |
| 12 import 'package:compiler/src/diagnostics/diagnostic_listener.dart' show | 11 import 'package:compiler/src/diagnostics/diagnostic_listener.dart' |
| 13 DiagnosticReporter; | 12 show DiagnosticReporter; |
| 14 | 13 |
| 15 void checkPosition(Spannable spannable, | 14 void checkPosition(Spannable spannable, Node node, String source, |
| 16 Node node, | 15 DiagnosticReporter reporter) { |
| 17 String source, | |
| 18 DiagnosticReporter reporter) { | |
| 19 SourceSpan span = reporter.spanFromSpannable(spannable); | 16 SourceSpan span = reporter.spanFromSpannable(spannable); |
| 20 Expect.isTrue(span.begin < span.end, | 17 Expect.isTrue( |
| 21 'begin = ${span.begin}; end = ${span.end}'); | 18 span.begin < span.end, 'begin = ${span.begin}; end = ${span.end}'); |
| 22 Expect.isTrue(span.end < source.length, | 19 Expect.isTrue( |
| 23 'end = ${span.end}; length = ${source.length}'); | 20 span.end < source.length, 'end = ${span.end}; length = ${source.length}'); |
| 24 String yield = source.substring(span.begin, span.end); | 21 String yield = source.substring(span.begin, span.end); |
| 25 | 22 |
| 26 // TODO(ahe): The node does not include "@". Fix that. | 23 // TODO(ahe): The node does not include "@". Fix that. |
| 27 Expect.stringEquals('@$node', yield); | 24 Expect.stringEquals('@$node', yield); |
| 28 } | 25 } |
| 29 | 26 |
| 30 void checkAnnotation(String name, String declaration, | 27 void checkAnnotation(String name, String declaration, |
| 31 {bool isTopLevelOnly: false}) { | 28 {bool isTopLevelOnly: false}) { |
| 32 // Ensure that a compile-time constant can be resolved from an | 29 // Ensure that a compile-time constant can be resolved from an |
| 33 // annotation. | 30 // annotation. |
| 34 var source1 = """const native = 'xyz'; | 31 var source1 = """const native = 'xyz'; |
| 35 @native | 32 @native |
| 36 $declaration | 33 $declaration |
| 37 main() {}"""; | 34 main() {}"""; |
| 38 | 35 |
| 39 compileAndCheck(source1, name, (compiler, element) { | 36 compileAndCheck(source1, name, (compiler, element) { |
| 40 compiler.enqueuer.resolution.queueIsClosed = false; | 37 compiler.enqueuer.resolution.queueIsClosed = false; |
| 41 Expect.equals(1, element.metadata.length, | 38 Expect.equals( |
| 42 'Unexpected metadata count on $element.'); | 39 1, element.metadata.length, 'Unexpected metadata count on $element.'); |
| 43 PartialMetadataAnnotation annotation = element.metadata.first; | 40 PartialMetadataAnnotation annotation = element.metadata.first; |
| 44 annotation.ensureResolved(compiler.resolution); | 41 annotation.ensureResolved(compiler.resolution); |
| 45 PrimitiveConstantValue value = | 42 PrimitiveConstantValue value = |
| 46 compiler.constants.getConstantValue(annotation.constant); | 43 compiler.constants.getConstantValue(annotation.constant); |
| 47 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 44 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
| 48 | 45 |
| 49 checkPosition( | 46 checkPosition( |
| 50 annotation, annotation.cachedNode, source1, compiler.reporter); | 47 annotation, annotation.cachedNode, source1, compiler.reporter); |
| 51 }); | 48 }); |
| 52 | 49 |
| 53 // Ensure that each repeated annotation has a unique instance of | 50 // Ensure that each repeated annotation has a unique instance of |
| 54 // [MetadataAnnotation]. | 51 // [MetadataAnnotation]. |
| 55 var source2 = """const native = 'xyz'; | 52 var source2 = """const native = 'xyz'; |
| 56 @native @native | 53 @native @native |
| 57 $declaration | 54 $declaration |
| 58 main() {}"""; | 55 main() {}"""; |
| 59 | 56 |
| 60 compileAndCheck(source2, name, (compiler, element) { | 57 compileAndCheck(source2, name, (compiler, element) { |
| 61 compiler.enqueuer.resolution.queueIsClosed = false; | 58 compiler.enqueuer.resolution.queueIsClosed = false; |
| 62 Expect.equals(2, element.metadata.length); | 59 Expect.equals(2, element.metadata.length); |
| 63 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); | 60 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); |
| 64 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); | 61 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); |
| 65 annotation1.ensureResolved(compiler.resolution); | 62 annotation1.ensureResolved(compiler.resolution); |
| 66 annotation2.ensureResolved(compiler.resolution); | 63 annotation2.ensureResolved(compiler.resolution); |
| 67 Expect.isFalse(identical(annotation1, annotation2), | 64 Expect.isFalse( |
| 68 'expected unique instances'); | 65 identical(annotation1, annotation2), 'expected unique instances'); |
| 69 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 66 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 70 PrimitiveConstantValue value1 = | 67 PrimitiveConstantValue value1 = |
| 71 compiler.constants.getConstantValue(annotation1.constant); | 68 compiler.constants.getConstantValue(annotation1.constant); |
| 72 PrimitiveConstantValue value2 = | 69 PrimitiveConstantValue value2 = |
| 73 compiler.constants.getConstantValue(annotation2.constant); | 70 compiler.constants.getConstantValue(annotation2.constant); |
| 74 Expect.identical(value1, value2, 'expected same compile-time constant'); | 71 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| 75 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); | 72 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); |
| 76 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); | 73 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); |
| 77 | 74 |
| 78 checkPosition( | 75 checkPosition( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 compiler.enqueuer.resolution.queueIsClosed = false; | 119 compiler.enqueuer.resolution.queueIsClosed = false; |
| 123 Expect.equals(0, element.metadata.length); | 120 Expect.equals(0, element.metadata.length); |
| 124 element.ensureResolved(compiler.resolution); | 121 element.ensureResolved(compiler.resolution); |
| 125 Expect.equals(0, element.metadata.length); | 122 Expect.equals(0, element.metadata.length); |
| 126 element = element.lookupLocalMember(name); | 123 element = element.lookupLocalMember(name); |
| 127 Expect.equals(2, element.metadata.length); | 124 Expect.equals(2, element.metadata.length); |
| 128 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); | 125 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); |
| 129 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); | 126 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); |
| 130 annotation1.ensureResolved(compiler.resolution); | 127 annotation1.ensureResolved(compiler.resolution); |
| 131 annotation2.ensureResolved(compiler.resolution); | 128 annotation2.ensureResolved(compiler.resolution); |
| 132 Expect.isFalse(identical(annotation1, annotation2), | 129 Expect.isFalse( |
| 133 'expected unique instances'); | 130 identical(annotation1, annotation2), 'expected unique instances'); |
| 134 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 131 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 135 PrimitiveConstantValue value1 = | 132 PrimitiveConstantValue value1 = |
| 136 compiler.constants.getConstantValue(annotation1.constant); | 133 compiler.constants.getConstantValue(annotation1.constant); |
| 137 PrimitiveConstantValue value2 = | 134 PrimitiveConstantValue value2 = |
| 138 compiler.constants.getConstantValue(annotation2.constant); | 135 compiler.constants.getConstantValue(annotation2.constant); |
| 139 Expect.identical(value1, value2, 'expected same compile-time constant'); | 136 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| 140 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); | 137 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); |
| 141 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); | 138 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); |
| 142 | 139 |
| 143 checkPosition( | 140 checkPosition( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 154 void testTopLevelMethodMetadata() { | 151 void testTopLevelMethodMetadata() { |
| 155 checkAnnotation('foo', 'foo() {}'); | 152 checkAnnotation('foo', 'foo() {}'); |
| 156 } | 153 } |
| 157 | 154 |
| 158 void testTopLevelFieldMetadata() { | 155 void testTopLevelFieldMetadata() { |
| 159 checkAnnotation('foo', 'var foo;'); | 156 checkAnnotation('foo', 'var foo;'); |
| 160 checkAnnotation('bar', 'var foo, bar;'); | 157 checkAnnotation('bar', 'var foo, bar;'); |
| 161 } | 158 } |
| 162 | 159 |
| 163 void testLibraryTags() { | 160 void testLibraryTags() { |
| 164 void compileAndCheckLibrary( | 161 void compileAndCheckLibrary(String source, |
| 165 String source, | |
| 166 List<MetadataAnnotation> extractMetadata(LibraryElement element)) { | 162 List<MetadataAnnotation> extractMetadata(LibraryElement element)) { |
| 167 Uri partUri = new Uri(scheme: 'source', path: 'part.dart'); | 163 Uri partUri = new Uri(scheme: 'source', path: 'part.dart'); |
| 168 String partSource = '@native part of foo;'; | 164 String partSource = '@native part of foo;'; |
| 169 | 165 |
| 170 Uri libUri = new Uri(scheme: 'source', path: 'lib.dart'); | 166 Uri libUri = new Uri(scheme: 'source', path: 'lib.dart'); |
| 171 String libSource = 'library lib;'; | 167 String libSource = 'library lib;'; |
| 172 | 168 |
| 173 Uri uri = new Uri(scheme: 'source', path: 'main.dart'); | 169 Uri uri = new Uri(scheme: 'source', path: 'main.dart'); |
| 174 | 170 |
| 175 var compiler = compilerFor(source, uri) | 171 var compiler = compilerFor(source, uri) |
| 176 ..registerSource(partUri, partSource) | 172 ..registerSource(partUri, partSource) |
| 177 ..registerSource(libUri, libSource); | 173 ..registerSource(libUri, libSource); |
| 178 | 174 |
| 179 asyncTest(() => compiler.run(uri).then((_) { | 175 asyncTest(() => compiler.run(uri).then((_) { |
| 180 compiler.enqueuer.resolution.queueIsClosed = false; | 176 compiler.enqueuer.resolution.queueIsClosed = false; |
| 181 LibraryElement element = compiler.libraryLoader.lookupLibrary(uri); | 177 LibraryElement element = compiler.libraryLoader.lookupLibrary(uri); |
| 182 Expect.isNotNull(element, 'Cannot find $uri'); | 178 Expect.isNotNull(element, 'Cannot find $uri'); |
| 183 | 179 |
| 184 List<MetadataAnnotation> metadata = extractMetadata(element); | 180 List<MetadataAnnotation> metadata = extractMetadata(element); |
| 185 Expect.equals(1, metadata.length); | 181 Expect.equals(1, metadata.length); |
| 186 | 182 |
| 187 PartialMetadataAnnotation annotation = metadata.first; | 183 PartialMetadataAnnotation annotation = metadata.first; |
| 188 annotation.ensureResolved(compiler.resolution); | 184 annotation.ensureResolved(compiler.resolution); |
| 189 PrimitiveConstantValue value = | 185 PrimitiveConstantValue value = |
| 190 compiler.constants.getConstantValue(annotation.constant); | 186 compiler.constants.getConstantValue(annotation.constant); |
| 191 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 187 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
| 192 | 188 |
| 193 checkPosition( | 189 checkPosition( |
| 194 annotation, annotation.cachedNode, source, compiler.reporter); | 190 annotation, annotation.cachedNode, source, compiler.reporter); |
| 195 })); | 191 })); |
| 196 } | 192 } |
| 197 | 193 |
| 198 var source; | 194 var source; |
| 199 | 195 |
| 200 source = """@native | 196 source = """@native |
| 201 library foo; | 197 library foo; |
| 202 const native = 'xyz'; | 198 const native = 'xyz'; |
| 203 main() {}"""; | 199 main() {}"""; |
| 204 compileAndCheckLibrary(source, (e) => e.libraryTag.metadata); | 200 compileAndCheckLibrary(source, (e) => e.libraryTag.metadata); |
| 205 | 201 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 source = """@native | 214 source = """@native |
| 219 part 'part.dart'; | 215 part 'part.dart'; |
| 220 const native = 'xyz'; | 216 const native = 'xyz'; |
| 221 main() {}"""; | 217 main() {}"""; |
| 222 compileAndCheckLibrary(source, (e) => e.tags.single.metadata); | 218 compileAndCheckLibrary(source, (e) => e.tags.single.metadata); |
| 223 | 219 |
| 224 source = """@native | 220 source = """@native |
| 225 part 'part.dart'; | 221 part 'part.dart'; |
| 226 const native = 'xyz'; | 222 const native = 'xyz'; |
| 227 main() {}"""; | 223 main() {}"""; |
| 228 compileAndCheckLibrary(source, | 224 compileAndCheckLibrary( |
| 229 (e) => e.compilationUnits.first.partTag.metadata); | 225 source, (e) => e.compilationUnits.first.partTag.metadata); |
| 230 } | 226 } |
| 231 | 227 |
| 232 void main() { | 228 void main() { |
| 233 testClassMetadata(); | 229 testClassMetadata(); |
| 234 testTopLevelMethodMetadata(); | 230 testTopLevelMethodMetadata(); |
| 235 testTopLevelFieldMetadata(); | 231 testTopLevelFieldMetadata(); |
| 236 testLibraryTags(); | 232 testLibraryTags(); |
| 237 } | 233 } |
| OLD | NEW |