Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "package:compiler/implementation/resolution/resolution.dart"; | 10 import "package:compiler/implementation/resolution/resolution.dart"; |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 } | 408 } |
| 409 | 409 |
| 410 Future testFor() { | 410 Future testFor() { |
| 411 return MockCompiler.create((MockCompiler compiler) { | 411 return MockCompiler.create((MockCompiler compiler) { |
| 412 ResolverVisitor visitor = compiler.resolverVisitor(); | 412 ResolverVisitor visitor = compiler.resolverVisitor(); |
| 413 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); | 413 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); |
| 414 visitor.visit(tree); | 414 visitor.visit(tree); |
| 415 | 415 |
| 416 MethodScope scope = visitor.scope; | 416 MethodScope scope = visitor.scope; |
| 417 Expect.equals(0, scope.elements.length); | 417 Expect.equals(0, scope.elements.length); |
| 418 Expect.equals(10, map(visitor).length); | 418 Expect.equals(9, map(visitor).length); |
|
Johnni Winther
2014/07/25 10:22:20
We don't store the element for the type anymore.
| |
| 419 | 419 |
| 420 VariableDefinitions initializer = tree.initializer; | 420 VariableDefinitions initializer = tree.initializer; |
| 421 Node iNode = initializer.definitions.nodes.head; | 421 Node iNode = initializer.definitions.nodes.head; |
| 422 Element iElement = visitor.registry.mapping[iNode]; | 422 Element iElement = visitor.registry.mapping[iNode]; |
| 423 | 423 |
| 424 // Check that we have the expected nodes. This test relies on the mapping | 424 // Check that we have the expected nodes. This test relies on the mapping |
| 425 // field to be a linked hash map (preserving insertion order). | 425 // field to be a linked hash map (preserving insertion order). |
| 426 Expect.isTrue(map(visitor) is LinkedHashMap); | 426 Expect.isTrue(map(visitor) is LinkedHashMap); |
| 427 List<Node> nodes = map(visitor).keys.toList(); | 427 List<Node> nodes = map(visitor).keys.toList(); |
| 428 List<Element> elements = map(visitor).values.toList(); | 428 List<Element> elements = map(visitor).values.toList(); |
| 429 | 429 |
| 430 | |
| 431 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | |
| 432 // ^^^ | |
| 433 Expect.isTrue(nodes[0] is TypeAnnotation); | |
| 434 | |
| 435 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 430 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 436 // ^^^^^ | 431 // ^^^^^ |
| 437 checkSendSet(iElement, nodes[1], elements[1]); | 432 checkSendSet(iElement, nodes[0], elements[0]); |
| 438 | 433 |
| 439 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 434 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 440 // ^ | 435 // ^ |
| 441 checkIdentifier(iElement, nodes[2], elements[2]); | 436 checkIdentifier(iElement, nodes[1], elements[1]); |
| 442 | 437 |
| 443 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 438 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 444 // ^ | 439 // ^ |
| 445 checkSend(iElement, nodes[3], elements[3]); | 440 checkSend(iElement, nodes[2], elements[2]); |
| 446 | 441 |
| 447 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 442 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 448 // ^ | 443 // ^ |
| 444 checkIdentifier(iElement, nodes[3], elements[3]); | |
| 445 | |
| 446 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | |
| 447 // ^ | |
| 449 checkIdentifier(iElement, nodes[4], elements[4]); | 448 checkIdentifier(iElement, nodes[4], elements[4]); |
| 450 | 449 |
| 451 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 450 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 452 // ^ | 451 // ^ |
| 453 checkIdentifier(iElement, nodes[5], elements[5]); | 452 checkSend(iElement, nodes[5], elements[5]); |
| 454 | |
| 455 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | |
| 456 // ^ | |
| 457 checkSend(iElement, nodes[6], elements[6]); | |
| 458 | 453 |
| 459 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 454 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 460 // ^^^^^^^^^ | 455 // ^^^^^^^^^ |
| 461 checkSendSet(iElement, nodes[7], elements[7]); | 456 checkSendSet(iElement, nodes[6], elements[6]); |
| 462 | 457 |
| 463 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 458 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 464 // ^ | 459 // ^ |
| 465 checkIdentifier(iElement, nodes[8], elements[8]); | 460 checkIdentifier(iElement, nodes[7], elements[7]); |
| 466 | 461 |
| 467 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; | 462 // for (int i = 0; i < 10; i = i + 1) { i = 5; }; |
| 468 // ^^^^^ | 463 // ^^^^^ |
| 469 checkSendSet(iElement, nodes[9], elements[9]); | 464 checkSendSet(iElement, nodes[8], elements[8]); |
| 470 }); | 465 }); |
| 471 } | 466 } |
| 472 | 467 |
| 473 checkIdentifier(Element expected, Node node, Element actual) { | 468 checkIdentifier(Element expected, Node node, Element actual) { |
| 474 Expect.isTrue(node is Identifier, node.toDebugString()); | 469 Expect.isTrue(node is Identifier, node.toDebugString()); |
| 475 Expect.equals(expected, actual); | 470 Expect.equals(expected, actual); |
| 476 } | 471 } |
| 477 | 472 |
| 478 checkSend(Element expected, Node node, Element actual) { | 473 checkSend(Element expected, Node node, Element actual) { |
| 479 Expect.isTrue(node is Send, node.toDebugString()); | 474 Expect.isTrue(node is Send, node.toDebugString()); |
| 480 Expect.isTrue(node is !SendSet, node.toDebugString()); | 475 Expect.isTrue(node is !SendSet, node.toDebugString()); |
| 481 Expect.equals(expected, actual); | 476 Expect.equals(expected, actual); |
| 482 } | 477 } |
| 483 | 478 |
| 484 checkSendSet(Element expected, Node node, Element actual) { | 479 checkSendSet(Element expected, Node node, Element actual) { |
| 485 Expect.isTrue(node is SendSet, node.toDebugString()); | 480 Expect.isTrue(node is SendSet, node.toDebugString()); |
| 486 Expect.equals(expected, actual); | 481 Expect.equals(expected, actual); |
| 487 } | 482 } |
| 488 | 483 |
| 489 Future testTypeAnnotation() { | 484 Future testTypeAnnotation() { |
| 490 return MockCompiler.create((MockCompiler compiler) { | 485 return MockCompiler.create((MockCompiler compiler) { |
| 491 String statement = "Foo bar;"; | 486 String statement = "Foo bar;"; |
| 492 | 487 |
| 493 // Test that we get a warning when Foo is not defined. | 488 // Test that we get a warning when Foo is not defined. |
| 494 Map mapping = compiler.resolveStatement(statement).map; | 489 Map mapping = compiler.resolveStatement(statement).map; |
| 495 | 490 |
| 496 Expect.equals(2, mapping.length); // Both Foo and bar have an element. | 491 Expect.equals(1, mapping.length); // Only [bar] has an element. |
| 497 Expect.equals(1, compiler.warnings.length); | 492 Expect.equals(1, compiler.warnings.length); |
| 498 | 493 |
| 499 Node warningNode = compiler.warnings[0].node; | 494 Node warningNode = compiler.warnings[0].node; |
| 500 | 495 |
| 501 Expect.equals( | 496 Expect.equals( |
| 502 new Message( | 497 new Message( |
| 503 MessageKind.CANNOT_RESOLVE_TYPE, {'typeName': 'Foo'}, false), | 498 MessageKind.CANNOT_RESOLVE_TYPE, {'typeName': 'Foo'}, false), |
| 504 compiler.warnings[0].message); | 499 compiler.warnings[0].message); |
| 505 VariableDefinitions definition = compiler.parsedTree; | 500 VariableDefinitions definition = compiler.parsedTree; |
| 506 Expect.equals(warningNode, definition.type); | 501 Expect.equals(warningNode, definition.type); |
| 507 compiler.clearMessages(); | 502 compiler.clearMessages(); |
| 508 | 503 |
| 509 // Test that there is no warning after defining Foo. | 504 // Test that there is no warning after defining Foo. |
| 510 compiler.parseScript("class Foo {}"); | 505 compiler.parseScript("class Foo {}"); |
| 511 mapping = compiler.resolveStatement(statement).map; | 506 mapping = compiler.resolveStatement(statement).map; |
| 512 Expect.equals(2, mapping.length); | 507 Expect.equals(1, mapping.length); |
| 513 Expect.equals(0, compiler.warnings.length); | 508 Expect.equals(0, compiler.warnings.length); |
| 514 | 509 |
| 515 // Test that 'var' does not create a warning. | 510 // Test that 'var' does not create a warning. |
| 516 mapping = compiler.resolveStatement("var foo;").map; | 511 mapping = compiler.resolveStatement("var foo;").map; |
| 517 Expect.equals(1, mapping.length); | 512 Expect.equals(1, mapping.length); |
| 518 Expect.equals(0, compiler.warnings.length); | 513 Expect.equals(0, compiler.warnings.length); |
| 519 }); | 514 }); |
| 520 } | 515 } |
| 521 | 516 |
| 522 Future testSuperclass() { | 517 Future testSuperclass() { |
| 523 return Future.wait([ | 518 return Future.wait([ |
| 524 MockCompiler.create((MockCompiler compiler) { | 519 MockCompiler.create((MockCompiler compiler) { |
| 525 compiler.parseScript("class Foo extends Bar {}"); | 520 compiler.parseScript("class Foo extends Bar {}"); |
| 526 compiler.resolveStatement("Foo bar;"); | 521 compiler.resolveStatement("Foo bar;"); |
| 527 Expect.equals(1, compiler.errors.length); | 522 Expect.equals(1, compiler.errors.length); |
| 528 var cannotResolveBar = new Message(MessageKind.CANNOT_EXTEND_MALFORMED, | 523 var cannotResolveBar = new Message(MessageKind.CANNOT_EXTEND_MALFORMED, |
| 529 {'className': 'Foo', 'malformedType': 'Bar'}, false); | 524 {'className': 'Foo', 'malformedType': 'Bar'}, false); |
| 530 Expect.equals(cannotResolveBar, compiler.errors[0].message); | 525 Expect.equals(cannotResolveBar, compiler.errors[0].message); |
| 531 compiler.clearMessages(); | 526 compiler.clearMessages(); |
| 532 }), | 527 }), |
| 533 MockCompiler.create((MockCompiler compiler) { | 528 MockCompiler.create((MockCompiler compiler) { |
| 534 compiler.parseScript("class Foo extends Bar {}"); | 529 compiler.parseScript("class Foo extends Bar {}"); |
| 535 compiler.parseScript("class Bar {}"); | 530 compiler.parseScript("class Bar {}"); |
| 536 Map mapping = compiler.resolveStatement("Foo bar;").map; | 531 Map mapping = compiler.resolveStatement("Foo bar;").map; |
| 537 Expect.equals(2, mapping.length); | 532 Expect.equals(1, mapping.length); |
| 538 | 533 |
| 539 ClassElement fooElement = compiler.mainApp.find('Foo'); | 534 ClassElement fooElement = compiler.mainApp.find('Foo'); |
| 540 ClassElement barElement = compiler.mainApp.find('Bar'); | 535 ClassElement barElement = compiler.mainApp.find('Bar'); |
| 541 Expect.equals(barElement.computeType(compiler), | 536 Expect.equals(barElement.computeType(compiler), |
| 542 fooElement.supertype); | 537 fooElement.supertype); |
| 543 Expect.isTrue(fooElement.interfaces.isEmpty); | 538 Expect.isTrue(fooElement.interfaces.isEmpty); |
| 544 Expect.isTrue(barElement.interfaces.isEmpty); | 539 Expect.isTrue(barElement.interfaces.isEmpty); |
| 545 }), | 540 }), |
| 546 ]); | 541 ]); |
| 547 } | 542 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 607 Expect.equals(2, length(c.interfaces)); | 602 Expect.equals(2, length(c.interfaces)); |
| 608 Expect.equals(i1.computeType(compiler), at(c.interfaces, 0)); | 603 Expect.equals(i1.computeType(compiler), at(c.interfaces, 0)); |
| 609 Expect.equals(i2.computeType(compiler), at(c.interfaces, 1)); | 604 Expect.equals(i2.computeType(compiler), at(c.interfaces, 1)); |
| 610 }); | 605 }); |
| 611 } | 606 } |
| 612 | 607 |
| 613 Future testFunctionExpression() { | 608 Future testFunctionExpression() { |
| 614 return MockCompiler.create((MockCompiler compiler) { | 609 return MockCompiler.create((MockCompiler compiler) { |
| 615 ResolverVisitor visitor = compiler.resolverVisitor(); | 610 ResolverVisitor visitor = compiler.resolverVisitor(); |
| 616 Map mapping = compiler.resolveStatement("int f() {}").map; | 611 Map mapping = compiler.resolveStatement("int f() {}").map; |
| 617 Expect.equals(3, mapping.length); | 612 Expect.equals(1, mapping.length); |
|
Johnni Winther
2014/07/25 10:22:20
We don't store the element for the type and the el
| |
| 618 Element element; | 613 Element element; |
| 619 Node node; | 614 Node node; |
| 620 mapping.forEach((Node n, Element e) { | 615 mapping.forEach((Node n, Element e) { |
| 621 if (n is FunctionExpression) { | 616 if (n is FunctionExpression) { |
| 622 element = e; | 617 element = e; |
| 623 node = n; | 618 node = n; |
| 624 } | 619 } |
| 625 }); | 620 }); |
| 626 Expect.equals(ElementKind.FUNCTION, element.kind); | 621 Expect.equals(ElementKind.FUNCTION, element.kind); |
| 627 Expect.equals('f', element.name); | 622 Expect.equals('f', element.name); |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1069 }"""; | 1064 }"""; |
| 1070 asyncTest(() => compileScript(script2).then((compiler) { | 1065 asyncTest(() => compileScript(script2).then((compiler) { |
| 1071 expect(compiler, | 1066 expect(compiler, |
| 1072 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS], | 1067 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS], |
| 1073 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, | 1068 [MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, |
| 1074 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, | 1069 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_CONSTRUCTOR, |
| 1075 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD, | 1070 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD, |
| 1076 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD]); | 1071 MessageKind.CONST_CONSTRUCTOR_WITH_NONFINAL_FIELDS_FIELD]); |
| 1077 })); | 1072 })); |
| 1078 } | 1073 } |
| OLD | NEW |