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('../../../lib/compiler/implementation/elements/elements.dart'); | 10 #import('../../../lib/compiler/implementation/elements/elements.dart'); |
11 #import('../../../lib/compiler/implementation/leg.dart'); | 11 #import('../../../lib/compiler/implementation/leg.dart'); |
12 | 12 |
13 void checkAnnotation(String name, String declaration, | 13 void checkAnnotation(String name, String declaration, |
14 [bool isTopLevelOnly = false]) { | 14 [bool isTopLevelOnly = false]) { |
15 var source; | 15 var source; |
16 | 16 |
17 // Ensure that a compile-time constant can be resolved from an | 17 // Ensure that a compile-time constant can be resolved from an |
18 // annotation. | 18 // annotation. |
19 source = """const native = 'xyz'; | 19 source = """const native = 'xyz'; |
20 @native | 20 @native |
21 $declaration | 21 $declaration |
22 main() {}"""; | 22 main() {}"""; |
23 | 23 |
24 compileAndCheck(source, name, (compiler, element) { | 24 compileAndCheck(source, name, (compiler, element) { |
| 25 compiler.enqueuer.resolution.queueIsClosed = false; |
25 Expect.equals(1, length(element.metadata)); | 26 Expect.equals(1, length(element.metadata)); |
26 MetadataAnnotation annotation = element.metadata.head; | 27 MetadataAnnotation annotation = element.metadata.head; |
27 annotation.ensureResolved(compiler); | 28 annotation.ensureResolved(compiler); |
28 Constant value = annotation.value; | 29 Constant value = annotation.value; |
29 Expect.stringEquals('xyz', value.value.slowToString()); | 30 Expect.stringEquals('xyz', value.value.slowToString()); |
30 }); | 31 }); |
31 | 32 |
32 // Ensure that each repeated annotation has a unique instance of | 33 // Ensure that each repeated annotation has a unique instance of |
33 // [MetadataAnnotation]. | 34 // [MetadataAnnotation]. |
34 source = """const native = 'xyz'; | 35 source = """const native = 'xyz'; |
35 @native @native | 36 @native @native |
36 $declaration | 37 $declaration |
37 main() {}"""; | 38 main() {}"""; |
38 | 39 |
39 compileAndCheck(source, name, (compiler, element) { | 40 compileAndCheck(source, name, (compiler, element) { |
| 41 compiler.enqueuer.resolution.queueIsClosed = false; |
40 Expect.equals(2, length(element.metadata)); | 42 Expect.equals(2, length(element.metadata)); |
41 MetadataAnnotation annotation1 = element.metadata.head; | 43 MetadataAnnotation annotation1 = element.metadata.head; |
42 MetadataAnnotation annotation2 = element.metadata.tail.head; | 44 MetadataAnnotation annotation2 = element.metadata.tail.head; |
43 annotation1.ensureResolved(compiler); | 45 annotation1.ensureResolved(compiler); |
44 annotation2.ensureResolved(compiler); | 46 annotation2.ensureResolved(compiler); |
45 Expect.isTrue(annotation1 !== annotation2, 'expected unique instances'); | 47 Expect.isTrue(annotation1 !== annotation2, 'expected unique instances'); |
46 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 48 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
47 Constant value1 = annotation1.value; | 49 Constant value1 = annotation1.value; |
48 Constant value2 = annotation2.value; | 50 Constant value2 = annotation2.value; |
49 Expect.identical(value1, value2, 'expected same compile-time constant'); | 51 Expect.identical(value1, value2, 'expected same compile-time constant'); |
50 Expect.stringEquals('xyz', value1.value.slowToString()); | 52 Expect.stringEquals('xyz', value1.value.slowToString()); |
51 Expect.stringEquals('xyz', value2.value.slowToString()); | 53 Expect.stringEquals('xyz', value2.value.slowToString()); |
52 }); | 54 }); |
53 | 55 |
54 if (isTopLevelOnly) return; | 56 if (isTopLevelOnly) return; |
55 | 57 |
56 // Ensure that a compile-time constant can be resolved from an | 58 // Ensure that a compile-time constant can be resolved from an |
57 // annotation. | 59 // annotation. |
58 source = """const native = 'xyz'; | 60 source = """const native = 'xyz'; |
59 class Foo { | 61 class Foo { |
60 @native | 62 @native |
61 $declaration | 63 $declaration |
62 } | 64 } |
63 main() {}"""; | 65 main() {}"""; |
64 | 66 |
65 compileAndCheck(source, 'Foo', (compiler, element) { | 67 compileAndCheck(source, 'Foo', (compiler, element) { |
| 68 compiler.enqueuer.resolution.queueIsClosed = false; |
66 Expect.equals(0, length(element.metadata)); | 69 Expect.equals(0, length(element.metadata)); |
67 element.ensureResolved(compiler); | 70 element.ensureResolved(compiler); |
68 Expect.equals(0, length(element.metadata)); | 71 Expect.equals(0, length(element.metadata)); |
69 element = element.lookupLocalMember(buildSourceString(name)); | 72 element = element.lookupLocalMember(buildSourceString(name)); |
70 Expect.equals(1, length(element.metadata)); | 73 Expect.equals(1, length(element.metadata)); |
71 MetadataAnnotation annotation = element.metadata.head; | 74 MetadataAnnotation annotation = element.metadata.head; |
72 annotation.ensureResolved(compiler); | 75 annotation.ensureResolved(compiler); |
73 Constant value = annotation.value; | 76 Constant value = annotation.value; |
74 Expect.stringEquals('xyz', value.value.slowToString()); | 77 Expect.stringEquals('xyz', value.value.slowToString()); |
75 }); | 78 }); |
76 | 79 |
77 // Ensure that each repeated annotation has a unique instance of | 80 // Ensure that each repeated annotation has a unique instance of |
78 // [MetadataAnnotation]. | 81 // [MetadataAnnotation]. |
79 source = """const native = 'xyz'; | 82 source = """const native = 'xyz'; |
80 class Foo { | 83 class Foo { |
81 @native @native | 84 @native @native |
82 $declaration | 85 $declaration |
83 } | 86 } |
84 main() {}"""; | 87 main() {}"""; |
85 | 88 |
86 compileAndCheck(source, 'Foo', (compiler, element) { | 89 compileAndCheck(source, 'Foo', (compiler, element) { |
| 90 compiler.enqueuer.resolution.queueIsClosed = false; |
87 Expect.equals(0, length(element.metadata)); | 91 Expect.equals(0, length(element.metadata)); |
88 element.ensureResolved(compiler); | 92 element.ensureResolved(compiler); |
89 Expect.equals(0, length(element.metadata)); | 93 Expect.equals(0, length(element.metadata)); |
90 element = element.lookupLocalMember(buildSourceString(name)); | 94 element = element.lookupLocalMember(buildSourceString(name)); |
91 Expect.equals(2, length(element.metadata)); | 95 Expect.equals(2, length(element.metadata)); |
92 MetadataAnnotation annotation1 = element.metadata.head; | 96 MetadataAnnotation annotation1 = element.metadata.head; |
93 MetadataAnnotation annotation2 = element.metadata.tail.head; | 97 MetadataAnnotation annotation2 = element.metadata.tail.head; |
94 annotation1.ensureResolved(compiler); | 98 annotation1.ensureResolved(compiler); |
95 annotation2.ensureResolved(compiler); | 99 annotation2.ensureResolved(compiler); |
96 Expect.isTrue(annotation1 !== annotation2, 'expected unique instances'); | 100 Expect.isTrue(annotation1 !== annotation2, 'expected unique instances'); |
(...skipping 16 matching lines...) Expand all Loading... |
113 | 117 |
114 void testTopLevelFieldMetadata() { | 118 void testTopLevelFieldMetadata() { |
115 checkAnnotation('foo', 'var foo;'); | 119 checkAnnotation('foo', 'var foo;'); |
116 } | 120 } |
117 | 121 |
118 void main() { | 122 void main() { |
119 testClassMetadata(); | 123 testClassMetadata(); |
120 testTopLevelMethodMetadata(); | 124 testTopLevelMethodMetadata(); |
121 testTopLevelFieldMetadata(); | 125 testTopLevelFieldMetadata(); |
122 } | 126 } |
OLD | NEW |