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

Side by Side 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: Don't call function that does not exist. 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 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 testParametersOne(); 57 testParametersOne();
58 testFor(); 58 testFor();
59 testTypeAnnotation(); 59 testTypeAnnotation();
60 testSuperclass(); 60 testSuperclass();
61 // testVarSuperclass(); // The parser crashes with 'class Foo extends var'. 61 // testVarSuperclass(); // The parser crashes with 'class Foo extends var'.
62 // testOneInterface(); // The parser does not handle interfaces. 62 // testOneInterface(); // The parser does not handle interfaces.
63 // testTwoInterfaces(); // The parser does not handle interfaces. 63 // testTwoInterfaces(); // The parser does not handle interfaces.
64 testFunctionExpression(); 64 testFunctionExpression();
65 testNewExpression(); 65 testNewExpression();
66 testTopLevelFields(); 66 testTopLevelFields();
67 testInitializers();
67 } 68 }
68 69
69 testLocalsOne() { 70 testLocalsOne() {
70 testLocals([["foo", false]]); 71 testLocals([["foo", false]]);
71 testLocals([["foo", false], ["bar", false]]); 72 testLocals([["foo", false], ["bar", false]]);
72 testLocals([["foo", false], ["bar", false], ["foobar", false]]); 73 testLocals([["foo", false], ["bar", false], ["foobar", false]]);
73 74
74 testLocals([["foo", true]]); 75 testLocals([["foo", true]]);
75 testLocals([["foo", false], ["bar", true]]); 76 testLocals([["foo", false], ["bar", true]]);
76 testLocals([["foo", true], ["bar", true]]); 77 testLocals([["foo", true], ["bar", true]]);
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 Expect.equals(ElementKind.FIELD, cElement.kind); 372 Expect.equals(ElementKind.FIELD, cElement.kind);
372 Expect.isTrue(bElement != cElement); 373 Expect.isTrue(bElement != cElement);
373 374
374 VariableDefinitions bNode = bElement.parseNode(compiler, compiler); 375 VariableDefinitions bNode = bElement.parseNode(compiler, compiler);
375 VariableDefinitions cNode = cElement.parseNode(compiler, compiler); 376 VariableDefinitions cNode = cElement.parseNode(compiler, compiler);
376 Expect.equals(bNode, cNode); 377 Expect.equals(bNode, cNode);
377 Expect.isNull(bNode.type); 378 Expect.isNull(bNode.type);
378 Expect.isTrue(bNode.modifiers.isVar()); 379 Expect.isTrue(bNode.modifiers.isVar());
379 } 380 }
380 381
382 resolveConstructor(String script, String statement, String className,
383 String constructor, int expectedElementCount,
384 [List expectedWarnings = const [],
385 List expectedErrors = const []]) {
386 MockCompiler compiler = new MockCompiler();
387 compiler.parseScript(script);
388 compiler.resolveStatement(statement);
389 ClassElement classElement =
390 compiler.universe.find(buildSourceString(className));
391 Element element =
392 classElement.lookupLocalElement(buildSourceString(constructor));
393 FunctionExpression tree = element.parseNode(compiler, compiler);
394 ResolverVisitor visitor = new SignatureResolverVisitor(compiler, element);
395 visitor.visit(tree);
396 Expect.equals(expectedElementCount, visitor.mapping.map.length);
397
398 visitor = new FullResolverVisitor.from(visitor);
399 for (Link<Node> link = tree.initializers.nodes;
400 !link.isEmpty();
401 link = link.tail) {
402 if (link.head.asSendSet() != null) {
403 SendSet init = link.head;
404 Node value = init.arguments.head;
405 visitor.visitInStaticContext(value);
406 } else {
407 // TODO(karlklose): super(...), this(...).
408 Expect.fail('SendSet expected');
409 }
410 }
411
412 compareWarningKinds(script, expectedWarnings, compiler.warnings);
413 compareWarningKinds(script, expectedErrors, compiler.errors);
414 }
415
416 testInitializers() {
417 String script;
418 script = """class A {
419 int foo; int bar;
420 A() : this.foo = 1, bar = 2;
421 }""";
422 resolveConstructor(script, "A a = new A();", "A", "A", 3);
423
424 script = """class A {
425 int foo; A a;
426 A() : a.foo = 1;
427 }""";
428 resolveConstructor(script, "A a = new A();", "A", "A", 2,
429 [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]);
430
431 script = """class A {
432 int foo;
433 A() : this.foo = 1, this.foo = 2;
434 }""";
435 resolveConstructor(script, "A a = new A();", "A", "A", 3,
436 [MessageKind.DUPLICATE_INITIALIZER]);
ahe 2011/12/21 12:01:58 I expected two diagnostics here: foo.dart:line:co
karlklose 2011/12/21 16:33:46 Done.
437
438 script = """class A {
439 A() : this.foo = 1;
440 }""";
441 resolveConstructor(script, "A a = new A();", "A", "A", 1,
442 [], [MessageKind.CANNOT_RESOLVE]);
443
444 script = """class A {
445 int foo;
446 int bar;
447 A() : this.foo = bar;
448 }""";
449 resolveConstructor(script, "A a = new A();", "A", "A", 2,
450 [], [MessageKind.CANNOT_RESOLVE]);
451 }
452
381 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; 453 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1;
382 454
383 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); 455 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698