aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-14 20:41:16 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-14 20:43:22 +0000
commit4ea4f22e89e38775fb0436d2c34bf5e0cf1ecfb4 (patch)
treeab73a9eccd008c231356e3f773d1a14deaf182ef /src/test/java/org
parent2630c97f8b21c1b9d741eeafd8137c1d2b57c07d (diff)
Remove some unnecessary parens from Python output
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java
index 59ab22c..7ff9cee 100644
--- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java
@@ -5,11 +5,11 @@ import org.zwobble.couscous.backends.python.ast.PythonClassNode;
import org.zwobble.couscous.backends.python.ast.PythonFunctionDefinitionNode;
import org.zwobble.couscous.backends.python.ast.PythonModuleNode;
-import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.zwobble.couscous.backends.python.PythonSerializer.serialize;
import static org.zwobble.couscous.backends.python.ast.PythonAssignmentNode.pythonAssignment;
import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess;
+import static org.zwobble.couscous.backends.python.ast.PythonBinaryOperation.pythonAnd;
import static org.zwobble.couscous.backends.python.ast.PythonBinaryOperation.pythonIs;
import static org.zwobble.couscous.backends.python.ast.PythonBooleanLiteralNode.pythonBooleanLiteral;
import static org.zwobble.couscous.backends.python.ast.PythonCallNode.pythonCall;
@@ -59,36 +59,47 @@ public class PythonSerializerTests {
}
@Test
- public void conditionalExpressionIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(1) if (True) else (2)", serialize(pythonConditionalExpression(pythonBooleanLiteral(true), pythonIntegerLiteral(1), pythonIntegerLiteral(2))));
+ public void conditionalExpressionIsSerializedUsingSubExpressions() {
+ assertEquals("1 if True else 2", serialize(pythonConditionalExpression(pythonBooleanLiteral(true), pythonIntegerLiteral(1), pythonIntegerLiteral(2))));
}
@Test
- public void attributeAccessIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(x).y", serialize(pythonAttributeAccess(pythonVariableReference("x"), "y")));
+ public void attributeAccessIsSerializedUsingSubExpression() {
+ assertEquals("x.y", serialize(pythonAttributeAccess(pythonVariableReference("x"), "y")));
}
@Test
- public void callIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(f)(x, y)", serialize(pythonCall(pythonVariableReference("f"), list(pythonVariableReference("x"), pythonVariableReference("y")))));
+ public void callIsSerializedUsingSubExpression() {
+ assertEquals("f(x, y)", serialize(pythonCall(pythonVariableReference("f"), list(pythonVariableReference("x"), pythonVariableReference("y")))));
}
@Test
- public void getSliceIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(x)[y:z]", serialize(pythonGetSlice(pythonVariableReference("x"), list(pythonVariableReference("y"), pythonVariableReference("z")))));
+ public void getSliceIsSerializedUsingSubExpression() {
+ assertEquals("x[y:z]", serialize(pythonGetSlice(pythonVariableReference("x"), list(pythonVariableReference("y"), pythonVariableReference("z")))));
}
@Test
- public void notOperationIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("not (x)", serialize(pythonNot(pythonVariableReference("x"))));
+ public void notOperationIsSerializedUsingSubExpression() {
+ assertEquals("not x", serialize(pythonNot(pythonVariableReference("x"))));
}
@Test
- public void binaryOperationIsSerializedUsingParenthesisedSubExpressions() {
+ public void binaryOperationIsSerializedUsingSubExpressions() {
assertEquals(
- "(x) is (y)",
+ "x is y",
serialize(pythonIs(pythonVariableReference("x"), pythonVariableReference("y"))));
}
+
+ @Test
+ public void operationsWithSameOrLowerPrecedenceIsParenthesised() {
+ assertEquals(
+ "not (x and y)",
+ serialize(pythonNot(pythonAnd(pythonVariableReference("x"), pythonVariableReference("y")))));
+
+ assertEquals(
+ "not x and y",
+ serialize(pythonAnd(pythonNot(pythonVariableReference("x")), pythonVariableReference("y"))));
+ }
@Test
public void returnKeywordIsUsedForReturns() {