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