OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library analyzer.test.generated.simple_resolver_test; | 5 library analyzer.test.generated.simple_resolver_test; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/resolution_accessors.dart'; |
8 import 'package:analyzer/dart/ast/visitor.dart'; | 9 import 'package:analyzer/dart/ast/visitor.dart'; |
9 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 11 import 'package:analyzer/dart/element/type.dart'; |
11 import 'package:analyzer/exception/exception.dart'; | 12 import 'package:analyzer/exception/exception.dart'; |
12 import 'package:analyzer/src/error/codes.dart'; | 13 import 'package:analyzer/src/error/codes.dart'; |
13 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
14 import 'package:analyzer/src/generated/source_io.dart'; | 15 import 'package:analyzer/src/generated/source_io.dart'; |
15 import 'package:test/test.dart'; | 16 import 'package:test/test.dart'; |
16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 17 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
17 | 18 |
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 assertNoErrors(source); | 742 assertNoErrors(source); |
742 verify([source]); | 743 verify([source]); |
743 // Verify that both the getter and setter for "x" in C.f() refer to the | 744 // Verify that both the getter and setter for "x" in C.f() refer to the |
744 // accessors defined in M2. | 745 // accessors defined in M2. |
745 ClassElement classC = library.definingCompilationUnit.types[3]; | 746 ClassElement classC = library.definingCompilationUnit.types[3]; |
746 MethodDeclaration f = classC.getMethod('f').computeNode(); | 747 MethodDeclaration f = classC.getMethod('f').computeNode(); |
747 BlockFunctionBody body = f.body; | 748 BlockFunctionBody body = f.body; |
748 ExpressionStatement stmt = body.block.statements[0]; | 749 ExpressionStatement stmt = body.block.statements[0]; |
749 AssignmentExpression assignment = stmt.expression; | 750 AssignmentExpression assignment = stmt.expression; |
750 SimpleIdentifier leftHandSide = assignment.leftHandSide; | 751 SimpleIdentifier leftHandSide = assignment.leftHandSide; |
751 expect(leftHandSide.staticElement.enclosingElement.name, 'M2'); | 752 expect( |
| 753 staticElementForIdentifier(leftHandSide).enclosingElement.name, 'M2'); |
752 expect(leftHandSide.auxiliaryElements.staticElement.enclosingElement.name, | 754 expect(leftHandSide.auxiliaryElements.staticElement.enclosingElement.name, |
753 'M2'); | 755 'M2'); |
754 } | 756 } |
755 | 757 |
756 @failingTest | 758 @failingTest |
757 void test_getter_and_setter_fromMixins_property_access() { | 759 void test_getter_and_setter_fromMixins_property_access() { |
758 // TODO(paulberry): it appears that auxiliaryElements isn't properly set on | 760 // TODO(paulberry): it appears that auxiliaryElements isn't properly set on |
759 // a SimpleIdentifier that's inside a property access. This bug should be | 761 // a SimpleIdentifier that's inside a property access. This bug should be |
760 // fixed. | 762 // fixed. |
761 Source source = addSource(''' | 763 Source source = addSource(''' |
(...skipping 16 matching lines...) Expand all Loading... |
778 verify([source]); | 780 verify([source]); |
779 // Verify that both the getter and setter for "x" in "new C().x" refer to | 781 // Verify that both the getter and setter for "x" in "new C().x" refer to |
780 // the accessors defined in M2. | 782 // the accessors defined in M2. |
781 FunctionDeclaration main = | 783 FunctionDeclaration main = |
782 library.definingCompilationUnit.functions[0].computeNode(); | 784 library.definingCompilationUnit.functions[0].computeNode(); |
783 BlockFunctionBody body = main.functionExpression.body; | 785 BlockFunctionBody body = main.functionExpression.body; |
784 ExpressionStatement stmt = body.block.statements[0]; | 786 ExpressionStatement stmt = body.block.statements[0]; |
785 AssignmentExpression assignment = stmt.expression; | 787 AssignmentExpression assignment = stmt.expression; |
786 PropertyAccess propertyAccess = assignment.leftHandSide; | 788 PropertyAccess propertyAccess = assignment.leftHandSide; |
787 expect( | 789 expect( |
788 propertyAccess.propertyName.staticElement.enclosingElement.name, 'M2'); | 790 staticElementForIdentifier(propertyAccess.propertyName) |
| 791 .enclosingElement |
| 792 .name, |
| 793 'M2'); |
789 expect( | 794 expect( |
790 propertyAccess | 795 propertyAccess |
791 .propertyName.auxiliaryElements.staticElement.enclosingElement.name, | 796 .propertyName.auxiliaryElements.staticElement.enclosingElement.name, |
792 'M2'); | 797 'M2'); |
793 } | 798 } |
794 | 799 |
795 void test_getter_fromMixins_bare_identifier() { | 800 void test_getter_fromMixins_bare_identifier() { |
796 Source source = addSource(''' | 801 Source source = addSource(''' |
797 class B {} | 802 class B {} |
798 class M1 { | 803 class M1 { |
(...skipping 11 matching lines...) Expand all Loading... |
810 LibraryElement library = resolve2(source); | 815 LibraryElement library = resolve2(source); |
811 assertNoErrors(source); | 816 assertNoErrors(source); |
812 verify([source]); | 817 verify([source]); |
813 // Verify that the getter for "x" in C.f() refers to the getter defined in | 818 // Verify that the getter for "x" in C.f() refers to the getter defined in |
814 // M2. | 819 // M2. |
815 ClassElement classC = library.definingCompilationUnit.types[3]; | 820 ClassElement classC = library.definingCompilationUnit.types[3]; |
816 MethodDeclaration f = classC.getMethod('f').computeNode(); | 821 MethodDeclaration f = classC.getMethod('f').computeNode(); |
817 BlockFunctionBody body = f.body; | 822 BlockFunctionBody body = f.body; |
818 ReturnStatement stmt = body.block.statements[0]; | 823 ReturnStatement stmt = body.block.statements[0]; |
819 SimpleIdentifier x = stmt.expression; | 824 SimpleIdentifier x = stmt.expression; |
820 expect(x.staticElement.enclosingElement.name, 'M2'); | 825 expect(staticElementForIdentifier(x).enclosingElement.name, 'M2'); |
821 } | 826 } |
822 | 827 |
823 void test_getter_fromMixins_property_access() { | 828 void test_getter_fromMixins_property_access() { |
824 Source source = addSource(''' | 829 Source source = addSource(''' |
825 class B {} | 830 class B {} |
826 class M1 { | 831 class M1 { |
827 get x => null; | 832 get x => null; |
828 } | 833 } |
829 class M2 { | 834 class M2 { |
830 get x => null; | 835 get x => null; |
831 } | 836 } |
832 class C extends B with M1, M2 {} | 837 class C extends B with M1, M2 {} |
833 void main() { | 838 void main() { |
834 var y = new C().x; | 839 var y = new C().x; |
835 } | 840 } |
836 '''); | 841 '''); |
837 LibraryElement library = resolve2(source); | 842 LibraryElement library = resolve2(source); |
838 assertNoErrors(source); | 843 assertNoErrors(source); |
839 verify([source]); | 844 verify([source]); |
840 // Verify that the getter for "x" in "new C().x" refers to the getter | 845 // Verify that the getter for "x" in "new C().x" refers to the getter |
841 // defined in M2. | 846 // defined in M2. |
842 FunctionDeclaration main = | 847 FunctionDeclaration main = |
843 library.definingCompilationUnit.functions[0].computeNode(); | 848 library.definingCompilationUnit.functions[0].computeNode(); |
844 BlockFunctionBody body = main.functionExpression.body; | 849 BlockFunctionBody body = main.functionExpression.body; |
845 VariableDeclarationStatement stmt = body.block.statements[0]; | 850 VariableDeclarationStatement stmt = body.block.statements[0]; |
846 PropertyAccess propertyAccess = stmt.variables.variables[0].initializer; | 851 PropertyAccess propertyAccess = stmt.variables.variables[0].initializer; |
847 expect( | 852 expect( |
848 propertyAccess.propertyName.staticElement.enclosingElement.name, 'M2'); | 853 staticElementForIdentifier(propertyAccess.propertyName) |
| 854 .enclosingElement |
| 855 .name, |
| 856 'M2'); |
849 } | 857 } |
850 | 858 |
851 void test_getterAndSetterWithDifferentTypes() { | 859 void test_getterAndSetterWithDifferentTypes() { |
852 Source source = addSource(r''' | 860 Source source = addSource(r''' |
853 class A { | 861 class A { |
854 int get f => 0; | 862 int get f => 0; |
855 void set f(String s) {} | 863 void set f(String s) {} |
856 } | 864 } |
857 g (A a) { | 865 g (A a) { |
858 a.f = a.f.toString(); | 866 a.f = a.f.toString(); |
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1528 '''); | 1536 '''); |
1529 LibraryElement library = resolve2(source); | 1537 LibraryElement library = resolve2(source); |
1530 assertNoErrors(source); | 1538 assertNoErrors(source); |
1531 verify([source]); | 1539 verify([source]); |
1532 // Verify that the "f" in "new C().f()" refers to the "f" defined in M2. | 1540 // Verify that the "f" in "new C().f()" refers to the "f" defined in M2. |
1533 FunctionDeclaration main = | 1541 FunctionDeclaration main = |
1534 library.definingCompilationUnit.functions[0].computeNode(); | 1542 library.definingCompilationUnit.functions[0].computeNode(); |
1535 BlockFunctionBody body = main.functionExpression.body; | 1543 BlockFunctionBody body = main.functionExpression.body; |
1536 ExpressionStatement stmt = body.block.statements[0]; | 1544 ExpressionStatement stmt = body.block.statements[0]; |
1537 MethodInvocation expr = stmt.expression; | 1545 MethodInvocation expr = stmt.expression; |
1538 expect(expr.methodName.staticElement.enclosingElement.name, 'M2'); | 1546 expect(staticElementForIdentifier(expr.methodName).enclosingElement.name, |
| 1547 'M2'); |
1539 } | 1548 } |
1540 | 1549 |
1541 void test_method_fromMixins_bare_identifier() { | 1550 void test_method_fromMixins_bare_identifier() { |
1542 Source source = addSource(''' | 1551 Source source = addSource(''' |
1543 class B {} | 1552 class B {} |
1544 class M1 { | 1553 class M1 { |
1545 void f() {} | 1554 void f() {} |
1546 } | 1555 } |
1547 class M2 { | 1556 class M2 { |
1548 void f() {} | 1557 void f() {} |
1549 } | 1558 } |
1550 class C extends B with M1, M2 { | 1559 class C extends B with M1, M2 { |
1551 void g() { | 1560 void g() { |
1552 f(); | 1561 f(); |
1553 } | 1562 } |
1554 } | 1563 } |
1555 '''); | 1564 '''); |
1556 LibraryElement library = resolve2(source); | 1565 LibraryElement library = resolve2(source); |
1557 assertNoErrors(source); | 1566 assertNoErrors(source); |
1558 verify([source]); | 1567 verify([source]); |
1559 // Verify that the call to f() in C.g() refers to the method defined in M2. | 1568 // Verify that the call to f() in C.g() refers to the method defined in M2. |
1560 ClassElement classC = library.definingCompilationUnit.types[3]; | 1569 ClassElement classC = library.definingCompilationUnit.types[3]; |
1561 MethodDeclaration g = classC.getMethod('g').computeNode(); | 1570 MethodDeclaration g = classC.getMethod('g').computeNode(); |
1562 BlockFunctionBody body = g.body; | 1571 BlockFunctionBody body = g.body; |
1563 ExpressionStatement stmt = body.block.statements[0]; | 1572 ExpressionStatement stmt = body.block.statements[0]; |
1564 MethodInvocation invocation = stmt.expression; | 1573 MethodInvocation invocation = stmt.expression; |
1565 SimpleIdentifier methodName = invocation.methodName; | 1574 SimpleIdentifier methodName = invocation.methodName; |
1566 expect(methodName.staticElement.enclosingElement.name, 'M2'); | 1575 expect(staticElementForIdentifier(methodName).enclosingElement.name, 'M2'); |
1567 } | 1576 } |
1568 | 1577 |
1569 void test_method_fromMixins_invked_from_outside_class() { | 1578 void test_method_fromMixins_invked_from_outside_class() { |
1570 Source source = addSource(''' | 1579 Source source = addSource(''' |
1571 class B {} | 1580 class B {} |
1572 class M1 { | 1581 class M1 { |
1573 void f() {} | 1582 void f() {} |
1574 } | 1583 } |
1575 class M2 { | 1584 class M2 { |
1576 void f() {} | 1585 void f() {} |
1577 } | 1586 } |
1578 class C extends B with M1, M2 {} | 1587 class C extends B with M1, M2 {} |
1579 void main() { | 1588 void main() { |
1580 new C().f(); | 1589 new C().f(); |
1581 } | 1590 } |
1582 '''); | 1591 '''); |
1583 LibraryElement library = resolve2(source); | 1592 LibraryElement library = resolve2(source); |
1584 assertNoErrors(source); | 1593 assertNoErrors(source); |
1585 verify([source]); | 1594 verify([source]); |
1586 // Verify that the call to f() in "new C().f()" refers to the method | 1595 // Verify that the call to f() in "new C().f()" refers to the method |
1587 // defined in M2. | 1596 // defined in M2. |
1588 FunctionDeclaration main = | 1597 FunctionDeclaration main = |
1589 library.definingCompilationUnit.functions[0].computeNode(); | 1598 library.definingCompilationUnit.functions[0].computeNode(); |
1590 BlockFunctionBody body = main.functionExpression.body; | 1599 BlockFunctionBody body = main.functionExpression.body; |
1591 ExpressionStatement stmt = body.block.statements[0]; | 1600 ExpressionStatement stmt = body.block.statements[0]; |
1592 MethodInvocation invocation = stmt.expression; | 1601 MethodInvocation invocation = stmt.expression; |
1593 expect(invocation.methodName.staticElement.enclosingElement.name, 'M2'); | 1602 expect( |
| 1603 staticElementForIdentifier(invocation.methodName).enclosingElement.name, |
| 1604 'M2'); |
1594 } | 1605 } |
1595 | 1606 |
1596 void test_method_fromSuperclassMixin() { | 1607 void test_method_fromSuperclassMixin() { |
1597 Source source = addSource(r''' | 1608 Source source = addSource(r''' |
1598 class A { | 1609 class A { |
1599 void m1() {} | 1610 void m1() {} |
1600 } | 1611 } |
1601 class B extends Object with A { | 1612 class B extends Object with A { |
1602 } | 1613 } |
1603 class C extends B { | 1614 class C extends B { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1673 assertNoErrors(source); | 1684 assertNoErrors(source); |
1674 verify([source]); | 1685 verify([source]); |
1675 // Verify that the setter for "x" in C.f() refers to the setter defined in | 1686 // Verify that the setter for "x" in C.f() refers to the setter defined in |
1676 // M2. | 1687 // M2. |
1677 ClassElement classC = library.definingCompilationUnit.types[3]; | 1688 ClassElement classC = library.definingCompilationUnit.types[3]; |
1678 MethodDeclaration f = classC.getMethod('f').computeNode(); | 1689 MethodDeclaration f = classC.getMethod('f').computeNode(); |
1679 BlockFunctionBody body = f.body; | 1690 BlockFunctionBody body = f.body; |
1680 ExpressionStatement stmt = body.block.statements[0]; | 1691 ExpressionStatement stmt = body.block.statements[0]; |
1681 AssignmentExpression assignment = stmt.expression; | 1692 AssignmentExpression assignment = stmt.expression; |
1682 SimpleIdentifier leftHandSide = assignment.leftHandSide; | 1693 SimpleIdentifier leftHandSide = assignment.leftHandSide; |
1683 expect(leftHandSide.staticElement.enclosingElement.name, 'M2'); | 1694 expect( |
| 1695 staticElementForIdentifier(leftHandSide).enclosingElement.name, 'M2'); |
1684 } | 1696 } |
1685 | 1697 |
1686 void test_setter_fromMixins_property_access() { | 1698 void test_setter_fromMixins_property_access() { |
1687 Source source = addSource(''' | 1699 Source source = addSource(''' |
1688 class B {} | 1700 class B {} |
1689 class M1 { | 1701 class M1 { |
1690 set x(value) {} | 1702 set x(value) {} |
1691 } | 1703 } |
1692 class M2 { | 1704 class M2 { |
1693 set x(value) {} | 1705 set x(value) {} |
1694 } | 1706 } |
1695 class C extends B with M1, M2 {} | 1707 class C extends B with M1, M2 {} |
1696 void main() { | 1708 void main() { |
1697 new C().x = 1; | 1709 new C().x = 1; |
1698 } | 1710 } |
1699 '''); | 1711 '''); |
1700 LibraryElement library = resolve2(source); | 1712 LibraryElement library = resolve2(source); |
1701 assertNoErrors(source); | 1713 assertNoErrors(source); |
1702 verify([source]); | 1714 verify([source]); |
1703 // Verify that the setter for "x" in "new C().x" refers to the setter | 1715 // Verify that the setter for "x" in "new C().x" refers to the setter |
1704 // defined in M2. | 1716 // defined in M2. |
1705 FunctionDeclaration main = | 1717 FunctionDeclaration main = |
1706 library.definingCompilationUnit.functions[0].computeNode(); | 1718 library.definingCompilationUnit.functions[0].computeNode(); |
1707 BlockFunctionBody body = main.functionExpression.body; | 1719 BlockFunctionBody body = main.functionExpression.body; |
1708 ExpressionStatement stmt = body.block.statements[0]; | 1720 ExpressionStatement stmt = body.block.statements[0]; |
1709 AssignmentExpression assignment = stmt.expression; | 1721 AssignmentExpression assignment = stmt.expression; |
1710 PropertyAccess propertyAccess = assignment.leftHandSide; | 1722 PropertyAccess propertyAccess = assignment.leftHandSide; |
1711 expect( | 1723 expect( |
1712 propertyAccess.propertyName.staticElement.enclosingElement.name, 'M2'); | 1724 staticElementForIdentifier(propertyAccess.propertyName) |
| 1725 .enclosingElement |
| 1726 .name, |
| 1727 'M2'); |
1713 } | 1728 } |
1714 | 1729 |
1715 void test_setter_inherited() { | 1730 void test_setter_inherited() { |
1716 Source source = addSource(r''' | 1731 Source source = addSource(r''' |
1717 class A { | 1732 class A { |
1718 int get x => 0; | 1733 int get x => 0; |
1719 set x(int p) {} | 1734 set x(int p) {} |
1720 } | 1735 } |
1721 class B extends A { | 1736 class B extends A { |
1722 int get x => super.x == null ? 0 : super.x; | 1737 int get x => super.x == null ? 0 : super.x; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1830 // check propagated type | 1845 // check propagated type |
1831 FunctionType propagatedType = node.propagatedType as FunctionType; | 1846 FunctionType propagatedType = node.propagatedType as FunctionType; |
1832 expect(propagatedType.returnType, test.typeProvider.stringType); | 1847 expect(propagatedType.returnType, test.typeProvider.stringType); |
1833 } on AnalysisException catch (e, stackTrace) { | 1848 } on AnalysisException catch (e, stackTrace) { |
1834 thrownException[0] = new CaughtException(e, stackTrace); | 1849 thrownException[0] = new CaughtException(e, stackTrace); |
1835 } | 1850 } |
1836 } | 1851 } |
1837 return null; | 1852 return null; |
1838 } | 1853 } |
1839 } | 1854 } |
OLD | NEW |