| 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 | 8 |
| 9 #import('../../../lib/compiler/implementation/elements/elements.dart'); | 9 #import('../../../lib/compiler/implementation/elements/elements.dart'); |
| 10 #import('../../../lib/compiler/implementation/leg.dart'); | 10 #import('../../../lib/compiler/implementation/leg.dart'); |
| 11 | 11 |
| 12 void testClassMetadata() { | 12 void checkTopLevelAnnotation(String name, String declaration) { |
| 13 // TODO(ahe): native should be "const", not "final". | 13 var source; |
| 14 final source = """final native = 'xyz'; | |
| 15 @native class Foo {} | |
| 16 main() {}"""; | |
| 17 | 14 |
| 18 check(compiler, element) { | 15 // Ensure that a compile-time constant can be resolved from an |
| 19 Expect.isFalse(element.metadata.isEmpty()); | 16 // annotation. |
| 20 Expect.isTrue(element.metadata.tail.isEmpty()); | 17 source = """const native = 'xyz'; |
| 18 @native |
| 19 $declaration |
| 20 main() {}"""; |
| 21 |
| 22 compileAndCheck(source, name, (compiler, element) { |
| 23 Expect.equals(1, length(element.metadata)); |
| 21 MetadataAnnotation annotation = element.metadata.head; | 24 MetadataAnnotation annotation = element.metadata.head; |
| 22 annotation.ensureResolved(compiler); | 25 annotation.ensureResolved(compiler); |
| 23 Constant value = annotation.value; | 26 Constant value = annotation.value; |
| 24 Expect.stringEquals('xyz', value.value.slowToString()); | 27 Expect.stringEquals('xyz', value.value.slowToString()); |
| 25 } | 28 }); |
| 26 | 29 |
| 27 compileAndCheck(source, 'Foo', check); | 30 // Ensure that each repeated annotation has a unique instance of |
| 31 // [MetadataAnnotation]. |
| 32 source = """const native = 'xyz'; |
| 33 @native @native |
| 34 $declaration |
| 35 main() {}"""; |
| 36 |
| 37 compileAndCheck(source, name, (compiler, element) { |
| 38 Expect.equals(2, length(element.metadata)); |
| 39 MetadataAnnotation annotation1 = element.metadata.head; |
| 40 MetadataAnnotation annotation2 = element.metadata.tail.head; |
| 41 annotation1.ensureResolved(compiler); |
| 42 annotation2.ensureResolved(compiler); |
| 43 Expect.isTrue(annotation1 !== annotation2, 'expected unique instances'); |
| 44 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
| 45 Constant value1 = annotation1.value; |
| 46 Constant value2 = annotation2.value; |
| 47 Expect.identical(value1, value2, 'expected same compile-time constant'); |
| 48 Expect.stringEquals('xyz', value1.value.slowToString()); |
| 49 Expect.stringEquals('xyz', value2.value.slowToString()); |
| 50 }); |
| 51 } |
| 52 |
| 53 void testClassMetadata() { |
| 54 checkTopLevelAnnotation('Foo', 'class Foo {}'); |
| 28 } | 55 } |
| 29 | 56 |
| 30 void testTopLevelMethodMetadata() { | 57 void testTopLevelMethodMetadata() { |
| 31 // TODO(ahe): native should be "const", not "final". | 58 checkTopLevelAnnotation('foo', 'foo() {}'); |
| 32 final source = """final native = 'xyz'; | 59 } |
| 33 @native | |
| 34 main() {}"""; | |
| 35 | 60 |
| 36 check(compiler, element) { | 61 void testTopLevelFieldMetadata() { |
| 37 Expect.isFalse(element.metadata.isEmpty()); | 62 checkTopLevelAnnotation('foo', 'var foo;'); |
| 38 Expect.isTrue(element.metadata.tail.isEmpty()); | |
| 39 MetadataAnnotation annotation = element.metadata.head; | |
| 40 annotation.ensureResolved(compiler); | |
| 41 Constant value = annotation.value; | |
| 42 Expect.stringEquals('xyz', value.value.slowToString()); | |
| 43 } | |
| 44 | |
| 45 compileAndCheck(source, 'main', check); | |
| 46 } | 63 } |
| 47 | 64 |
| 48 void main() { | 65 void main() { |
| 49 testClassMetadata(); | 66 testClassMetadata(); |
| 50 testTopLevelMethodMetadata(); | 67 testTopLevelMethodMetadata(); |
| 68 testTopLevelFieldMetadata(); |
| 51 } | 69 } |
| OLD | NEW |