| 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' show PrimitiveConstantValue; | 6 import 'package:compiler/src/constants/values.dart' show PrimitiveConstantValue; |
| 7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
| 8 import 'compiler_helper.dart'; | 8 import 'compiler_helper.dart'; |
| 9 import 'package:compiler/src/parser/partial_elements.dart' | 9 import 'package:compiler/src/parser/partial_elements.dart' |
| 10 show PartialMetadataAnnotation; | 10 show PartialMetadataAnnotation; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 main() {}"""; | 34 main() {}"""; |
| 35 | 35 |
| 36 analyzeAndCheck(source1, name, (compiler, element) { | 36 analyzeAndCheck(source1, name, (compiler, element) { |
| 37 compiler.enqueuer.resolution.queueIsClosed = false; | 37 compiler.enqueuer.resolution.queueIsClosed = false; |
| 38 Expect.equals( | 38 Expect.equals( |
| 39 1, element.metadata.length, 'Unexpected metadata count on $element.'); | 39 1, element.metadata.length, 'Unexpected metadata count on $element.'); |
| 40 PartialMetadataAnnotation annotation = element.metadata.first; | 40 PartialMetadataAnnotation annotation = element.metadata.first; |
| 41 annotation.ensureResolved(compiler.resolution); | 41 annotation.ensureResolved(compiler.resolution); |
| 42 PrimitiveConstantValue value = | 42 PrimitiveConstantValue value = |
| 43 compiler.constants.getConstantValue(annotation.constant); | 43 compiler.constants.getConstantValue(annotation.constant); |
| 44 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 44 Expect.stringEquals('xyz', value.primitiveValue); |
| 45 | 45 |
| 46 checkPosition( | 46 checkPosition( |
| 47 annotation, annotation.cachedNode, source1, compiler.reporter); | 47 annotation, annotation.cachedNode, source1, compiler.reporter); |
| 48 }); | 48 }); |
| 49 | 49 |
| 50 // Ensure that each repeated annotation has a unique instance of | 50 // Ensure that each repeated annotation has a unique instance of |
| 51 // [MetadataAnnotation]. | 51 // [MetadataAnnotation]. |
| 52 var source2 = """const native = 'xyz'; | 52 var source2 = """const native = 'xyz'; |
| 53 @native @native | 53 @native @native |
| 54 $declaration | 54 $declaration |
| 55 main() {}"""; | 55 main() {}"""; |
| 56 | 56 |
| 57 analyzeAndCheck(source2, name, (compiler, element) { | 57 analyzeAndCheck(source2, name, (compiler, element) { |
| 58 compiler.enqueuer.resolution.queueIsClosed = false; | 58 compiler.enqueuer.resolution.queueIsClosed = false; |
| 59 Expect.equals(2, element.metadata.length); | 59 Expect.equals(2, element.metadata.length); |
| 60 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); | 60 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); |
| 61 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); | 61 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); |
| 62 annotation1.ensureResolved(compiler.resolution); | 62 annotation1.ensureResolved(compiler.resolution); |
| 63 annotation2.ensureResolved(compiler.resolution); | 63 annotation2.ensureResolved(compiler.resolution); |
| 64 Expect.isFalse( | 64 Expect.isFalse( |
| 65 identical(annotation1, annotation2), 'expected unique instances'); | 65 identical(annotation1, annotation2), 'expected unique instances'); |
| 66 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 66 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 67 PrimitiveConstantValue value1 = | 67 PrimitiveConstantValue value1 = |
| 68 compiler.constants.getConstantValue(annotation1.constant); | 68 compiler.constants.getConstantValue(annotation1.constant); |
| 69 PrimitiveConstantValue value2 = | 69 PrimitiveConstantValue value2 = |
| 70 compiler.constants.getConstantValue(annotation2.constant); | 70 compiler.constants.getConstantValue(annotation2.constant); |
| 71 Expect.identical(value1, value2, 'expected same compile-time constant'); | 71 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| 72 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); | 72 Expect.stringEquals('xyz', value1.primitiveValue); |
| 73 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); | 73 Expect.stringEquals('xyz', value2.primitiveValue); |
| 74 | 74 |
| 75 checkPosition( | 75 checkPosition( |
| 76 annotation1, annotation1.cachedNode, source2, compiler.reporter); | 76 annotation1, annotation1.cachedNode, source2, compiler.reporter); |
| 77 checkPosition( | 77 checkPosition( |
| 78 annotation2, annotation2.cachedNode, source2, compiler.reporter); | 78 annotation2, annotation2.cachedNode, source2, compiler.reporter); |
| 79 }); | 79 }); |
| 80 | 80 |
| 81 if (isTopLevelOnly) return; | 81 if (isTopLevelOnly) return; |
| 82 | 82 |
| 83 // Ensure that a compile-time constant can be resolved from an | 83 // Ensure that a compile-time constant can be resolved from an |
| 84 // annotation. | 84 // annotation. |
| 85 var source3 = """const native = 'xyz'; | 85 var source3 = """const native = 'xyz'; |
| 86 class Foo { | 86 class Foo { |
| 87 @native | 87 @native |
| 88 $declaration | 88 $declaration |
| 89 } | 89 } |
| 90 main() {}"""; | 90 main() {}"""; |
| 91 | 91 |
| 92 analyzeAndCheck(source3, 'Foo', (compiler, element) { | 92 analyzeAndCheck(source3, 'Foo', (compiler, element) { |
| 93 compiler.enqueuer.resolution.queueIsClosed = false; | 93 compiler.enqueuer.resolution.queueIsClosed = false; |
| 94 Expect.equals(0, element.metadata.length); | 94 Expect.equals(0, element.metadata.length); |
| 95 element.ensureResolved(compiler.resolution); | 95 element.ensureResolved(compiler.resolution); |
| 96 Expect.equals(0, element.metadata.length); | 96 Expect.equals(0, element.metadata.length); |
| 97 element = element.lookupLocalMember(name); | 97 element = element.lookupLocalMember(name); |
| 98 Expect.equals(1, element.metadata.length); | 98 Expect.equals(1, element.metadata.length); |
| 99 PartialMetadataAnnotation annotation = element.metadata.first; | 99 PartialMetadataAnnotation annotation = element.metadata.first; |
| 100 annotation.ensureResolved(compiler.resolution); | 100 annotation.ensureResolved(compiler.resolution); |
| 101 PrimitiveConstantValue value = | 101 PrimitiveConstantValue value = |
| 102 compiler.constants.getConstantValue(annotation.constant); | 102 compiler.constants.getConstantValue(annotation.constant); |
| 103 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 103 Expect.stringEquals('xyz', value.primitiveValue); |
| 104 | 104 |
| 105 checkPosition( | 105 checkPosition( |
| 106 annotation, annotation.cachedNode, source3, compiler.reporter); | 106 annotation, annotation.cachedNode, source3, compiler.reporter); |
| 107 }); | 107 }); |
| 108 | 108 |
| 109 // Ensure that each repeated annotation has a unique instance of | 109 // Ensure that each repeated annotation has a unique instance of |
| 110 // [MetadataAnnotation]. | 110 // [MetadataAnnotation]. |
| 111 var source4 = """const native = 'xyz'; | 111 var source4 = """const native = 'xyz'; |
| 112 class Foo { | 112 class Foo { |
| 113 @native @native | 113 @native @native |
| (...skipping 13 matching lines...) Expand all Loading... |
| 127 annotation1.ensureResolved(compiler.resolution); | 127 annotation1.ensureResolved(compiler.resolution); |
| 128 annotation2.ensureResolved(compiler.resolution); | 128 annotation2.ensureResolved(compiler.resolution); |
| 129 Expect.isFalse( | 129 Expect.isFalse( |
| 130 identical(annotation1, annotation2), 'expected unique instances'); | 130 identical(annotation1, annotation2), 'expected unique instances'); |
| 131 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 131 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 132 PrimitiveConstantValue value1 = | 132 PrimitiveConstantValue value1 = |
| 133 compiler.constants.getConstantValue(annotation1.constant); | 133 compiler.constants.getConstantValue(annotation1.constant); |
| 134 PrimitiveConstantValue value2 = | 134 PrimitiveConstantValue value2 = |
| 135 compiler.constants.getConstantValue(annotation2.constant); | 135 compiler.constants.getConstantValue(annotation2.constant); |
| 136 Expect.identical(value1, value2, 'expected same compile-time constant'); | 136 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| 137 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); | 137 Expect.stringEquals('xyz', value1.primitiveValue); |
| 138 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); | 138 Expect.stringEquals('xyz', value2.primitiveValue); |
| 139 | 139 |
| 140 checkPosition( | 140 checkPosition( |
| 141 annotation1, annotation1.cachedNode, source4, compiler.reporter); | 141 annotation1, annotation1.cachedNode, source4, compiler.reporter); |
| 142 checkPosition( | 142 checkPosition( |
| 143 annotation1, annotation2.cachedNode, source4, compiler.reporter); | 143 annotation1, annotation2.cachedNode, source4, compiler.reporter); |
| 144 }); | 144 }); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void testClassMetadata() { | 147 void testClassMetadata() { |
| 148 checkAnnotation('Foo', 'class Foo {}', isTopLevelOnly: true); | 148 checkAnnotation('Foo', 'class Foo {}', isTopLevelOnly: true); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 177 LibraryElement element = compiler.libraryLoader.lookupLibrary(uri); | 177 LibraryElement element = compiler.libraryLoader.lookupLibrary(uri); |
| 178 Expect.isNotNull(element, 'Cannot find $uri'); | 178 Expect.isNotNull(element, 'Cannot find $uri'); |
| 179 | 179 |
| 180 List<MetadataAnnotation> metadata = extractMetadata(element); | 180 List<MetadataAnnotation> metadata = extractMetadata(element); |
| 181 Expect.equals(1, metadata.length); | 181 Expect.equals(1, metadata.length); |
| 182 | 182 |
| 183 PartialMetadataAnnotation annotation = metadata.first; | 183 PartialMetadataAnnotation annotation = metadata.first; |
| 184 annotation.ensureResolved(compiler.resolution); | 184 annotation.ensureResolved(compiler.resolution); |
| 185 PrimitiveConstantValue value = | 185 PrimitiveConstantValue value = |
| 186 compiler.constants.getConstantValue(annotation.constant); | 186 compiler.constants.getConstantValue(annotation.constant); |
| 187 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 187 Expect.stringEquals('xyz', value.primitiveValue); |
| 188 | 188 |
| 189 checkPosition( | 189 checkPosition( |
| 190 annotation, annotation.cachedNode, source, compiler.reporter); | 190 annotation, annotation.cachedNode, source, compiler.reporter); |
| 191 })); | 191 })); |
| 192 } | 192 } |
| 193 | 193 |
| 194 var source; | 194 var source; |
| 195 | 195 |
| 196 source = """@native | 196 source = """@native |
| 197 library foo; | 197 library foo; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 224 compileAndCheckLibrary( | 224 compileAndCheckLibrary( |
| 225 source, (e) => e.compilationUnits.first.partTag.metadata); | 225 source, (e) => e.compilationUnits.first.partTag.metadata); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void main() { | 228 void main() { |
| 229 testClassMetadata(); | 229 testClassMetadata(); |
| 230 testTopLevelMethodMetadata(); | 230 testTopLevelMethodMetadata(); |
| 231 testTopLevelFieldMetadata(); | 231 testTopLevelFieldMetadata(); |
| 232 testLibraryTags(); | 232 testLibraryTags(); |
| 233 } | 233 } |
| OLD | NEW |