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

Unified Diff: frog/tests/leg/src/ResolverTest.dart

Issue 8974014: Resolve initializers in constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add tests. Created 9 years 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 side-by-side diff with in-line comments
Download patch
Index: frog/tests/leg/src/ResolverTest.dart
diff --git a/frog/tests/leg/src/ResolverTest.dart b/frog/tests/leg/src/ResolverTest.dart
index be28905929a053488f3ab105b348f3482daca385..4a30933291b71eb087383f9a714b0023e25d9fe0 100644
--- a/frog/tests/leg/src/ResolverTest.dart
+++ b/frog/tests/leg/src/ResolverTest.dart
@@ -64,6 +64,7 @@ main() {
testFunctionExpression();
testNewExpression();
testTopLevelFields();
+ testInitializers();
}
testLocalsOne() {
@@ -377,6 +378,72 @@ testTopLevelFields() {
Expect.equals(bNode.type.typeName.source.stringValue, 'var');
}
+resolveConstructor(String script, String statement, String className,
+ String constructor, int expectedElementCount,
+ [List expectedWarnings = const [],
+ List expectedErrors = const []]) {
+ MockCompiler compiler = new MockCompiler();
+ compiler.parseScript(script);
+ compiler.resolveStatement(statement);
+ ClassElement classElement =
+ compiler.universe.find(buildSourceString(className));
+ Element element =
+ classElement.lookupLocalElement(buildSourceString(constructor));
+ Node tree = element.parseNode(compiler, compiler);
+ ResolverVisitor visitor = new SignatureResolverVisitor(compiler, element);
+ visitor.visit(tree);
+ Expect.equals(expectedElementCount, visitor.mapping.map.length);
+
+ visitor = new FullResolverVisitor.from(visitor);
+ for (Link<Node> link = tree.initializers.nodes;
+ !link.isEmpty();
+ link = link.tail) {
+ SendSet init = link.head;
+ Node value = init.arguments.head;
+ visitor.visitIn(value, new StaticScope(visitor.context));
+ }
+
+ compareWarningKinds(script, expectedWarnings, compiler.warnings);
+ compareWarningKinds(script, expectedErrors, compiler.errors);
+}
+
+testInitializers() {
+ String script;
+ script = """class A {
+ int foo; int bar;
+ A() : this.foo = 1, bar = 2;
+ }""";
+ resolveConstructor(script, "A a = new A();", "A", "A", 3);
+
+ script = """class A {
+ int foo; A a;
+ A() : a.foo = 1;
+ }""";
+ resolveConstructor(script, "A a = new A();", "A", "A", 2,
+ [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]);
+
+ script = """class A {
+ int foo;
+ A() : this.foo = 1, this.foo = 2;
+ }""";
+ resolveConstructor(script, "A a = new A();", "A", "A", 3,
+ [MessageKind.DUPLICATION_INITIALIZATION]);
+
+ script = """class A {
+ A() : this.foo = 1;
+ }""";
+ resolveConstructor(script, "A a = new A();", "A", "A", 1,
+ [], [MessageKind.CANNOT_RESOLVE]);
+
+ script = """class A {
+ int foo;
+ int bar;
+ A() : this.foo = bar;
+ }""";
+ resolveConstructor(script, "A a = new A();", "A", "A", 2,
+ [], [MessageKind.CANNOT_RESOLVE]);
+}
+
length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1;
at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1);

Powered by Google App Engine
This is Rietveld 408576698