From 9e850b3a09ceebf6140e77982b45abab0c57ad6c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 5 Oct 2015 17:59:27 +0100 Subject: Implement attribute access and calls in Python backend --- .../zwobble/couscous/tests/BackendEvalTests.java | 7 +++++++ .../backends/python/PythonCompilerEvalTests.java | 9 +++++++++ .../backends/python/PythonSerializerTests.java | 21 ++++++++++++++++++++- .../couscous/tests/interpreter/EvaluatorTests.java | 7 ------- 4 files changed, 36 insertions(+), 8 deletions(-) (limited to 'src/test') diff --git a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java index 4eb925a..68e9761 100644 --- a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java +++ b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java @@ -7,6 +7,7 @@ import org.zwobble.couscous.values.PrimitiveValue; import static org.junit.Assert.assertEquals; import static org.zwobble.couscous.ast.LiteralNode.literal; +import static org.zwobble.couscous.ast.MethodCallNode.methodCall; import static org.zwobble.couscous.values.PrimitiveValues.value; import lombok.val; @@ -32,5 +33,11 @@ public abstract class BackendEvalTests { assertEquals(value("F"), result); } + @Test + public void canCallMethodWithNoArgumentsOnBuiltin() { + val result = evalExpression(methodCall(literal("hello"), "length")); + assertEquals(value(5), result); + } + protected abstract PrimitiveValue evalExpression(ExpressionNode expression); } diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java index 6f19eb3..ff51f60 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.tests.backends.python; +import org.junit.Ignore; +import org.junit.Test; import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.ExpressionNode; import org.zwobble.couscous.ast.MethodNode; @@ -12,6 +14,13 @@ import com.google.common.collect.ImmutableList; import lombok.val; public class PythonCompilerEvalTests extends BackendEvalTests { + @Override + @Test + @Ignore("Work in progress") + public void canCallMethodWithNoArgumentsOnBuiltin() { + + } + @Override protected PrimitiveValue evalExpression(ExpressionNode expression) { val runner = new PythonMethodRunner(); diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java index 60aed36..a7708da 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java @@ -8,7 +8,9 @@ import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.zwobble.couscous.backends.python.PythonSerializer.serialize; import static org.zwobble.couscous.backends.python.ast.PythonAssignmentNode.pythonAssignment; +import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess; import static org.zwobble.couscous.backends.python.ast.PythonBooleanLiteralNode.pythonBooleanLiteral; +import static org.zwobble.couscous.backends.python.ast.PythonCallNode.pythonCall; import static org.zwobble.couscous.backends.python.ast.PythonConditionalExpressionNode.pythonConditionalExpression; import static org.zwobble.couscous.backends.python.ast.PythonIntegerLiteralNode.pythonIntegerLiteral; import static org.zwobble.couscous.backends.python.ast.PythonModuleNode.pythonModule; @@ -45,7 +47,7 @@ public class PythonSerializerTests { } @Test - public void conditionalExpressionIsSerializedUsingParens() { + public void conditionalExpressionIsSerializedUsingParenthesisedSubExpression() { assertEquals("(1) if (True) else (2)", serialize( pythonConditionalExpression( pythonBooleanLiteral(true), @@ -53,6 +55,23 @@ public class PythonSerializerTests { pythonIntegerLiteral(2)))); } + @Test + public void attributeAccessIsSerializedUsingParenthesisedSubExpression() { + assertEquals("(x).y", serialize( + pythonAttributeAccess( + pythonVariableReference("x"), + "y"))); + } + + @Test + public void callIsSerializedUsingParenthesisedSubExpression() { + assertEquals("(f)(x, y)", serialize( + pythonCall( + pythonVariableReference("f"), + asList(pythonVariableReference("x"), + pythonVariableReference("y"))))); + } + @Test public void returnKeywordIsUsedForReturns() { val output = serialize(pythonReturn(pythonIntegerLiteral(42))); diff --git a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java index 0a2a947..766cf3c 100644 --- a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java +++ b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java @@ -56,13 +56,6 @@ public class EvaluatorTests extends BackendEvalTests { assertEquals(new ConditionMustBeBoolean(new IntegerInterpreterValue(1)), exception); } - @Test - public void canCallMethodWithNoArgumentsOnBuiltin() { - val result = eval(emptyEnvironment(), - methodCall(literal("hello"), "length")); - assertEquals(new IntegerInterpreterValue(5), result); - } - @Test public void canCallMethodWithArgumentsOnBuiltin() { val result = eval(emptyEnvironment(), -- cgit v1.2.3