diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-11-15 13:40:21 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-11-15 13:40:21 +0000 |
| commit | 63956680fa5f6e61210e090036c797d94e4a340c (patch) | |
| tree | 13680a800766d06f079720cedc21b5e24e7a4db5 /src/test/java | |
| parent | b590542b4cb33e456a2d5e5922fbd68c391e681d (diff) | |
Handle boxing in infix expressions more accurately
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java | 51 |
1 files changed, 45 insertions, 6 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 c08035f..ad2f755 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 @@ -2,7 +2,6 @@ package org.zwobble.couscous.tests.frontends.java; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; import java.util.List; import org.junit.Test; @@ -14,6 +13,7 @@ import org.zwobble.couscous.values.ObjectValues; import org.zwobble.couscous.values.StringValue; import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; import static org.junit.Assert.assertEquals; import static org.zwobble.couscous.ast.AnnotationNode.annotation; import static org.zwobble.couscous.ast.AssignmentNode.assign; @@ -168,14 +168,14 @@ public class JavaReaderTests { public void canUseOperatorsOnReferences() { assertEquals( same( - constructorCall(TypeName.of("java.lang.Object"), Collections.emptyList()), - constructorCall(TypeName.of("java.lang.Object"), Collections.emptyList())), + constructorCall(TypeName.of("java.lang.Object"), emptyList()), + constructorCall(TypeName.of("java.lang.Object"), emptyList())), readExpression("new Object() == new Object()")); assertEquals( not(same( - constructorCall(TypeName.of("java.lang.Object"), Collections.emptyList()), - constructorCall(TypeName.of("java.lang.Object"), Collections.emptyList()))), + constructorCall(TypeName.of("java.lang.Object"), emptyList()), + constructorCall(TypeName.of("java.lang.Object"), emptyList()))), readExpression("new Object() != new Object()")); } @@ -216,7 +216,46 @@ public class JavaReaderTests { lessThanOrEqual(literal(1), literal(2)), readExpression("1 <= 2")); } - + + @Test + public void equalityOperatorUnboxesIfExactlyOneOperandIsPrimitive() { + assertEquals( + equal( + literal(1), + unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))))), + readExpression("1 == new Integer(1)")); + + assertEquals( + notEqual( + literal(1), + unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))))), + readExpression("1 != new Integer(1)")); + } + + @Test + public void equalityOperatorDoesNotUnboxIfBothOperandsAreBoxed() { + assertEquals( + same( + constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))), + constructorCall(TypeName.of("java.lang.Integer"), asList(literal(2)))), + readExpression("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))))), + readExpression("new Integer(1) != new Integer(2)")); + } + + @Test + public void integerOperatorUnboxesWhenBothOperandsAreBoxed() { + assertEquals( + integerAdd( + unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1)))), + unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(2))))), + readExpression("new Integer(1) + new Integer(2)")); + } + @Test public void canReadTernaryConditionals() { assertEquals( |
