diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-31 18:56:24 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-31 18:56:24 +0000 |
| commit | d8e0031c6d73ba960505601d8e4b3c1933e584ea (patch) | |
| tree | c2456a223423d297812ce40089c363b5f96dfabf /src/test | |
| parent | 493d00f3ccf6af2d4ccb4421a82c143d96945178 (diff) | |
Support reading of unboxing
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java | 38 |
1 files changed, 33 insertions, 5 deletions
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 cc95f0f..b8309b3 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 @@ -35,9 +35,11 @@ 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.boxBoolean; 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.StaticMethodCallNode.unboxInt; import static org.zwobble.couscous.ast.TernaryConditionalNode.ternaryConditional; import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference; import static org.zwobble.couscous.ast.VariableReferenceNode.reference; @@ -250,7 +252,7 @@ public class JavaReaderTests { } @Test - public void integersAreBoxedIfAssignedToObjectVariable() { + public void valuesOfAssignmentsAreBoxedIfNecessary() { ClassNode classNode = readClass( "private Object value;" + "public Object getValue() {" + @@ -283,19 +285,45 @@ public class JavaReaderTests { } @Test - public void integersAreBoxedIfDeclaredAsObjectVariable() { + public void initialValueOfLocalVariableDeclarationsAreBoxedIfNecessary() { List<StatementNode> statements = readStatements("Object x = 4;"); LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0); - assertEquals(StaticMethodCallNode.boxInt(literal(4)), declaration.getInitialValue()); + assertEquals(boxInt(literal(4)), declaration.getInitialValue()); } @Test - public void booleansAreBoxedIfDeclaredAsObjectVariable() { + 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(StaticMethodCallNode.boxBoolean(literal(true)), declaration.getInitialValue()); + 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()); } @Test |
