| OLD | NEW |
| 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 Loading... |
| 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 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 Expect.equals(ElementKind.FIELD, bElement.kind); | 371 Expect.equals(ElementKind.FIELD, bElement.kind); |
| 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.equals(bNode.type.typeName.source.stringValue, 'var'); | 378 Expect.equals(bNode.type.typeName.source.stringValue, 'var'); |
| 378 } | 379 } |
| 379 | 380 |
| 381 resolveConstructor(String script, String statement, String className, |
| 382 String constructor, int expectedElementCount, |
| 383 [List expectedWarnings = const [], |
| 384 List expectedErrors = const []]) { |
| 385 MockCompiler compiler = new MockCompiler(); |
| 386 compiler.parseScript(script); |
| 387 compiler.resolveStatement(statement); |
| 388 ClassElement classElement = |
| 389 compiler.universe.find(buildSourceString(className)); |
| 390 Element element = |
| 391 classElement.lookupLocalElement(buildSourceString(constructor)); |
| 392 Node tree = element.parseNode(compiler, compiler); |
| 393 ResolverVisitor visitor = new SignatureResolverVisitor(compiler, element); |
| 394 visitor.visit(tree); |
| 395 Expect.equals(expectedElementCount, visitor.mapping.map.length); |
| 396 |
| 397 visitor = new FullResolverVisitor.from(visitor); |
| 398 for (Link<Node> link = tree.initializers.nodes; |
| 399 !link.isEmpty(); |
| 400 link = link.tail) { |
| 401 SendSet init = link.head; |
| 402 Node value = init.arguments.head; |
| 403 visitor.visitIn(value, new StaticScope(visitor.context)); |
| 404 } |
| 405 |
| 406 compareWarningKinds(script, expectedWarnings, compiler.warnings); |
| 407 compareWarningKinds(script, expectedErrors, compiler.errors); |
| 408 } |
| 409 |
| 410 testInitializers() { |
| 411 String script; |
| 412 script = """class A { |
| 413 int foo; int bar; |
| 414 A() : this.foo = 1, bar = 2; |
| 415 }"""; |
| 416 resolveConstructor(script, "A a = new A();", "A", "A", 3); |
| 417 |
| 418 script = """class A { |
| 419 int foo; A a; |
| 420 A() : a.foo = 1; |
| 421 }"""; |
| 422 resolveConstructor(script, "A a = new A();", "A", "A", 2, |
| 423 [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]); |
| 424 |
| 425 script = """class A { |
| 426 int foo; |
| 427 A() : this.foo = 1, this.foo = 2; |
| 428 }"""; |
| 429 resolveConstructor(script, "A a = new A();", "A", "A", 3, |
| 430 [MessageKind.DUPLICATION_INITIALIZATION]); |
| 431 |
| 432 script = """class A { |
| 433 A() : this.foo = 1; |
| 434 }"""; |
| 435 resolveConstructor(script, "A a = new A();", "A", "A", 1, |
| 436 [], [MessageKind.CANNOT_RESOLVE]); |
| 437 |
| 438 script = """class A { |
| 439 int foo; |
| 440 int bar; |
| 441 A() : this.foo = bar; |
| 442 }"""; |
| 443 resolveConstructor(script, "A a = new A();", "A", "A", 2, |
| 444 [], [MessageKind.CANNOT_RESOLVE]); |
| 445 } |
| 446 |
| 380 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; | 447 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; |
| 381 | 448 |
| 382 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); | 449 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); |
| OLD | NEW |