From 63956680fa5f6e61210e090036c797d94e4a340c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 15 Nov 2015 13:40:21 +0000 Subject: Handle boxing in infix expressions more accurately --- .../tests/frontends/java/JavaReaderTests.java | 51 +++++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'src/test/java') 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( -- cgit v1.2.3