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:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import "package:async_helper/async_helper.dart"; | 6 import "package:async_helper/async_helper.dart"; |
7 import 'compiler_helper.dart'; | 7 import 'compiler_helper.dart'; |
8 import 'parser_helper.dart'; | 8 import 'parser_helper.dart'; |
| 9 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' |
| 10 show PrimitiveConstant; |
9 | 11 |
10 void checkPosition(Spannable spannable, Node node, String source, compiler) { | 12 void checkPosition(Spannable spannable, Node node, String source, compiler) { |
11 SourceSpan span = compiler.spanFromSpannable(spannable); | 13 SourceSpan span = compiler.spanFromSpannable(spannable); |
12 Expect.isTrue(span.begin < span.end, | 14 Expect.isTrue(span.begin < span.end, |
13 'begin = ${span.begin}; end = ${span.end}'); | 15 'begin = ${span.begin}; end = ${span.end}'); |
14 Expect.isTrue(span.end < source.length, | 16 Expect.isTrue(span.end < source.length, |
15 'end = ${span.end}; length = ${source.length}'); | 17 'end = ${span.end}; length = ${source.length}'); |
16 String yield = source.substring(span.begin, span.end); | 18 String yield = source.substring(span.begin, span.end); |
17 | 19 |
18 // TODO(ahe): The node does not include "@". Fix that. | 20 // TODO(ahe): The node does not include "@". Fix that. |
19 Expect.stringEquals('@$node', yield); | 21 Expect.stringEquals('@$node', yield); |
20 } | 22 } |
21 | 23 |
22 void checkAnnotation(String name, String declaration, | 24 void checkAnnotation(String name, String declaration, |
23 {bool isTopLevelOnly: false}) { | 25 {bool isTopLevelOnly: false}) { |
24 // Ensure that a compile-time constant can be resolved from an | 26 // Ensure that a compile-time constant can be resolved from an |
25 // annotation. | 27 // annotation. |
26 var source1 = """const native = 'xyz'; | 28 var source1 = """const native = 'xyz'; |
27 @native | 29 @native |
28 $declaration | 30 $declaration |
29 main() {}"""; | 31 main() {}"""; |
30 | 32 |
31 compileAndCheck(source1, name, (compiler, element) { | 33 compileAndCheck(source1, name, (compiler, element) { |
32 compiler.enqueuer.resolution.queueIsClosed = false; | 34 compiler.enqueuer.resolution.queueIsClosed = false; |
33 Expect.equals(1, length(element.metadata), | 35 Expect.equals(1, length(element.metadata), |
34 'Unexpected metadata count on $element.'); | 36 'Unexpected metadata count on $element.'); |
35 PartialMetadataAnnotation annotation = element.metadata.head; | 37 PartialMetadataAnnotation annotation = element.metadata.head; |
36 annotation.ensureResolved(compiler); | 38 annotation.ensureResolved(compiler); |
37 Constant value = annotation.value; | 39 PrimitiveConstant value = annotation.value; |
38 Expect.stringEquals('xyz', value.value.slowToString()); | 40 Expect.stringEquals('xyz', value.value.slowToString()); |
39 | 41 |
40 checkPosition(annotation, annotation.cachedNode, source1, compiler); | 42 checkPosition(annotation, annotation.cachedNode, source1, compiler); |
41 }); | 43 }); |
42 | 44 |
43 // Ensure that each repeated annotation has a unique instance of | 45 // Ensure that each repeated annotation has a unique instance of |
44 // [MetadataAnnotation]. | 46 // [MetadataAnnotation]. |
45 var source2 = """const native = 'xyz'; | 47 var source2 = """const native = 'xyz'; |
46 @native @native | 48 @native @native |
47 $declaration | 49 $declaration |
48 main() {}"""; | 50 main() {}"""; |
49 | 51 |
50 compileAndCheck(source2, name, (compiler, element) { | 52 compileAndCheck(source2, name, (compiler, element) { |
51 compiler.enqueuer.resolution.queueIsClosed = false; | 53 compiler.enqueuer.resolution.queueIsClosed = false; |
52 Expect.equals(2, length(element.metadata)); | 54 Expect.equals(2, length(element.metadata)); |
53 PartialMetadataAnnotation annotation1 = element.metadata.head; | 55 PartialMetadataAnnotation annotation1 = element.metadata.head; |
54 PartialMetadataAnnotation annotation2 = element.metadata.tail.head; | 56 PartialMetadataAnnotation annotation2 = element.metadata.tail.head; |
55 annotation1.ensureResolved(compiler); | 57 annotation1.ensureResolved(compiler); |
56 annotation2.ensureResolved(compiler); | 58 annotation2.ensureResolved(compiler); |
57 Expect.isFalse(identical(annotation1, annotation2), | 59 Expect.isFalse(identical(annotation1, annotation2), |
58 'expected unique instances'); | 60 'expected unique instances'); |
59 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 61 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
60 Constant value1 = annotation1.value; | 62 PrimitiveConstant value1 = annotation1.value; |
61 Constant value2 = annotation2.value; | 63 PrimitiveConstant value2 = annotation2.value; |
62 Expect.identical(value1, value2, 'expected same compile-time constant'); | 64 Expect.identical(value1, value2, 'expected same compile-time constant'); |
63 Expect.stringEquals('xyz', value1.value.slowToString()); | 65 Expect.stringEquals('xyz', value1.value.slowToString()); |
64 Expect.stringEquals('xyz', value2.value.slowToString()); | 66 Expect.stringEquals('xyz', value2.value.slowToString()); |
65 | 67 |
66 checkPosition(annotation1, annotation1.cachedNode, source2, compiler); | 68 checkPosition(annotation1, annotation1.cachedNode, source2, compiler); |
67 checkPosition(annotation2, annotation2.cachedNode, source2, compiler); | 69 checkPosition(annotation2, annotation2.cachedNode, source2, compiler); |
68 }); | 70 }); |
69 | 71 |
70 if (isTopLevelOnly) return; | 72 if (isTopLevelOnly) return; |
71 | 73 |
72 // Ensure that a compile-time constant can be resolved from an | 74 // Ensure that a compile-time constant can be resolved from an |
73 // annotation. | 75 // annotation. |
74 var source3 = """const native = 'xyz'; | 76 var source3 = """const native = 'xyz'; |
75 class Foo { | 77 class Foo { |
76 @native | 78 @native |
77 $declaration | 79 $declaration |
78 } | 80 } |
79 main() {}"""; | 81 main() {}"""; |
80 | 82 |
81 compileAndCheck(source3, 'Foo', (compiler, element) { | 83 compileAndCheck(source3, 'Foo', (compiler, element) { |
82 compiler.enqueuer.resolution.queueIsClosed = false; | 84 compiler.enqueuer.resolution.queueIsClosed = false; |
83 Expect.equals(0, length(element.metadata)); | 85 Expect.equals(0, length(element.metadata)); |
84 element.ensureResolved(compiler); | 86 element.ensureResolved(compiler); |
85 Expect.equals(0, length(element.metadata)); | 87 Expect.equals(0, length(element.metadata)); |
86 element = element.lookupLocalMember(name); | 88 element = element.lookupLocalMember(name); |
87 Expect.equals(1, length(element.metadata)); | 89 Expect.equals(1, length(element.metadata)); |
88 PartialMetadataAnnotation annotation = element.metadata.head; | 90 PartialMetadataAnnotation annotation = element.metadata.head; |
89 annotation.ensureResolved(compiler); | 91 annotation.ensureResolved(compiler); |
90 Constant value = annotation.value; | 92 PrimitiveConstant value = annotation.value; |
91 Expect.stringEquals('xyz', value.value.slowToString()); | 93 Expect.stringEquals('xyz', value.value.slowToString()); |
92 | 94 |
93 checkPosition(annotation, annotation.cachedNode, source3, compiler); | 95 checkPosition(annotation, annotation.cachedNode, source3, compiler); |
94 }); | 96 }); |
95 | 97 |
96 // Ensure that each repeated annotation has a unique instance of | 98 // Ensure that each repeated annotation has a unique instance of |
97 // [MetadataAnnotation]. | 99 // [MetadataAnnotation]. |
98 var source4 = """const native = 'xyz'; | 100 var source4 = """const native = 'xyz'; |
99 class Foo { | 101 class Foo { |
100 @native @native | 102 @native @native |
101 $declaration | 103 $declaration |
102 } | 104 } |
103 main() {}"""; | 105 main() {}"""; |
104 | 106 |
105 compileAndCheck(source4, 'Foo', (compiler, element) { | 107 compileAndCheck(source4, 'Foo', (compiler, element) { |
106 compiler.enqueuer.resolution.queueIsClosed = false; | 108 compiler.enqueuer.resolution.queueIsClosed = false; |
107 Expect.equals(0, length(element.metadata)); | 109 Expect.equals(0, length(element.metadata)); |
108 element.ensureResolved(compiler); | 110 element.ensureResolved(compiler); |
109 Expect.equals(0, length(element.metadata)); | 111 Expect.equals(0, length(element.metadata)); |
110 element = element.lookupLocalMember(name); | 112 element = element.lookupLocalMember(name); |
111 Expect.equals(2, length(element.metadata)); | 113 Expect.equals(2, length(element.metadata)); |
112 PartialMetadataAnnotation annotation1 = element.metadata.head; | 114 PartialMetadataAnnotation annotation1 = element.metadata.head; |
113 PartialMetadataAnnotation annotation2 = element.metadata.tail.head; | 115 PartialMetadataAnnotation annotation2 = element.metadata.tail.head; |
114 annotation1.ensureResolved(compiler); | 116 annotation1.ensureResolved(compiler); |
115 annotation2.ensureResolved(compiler); | 117 annotation2.ensureResolved(compiler); |
116 Expect.isFalse(identical(annotation1, annotation2), | 118 Expect.isFalse(identical(annotation1, annotation2), |
117 'expected unique instances'); | 119 'expected unique instances'); |
118 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 120 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
119 Constant value1 = annotation1.value; | 121 PrimitiveConstant value1 = annotation1.value; |
120 Constant value2 = annotation2.value; | 122 PrimitiveConstant value2 = annotation2.value; |
121 Expect.identical(value1, value2, 'expected same compile-time constant'); | 123 Expect.identical(value1, value2, 'expected same compile-time constant'); |
122 Expect.stringEquals('xyz', value1.value.slowToString()); | 124 Expect.stringEquals('xyz', value1.value.slowToString()); |
123 Expect.stringEquals('xyz', value2.value.slowToString()); | 125 Expect.stringEquals('xyz', value2.value.slowToString()); |
124 | 126 |
125 checkPosition(annotation1, annotation1.cachedNode, source4, compiler); | 127 checkPosition(annotation1, annotation1.cachedNode, source4, compiler); |
126 checkPosition(annotation1, annotation2.cachedNode, source4, compiler); | 128 checkPosition(annotation1, annotation2.cachedNode, source4, compiler); |
127 }); | 129 }); |
128 } | 130 } |
129 | 131 |
130 void testClassMetadata() { | 132 void testClassMetadata() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 asyncTest(() => compiler.runCompiler(uri).then((_) { | 164 asyncTest(() => compiler.runCompiler(uri).then((_) { |
163 compiler.enqueuer.resolution.queueIsClosed = false; | 165 compiler.enqueuer.resolution.queueIsClosed = false; |
164 LibraryElement element = compiler.libraries['$uri']; | 166 LibraryElement element = compiler.libraries['$uri']; |
165 Expect.isNotNull(element, 'Cannot find $uri'); | 167 Expect.isNotNull(element, 'Cannot find $uri'); |
166 | 168 |
167 Link<MetadataAnnotation> metadata = extractMetadata(element); | 169 Link<MetadataAnnotation> metadata = extractMetadata(element); |
168 Expect.equals(1, length(metadata)); | 170 Expect.equals(1, length(metadata)); |
169 | 171 |
170 PartialMetadataAnnotation annotation = metadata.head; | 172 PartialMetadataAnnotation annotation = metadata.head; |
171 annotation.ensureResolved(compiler); | 173 annotation.ensureResolved(compiler); |
172 Constant value = annotation.value; | 174 PrimitiveConstant value = annotation.value; |
173 Expect.stringEquals('xyz', value.value.slowToString()); | 175 Expect.stringEquals('xyz', value.value.slowToString()); |
174 | 176 |
175 checkPosition(annotation, annotation.cachedNode, source, compiler); | 177 checkPosition(annotation, annotation.cachedNode, source, compiler); |
176 })); | 178 })); |
177 } | 179 } |
178 | 180 |
179 var source; | 181 var source; |
180 | 182 |
181 source = """@native | 183 source = """@native |
182 library foo; | 184 library foo; |
(...skipping 26 matching lines...) Expand all Loading... |
209 compileAndCheckLibrary(source, | 211 compileAndCheckLibrary(source, |
210 (e) => e.compilationUnits.first.partTag.metadata); | 212 (e) => e.compilationUnits.first.partTag.metadata); |
211 } | 213 } |
212 | 214 |
213 void main() { | 215 void main() { |
214 testClassMetadata(); | 216 testClassMetadata(); |
215 testTopLevelMethodMetadata(); | 217 testTopLevelMethodMetadata(); |
216 testTopLevelFieldMetadata(); | 218 testTopLevelFieldMetadata(); |
217 testLibraryTags(); | 219 testLibraryTags(); |
218 } | 220 } |
OLD | NEW |