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

Unified Diff: compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java

Issue 10914215: Issue 5049. Consistently resolve instance/static/top-level property access (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months 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
« no previous file with comments | « compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java
diff --git a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java
index 0d0f00ebdb3d5f65dd208a8ac4f4a6706f87ad00..1c080daf1a569af1cdb6dbfc08da953947e14cae 100644
--- a/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java
+++ b/compiler/javatests/com/google/dart/compiler/type/TypeAnalyzerCompilerTest.java
@@ -38,6 +38,7 @@ import com.google.dart.compiler.ast.DartMethodDefinition;
import com.google.dart.compiler.ast.DartNewExpression;
import com.google.dart.compiler.ast.DartNode;
import com.google.dart.compiler.ast.DartParameter;
+import com.google.dart.compiler.ast.DartPropertyAccess;
import com.google.dart.compiler.ast.DartTypeNode;
import com.google.dart.compiler.ast.DartUnaryExpression;
import com.google.dart.compiler.ast.DartUnit;
@@ -47,8 +48,9 @@ import com.google.dart.compiler.parser.ParserErrorCode;
import com.google.dart.compiler.resolver.ClassElement;
import com.google.dart.compiler.resolver.Element;
import com.google.dart.compiler.resolver.ElementKind;
+import com.google.dart.compiler.resolver.EnclosingElement;
+import com.google.dart.compiler.resolver.LibraryElement;
import com.google.dart.compiler.resolver.MethodElement;
-import com.google.dart.compiler.resolver.MethodNodeElement;
import com.google.dart.compiler.resolver.NodeElement;
import com.google.dart.compiler.resolver.ResolverErrorCode;
import com.google.dart.compiler.resolver.TypeErrorCode;
@@ -3745,80 +3747,261 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
{
DartArrayAccess access = findNode(DartArrayAccess.class, "a[2]");
// a[2] is invocation of method "[]"
- {
- MethodElement element = access.getElement();
- assertEquals("[]", element.getName());
- assertEquals("A", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(access, "A", "[]");
}
// a[0]++
{
DartUnaryExpression unary = findNode(DartUnaryExpression.class, "a[0]++");
// a[0]++ is invocation of method "+"
- {
- MethodElement element = unary.getElement();
- assertEquals("+", element.getName());
- assertEquals("B", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(unary, "B", "+");
// a[0] is invocation of method []
- {
- DartArrayAccess access = (DartArrayAccess) unary.getArg();
- MethodNodeElement element = access.getElement();
- assertEquals("[]", element.getName());
- assertEquals("A", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(unary.getArg(), "A", "[]");
}
// ++a[0]
{
DartUnaryExpression unary = findNode(DartUnaryExpression.class, "++a[0]");
// ++a[0] is invocation of method "+"
- {
- MethodElement element = unary.getElement();
- assertEquals("+", element.getName());
- assertEquals("B", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(unary, "B", "+");
// a[0] is invocation of method []
- {
- DartArrayAccess access = (DartArrayAccess) unary.getArg();
- MethodNodeElement element = access.getElement();
- assertEquals("[]", element.getName());
- assertEquals("A", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(unary.getArg(), "A", "[]");
}
// a[0] += 1
{
DartBinaryExpression binary = findNode(DartBinaryExpression.class, "a[0] += 1");
// a[0] += 1 is invocation of method "+"
- {
- MethodElement element = binary.getElement();
- assertEquals("+", element.getName());
- assertEquals("B", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(binary, "B", "+");
// a[0] is invocation of method []
- {
- DartArrayAccess access = (DartArrayAccess) binary.getArg1();
- MethodNodeElement element = access.getElement();
- assertEquals("[]", element.getName());
- assertEquals("A", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(binary.getArg1(), "A", "[]");
}
// a[0] = 1
{
DartBinaryExpression binary = findNode(DartBinaryExpression.class, "a[0] = 1");
// a[0] = 1 is invocation of method "[]="
- {
- MethodElement element = binary.getElement();
- assertEquals("[]=", element.getName());
- assertEquals("A", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(binary, "A", "[]=");
// a[0] is invocation of method []=
- {
- DartArrayAccess access = (DartArrayAccess) binary.getArg1();
- MethodNodeElement element = access.getElement();
- assertEquals("[]=", element.getName());
- assertEquals("A", element.getEnclosingElement().getName());
- }
+ assertHasMethodElement(binary.getArg1(), "A", "[]=");
+ }
+ }
+
+ /**
+ * Test for resolving variants of property access and unary/binary expressions.
+ * <p>
+ * http://code.google.com/p/dart/issues/detail?id=5049
+ */
+ public void test_opAssignPropertyAccess_instance() throws Exception {
+ AnalyzeLibraryResult libraryResult = analyzeLibrary(
+ "// filler filler filler filler filler filler filler filler filler filler",
+ "class A {",
+ " B get b => new B();",
+ " set b(B x) {}",
+ "}",
+ "class B {",
+ " B operator +(x) => new B();",
+ "}",
+ "main () {",
+ " A a = new A();",
+ " process( a.b );",
+ " a.b++;",
+ " ++a.b;",
+ " a.b += 1;",
+ " a.b = null;",
+ "}",
+ "process(x) {}",
+ "");
+ assertErrors(libraryResult.getErrors());
+ // print( a.b )
+ {
+ DartPropertyAccess access = findNode(DartPropertyAccess.class, "a.b");
+ // a.b is invocation of method "get b"
+ assertHasMethodElement(access, "A", "get b");
+ }
+ // a.b++
+ {
+ DartUnaryExpression unary = findNode(DartUnaryExpression.class, "a.b++");
+ // a.b++ is invocation of method "+"
+ assertHasMethodElement(unary, "B", "+");
+ // a.b is invocation of method "get b"
+ assertHasMethodElement(unary.getArg(), "A", "get b");
+ }
+ // ++a.b
+ {
+ DartUnaryExpression unary = findNode(DartUnaryExpression.class, "++a.b");
+ // ++a.b is invocation of method "+"
+ assertHasMethodElement(unary, "B", "+");
+ // a.b is invocation of method "get b"
+ assertHasMethodElement(unary.getArg(), "A", "get b");
+ }
+ // a.b += 1
+ {
+ DartBinaryExpression binary = findNode(DartBinaryExpression.class, "a.b += 1");
+ // a.b += 1 is invocation of method "+"
+ assertHasMethodElement(binary, "B", "+");
+ // a.b is invocation of method "get b"
+ assertHasMethodElement(binary.getArg1(), "A", "get b");
+ }
+ // a.b = null
+ {
+ DartBinaryExpression binary = findNode(DartBinaryExpression.class, "a.b = null");
+ // a.b = null is invocation of method "set b"
+ assertHasMethodElement(binary, "A", "set b");
+ // a.b is invocation of method "set b"
+ assertHasMethodElement(binary.getArg1(), "A", "set b");
+ }
+ }
+
+ /**
+ * Test for resolving variants of static property access and unary/binary expressions.
+ * <p>
+ * http://code.google.com/p/dart/issues/detail?id=5049
+ */
+ public void test_opAssignPropertyAccess_static() throws Exception {
+ AnalyzeLibraryResult libraryResult = analyzeLibrary(
+ "// filler filler filler filler filler filler filler filler filler filler",
+ "class A {",
+ " static B get b => new B();",
+ " static set b(B x) {}",
+ "}",
+ "class B {",
+ " B operator +(x) => new B();",
+ "}",
+ "main () {",
+ " process( A.b );",
+ " A.b++;",
+ " ++A.b;",
+ " A.b += 1;",
+ " A.b = null;",
+ "}",
+ "process(x) {}",
+ "");
+ assertErrors(libraryResult.getErrors());
+ // print( A.b )
+ {
+ DartPropertyAccess access = findNode(DartPropertyAccess.class, "A.b");
+ // a.b is invocation of method "get b"
+ assertHasMethodElement(access, "A", "get b");
+ }
+ // A.b++
+ {
+ DartUnaryExpression unary = findNode(DartUnaryExpression.class, "A.b++");
+ // A.b++ is invocation of method "+"
+ assertHasMethodElement(unary, "B", "+");
+ // A.b is invocation of method "get b"
+ assertHasMethodElement(unary.getArg(), "A", "get b");
+ }
+ // ++A.b
+ {
+ DartUnaryExpression unary = findNode(DartUnaryExpression.class, "++A.b");
+ // ++A.b is invocation of method "+"
+ assertHasMethodElement(unary, "B", "+");
+ // A.b is invocation of method "get b"
+ assertHasMethodElement(unary.getArg(), "A", "get b");
+ }
+ // A.b += 1
+ {
+ DartBinaryExpression binary = findNode(DartBinaryExpression.class, "A.b += 1");
+ // A.b += 1 is invocation of method "+"
+ assertHasMethodElement(binary, "B", "+");
+ // A.b is invocation of method "get b"
+ assertHasMethodElement(binary.getArg1(), "A", "get b");
+ }
+ // A.b = null
+ {
+ DartBinaryExpression binary = findNode(DartBinaryExpression.class, "A.b = null");
+ // A.b = null is invocation of method "set b"
+ assertHasMethodElement(binary, "A", "set b");
+ // A.b is invocation of method "set b"
+ assertHasMethodElement(binary.getArg1(), "A", "set b");
+ }
+ }
+
+ /**
+ * Test for resolving variants of top-level property access and unary/binary expressions.
+ * <p>
+ * http://code.google.com/p/dart/issues/detail?id=5049
+ */
+ public void test_opAssignPropertyAccess_topLevel() throws Exception {
+ AnalyzeLibraryResult libraryResult = analyzeLibrary(
+ "// filler filler filler filler filler filler filler filler filler filler",
+ "B get field => new B();",
+ " set field(B x) {}",
+ "class B {",
+ " B operator +(x) => new B();",
+ "}",
+ "main () {",
+ " process( field );",
+ " field++;",
+ " ++field;",
+ " field += 1;",
+ " field = null;",
+ "}",
+ "process(x) {}",
+ "");
+ assertErrors(libraryResult.getErrors());
+ // print( field )
+ {
+ DartIdentifier access = findNode(DartIdentifier.class, "field );");
+ // "field" is invocation of method "get field"
+ assertHasMethodElement(access, "<library>", "get field");
+ }
+ // field++
+ {
+ DartUnaryExpression unary = findNode(DartUnaryExpression.class, "field++");
+ // field++ is invocation of method "+"
+ assertHasMethodElement(unary, "B", "+");
+ // "field" is invocation of method "get field"
+ assertHasMethodElement(unary.getArg(), "<library>", "get field");
+ }
+ // ++field
+ {
+ DartUnaryExpression unary = findNode(DartUnaryExpression.class, "++field");
+ // ++field is invocation of method "+"
+ assertHasMethodElement(unary, "B", "+");
+ // "field" is invocation of method "get field"
+ assertHasMethodElement(unary.getArg(), "<library>", "get field");
+ }
+ // field += 1
+ {
+ DartBinaryExpression binary = findNode(DartBinaryExpression.class, "field += 1");
+ // field += 1 is invocation of method "+"
+ assertHasMethodElement(binary, "B", "+");
+ // "field" is invocation of method "get field"
+ assertHasMethodElement(binary.getArg1(), "<library>", "get field");
+ }
+ // field = null
+ {
+ DartBinaryExpression binary = findNode(DartBinaryExpression.class, "field = null");
+ // field = null is invocation of method "set field"
+ assertHasMethodElement(binary, "<library>", "set field");
+ // "field" is invocation of method "set field"
+ assertHasMethodElement(binary.getArg1(), "<library>", "set field");
+ }
+ }
+
+ private static void assertHasMethodElement(DartNode node, String className, String methodName) {
+ Element element = node.getElement();
+ assertTrue("" + node + " " + element, element instanceof MethodElement);
+ MethodElement methodElement = (MethodElement) element;
+ assertMethodElement(methodElement, className, methodName);
+ }
+
+ private static void assertMethodElement(MethodElement element, String className, String methodName) {
+ EnclosingElement enclosingElement = element.getEnclosingElement();
+ String enclosingName;
+ if (enclosingElement instanceof LibraryElement) {
+ enclosingName = "<library>";
+ } else {
+ enclosingName = enclosingElement.getName();
+ }
+ assertEquals(className, enclosingName);
+ //
+ String elementName = element.getName();
+ if (element.getModifiers().isGetter()) {
+ elementName = "get " + elementName;
+ }
+ if (element.getModifiers().isSetter()) {
+ elementName = "set " + elementName;
}
+ assertEquals(methodName, elementName);
}
public void test_invokeStaticFieldAsMethod() throws Exception {
« no previous file with comments | « compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698