aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java4
-rw-r--r--src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java1
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java71
3 files changed, 20 insertions, 56 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java
index 3e10586..2c8dfeb 100644
--- a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java
@@ -1,5 +1,6 @@
package org.zwobble.couscous.tests;
+import org.junit.Test;
import org.zwobble.couscous.CouscousCompiler;
import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.backends.python.PythonBackend;
@@ -8,13 +9,12 @@ import org.zwobble.couscous.tests.backends.python.PythonMethodRunner;
import org.zwobble.couscous.values.PrimitiveValue;
import java.io.IOException;
+import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
-import static org.junit.Assert.assertEquals;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
-import static org.zwobble.couscous.values.PrimitiveValues.value;
public class JavaToPythonTests extends CompilerTests {
protected PrimitiveValue execProgram(
diff --git a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java
index b9ca169..4366ac4 100644
--- a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java
@@ -57,6 +57,7 @@ public class ValueObjectTests {
{StaticMethodCallNode.class},
{TernaryConditionalNode.class},
{ThisReferenceNode.class},
+ {TypeCoercionNode.class},
{TypeName.class},
{VariableDeclaration.class},
{VariableReferenceNode.class},
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 0e929d8..2e61fe0 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
@@ -1,6 +1,5 @@
package org.zwobble.couscous.tests.frontends.java;
-import org.junit.Ignore;
import org.junit.Test;
import org.zwobble.couscous.ast.*;
import org.zwobble.couscous.frontends.java.JavaReader;
@@ -33,6 +32,7 @@ import static org.zwobble.couscous.ast.ReturnNode.returns;
import static org.zwobble.couscous.ast.StaticMethodCallNode.*;
import static org.zwobble.couscous.ast.TernaryConditionalNode.ternaryConditional;
import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference;
+import static org.zwobble.couscous.ast.TypeCoercionNode.typeCoercion;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.ast.WhileNode.whileLoop;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
@@ -40,7 +40,7 @@ import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
public class JavaReaderTests {
@Test
public void canReadLiterals() {
- assertEquals(literal("hello"), readObjectExpression("\"hello\""));
+ assertEquals(literal("hello"), readExpression("String", "\"hello\""));
assertEquals(literal(true), readBooleanExpression("true"));
assertEquals(literal(false), readBooleanExpression("false"));
assertEquals(literal(42), readIntExpression("42"));
@@ -51,7 +51,7 @@ public class JavaReaderTests {
public void canReadThisReference() {
assertEquals(
thisReference(TypeName.of("com.example.Example")),
- readExpressionInInstanceMethod("this"));
+ readExpressionInInstanceMethod("com.example.Example", "this"));
}
@Test
@@ -153,7 +153,7 @@ public class JavaReaderTests {
public void canReadConstructorCalls() {
assertEquals(
constructorCall(TypeName.of("java.lang.String"), asList(literal("_"))),
- readObjectExpression("new String(\"_\")"));
+ readExpression("String", "new String(\"_\")"));
}
@Test
@@ -207,10 +207,11 @@ public class JavaReaderTests {
@Test
public void argumentIsBoxedIfNecessary() {
- StaticMethodCallNode expression = (StaticMethodCallNode) readObjectExpression(
+ StaticMethodCallNode expression = (StaticMethodCallNode) readExpression(
+ "String",
"java.util.Objects.toString(42)");
assertEquals(
- boxInt(literal(42)),
+ typeCoercion(literal(42), ObjectValues.OBJECT),
expression.getArguments().get(0));
}
@@ -286,14 +287,14 @@ public class JavaReaderTests {
public void equalityOperatorDoesNotUnboxIfBothOperandsAreBoxed() {
assertEquals(
same(
- constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))),
- constructorCall(TypeName.of("java.lang.Integer"), asList(literal(2)))),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(1))), ObjectValues.OBJECT),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(2))), ObjectValues.OBJECT)),
readBooleanExpression("new Integer(1) == new Integer(2)"));
assertEquals(
not(same(
- constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))),
- constructorCall(TypeName.of("java.lang.Integer"), asList(literal(2))))),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(1))), ObjectValues.OBJECT),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(2))), ObjectValues.OBJECT))),
readBooleanExpression("new Integer(1) != new Integer(2)"));
}
@@ -333,7 +334,7 @@ public class JavaReaderTests {
}
@Test
- public void valuesOfAssignmentsAreBoxedIfNecessary() {
+ public void valuesOfAssignmentsAreTypeCoercedIfNecessary() {
ClassNode classNode = readClass(
"private Object value;" +
"public Object getValue() {" +
@@ -347,7 +348,7 @@ public class JavaReaderTests {
thisReference(TypeName.of("com.example.Example")),
"value",
ObjectValues.OBJECT),
- boxInt(literal(4))),
+ typeCoercion(literal(4), ObjectValues.OBJECT)),
returnNode.getValue());
}
@@ -366,45 +367,11 @@ public class JavaReaderTests {
}
@Test
- public void initialValueOfLocalVariableDeclarationsAreBoxedIfNecessary() {
+ public void initialValueOfLocalVariableDeclarationsAreTypeCoercedIfNecessary() {
List<StatementNode> statements = readStatements("Object x = 4;");
LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0);
- assertEquals(boxInt(literal(4)), declaration.getInitialValue());
- }
-
- @Test
- public void integersAreBoxedWhenTargetTypeIsObject() {
- List<StatementNode> statements = readStatements("Object x = 4;");
-
- LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0);
- assertEquals(boxInt(literal(4)), declaration.getInitialValue());
- }
-
- @Test
- public void booleansAreBoxedWhenTargetTypeIsObject() {
- List<StatementNode> statements = readStatements("Object x = true;");
-
- LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0);
- assertEquals(boxBoolean(literal(true)), declaration.getInitialValue());
- }
-
- @Test
- public void integersAreUnboxedIfTargetTypeIsPrimitive() {
- List<StatementNode> statements = readStatements("Integer x = 4; int y = x;");
-
- LocalVariableDeclarationNode boxedDeclaration = (LocalVariableDeclarationNode) statements.get(0);
- LocalVariableDeclarationNode unboxedDeclaration = (LocalVariableDeclarationNode) statements.get(1);
- assertEquals(unboxInt(reference(boxedDeclaration)), unboxedDeclaration.getInitialValue());
- }
-
- @Test
- public void booleansAreUnboxedIfTargetTypeIsPrimitive() {
- List<StatementNode> statements = readStatements("Boolean x = false; boolean y = x;");
-
- LocalVariableDeclarationNode boxedDeclaration = (LocalVariableDeclarationNode) statements.get(0);
- LocalVariableDeclarationNode unboxedDeclaration = (LocalVariableDeclarationNode) statements.get(1);
- assertEquals(StaticMethodCallNode.unboxBoolean(reference(boxedDeclaration)), unboxedDeclaration.getInitialValue());
+ assertEquals(typeCoercion(literal(4), ObjectValues.OBJECT), declaration.getInitialValue());
}
@Test
@@ -621,9 +588,9 @@ public class JavaReaderTests {
method.getBody());
}
- private ExpressionNode readExpressionInInstanceMethod(String expressionSource) {
+ private ExpressionNode readExpressionInInstanceMethod(String returnType, String expressionSource) {
String javaClass =
- "public Object main() {" +
+ "public " + returnType + " main() {" +
" return " + expressionSource + ";" +
"}";
@@ -632,10 +599,6 @@ public class JavaReaderTests {
return returnStatement.getValue();
}
- private ExpressionNode readObjectExpression(String expressionSource) {
- return readExpression("Object", expressionSource);
- }
-
private ExpressionNode readBooleanExpression(String expressionSource) {
return readExpression("boolean", expressionSource);
}