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

Side by Side Diff: frog/tests/leg/src/ResolverTest.dart

Issue 9243011: Implement named constructors and resolving of redirecting constructors and super-initializers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 11 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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("../../../leg/leg.dart"); 5 #import("../../../leg/leg.dart");
6 #import("../../../leg/elements/elements.dart"); 6 #import("../../../leg/elements/elements.dart");
7 #import("../../../leg/tree/tree.dart"); 7 #import("../../../leg/tree/tree.dart");
8 #import("../../../leg/util/util.dart"); 8 #import("../../../leg/util/util.dart");
9 #import("mock_compiler.dart"); 9 #import("mock_compiler.dart");
10 #import("parser_helper.dart"); 10 #import("parser_helper.dart");
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 visitor.visit(function.body); 106 visitor.visit(function.body);
107 Map mapping = visitor.mapping.map; 107 Map mapping = visitor.mapping.map;
108 List<Element> values = mapping.getValues(); 108 List<Element> values = mapping.getValues();
109 Expect.equals(0, mapping.length); 109 Expect.equals(0, mapping.length);
110 Expect.equals(0, compiler.warnings.length); 110 Expect.equals(0, compiler.warnings.length);
111 111
112 compiler = new MockCompiler(); 112 compiler = new MockCompiler();
113 compiler.resolveStatement("main() { return this; }"); 113 compiler.resolveStatement("main() { return this; }");
114 Expect.equals(0, compiler.warnings.length); 114 Expect.equals(0, compiler.warnings.length);
115 Expect.equals(1, compiler.errors.length); 115 Expect.equals(1, compiler.errors.length);
116 Expect.equals(MessageKind.NO_THIS_IN_STATIC, 116 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE,
117 compiler.errors[0].message.kind); 117 compiler.errors[0].message.kind);
118 118
119 compiler = new MockCompiler(); 119 compiler = new MockCompiler();
120 universe = compiler.universe; 120 universe = compiler.universe;
121 compiler.parseScript("class Foo { static foo() { return this; } }"); 121 compiler.parseScript("class Foo { static foo() { return this; } }");
122 compiler.resolveStatement("Foo foo;"); 122 compiler.resolveStatement("Foo foo;");
123 fooElement = universe.find(buildSourceString("Foo")); 123 fooElement = universe.find(buildSourceString("Foo"));
124 funElement = 124 funElement =
125 fooElement.lookupLocalMember(buildSourceString("foo")); 125 fooElement.lookupLocalMember(buildSourceString("foo"));
126 visitor = new FullResolverVisitor(compiler, funElement); 126 visitor = new FullResolverVisitor(compiler, funElement);
127 function = funElement.parseNode(compiler, compiler); 127 function = funElement.parseNode(compiler, compiler);
128 visitor.visit(function.body); 128 visitor.visit(function.body);
129 Expect.equals(0, compiler.warnings.length); 129 Expect.equals(0, compiler.warnings.length);
130 Expect.equals(1, compiler.errors.length); 130 Expect.equals(1, compiler.errors.length);
131 Expect.equals(MessageKind.NO_THIS_IN_STATIC, 131 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE,
132 compiler.errors[0].message.kind); 132 compiler.errors[0].message.kind);
133 } 133 }
134 134
135 testLocalsOne() { 135 testLocalsOne() {
136 testLocals([["foo", false]]); 136 testLocals([["foo", false]]);
137 testLocals([["foo", false], ["bar", false]]); 137 testLocals([["foo", false], ["bar", false]]);
138 testLocals([["foo", false], ["bar", false], ["foobar", false]]); 138 testLocals([["foo", false], ["bar", false], ["foobar", false]]);
139 139
140 testLocals([["foo", true]]); 140 testLocals([["foo", true]]);
141 testLocals([["foo", false], ["bar", true]]); 141 testLocals([["foo", false], ["bar", true]]);
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 Expect.equals(ElementKind.GENERATIVE_CONSTRUCTOR, element.kind); 422 Expect.equals(ElementKind.GENERATIVE_CONSTRUCTOR, element.kind);
423 Expect.isTrue(element is SynthesizedConstructorElement); 423 Expect.isTrue(element is SynthesizedConstructorElement);
424 } 424 }
425 425
426 testTopLevelFields() { 426 testTopLevelFields() {
427 MockCompiler compiler = new MockCompiler(); 427 MockCompiler compiler = new MockCompiler();
428 compiler.parseScript("int a;"); 428 compiler.parseScript("int a;");
429 VariableElement element = compiler.universe.find(buildSourceString("a")); 429 VariableElement element = compiler.universe.find(buildSourceString("a"));
430 Expect.equals(ElementKind.FIELD, element.kind); 430 Expect.equals(ElementKind.FIELD, element.kind);
431 VariableDefinitions node = element.variables.parseNode(compiler, compiler); 431 VariableDefinitions node = element.variables.parseNode(compiler, compiler);
432 Expect.equals(node.type.typeName.asIdentifier().source.stringValue, 'int'); 432 Identifier typeName = node.type.typeName;
433 Expect.equals(typeName.source.stringValue, 'int');
433 434
434 compiler.parseScript("var b, c;"); 435 compiler.parseScript("var b, c;");
435 VariableElement bElement = compiler.universe.find(buildSourceString("b")); 436 VariableElement bElement = compiler.universe.find(buildSourceString("b"));
436 VariableElement cElement = compiler.universe.find(buildSourceString("c")); 437 VariableElement cElement = compiler.universe.find(buildSourceString("c"));
437 Expect.equals(ElementKind.FIELD, bElement.kind); 438 Expect.equals(ElementKind.FIELD, bElement.kind);
438 Expect.equals(ElementKind.FIELD, cElement.kind); 439 Expect.equals(ElementKind.FIELD, cElement.kind);
439 Expect.isTrue(bElement != cElement); 440 Expect.isTrue(bElement != cElement);
440 441
441 VariableDefinitions bNode = bElement.variables.parseNode(compiler, compiler); 442 VariableDefinitions bNode = bElement.variables.parseNode(compiler, compiler);
442 VariableDefinitions cNode = cElement.variables.parseNode(compiler, compiler); 443 VariableDefinitions cNode = cElement.variables.parseNode(compiler, compiler);
443 Expect.equals(bNode, cNode); 444 Expect.equals(bNode, cNode);
444 Expect.isNull(bNode.type); 445 Expect.isNull(bNode.type);
445 Expect.isTrue(bNode.modifiers.isVar()); 446 Expect.isTrue(bNode.modifiers.isVar());
446 } 447 }
447 448
448 resolveConstructor(String script, String statement, String className, 449 resolveConstructor(String script, String statement, String className,
449 String constructor, int expectedElementCount, 450 String constructor, int expectedElementCount,
450 [List expectedWarnings = const [], 451 [List expectedWarnings = const [],
451 List expectedErrors = const []]) { 452 List expectedErrors = const []]) {
452 MockCompiler compiler = new MockCompiler(); 453 MockCompiler compiler = new MockCompiler();
453 compiler.parseScript(script); 454 compiler.parseScript(script);
454 compiler.resolveStatement(statement); 455 compiler.resolveStatement(statement);
455 ClassElement classElement = 456 ClassElement classElement =
456 compiler.universe.find(buildSourceString(className)); 457 compiler.universe.find(buildSourceString(className));
457 Element element = 458 Element element =
458 classElement.lookupConstructor(buildSourceString(constructor)); 459 classElement.lookupConstructor(buildSourceString(constructor));
459 FunctionExpression tree = element.parseNode(compiler, compiler); 460 FunctionExpression tree = element.parseNode(compiler, compiler);
460 ResolverVisitor visitor = new FullResolverVisitor(compiler, element); 461 ResolverVisitor visitor = new FullResolverVisitor(compiler, element);
461 compiler.resolver.resolveInitializers(element, tree, visitor); 462 new InitializerResolver(visitor, element).resolveInitializers(tree);
463 visitor.visit(tree.body);
462 Expect.equals(expectedElementCount, visitor.mapping.map.length); 464 Expect.equals(expectedElementCount, visitor.mapping.map.length);
463 465
464 compareWarningKinds(script, expectedWarnings, compiler.warnings); 466 compareWarningKinds(script, expectedWarnings, compiler.warnings);
465 compareWarningKinds(script, expectedErrors, compiler.errors); 467 compareWarningKinds(script, expectedErrors, compiler.errors);
466 } 468 }
467 469
468 testInitializers() { 470 testInitializers() {
469 String script; 471 String script;
470 script = """class A { 472 script = """class A {
471 int foo; int bar; 473 int foo; int bar;
472 A() : this.foo = 1, bar = 2; 474 A() : this.foo = 1, bar = 2;
473 }"""; 475 }""";
474 resolveConstructor(script, "A a = new A();", "A", "A", 2); 476 resolveConstructor(script, "A a = new A();", "A", "A", 2);
475 477
476 script = """class A { 478 script = """class A {
477 int foo; A a; 479 int foo; A a;
478 A() : a.foo = 1; 480 A() : a.foo = 1;
479 }"""; 481 }""";
480 resolveConstructor(script, "A a = new A();", "A", "A", 1, 482 resolveConstructor(script, "A a = new A();", "A", "A", 0,
481 [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]); 483 [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]);
482 484
483 script = """class A { 485 script = """class A {
484 int foo; 486 int foo;
485 A() : this.foo = 1, this.foo = 2; 487 A() : this.foo = 1, this.foo = 2;
486 }"""; 488 }""";
487 resolveConstructor(script, "A a = new A();", "A", "A", 2, 489 resolveConstructor(script, "A a = new A();", "A", "A", 2,
488 [MessageKind.ALREADY_INITIALIZED], 490 [MessageKind.ALREADY_INITIALIZED],
489 [MessageKind.DUPLICATE_INITIALIZER]); 491 [MessageKind.DUPLICATE_INITIALIZER]);
490 492
491 script = """class A { 493 script = """class A {
492 A() : this.foo = 1; 494 A() : this.foo = 1;
493 }"""; 495 }""";
494 resolveConstructor(script, "A a = new A();", "A", "A", 0, 496 resolveConstructor(script, "A a = new A();", "A", "A", 0,
495 [], [MessageKind.CANNOT_RESOLVE]); 497 [], [MessageKind.CANNOT_RESOLVE]);
496 498
497 script = """class A { 499 script = """class A {
498 int foo; 500 int foo;
499 int bar; 501 int bar;
500 A() : this.foo = bar; 502 A() : this.foo = bar;
501 }"""; 503 }""";
502 resolveConstructor(script, "A a = new A();", "A", "A", 2, 504 resolveConstructor(script, "A a = new A();", "A", "A", 2,
503 [], [MessageKind.NOT_STATIC]); 505 [], [MessageKind.NO_INSTANCE_AVAILABLE]);
506
507 script = """class A {
508 int foo() => 42;
509 A() : foo();
510 }""";
511 resolveConstructor(script, "A a = new A();", "A", "A", 1,
512 [], [MessageKind.CONSTRUCTOR_CALL_EXPECTED]);
513
514 script = """class A {
515 int i;
516 A.a() : this.b(0);
517 A.b(int this.i);
518 }""";
519 resolveConstructor(script, "A a = new A.a();", "A", "A.a", 1,
520 [], []);
521
522 script = """class A {
523 int i;
524 A.a() : i = 42, this(0);
525 A(int this.i);
526 }""";
527 resolveConstructor(script, "A a = new A.a();", "A", "A.a", 2,
528 [], [MessageKind.REDIRECTING_CTOR_HAS_INITIALIZER]);
529
530 script = """class A {
531 int i;
532 A(this.i);
533 }
534 class B extends A {
535 B() : super(0);
536 }""";
537 resolveConstructor(script, "B a = new B();", "B", "B", 1,
538 [], []);
539
540 script = """class A {
541 int i;
542 A(this.i);
543 }
544 class B extends A {
545 B() : super(0), super(1);
546 }""";
547 resolveConstructor(script, "B b = new B();", "B", "B", 2,
548 [], [MessageKind.DUPLICATE_SUPER_INITIALIZER]);
ngeoffray 2012/01/19 08:56:12 Could you also add tests for class Object?
karlklose 2012/01/19 13:51:24 Done.
504 } 549 }
505 550
506 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; 551 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1;
507 552
508 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); 553 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698