Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: dart/tests/compiler/dart2js/metadata_test.dart

Issue 12184017: MetadataAnnotation should be spannable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dart/sdk/lib/_internal/compiler/implementation/scanner/listener.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar t'; 10 import '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar t';
11 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart'; 11 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart';
12 import '../../../sdk/lib/_internal/compiler/implementation/util/util.dart'
13 show Spannable;
14 import '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart'
15 show Node;
16
17 void checkPosition(Spannable spannable, Node node, String source, compiler) {
18 SourceSpan span = compiler.spanFromSpannable(spannable);
19 Expect.isTrue(span.begin < span.end,
20 'begin = ${span.begin}; end = ${span.end}');
21 Expect.isTrue(span.end < source.length,
22 'end = ${span.end}; length = ${source.length}');
23 String yield = source.substring(span.begin, span.end);
24
25 // TODO(ahe): The node does not include "@". Fix that.
ahe 2013/02/05 09:54:10 Filed http://dartbug.com/8326.
26 Expect.stringEquals('@$node', yield);
27 }
12 28
13 void checkAnnotation(String name, String declaration, 29 void checkAnnotation(String name, String declaration,
14 {bool isTopLevelOnly: false}) { 30 {bool isTopLevelOnly: false}) {
15 var source; 31 var source;
16 32
17 // Ensure that a compile-time constant can be resolved from an 33 // Ensure that a compile-time constant can be resolved from an
18 // annotation. 34 // annotation.
19 source = """const native = 'xyz'; 35 source = """const native = 'xyz';
20 @native 36 @native
21 $declaration 37 $declaration
22 main() {}"""; 38 main() {}""";
23 39
24 compileAndCheck(source, name, (compiler, element) { 40 compileAndCheck(source, name, (compiler, element) {
25 compiler.enqueuer.resolution.queueIsClosed = false; 41 compiler.enqueuer.resolution.queueIsClosed = false;
26 Expect.equals(1, length(element.metadata)); 42 Expect.equals(1, length(element.metadata));
27 MetadataAnnotation annotation = element.metadata.head; 43 PartialMetadataAnnotation annotation = element.metadata.head;
28 annotation.ensureResolved(compiler); 44 annotation.ensureResolved(compiler);
29 Constant value = annotation.value; 45 Constant value = annotation.value;
30 Expect.stringEquals('xyz', value.value.slowToString()); 46 Expect.stringEquals('xyz', value.value.slowToString());
47
48 checkPosition(annotation, annotation.cachedNode, source, compiler);
31 }); 49 });
32 50
33 // Ensure that each repeated annotation has a unique instance of 51 // Ensure that each repeated annotation has a unique instance of
34 // [MetadataAnnotation]. 52 // [MetadataAnnotation].
35 source = """const native = 'xyz'; 53 source = """const native = 'xyz';
36 @native @native 54 @native @native
37 $declaration 55 $declaration
38 main() {}"""; 56 main() {}""";
39 57
40 compileAndCheck(source, name, (compiler, element) { 58 compileAndCheck(source, name, (compiler, element) {
41 compiler.enqueuer.resolution.queueIsClosed = false; 59 compiler.enqueuer.resolution.queueIsClosed = false;
42 Expect.equals(2, length(element.metadata)); 60 Expect.equals(2, length(element.metadata));
43 MetadataAnnotation annotation1 = element.metadata.head; 61 PartialMetadataAnnotation annotation1 = element.metadata.head;
44 MetadataAnnotation annotation2 = element.metadata.tail.head; 62 PartialMetadataAnnotation annotation2 = element.metadata.tail.head;
45 annotation1.ensureResolved(compiler); 63 annotation1.ensureResolved(compiler);
46 annotation2.ensureResolved(compiler); 64 annotation2.ensureResolved(compiler);
47 Expect.isFalse(identical(annotation1, annotation2), 65 Expect.isFalse(identical(annotation1, annotation2),
48 'expected unique instances'); 66 'expected unique instances');
49 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); 67 Expect.notEquals(annotation1, annotation2, 'expected unequal instances');
50 Constant value1 = annotation1.value; 68 Constant value1 = annotation1.value;
51 Constant value2 = annotation2.value; 69 Constant value2 = annotation2.value;
52 Expect.identical(value1, value2, 'expected same compile-time constant'); 70 Expect.identical(value1, value2, 'expected same compile-time constant');
53 Expect.stringEquals('xyz', value1.value.slowToString()); 71 Expect.stringEquals('xyz', value1.value.slowToString());
54 Expect.stringEquals('xyz', value2.value.slowToString()); 72 Expect.stringEquals('xyz', value2.value.slowToString());
73
74 checkPosition(annotation1, annotation1.cachedNode, source, compiler);
75 checkPosition(annotation2, annotation2.cachedNode, source, compiler);
55 }); 76 });
56 77
57 if (isTopLevelOnly) return; 78 if (isTopLevelOnly) return;
58 79
59 // Ensure that a compile-time constant can be resolved from an 80 // Ensure that a compile-time constant can be resolved from an
60 // annotation. 81 // annotation.
61 source = """const native = 'xyz'; 82 source = """const native = 'xyz';
62 class Foo { 83 class Foo {
63 @native 84 @native
64 $declaration 85 $declaration
65 } 86 }
66 main() {}"""; 87 main() {}""";
67 88
68 compileAndCheck(source, 'Foo', (compiler, element) { 89 compileAndCheck(source, 'Foo', (compiler, element) {
69 compiler.enqueuer.resolution.queueIsClosed = false; 90 compiler.enqueuer.resolution.queueIsClosed = false;
70 Expect.equals(0, length(element.metadata)); 91 Expect.equals(0, length(element.metadata));
71 element.ensureResolved(compiler); 92 element.ensureResolved(compiler);
72 Expect.equals(0, length(element.metadata)); 93 Expect.equals(0, length(element.metadata));
73 element = element.lookupLocalMember(buildSourceString(name)); 94 element = element.lookupLocalMember(buildSourceString(name));
74 Expect.equals(1, length(element.metadata)); 95 Expect.equals(1, length(element.metadata));
75 MetadataAnnotation annotation = element.metadata.head; 96 PartialMetadataAnnotation annotation = element.metadata.head;
76 annotation.ensureResolved(compiler); 97 annotation.ensureResolved(compiler);
77 Constant value = annotation.value; 98 Constant value = annotation.value;
78 Expect.stringEquals('xyz', value.value.slowToString()); 99 Expect.stringEquals('xyz', value.value.slowToString());
100
101 checkPosition(annotation, annotation.cachedNode, source, compiler);
79 }); 102 });
80 103
81 // Ensure that each repeated annotation has a unique instance of 104 // Ensure that each repeated annotation has a unique instance of
82 // [MetadataAnnotation]. 105 // [MetadataAnnotation].
83 source = """const native = 'xyz'; 106 source = """const native = 'xyz';
84 class Foo { 107 class Foo {
85 @native @native 108 @native @native
86 $declaration 109 $declaration
87 } 110 }
88 main() {}"""; 111 main() {}""";
89 112
90 compileAndCheck(source, 'Foo', (compiler, element) { 113 compileAndCheck(source, 'Foo', (compiler, element) {
91 compiler.enqueuer.resolution.queueIsClosed = false; 114 compiler.enqueuer.resolution.queueIsClosed = false;
92 Expect.equals(0, length(element.metadata)); 115 Expect.equals(0, length(element.metadata));
93 element.ensureResolved(compiler); 116 element.ensureResolved(compiler);
94 Expect.equals(0, length(element.metadata)); 117 Expect.equals(0, length(element.metadata));
95 element = element.lookupLocalMember(buildSourceString(name)); 118 element = element.lookupLocalMember(buildSourceString(name));
96 Expect.equals(2, length(element.metadata)); 119 Expect.equals(2, length(element.metadata));
97 MetadataAnnotation annotation1 = element.metadata.head; 120 PartialMetadataAnnotation annotation1 = element.metadata.head;
98 MetadataAnnotation annotation2 = element.metadata.tail.head; 121 PartialMetadataAnnotation annotation2 = element.metadata.tail.head;
99 annotation1.ensureResolved(compiler); 122 annotation1.ensureResolved(compiler);
100 annotation2.ensureResolved(compiler); 123 annotation2.ensureResolved(compiler);
101 Expect.isFalse(identical(annotation1, annotation2), 124 Expect.isFalse(identical(annotation1, annotation2),
102 'expected unique instances'); 125 'expected unique instances');
103 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); 126 Expect.notEquals(annotation1, annotation2, 'expected unequal instances');
104 Constant value1 = annotation1.value; 127 Constant value1 = annotation1.value;
105 Constant value2 = annotation2.value; 128 Constant value2 = annotation2.value;
106 Expect.identical(value1, value2, 'expected same compile-time constant'); 129 Expect.identical(value1, value2, 'expected same compile-time constant');
107 Expect.stringEquals('xyz', value1.value.slowToString()); 130 Expect.stringEquals('xyz', value1.value.slowToString());
108 Expect.stringEquals('xyz', value2.value.slowToString()); 131 Expect.stringEquals('xyz', value2.value.slowToString());
132
133 checkPosition(annotation1, annotation1.cachedNode, source, compiler);
134 checkPosition(annotation1, annotation2.cachedNode, source, compiler);
109 }); 135 });
110 } 136 }
111 137
112 void testClassMetadata() { 138 void testClassMetadata() {
113 checkAnnotation('Foo', 'class Foo {}', isTopLevelOnly: true); 139 checkAnnotation('Foo', 'class Foo {}', isTopLevelOnly: true);
114 } 140 }
115 141
116 void testTopLevelMethodMetadata() { 142 void testTopLevelMethodMetadata() {
117 checkAnnotation('foo', 'foo() {}'); 143 checkAnnotation('foo', 'foo() {}');
118 } 144 }
119 145
120 void testTopLevelFieldMetadata() { 146 void testTopLevelFieldMetadata() {
121 checkAnnotation('foo', 'var foo;'); 147 checkAnnotation('foo', 'var foo;');
122 } 148 }
123 149
124 void main() { 150 void main() {
125 testClassMetadata(); 151 testClassMetadata();
126 testTopLevelMethodMetadata(); 152 testTopLevelMethodMetadata();
127 testTopLevelFieldMetadata(); 153 testTopLevelFieldMetadata();
128 } 154 }
OLDNEW
« no previous file with comments | « dart/sdk/lib/_internal/compiler/implementation/scanner/listener.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698