From 971fb3c743df26c408d752f1bc4137e645f455a2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 31 Oct 2015 18:44:13 +0000 Subject: Add partial support for boxing integers --- .../zwobble/couscous/tests/JavaToPythonTests.java | 5 +++ .../tests/frontends/java/JavaReaderTests.java | 43 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) (limited to 'src/test/java/org') diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java index e5a6bf7..b639c6f 100644 --- a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java +++ b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java @@ -37,6 +37,11 @@ public class JavaToPythonTests { assertEquals(value(true), exec("Object x = new Object(); return x == x;")); } + @Test + public void integersCanBeAssignedToObjectVariable() { + assertEquals(value(false), exec("Object x = 1; return x.equals(new Object());")); + } + private PrimitiveValue exec(String source) { try { String javaClass = diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java index dffa7df..c455e8d 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java @@ -14,11 +14,13 @@ import org.zwobble.couscous.ast.LocalVariableDeclarationNode; import org.zwobble.couscous.ast.MethodNode; import org.zwobble.couscous.ast.ReturnNode; import org.zwobble.couscous.ast.StatementNode; +import org.zwobble.couscous.ast.StaticMethodCallNode; import org.zwobble.couscous.ast.ThisReferenceNode; import org.zwobble.couscous.ast.TypeName; import org.zwobble.couscous.frontends.java.JavaReader; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; +import org.zwobble.couscous.values.ObjectValues; import org.zwobble.couscous.values.StringValue; import static java.util.Arrays.asList; @@ -33,6 +35,7 @@ import static org.zwobble.couscous.ast.FieldDeclarationNode.field; import static org.zwobble.couscous.ast.LiteralNode.literal; import static org.zwobble.couscous.ast.MethodCallNode.methodCall; import static org.zwobble.couscous.ast.MethodCallNode.not; +import static org.zwobble.couscous.ast.StaticMethodCallNode.boxInt; import static org.zwobble.couscous.ast.StaticMethodCallNode.same; import static org.zwobble.couscous.ast.StaticMethodCallNode.staticMethodCall; import static org.zwobble.couscous.ast.TernaryConditionalNode.ternaryConditional; @@ -154,8 +157,17 @@ public class JavaReaderTests { @Test public void canReadConstructorCalls() { assertEquals( - constructorCall(TypeName.of("java.lang.String"), asList(literal("_"), literal(42))), - readExpression("new String(\"_\", 42)")); + constructorCall(TypeName.of("java.lang.String"), asList(literal("_"))), + readExpression("new String(\"_\")")); + } + + @Test + public void argumentIsBoxedIfNecessary() { + StaticMethodCallNode expression = (StaticMethodCallNode)readExpression( + "java.util.Objects.toString(42)"); + assertEquals( + boxInt(literal(42)), + expression.getArguments().get(0)); } @Test @@ -237,6 +249,25 @@ public class JavaReaderTests { returnNode.getValue()); } + @Test + public void integersAreBoxedIfAssignedToObjectVariable() { + ClassNode classNode = readClass( + "private Object value;" + + "public Object getValue() {" + + " return value = 4;" + + "}"); + + ReturnNode returnNode = (ReturnNode) classNode.getMethods().get(0).getBody().get(0); + assertEquals( + assign( + fieldAccess( + thisReference(TypeName.of("com.example.Example")), + "value", + ObjectValues.OBJECT), + boxInt(literal(4))), + returnNode.getValue()); + } + @Test public void canDeclareAndReferenceLocalVariables() { List statements = readStatements("int x = 4; return x;"); @@ -251,6 +282,14 @@ public class JavaReaderTests { returnNode.getValue()); } + @Test + public void integersAreBoxedIfDeclaredAsObjectVariable() { + List statements = readStatements("Object x = 4;"); + + LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0); + assertEquals(StaticMethodCallNode.boxInt(literal(4)), declaration.getInitialValue()); + } + @Test public void canDeclareAndReferenceArguments() { ClassNode classNode = readClass( -- cgit v1.2.3