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

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

Issue 27510003: Scanner for UTF-8 byte arrays (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixes compiler tests Created 7 years, 2 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
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 "package:expect/expect.dart"; 5 import "package:expect/expect.dart";
6 import 'dart:async'; 6 import 'dart:async';
7 import "package:async_helper/async_helper.dart"; 7 import "package:async_helper/async_helper.dart";
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import "../../../sdk/lib/_internal/compiler/implementation/resolution/resolution .dart"; 10 import "../../../sdk/lib/_internal/compiler/implementation/resolution/resolution .dart";
(...skipping 29 matching lines...) Expand all
40 Element element = visitor.visit(createLocals(variables)); 40 Element element = visitor.visit(createLocals(variables));
41 // A VariableDefinitions does not have an element. 41 // A VariableDefinitions does not have an element.
42 Expect.equals(null, element); 42 Expect.equals(null, element);
43 Expect.equals(variables.length, map(visitor).length); 43 Expect.equals(variables.length, map(visitor).length);
44 44
45 for (final variable in variables) { 45 for (final variable in variables) {
46 final name = variable[0]; 46 final name = variable[0];
47 Identifier id = buildIdentifier(name); 47 Identifier id = buildIdentifier(name);
48 final VariableElement variableElement = visitor.visit(id); 48 final VariableElement variableElement = visitor.visit(id);
49 MethodScope scope = visitor.scope; 49 MethodScope scope = visitor.scope;
50 Expect.equals(variableElement, scope.elements[buildSourceString(name)]); 50 Expect.equals(variableElement, scope.elements[name]);
51 } 51 }
52 return compiler; 52 return compiler;
53 } 53 }
54 54
55 main() { 55 main() {
56 testLocalsOne(); 56 testLocalsOne();
57 testLocalsTwo(); 57 testLocalsTwo();
58 testLocalsThree(); 58 testLocalsThree();
59 testLocalsFour(); 59 testLocalsFour();
60 testLocalsFive(); 60 testLocalsFive();
(...skipping 29 matching lines...) Expand all
90 class J1 extends K1 {} 90 class J1 extends K1 {}
91 class J2 implements K2 {} 91 class J2 implements K2 {}
92 class K1 {} 92 class K1 {}
93 class K2 {} 93 class K2 {}
94 class L1 {} 94 class L1 {}
95 class A implements I1, I2 {} 95 class A implements I1, I2 {}
96 class B extends A implements J1, J2 {} 96 class B extends A implements J1, J2 {}
97 class C extends B implements L1 {} 97 class C extends B implements L1 {}
98 """); 98 """);
99 compiler.resolveStatement("C c;"); 99 compiler.resolveStatement("C c;");
100 ClassElement classA = compiler.mainApp.find(buildSourceString("A")); 100 ClassElement classA = compiler.mainApp.find("A");
101 ClassElement classB = compiler.mainApp.find(buildSourceString("B")); 101 ClassElement classB = compiler.mainApp.find("B");
102 ClassElement classC = compiler.mainApp.find(buildSourceString("C")); 102 ClassElement classC = compiler.mainApp.find("C");
103 Expect.equals('[ I2, I1, Object ]', classA.allSupertypes.toString()); 103 Expect.equals('[ I2, I1, Object ]', classA.allSupertypes.toString());
104 Expect.equals('[ A, J2, J1, I2, I1, K2, K1, Object ]', 104 Expect.equals('[ A, J2, J1, I2, I1, K2, K1, Object ]',
105 classB.allSupertypes.toString()); 105 classB.allSupertypes.toString());
106 Expect.equals('[ B, L1, A, J2, J1, I2, I1, K2, K1, Object ]', 106 Expect.equals('[ B, L1, A, J2, J1, I2, I1, K2, K1, Object ]',
107 classC.allSupertypes.toString()); 107 classC.allSupertypes.toString());
108 108
109 compiler = new MockCompiler(); 109 compiler = new MockCompiler();
110 compiler.parseScript(""" 110 compiler.parseScript("""
111 class X<T> {} 111 class X<T> {}
112 class Foo extends X<Foo> {} 112 class Foo extends X<Foo> {}
113 class Bar extends Foo implements X<Bar> {} 113 class Bar extends Foo implements X<Bar> {}
114 """); 114 """);
115 compiler.resolveStatement("Bar bar;"); 115 compiler.resolveStatement("Bar bar;");
116 ClassElement classBar = compiler.mainApp.find(buildSourceString("Bar")); 116 ClassElement classBar = compiler.mainApp.find("Bar");
117 Expect.equals('[ Foo, X<Bar>, X<Foo>, Object ]', 117 Expect.equals('[ Foo, X<Bar>, X<Foo>, Object ]',
118 classBar.allSupertypes.toString()); 118 classBar.allSupertypes.toString());
119 } 119 }
120 120
121 testTypeVariables() { 121 testTypeVariables() {
122 matchResolvedTypes(visitor, text, name, expectedElements) { 122 matchResolvedTypes(visitor, text, name, expectedElements) {
123 VariableDefinitions definition = parseStatement(text); 123 VariableDefinitions definition = parseStatement(text);
124 visitor.visit(definition.type); 124 visitor.visit(definition.type);
125 InterfaceType type = visitor.mapping.getType(definition.type); 125 InterfaceType type = visitor.mapping.getType(definition.type);
126 Expect.equals(definition.type.typeArguments.slowLength(), 126 Expect.equals(definition.type.typeArguments.slowLength(),
127 length(type.typeArguments)); 127 length(type.typeArguments));
128 int index = 0; 128 int index = 0;
129 Link<DartType> arguments = type.typeArguments; 129 Link<DartType> arguments = type.typeArguments;
130 while (!arguments.isEmpty) { 130 while (!arguments.isEmpty) {
131 Expect.equals(true, index < expectedElements.length); 131 Expect.equals(true, index < expectedElements.length);
132 Expect.equals(expectedElements[index], arguments.head.element); 132 Expect.equals(expectedElements[index], arguments.head.element);
133 index++; 133 index++;
134 arguments = arguments.tail; 134 arguments = arguments.tail;
135 } 135 }
136 Expect.equals(index, expectedElements.length); 136 Expect.equals(index, expectedElements.length);
137 } 137 }
138 138
139 MockCompiler compiler = new MockCompiler(); 139 MockCompiler compiler = new MockCompiler();
140 ResolverVisitor visitor = compiler.resolverVisitor(); 140 ResolverVisitor visitor = compiler.resolverVisitor();
141 compiler.parseScript('class Foo<T, U> {}'); 141 compiler.parseScript('class Foo<T, U> {}');
142 ClassElement foo = compiler.mainApp.find(buildSourceString('Foo')); 142 ClassElement foo = compiler.mainApp.find('Foo');
143 matchResolvedTypes(visitor, 'Foo<int, String> x;', 'Foo', 143 matchResolvedTypes(visitor, 'Foo<int, String> x;', 'Foo',
144 [compiler.intClass, compiler.stringClass]); 144 [compiler.intClass, compiler.stringClass]);
145 matchResolvedTypes(visitor, 'Foo<Foo, Foo> x;', 'Foo', 145 matchResolvedTypes(visitor, 'Foo<Foo, Foo> x;', 'Foo',
146 [foo, foo]); 146 [foo, foo]);
147 147
148 compiler = new MockCompiler(); 148 compiler = new MockCompiler();
149 compiler.parseScript('class Foo<T, U> {}'); 149 compiler.parseScript('class Foo<T, U> {}');
150 compiler.resolveStatement('Foo<notype, int> x;'); 150 compiler.resolveStatement('Foo<notype, int> x;');
151 Expect.equals(1, compiler.warnings.length); 151 Expect.equals(1, compiler.warnings.length);
152 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE.warning, 152 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE.warning,
153 compiler.warnings[0].message.kind); 153 compiler.warnings[0].message.kind);
154 Expect.equals(0, compiler.errors.length); 154 Expect.equals(0, compiler.errors.length);
155 155
156 compiler = new MockCompiler(); 156 compiler = new MockCompiler();
157 compiler.parseScript('class Foo<T, U> {}'); 157 compiler.parseScript('class Foo<T, U> {}');
158 compiler.resolveStatement('var x = new Foo<notype, int>();'); 158 compiler.resolveStatement('var x = new Foo<notype, int>();');
159 Expect.equals(1, compiler.warnings.length); 159 Expect.equals(1, compiler.warnings.length);
160 Expect.equals(0, compiler.errors.length); 160 Expect.equals(0, compiler.errors.length);
161 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE.warning, 161 Expect.equals(MessageKind.CANNOT_RESOLVE_TYPE.warning,
162 compiler.warnings[0].message.kind); 162 compiler.warnings[0].message.kind);
163 163
164 compiler = new MockCompiler(); 164 compiler = new MockCompiler();
165 compiler.parseScript('class Foo<T> {' 165 compiler.parseScript('class Foo<T> {'
166 ' Foo<T> t;' 166 ' Foo<T> t;'
167 ' foo(Foo<T> f) {}' 167 ' foo(Foo<T> f) {}'
168 ' bar() { g(Foo<T> f) {}; g(); }' 168 ' bar() { g(Foo<T> f) {}; g(); }'
169 '}'); 169 '}');
170 foo = compiler.mainApp.find(buildSourceString('Foo')); 170 foo = compiler.mainApp.find('Foo');
171 foo.ensureResolved(compiler); 171 foo.ensureResolved(compiler);
172 foo.lookupLocalMember(buildSourceString('t')).computeType(compiler);; 172 foo.lookupLocalMember('t').computeType(compiler);;
173 foo.lookupLocalMember(buildSourceString('foo')).computeType(compiler);; 173 foo.lookupLocalMember('foo').computeType(compiler);;
174 compiler.resolver.resolve(foo.lookupLocalMember(buildSourceString('bar'))); 174 compiler.resolver.resolve(foo.lookupLocalMember('bar'));
175 Expect.equals(0, compiler.warnings.length); 175 Expect.equals(0, compiler.warnings.length);
176 Expect.equals(0, compiler.errors.length); 176 Expect.equals(0, compiler.errors.length);
177 } 177 }
178 178
179 testSuperCalls() { 179 testSuperCalls() {
180 MockCompiler compiler = new MockCompiler(); 180 MockCompiler compiler = new MockCompiler();
181 String script = """class A { foo() {} } 181 String script = """class A { foo() {} }
182 class B extends A { foo() => super.foo(); }"""; 182 class B extends A { foo() => super.foo(); }""";
183 compiler.parseScript(script); 183 compiler.parseScript(script);
184 compiler.resolveStatement("B b;"); 184 compiler.resolveStatement("B b;");
185 185
186 ClassElement classB = compiler.mainApp.find(buildSourceString("B")); 186 ClassElement classB = compiler.mainApp.find("B");
187 FunctionElement fooB = classB.lookupLocalMember(buildSourceString("foo")); 187 FunctionElement fooB = classB.lookupLocalMember("foo");
188 ClassElement classA = compiler.mainApp.find(buildSourceString("A")); 188 ClassElement classA = compiler.mainApp.find("A");
189 FunctionElement fooA = classA.lookupLocalMember(buildSourceString("foo")); 189 FunctionElement fooA = classA.lookupLocalMember("foo");
190 190
191 ResolverVisitor visitor = 191 ResolverVisitor visitor =
192 new ResolverVisitor(compiler, fooB, new CollectingTreeElements(fooB)); 192 new ResolverVisitor(compiler, fooB, new CollectingTreeElements(fooB));
193 FunctionExpression node = fooB.parseNode(compiler); 193 FunctionExpression node = fooB.parseNode(compiler);
194 visitor.visit(node.body); 194 visitor.visit(node.body);
195 Map mapping = map(visitor); 195 Map mapping = map(visitor);
196 196
197 Send superCall = node.body.asReturn().expression; 197 Send superCall = node.body.asReturn().expression;
198 FunctionElement called = mapping[superCall]; 198 FunctionElement called = mapping[superCall];
199 Expect.isNotNull(called); 199 Expect.isNotNull(called);
200 Expect.equals(fooA, called); 200 Expect.equals(fooA, called);
201 } 201 }
202 202
203 testThis() { 203 testThis() {
204 MockCompiler compiler = new MockCompiler(); 204 MockCompiler compiler = new MockCompiler();
205 compiler.parseScript("class Foo { foo() { return this; } }"); 205 compiler.parseScript("class Foo { foo() { return this; } }");
206 compiler.resolveStatement("Foo foo;"); 206 compiler.resolveStatement("Foo foo;");
207 ClassElement fooElement = compiler.mainApp.find(buildSourceString("Foo")); 207 ClassElement fooElement = compiler.mainApp.find("Foo");
208 FunctionElement funElement = 208 FunctionElement funElement =
209 fooElement.lookupLocalMember(buildSourceString("foo")); 209 fooElement.lookupLocalMember("foo");
ngeoffray 2013/10/18 10:19:37 One line.
lukas 2013/10/24 16:48:36 Done.
210 ResolverVisitor visitor = 210 ResolverVisitor visitor =
211 new ResolverVisitor(compiler, funElement, 211 new ResolverVisitor(compiler, funElement,
212 new CollectingTreeElements(funElement)); 212 new CollectingTreeElements(funElement));
213 FunctionExpression function = funElement.parseNode(compiler); 213 FunctionExpression function = funElement.parseNode(compiler);
214 visitor.visit(function.body); 214 visitor.visit(function.body);
215 Map mapping = map(visitor); 215 Map mapping = map(visitor);
216 List<Element> values = mapping.values.toList(); 216 List<Element> values = mapping.values.toList();
217 Expect.equals(0, mapping.length); 217 Expect.equals(0, mapping.length);
218 Expect.equals(0, compiler.warnings.length); 218 Expect.equals(0, compiler.warnings.length);
219 219
220 compiler = new MockCompiler(); 220 compiler = new MockCompiler();
221 compiler.resolveStatement("main() { return this; }"); 221 compiler.resolveStatement("main() { return this; }");
222 Expect.equals(0, compiler.warnings.length); 222 Expect.equals(0, compiler.warnings.length);
223 Expect.equals(1, compiler.errors.length); 223 Expect.equals(1, compiler.errors.length);
224 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, 224 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE,
225 compiler.errors[0].message.kind); 225 compiler.errors[0].message.kind);
226 226
227 compiler = new MockCompiler(); 227 compiler = new MockCompiler();
228 compiler.parseScript("class Foo { static foo() { return this; } }"); 228 compiler.parseScript("class Foo { static foo() { return this; } }");
229 compiler.resolveStatement("Foo foo;"); 229 compiler.resolveStatement("Foo foo;");
230 fooElement = compiler.mainApp.find(buildSourceString("Foo")); 230 fooElement = compiler.mainApp.find("Foo");
231 funElement = 231 funElement =
232 fooElement.lookupLocalMember(buildSourceString("foo")); 232 fooElement.lookupLocalMember("foo");
ngeoffray 2013/10/18 10:19:37 One line.
lukas 2013/10/24 16:48:36 Done.
233 visitor = new ResolverVisitor(compiler, funElement, 233 visitor = new ResolverVisitor(compiler, funElement,
234 new CollectingTreeElements(funElement)); 234 new CollectingTreeElements(funElement));
235 function = funElement.parseNode(compiler); 235 function = funElement.parseNode(compiler);
236 visitor.visit(function.body); 236 visitor.visit(function.body);
237 Expect.equals(0, compiler.warnings.length); 237 Expect.equals(0, compiler.warnings.length);
238 Expect.equals(1, compiler.errors.length); 238 Expect.equals(1, compiler.errors.length);
239 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, 239 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE,
240 compiler.errors[0].message.kind); 240 compiler.errors[0].message.kind);
241 } 241 }
242 242
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 {'typeName': 'Bar'}, false); 466 {'typeName': 'Bar'}, false);
467 Expect.equals(cannotResolveBar, compiler.errors[0].message); 467 Expect.equals(cannotResolveBar, compiler.errors[0].message);
468 compiler.clearMessages(); 468 compiler.clearMessages();
469 469
470 compiler = new MockCompiler(); 470 compiler = new MockCompiler();
471 compiler.parseScript("class Foo extends Bar {}"); 471 compiler.parseScript("class Foo extends Bar {}");
472 compiler.parseScript("class Bar {}"); 472 compiler.parseScript("class Bar {}");
473 Map mapping = compiler.resolveStatement("Foo bar;").map; 473 Map mapping = compiler.resolveStatement("Foo bar;").map;
474 Expect.equals(2, mapping.length); 474 Expect.equals(2, mapping.length);
475 475
476 ClassElement fooElement = compiler.mainApp.find(buildSourceString('Foo')); 476 ClassElement fooElement = compiler.mainApp.find('Foo');
477 ClassElement barElement = compiler.mainApp.find(buildSourceString('Bar')); 477 ClassElement barElement = compiler.mainApp.find('Bar');
478 Expect.equals(barElement.computeType(compiler), 478 Expect.equals(barElement.computeType(compiler),
479 fooElement.supertype); 479 fooElement.supertype);
480 Expect.isTrue(fooElement.interfaces.isEmpty); 480 Expect.isTrue(fooElement.interfaces.isEmpty);
481 Expect.isTrue(barElement.interfaces.isEmpty); 481 Expect.isTrue(barElement.interfaces.isEmpty);
482 } 482 }
483 483
484 testVarSuperclass() { 484 testVarSuperclass() {
485 MockCompiler compiler = new MockCompiler(); 485 MockCompiler compiler = new MockCompiler();
486 compiler.parseScript("class Foo extends var {}"); 486 compiler.parseScript("class Foo extends var {}");
487 compiler.resolveStatement("Foo bar;"); 487 compiler.resolveStatement("Foo bar;");
(...skipping 17 matching lines...) Expand all
505 compiler.clearMessages(); 505 compiler.clearMessages();
506 506
507 // Add the abstract class to the world and make sure everything is setup 507 // Add the abstract class to the world and make sure everything is setup
508 // correctly. 508 // correctly.
509 compiler.parseScript("abstract class Bar {}"); 509 compiler.parseScript("abstract class Bar {}");
510 510
511 ResolverVisitor visitor = 511 ResolverVisitor visitor =
512 new ResolverVisitor(compiler, null, new CollectingTreeElements(null)); 512 new ResolverVisitor(compiler, null, new CollectingTreeElements(null));
513 compiler.resolveStatement("Foo bar;"); 513 compiler.resolveStatement("Foo bar;");
514 514
515 ClassElement fooElement = compiler.mainApp.find(buildSourceString('Foo')); 515 ClassElement fooElement = compiler.mainApp.find('Foo');
516 ClassElement barElement = compiler.mainApp.find(buildSourceString('Bar')); 516 ClassElement barElement = compiler.mainApp.find('Bar');
517 517
518 Expect.equals(null, barElement.supertype); 518 Expect.equals(null, barElement.supertype);
519 Expect.isTrue(barElement.interfaces.isEmpty); 519 Expect.isTrue(barElement.interfaces.isEmpty);
520 520
521 Expect.equals(barElement.computeType(compiler), 521 Expect.equals(barElement.computeType(compiler),
522 fooElement.interfaces.head); 522 fooElement.interfaces.head);
523 Expect.equals(1, length(fooElement.interfaces)); 523 Expect.equals(1, length(fooElement.interfaces));
524 } 524 }
525 525
526 testTwoInterfaces() { 526 testTwoInterfaces() {
527 MockCompiler compiler = new MockCompiler(); 527 MockCompiler compiler = new MockCompiler();
528 compiler.parseScript( 528 compiler.parseScript(
529 "abstract class I1 {} abstract class I2 {} class C implements I1, I2 {}"); 529 "abstract class I1 {} abstract class I2 {} class C implements I1, I2 {}");
530 compiler.resolveStatement("Foo bar;"); 530 compiler.resolveStatement("Foo bar;");
531 531
532 ClassElement c = compiler.mainApp.find(buildSourceString('C')); 532 ClassElement c = compiler.mainApp.find('C');
533 Element i1 = compiler.mainApp.find(buildSourceString('I1')); 533 Element i1 = compiler.mainApp.find('I1');
534 Element i2 = compiler.mainApp.find(buildSourceString('I2')); 534 Element i2 = compiler.mainApp.find('I2');
535 535
536 Expect.equals(2, length(c.interfaces)); 536 Expect.equals(2, length(c.interfaces));
537 Expect.equals(i1.computeType(compiler), at(c.interfaces, 0)); 537 Expect.equals(i1.computeType(compiler), at(c.interfaces, 0));
538 Expect.equals(i2.computeType(compiler), at(c.interfaces, 1)); 538 Expect.equals(i2.computeType(compiler), at(c.interfaces, 1));
539 } 539 }
540 540
541 testFunctionExpression() { 541 testFunctionExpression() {
542 MockCompiler compiler = new MockCompiler(); 542 MockCompiler compiler = new MockCompiler();
543 ResolverVisitor visitor = compiler.resolverVisitor(); 543 ResolverVisitor visitor = compiler.resolverVisitor();
544 Map mapping = compiler.resolveStatement("int f() {}").map; 544 Map mapping = compiler.resolveStatement("int f() {}").map;
545 Expect.equals(3, mapping.length); 545 Expect.equals(3, mapping.length);
546 Element element; 546 Element element;
547 Node node; 547 Node node;
548 mapping.forEach((Node n, Element e) { 548 mapping.forEach((Node n, Element e) {
549 if (n is FunctionExpression) { 549 if (n is FunctionExpression) {
550 element = e; 550 element = e;
551 node = n; 551 node = n;
552 } 552 }
553 }); 553 });
554 Expect.equals(ElementKind.FUNCTION, element.kind); 554 Expect.equals(ElementKind.FUNCTION, element.kind);
555 Expect.equals(buildSourceString('f'), element.name); 555 Expect.equals('f', element.name);
556 Expect.equals(element.parseNode(compiler), node); 556 Expect.equals(element.parseNode(compiler), node);
557 } 557 }
558 558
559 testNewExpression() { 559 testNewExpression() {
560 MockCompiler compiler = new MockCompiler(); 560 MockCompiler compiler = new MockCompiler();
561 compiler.parseScript("class A {} foo() { print(new A()); }"); 561 compiler.parseScript("class A {} foo() { print(new A()); }");
562 ClassElement aElement = compiler.mainApp.find(buildSourceString('A')); 562 ClassElement aElement = compiler.mainApp.find('A');
563 FunctionElement fooElement = compiler.mainApp.find(buildSourceString('foo')); 563 FunctionElement fooElement = compiler.mainApp.find('foo');
564 Expect.isNotNull(aElement); 564 Expect.isNotNull(aElement);
565 Expect.isNotNull(fooElement); 565 Expect.isNotNull(fooElement);
566 566
567 fooElement.parseNode(compiler); 567 fooElement.parseNode(compiler);
568 compiler.resolver.resolve(fooElement); 568 compiler.resolver.resolve(fooElement);
569 569
570 TreeElements elements = compiler.resolveStatement("new A();"); 570 TreeElements elements = compiler.resolveStatement("new A();");
571 NewExpression expression = 571 NewExpression expression =
572 compiler.parsedTree.asExpressionStatement().expression; 572 compiler.parsedTree.asExpressionStatement().expression;
573 Element element = elements[expression.send]; 573 Element element = elements[expression.send];
574 Expect.equals(ElementKind.GENERATIVE_CONSTRUCTOR, element.kind); 574 Expect.equals(ElementKind.GENERATIVE_CONSTRUCTOR, element.kind);
575 Expect.isTrue(element.isSynthesized); 575 Expect.isTrue(element.isSynthesized);
576 } 576 }
577 577
578 testConstructorArgumentMismatch() { 578 testConstructorArgumentMismatch() {
579 String script = "class A {} foo() { print(new A(42)); }"; 579 String script = "class A {} foo() { print(new A(42)); }";
580 MockCompiler compiler = new MockCompiler(); 580 MockCompiler compiler = new MockCompiler();
581 compiler.parseScript(script); 581 compiler.parseScript(script);
582 FunctionElement fooElement = compiler.mainApp.find(buildSourceString('foo')); 582 FunctionElement fooElement = compiler.mainApp.find('foo');
583 Expect.isNotNull(fooElement); 583 Expect.isNotNull(fooElement);
584 fooElement.parseNode(compiler); 584 fooElement.parseNode(compiler);
585 compiler.resolver.resolve(fooElement); 585 compiler.resolver.resolve(fooElement);
586 586
587 compareWarningKinds( 587 compareWarningKinds(
588 script, [MessageKind.INVALID_ARGUMENTS.warning], compiler.warnings); 588 script, [MessageKind.INVALID_ARGUMENTS.warning], compiler.warnings);
589 compareWarningKinds(script, [], compiler.errors); 589 compareWarningKinds(script, [], compiler.errors);
590 } 590 }
591 591
592 testTopLevelFields() { 592 testTopLevelFields() {
593 MockCompiler compiler = new MockCompiler(); 593 MockCompiler compiler = new MockCompiler();
594 compiler.parseScript("int a;"); 594 compiler.parseScript("int a;");
595 VariableElement element = compiler.mainApp.find(buildSourceString("a")); 595 VariableElement element = compiler.mainApp.find("a");
596 Expect.equals(ElementKind.FIELD, element.kind); 596 Expect.equals(ElementKind.FIELD, element.kind);
597 VariableDefinitions node = element.variables.parseNode(compiler); 597 VariableDefinitions node = element.variables.parseNode(compiler);
598 Identifier typeName = node.type.typeName; 598 Identifier typeName = node.type.typeName;
599 Expect.equals(typeName.source.slowToString(), 'int'); 599 Expect.equals(typeName.source, 'int');
600 600
601 compiler.parseScript("var b, c;"); 601 compiler.parseScript("var b, c;");
602 VariableElement bElement = compiler.mainApp.find(buildSourceString("b")); 602 VariableElement bElement = compiler.mainApp.find("b");
603 VariableElement cElement = compiler.mainApp.find(buildSourceString("c")); 603 VariableElement cElement = compiler.mainApp.find("c");
604 Expect.equals(ElementKind.FIELD, bElement.kind); 604 Expect.equals(ElementKind.FIELD, bElement.kind);
605 Expect.equals(ElementKind.FIELD, cElement.kind); 605 Expect.equals(ElementKind.FIELD, cElement.kind);
606 Expect.isTrue(bElement != cElement); 606 Expect.isTrue(bElement != cElement);
607 607
608 VariableDefinitions bNode = bElement.variables.parseNode(compiler); 608 VariableDefinitions bNode = bElement.variables.parseNode(compiler);
609 VariableDefinitions cNode = cElement.variables.parseNode(compiler); 609 VariableDefinitions cNode = cElement.variables.parseNode(compiler);
610 Expect.equals(bNode, cNode); 610 Expect.equals(bNode, cNode);
611 Expect.isNull(bNode.type); 611 Expect.isNull(bNode.type);
612 Expect.isTrue(bNode.modifiers.isVar()); 612 Expect.isTrue(bNode.modifiers.isVar());
613 } 613 }
614 614
615 resolveConstructor(String script, String statement, String className, 615 resolveConstructor(String script, String statement, String className,
616 String constructor, int expectedElementCount, 616 String constructor, int expectedElementCount,
617 {List expectedWarnings: const [], 617 {List expectedWarnings: const [],
618 List expectedErrors: const [], 618 List expectedErrors: const [],
619 List expectedInfos: const [], 619 List expectedInfos: const [],
620 String corelib: DEFAULT_CORELIB}) { 620 String corelib: DEFAULT_CORELIB}) {
621 MockCompiler compiler = new MockCompiler(coreSource: corelib); 621 MockCompiler compiler = new MockCompiler(coreSource: corelib);
622 compiler.parseScript(script); 622 compiler.parseScript(script);
623 compiler.resolveStatement(statement); 623 compiler.resolveStatement(statement);
624 ClassElement classElement = 624 ClassElement classElement =
625 compiler.mainApp.find(buildSourceString(className)); 625 compiler.mainApp.find(className);
ngeoffray 2013/10/18 10:19:37 One line.
lukas 2013/10/24 16:48:36 Done.
626 Element element; 626 Element element;
627 if (constructor != '') { 627 if (constructor != '') {
628 element = classElement.lookupConstructor( 628 element = classElement.lookupConstructor(
629 new Selector.callConstructor(buildSourceString(constructor), 629 new Selector.callConstructor(constructor,
ngeoffray 2013/10/18 10:19:37 One line.
lukas 2013/10/24 16:48:36 Done.
630 classElement.getLibrary())); 630 classElement.getLibrary()));
631 } else { 631 } else {
632 element = classElement.lookupConstructor( 632 element = classElement.lookupConstructor(
633 new Selector.callDefaultConstructor(classElement.getLibrary())); 633 new Selector.callDefaultConstructor(classElement.getLibrary()));
634 } 634 }
635 635
636 FunctionExpression tree = element.parseNode(compiler); 636 FunctionExpression tree = element.parseNode(compiler);
637 ResolverVisitor visitor = 637 ResolverVisitor visitor =
638 new ResolverVisitor(compiler, element, 638 new ResolverVisitor(compiler, element,
639 new CollectingTreeElements(element)); 639 new CollectingTreeElements(element));
640 new InitializerResolver(visitor).resolveInitializers(element, tree); 640 new InitializerResolver(visitor).resolveInitializers(element, tree);
641 visitor.visit(tree.body); 641 visitor.visit(tree.body);
642 Expect.equals(expectedElementCount, map(visitor).length); 642 Expect.equals(expectedElementCount, map(visitor).length);
643 643
644 compareWarningKinds(script, expectedWarnings, compiler.warnings); 644 compareWarningKinds(script, expectedWarnings, compiler.warnings);
645 compareWarningKinds(script, expectedErrors, compiler.errors); 645 compareWarningKinds(script, expectedErrors, compiler.errors);
646 compareWarningKinds(script, expectedInfos, compiler.infos); 646 compareWarningKinds(script, expectedInfos, compiler.infos);
647 } 647 }
648 648
649 testClassHierarchy() { 649 testClassHierarchy() {
650 final MAIN = buildSourceString("main"); 650 final MAIN = "main";
651 MockCompiler compiler = new MockCompiler(); 651 MockCompiler compiler = new MockCompiler();
652 compiler.parseScript("""class A extends B {} 652 compiler.parseScript("""class A extends B {}
653 class B extends A {} 653 class B extends A {}
654 main() { return new A(); }"""); 654 main() { return new A(); }""");
655 FunctionElement mainElement = compiler.mainApp.find(MAIN); 655 FunctionElement mainElement = compiler.mainApp.find(MAIN);
656 compiler.resolver.resolve(mainElement); 656 compiler.resolver.resolve(mainElement);
657 Expect.equals(0, compiler.warnings.length); 657 Expect.equals(0, compiler.warnings.length);
658 Expect.equals(2, compiler.errors.length); 658 Expect.equals(2, compiler.errors.length);
659 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY, 659 Expect.equals(MessageKind.CYCLIC_CLASS_HIERARCHY,
660 compiler.errors[0].message.kind); 660 compiler.errors[0].message.kind);
(...skipping 14 matching lines...) Expand all
675 675
676 compiler = new MockCompiler(); 676 compiler = new MockCompiler();
677 compiler.parseScript("""class A extends B {} 677 compiler.parseScript("""class A extends B {}
678 class B extends C {} 678 class B extends C {}
679 class C {} 679 class C {}
680 main() { return new A(); }"""); 680 main() { return new A(); }""");
681 mainElement = compiler.mainApp.find(MAIN); 681 mainElement = compiler.mainApp.find(MAIN);
682 compiler.resolver.resolve(mainElement); 682 compiler.resolver.resolve(mainElement);
683 Expect.equals(0, compiler.warnings.length); 683 Expect.equals(0, compiler.warnings.length);
684 Expect.equals(0, compiler.errors.length); 684 Expect.equals(0, compiler.errors.length);
685 ClassElement aElement = compiler.mainApp.find(buildSourceString("A")); 685 ClassElement aElement = compiler.mainApp.find("A");
686 Link<DartType> supertypes = aElement.allSupertypes; 686 Link<DartType> supertypes = aElement.allSupertypes;
687 Expect.equals(<String>['B', 'C', 'Object'].toString(), 687 Expect.equals(<String>['B', 'C', 'Object'].toString(),
688 asSortedStrings(supertypes).toString()); 688 asSortedStrings(supertypes).toString());
689 689
690 compiler = new MockCompiler(); 690 compiler = new MockCompiler();
691 compiler.parseScript("""class A<T> {} 691 compiler.parseScript("""class A<T> {}
692 class B<Z,W> extends A<int> implements I<Z,List<W>> {} 692 class B<Z,W> extends A<int> implements I<Z,List<W>> {}
693 class I<X,Y> {} 693 class I<X,Y> {}
694 class C extends B<bool,String> {} 694 class C extends B<bool,String> {}
695 main() { return new C(); }"""); 695 main() { return new C(); }""");
696 mainElement = compiler.mainApp.find(MAIN); 696 mainElement = compiler.mainApp.find(MAIN);
697 compiler.resolver.resolve(mainElement); 697 compiler.resolver.resolve(mainElement);
698 Expect.equals(0, compiler.warnings.length); 698 Expect.equals(0, compiler.warnings.length);
699 Expect.equals(0, compiler.errors.length); 699 Expect.equals(0, compiler.errors.length);
700 aElement = compiler.mainApp.find(buildSourceString("C")); 700 aElement = compiler.mainApp.find("C");
701 supertypes = aElement.allSupertypes; 701 supertypes = aElement.allSupertypes;
702 // Object is once per inheritance path, that is from both A and I. 702 // Object is once per inheritance path, that is from both A and I.
703 Expect.equals(<String>['A<int>', 'B<bool, String>', 'I<bool, List<String>>', 703 Expect.equals(<String>['A<int>', 'B<bool, String>', 'I<bool, List<String>>',
704 'Object'].toString(), 704 'Object'].toString(),
705 asSortedStrings(supertypes).toString()); 705 asSortedStrings(supertypes).toString());
706 706
707 compiler = new MockCompiler(); 707 compiler = new MockCompiler();
708 compiler.parseScript("""class A<T> {} 708 compiler.parseScript("""class A<T> {}
709 class D extends A<E> {} 709 class D extends A<E> {}
710 class E extends D {} 710 class E extends D {}
711 main() { return new E(); }"""); 711 main() { return new E(); }""");
712 mainElement = compiler.mainApp.find(MAIN); 712 mainElement = compiler.mainApp.find(MAIN);
713 compiler.resolver.resolve(mainElement); 713 compiler.resolver.resolve(mainElement);
714 Expect.equals(0, compiler.warnings.length); 714 Expect.equals(0, compiler.warnings.length);
715 Expect.equals(0, compiler.errors.length); 715 Expect.equals(0, compiler.errors.length);
716 aElement = compiler.mainApp.find(buildSourceString("E")); 716 aElement = compiler.mainApp.find("E");
717 supertypes = aElement.allSupertypes; 717 supertypes = aElement.allSupertypes;
718 Expect.equals(<String>['A<E>', 'D', 'Object'].toString(), 718 Expect.equals(<String>['A<E>', 'D', 'Object'].toString(),
719 asSortedStrings(supertypes).toString()); 719 asSortedStrings(supertypes).toString());
720 } 720 }
721 721
722 testInitializers() { 722 testInitializers() {
723 String script; 723 String script;
724 script = """class A { 724 script = """class A {
725 int foo; int bar; 725 int foo; int bar;
726 A() : this.foo = 1, bar = 2; 726 A() : this.foo = 1, bar = 2;
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 ClassElement cls = findElement(compiler, className); 855 ClassElement cls = findElement(compiler, className);
856 Element memberElement = cls.lookupLocalMember(memberName); 856 Element memberElement = cls.lookupLocalMember(memberName);
857 Expect.isNotNull(memberElement); 857 Expect.isNotNull(memberElement);
858 Expect.isNotNull( 858 Expect.isNotNull(
859 compiler.enqueuer.resolution.getCachedElements(memberElement)); 859 compiler.enqueuer.resolution.getCachedElements(memberElement));
860 } 860 }
861 861
862 testToString() { 862 testToString() {
863 final script = r"class C { toString() => 'C'; } main() { '${new C()}'; }"; 863 final script = r"class C { toString() => 'C'; } main() { '${new C()}'; }";
864 asyncTest(() => compileScript(script).then((compiler) { 864 asyncTest(() => compileScript(script).then((compiler) {
865 checkMemberResolved(compiler, 'C', buildSourceString('toString')); 865 checkMemberResolved(compiler, 'C', 'toString');
866 })); 866 }));
867 } 867 }
868 868
869 operatorName(op, isUnary) { 869 operatorName(op, isUnary) {
870 return Elements.constructOperatorName(new SourceString(op), isUnary); 870 return Elements.constructOperatorName(op, isUnary);
871 } 871 }
872 872
873 testIndexedOperator() { 873 testIndexedOperator() {
874 final script = r""" 874 final script = r"""
875 class C { 875 class C {
876 operator[](ix) => ix; 876 operator[](ix) => ix;
877 operator[]=(ix, v) {} 877 operator[]=(ix, v) {}
878 } 878 }
879 main() { var c = new C(); c[0]++; }"""; 879 main() { var c = new C(); c[0]++; }""";
880 asyncTest(() => compileScript(script).then((compiler) { 880 asyncTest(() => compileScript(script).then((compiler) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 }"""; 968 }""";
969 asyncTest(() => compileScript(script2).then((compiler) { 969 asyncTest(() => compileScript(script2).then((compiler) {
970 expect(compiler, 970 expect(compiler,
971 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS], 971 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS],
972 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, 972 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR,
973 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, 973 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR,
974 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD, 974 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD,
975 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD]); 975 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD]);
976 })); 976 }));
977 } 977 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698