aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-31 18:44:13 +0000
committerMichael Williamson <mike@zwobble.org>2015-10-31 18:44:13 +0000
commit971fb3c743df26c408d752f1bc4137e645f455a2 (patch)
tree995caf9c1cfcc0e4d3ce7da683d59e95fb517d42 /src/test/java/org/zwobble
parenta5e68ab6528f62c891479f387877118f90dfa2b8 (diff)
Add partial support for boxing integers
Diffstat (limited to 'src/test/java/org/zwobble')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java5
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java43
2 files changed, 46 insertions, 2 deletions
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
@@ -238,6 +250,25 @@ public class JavaReaderTests {
}
@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<StatementNode> statements = readStatements("int x = 4; return x;");
@@ -252,6 +283,14 @@ public class JavaReaderTests {
}
@Test
+ public void integersAreBoxedIfDeclaredAsObjectVariable() {
+ List<StatementNode> 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(
"public String identity(int value) {" +