| 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' |
| 7 show PrimitiveConstantValue; | 7 show PrimitiveConstantValue; |
| 8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
| 9 import 'compiler_helper.dart'; | 9 import 'compiler_helper.dart'; |
| 10 import 'package:compiler/src/parser/partial_elements.dart' show | 10 import 'package:compiler/src/parser/partial_elements.dart' show |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 {bool isTopLevelOnly: false}) { | 26 {bool isTopLevelOnly: false}) { |
| 27 // Ensure that a compile-time constant can be resolved from an | 27 // Ensure that a compile-time constant can be resolved from an |
| 28 // annotation. | 28 // annotation. |
| 29 var source1 = """const native = 'xyz'; | 29 var source1 = """const native = 'xyz'; |
| 30 @native | 30 @native |
| 31 $declaration | 31 $declaration |
| 32 main() {}"""; | 32 main() {}"""; |
| 33 | 33 |
| 34 compileAndCheck(source1, name, (compiler, element) { | 34 compileAndCheck(source1, name, (compiler, element) { |
| 35 compiler.enqueuer.resolution.queueIsClosed = false; | 35 compiler.enqueuer.resolution.queueIsClosed = false; |
| 36 Expect.equals(1, length(element.metadata), | 36 Expect.equals(1, element.metadata.length, |
| 37 'Unexpected metadata count on $element.'); | 37 'Unexpected metadata count on $element.'); |
| 38 PartialMetadataAnnotation annotation = element.metadata.head; | 38 PartialMetadataAnnotation annotation = element.metadata.first; |
| 39 annotation.ensureResolved(compiler); | 39 annotation.ensureResolved(compiler); |
| 40 PrimitiveConstantValue value = | 40 PrimitiveConstantValue value = |
| 41 compiler.constants.getConstantValue(annotation.constant); | 41 compiler.constants.getConstantValue(annotation.constant); |
| 42 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 42 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
| 43 | 43 |
| 44 checkPosition(annotation, annotation.cachedNode, source1, compiler); | 44 checkPosition(annotation, annotation.cachedNode, source1, compiler); |
| 45 }); | 45 }); |
| 46 | 46 |
| 47 // Ensure that each repeated annotation has a unique instance of | 47 // Ensure that each repeated annotation has a unique instance of |
| 48 // [MetadataAnnotation]. | 48 // [MetadataAnnotation]. |
| 49 var source2 = """const native = 'xyz'; | 49 var source2 = """const native = 'xyz'; |
| 50 @native @native | 50 @native @native |
| 51 $declaration | 51 $declaration |
| 52 main() {}"""; | 52 main() {}"""; |
| 53 | 53 |
| 54 compileAndCheck(source2, name, (compiler, element) { | 54 compileAndCheck(source2, name, (compiler, element) { |
| 55 compiler.enqueuer.resolution.queueIsClosed = false; | 55 compiler.enqueuer.resolution.queueIsClosed = false; |
| 56 Expect.equals(2, length(element.metadata)); | 56 Expect.equals(2, element.metadata.length); |
| 57 PartialMetadataAnnotation annotation1 = element.metadata.head; | 57 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); |
| 58 PartialMetadataAnnotation annotation2 = element.metadata.tail.head; | 58 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); |
| 59 annotation1.ensureResolved(compiler); | 59 annotation1.ensureResolved(compiler); |
| 60 annotation2.ensureResolved(compiler); | 60 annotation2.ensureResolved(compiler); |
| 61 Expect.isFalse(identical(annotation1, annotation2), | 61 Expect.isFalse(identical(annotation1, annotation2), |
| 62 'expected unique instances'); | 62 'expected unique instances'); |
| 63 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 63 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 64 PrimitiveConstantValue value1 = | 64 PrimitiveConstantValue value1 = |
| 65 compiler.constants.getConstantValue(annotation1.constant); | 65 compiler.constants.getConstantValue(annotation1.constant); |
| 66 PrimitiveConstantValue value2 = | 66 PrimitiveConstantValue value2 = |
| 67 compiler.constants.getConstantValue(annotation2.constant); | 67 compiler.constants.getConstantValue(annotation2.constant); |
| 68 Expect.identical(value1, value2, 'expected same compile-time constant'); | 68 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 79 // annotation. | 79 // annotation. |
| 80 var source3 = """const native = 'xyz'; | 80 var source3 = """const native = 'xyz'; |
| 81 class Foo { | 81 class Foo { |
| 82 @native | 82 @native |
| 83 $declaration | 83 $declaration |
| 84 } | 84 } |
| 85 main() {}"""; | 85 main() {}"""; |
| 86 | 86 |
| 87 compileAndCheck(source3, 'Foo', (compiler, element) { | 87 compileAndCheck(source3, 'Foo', (compiler, element) { |
| 88 compiler.enqueuer.resolution.queueIsClosed = false; | 88 compiler.enqueuer.resolution.queueIsClosed = false; |
| 89 Expect.equals(0, length(element.metadata)); | 89 Expect.equals(0, element.metadata.length); |
| 90 element.ensureResolved(compiler); | 90 element.ensureResolved(compiler); |
| 91 Expect.equals(0, length(element.metadata)); | 91 Expect.equals(0, element.metadata.length); |
| 92 element = element.lookupLocalMember(name); | 92 element = element.lookupLocalMember(name); |
| 93 Expect.equals(1, length(element.metadata)); | 93 Expect.equals(1, element.metadata.length); |
| 94 PartialMetadataAnnotation annotation = element.metadata.head; | 94 PartialMetadataAnnotation annotation = element.metadata.first; |
| 95 annotation.ensureResolved(compiler); | 95 annotation.ensureResolved(compiler); |
| 96 PrimitiveConstantValue value = | 96 PrimitiveConstantValue value = |
| 97 compiler.constants.getConstantValue(annotation.constant); | 97 compiler.constants.getConstantValue(annotation.constant); |
| 98 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 98 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
| 99 | 99 |
| 100 checkPosition(annotation, annotation.cachedNode, source3, compiler); | 100 checkPosition(annotation, annotation.cachedNode, source3, compiler); |
| 101 }); | 101 }); |
| 102 | 102 |
| 103 // Ensure that each repeated annotation has a unique instance of | 103 // Ensure that each repeated annotation has a unique instance of |
| 104 // [MetadataAnnotation]. | 104 // [MetadataAnnotation]. |
| 105 var source4 = """const native = 'xyz'; | 105 var source4 = """const native = 'xyz'; |
| 106 class Foo { | 106 class Foo { |
| 107 @native @native | 107 @native @native |
| 108 $declaration | 108 $declaration |
| 109 } | 109 } |
| 110 main() {}"""; | 110 main() {}"""; |
| 111 | 111 |
| 112 compileAndCheck(source4, 'Foo', (compiler, element) { | 112 compileAndCheck(source4, 'Foo', (compiler, element) { |
| 113 compiler.enqueuer.resolution.queueIsClosed = false; | 113 compiler.enqueuer.resolution.queueIsClosed = false; |
| 114 Expect.equals(0, length(element.metadata)); | 114 Expect.equals(0, element.metadata.length); |
| 115 element.ensureResolved(compiler); | 115 element.ensureResolved(compiler); |
| 116 Expect.equals(0, length(element.metadata)); | 116 Expect.equals(0, element.metadata.length); |
| 117 element = element.lookupLocalMember(name); | 117 element = element.lookupLocalMember(name); |
| 118 Expect.equals(2, length(element.metadata)); | 118 Expect.equals(2, element.metadata.length); |
| 119 PartialMetadataAnnotation annotation1 = element.metadata.head; | 119 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); |
| 120 PartialMetadataAnnotation annotation2 = element.metadata.tail.head; | 120 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); |
| 121 annotation1.ensureResolved(compiler); | 121 annotation1.ensureResolved(compiler); |
| 122 annotation2.ensureResolved(compiler); | 122 annotation2.ensureResolved(compiler); |
| 123 Expect.isFalse(identical(annotation1, annotation2), | 123 Expect.isFalse(identical(annotation1, annotation2), |
| 124 'expected unique instances'); | 124 'expected unique instances'); |
| 125 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 125 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 126 PrimitiveConstantValue value1 = | 126 PrimitiveConstantValue value1 = |
| 127 compiler.constants.getConstantValue(annotation1.constant); | 127 compiler.constants.getConstantValue(annotation1.constant); |
| 128 PrimitiveConstantValue value2 = | 128 PrimitiveConstantValue value2 = |
| 129 compiler.constants.getConstantValue(annotation2.constant); | 129 compiler.constants.getConstantValue(annotation2.constant); |
| 130 Expect.identical(value1, value2, 'expected same compile-time constant'); | 130 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 145 } | 145 } |
| 146 | 146 |
| 147 void testTopLevelFieldMetadata() { | 147 void testTopLevelFieldMetadata() { |
| 148 checkAnnotation('foo', 'var foo;'); | 148 checkAnnotation('foo', 'var foo;'); |
| 149 checkAnnotation('bar', 'var foo, bar;'); | 149 checkAnnotation('bar', 'var foo, bar;'); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void testLibraryTags() { | 152 void testLibraryTags() { |
| 153 void compileAndCheckLibrary( | 153 void compileAndCheckLibrary( |
| 154 String source, | 154 String source, |
| 155 Link<MetadataAnnotation> extractMetadata(LibraryElement element)) { | 155 List<MetadataAnnotation> extractMetadata(LibraryElement element)) { |
| 156 Uri partUri = new Uri(scheme: 'source', path: 'part.dart'); | 156 Uri partUri = new Uri(scheme: 'source', path: 'part.dart'); |
| 157 String partSource = '@native part of foo;'; | 157 String partSource = '@native part of foo;'; |
| 158 | 158 |
| 159 Uri libUri = new Uri(scheme: 'source', path: 'lib.dart'); | 159 Uri libUri = new Uri(scheme: 'source', path: 'lib.dart'); |
| 160 String libSource = 'library lib;'; | 160 String libSource = 'library lib;'; |
| 161 | 161 |
| 162 Uri uri = new Uri(scheme: 'source', path: 'main.dart'); | 162 Uri uri = new Uri(scheme: 'source', path: 'main.dart'); |
| 163 | 163 |
| 164 var compiler = compilerFor(source, uri) | 164 var compiler = compilerFor(source, uri) |
| 165 ..registerSource(partUri, partSource) | 165 ..registerSource(partUri, partSource) |
| 166 ..registerSource(libUri, libSource); | 166 ..registerSource(libUri, libSource); |
| 167 | 167 |
| 168 asyncTest(() => compiler.runCompiler(uri).then((_) { | 168 asyncTest(() => compiler.runCompiler(uri).then((_) { |
| 169 compiler.enqueuer.resolution.queueIsClosed = false; | 169 compiler.enqueuer.resolution.queueIsClosed = false; |
| 170 LibraryElement element = compiler.libraryLoader.lookupLibrary(uri); | 170 LibraryElement element = compiler.libraryLoader.lookupLibrary(uri); |
| 171 Expect.isNotNull(element, 'Cannot find $uri'); | 171 Expect.isNotNull(element, 'Cannot find $uri'); |
| 172 | 172 |
| 173 Link<MetadataAnnotation> metadata = extractMetadata(element); | 173 List<MetadataAnnotation> metadata = extractMetadata(element); |
| 174 Expect.equals(1, length(metadata)); | 174 Expect.equals(1, metadata.length); |
| 175 | 175 |
| 176 PartialMetadataAnnotation annotation = metadata.head; | 176 PartialMetadataAnnotation annotation = metadata.first; |
| 177 annotation.ensureResolved(compiler); | 177 annotation.ensureResolved(compiler); |
| 178 PrimitiveConstantValue value = | 178 PrimitiveConstantValue value = |
| 179 compiler.constants.getConstantValue(annotation.constant); | 179 compiler.constants.getConstantValue(annotation.constant); |
| 180 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 180 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
| 181 | 181 |
| 182 checkPosition(annotation, annotation.cachedNode, source, compiler); | 182 checkPosition(annotation, annotation.cachedNode, source, compiler); |
| 183 })); | 183 })); |
| 184 } | 184 } |
| 185 | 185 |
| 186 var source; | 186 var source; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 216 compileAndCheckLibrary(source, | 216 compileAndCheckLibrary(source, |
| 217 (e) => e.compilationUnits.first.partTag.metadata); | 217 (e) => e.compilationUnits.first.partTag.metadata); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void main() { | 220 void main() { |
| 221 testClassMetadata(); | 221 testClassMetadata(); |
| 222 testTopLevelMethodMetadata(); | 222 testTopLevelMethodMetadata(); |
| 223 testTopLevelFieldMetadata(); | 223 testTopLevelFieldMetadata(); |
| 224 testLibraryTags(); | 224 testLibraryTags(); |
| 225 } | 225 } |
| OLD | NEW |