aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/couscous/tests/backends/python
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-25 19:18:09 +0000
committerMichael Williamson <mike@zwobble.org>2015-10-25 19:34:44 +0000
commit368f2dd010defaf5f4b6deb70f45285cee8f244d (patch)
tree55494f85341f23dd1e588fa070ef863ef7cfdef5 /src/test/java/org/zwobble/couscous/tests/backends/python
parent060ae04a695df6abac7445df3c5394b59da40f41 (diff)
Remove lombok
Diffstat (limited to 'src/test/java/org/zwobble/couscous/tests/backends/python')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java22
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java65
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java77
3 files changed, 59 insertions, 105 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java
index c273638..c5c7aab 100644
--- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java
@@ -1,39 +1,31 @@
package org.zwobble.couscous.tests.backends.python;
import java.util.List;
-
import org.zwobble.couscous.ast.ClassNode;
import org.zwobble.couscous.ast.ExpressionNode;
import org.zwobble.couscous.ast.MethodNode;
import org.zwobble.couscous.ast.ReturnNode;
import org.zwobble.couscous.tests.BackendEvalTests;
import org.zwobble.couscous.values.PrimitiveValue;
-
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
-
import static java.util.Arrays.asList;
-import lombok.val;
-
public class PythonCompilerEvalTests extends BackendEvalTests {
@Override
- protected PrimitiveValue evalExpression(
- List<ClassNode> classes,
- ExpressionNode expression) {
-
- val programNode = ClassNode.builder("Program")
+ protected PrimitiveValue evalExpression(List<ClassNode> classes, ExpressionNode expression) {
+ ClassNode programNode = ClassNode.builder("Program")
.method(MethodNode.staticMethod("run")
.statement(ReturnNode.returns(expression))
.build())
.build();
-
- val runner = new PythonMethodRunner();
-
+ PythonMethodRunner runner = new PythonMethodRunner();
return runner.runMethod(
- ImmutableList.copyOf(Iterables.concat(classes, asList(programNode))),
+ ImmutableList.copyOf(Iterables.concat(
+ classes,
+ asList(programNode))),
programNode.getName(),
"run",
ImmutableList.of());
}
-}
+} \ No newline at end of file
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java
index 50f0a0a..1fc0d03 100644
--- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java
+++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java
@@ -4,8 +4,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
-
import org.zwobble.couscous.ast.ClassNode;
import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.backends.python.PythonCodeGenerator;
@@ -14,59 +14,44 @@ import org.zwobble.couscous.backends.python.PythonSerializer;
import org.zwobble.couscous.tests.MethodRunner;
import org.zwobble.couscous.values.PrimitiveValue;
import org.zwobble.couscous.values.PrimitiveValues;
-
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.CharStreams;
-
import static java.lang.Integer.parseInt;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
import static org.zwobble.couscous.values.PrimitiveValues.value;
-import lombok.SneakyThrows;
-import lombok.val;
-
public class PythonMethodRunner implements MethodRunner {
@Override
- @SneakyThrows
- public PrimitiveValue runMethod(
- List<ClassNode> classNodes,
- TypeName className,
- String methodName,
- List<PrimitiveValue> arguments) {
- val directoryPath = Files.createTempDirectory(null);
- val compiler = new PythonCompiler(directoryPath, "couscous");
+ public PrimitiveValue runMethod(List<ClassNode> classNodes, TypeName className, String methodName, List<PrimitiveValue> arguments) {
try {
- compiler.compile(classNodes);
-
- val argumentsString = Joiner.on(", ").join(arguments.stream()
- .map(PythonCodeGenerator::generateCode)
- .map(PythonSerializer::serialize)
- .iterator());
-
- val program = "from couscous." + className.getQualifiedName() + " import " + className.getSimpleName() +
- ";print(repr(" + className.getSimpleName() + "." + methodName + "(" + argumentsString + ")))";
-
- val process = new ProcessBuilder("python3.4", "-c", program)
- .directory(directoryPath.toFile())
- .start();
- val exitCode = process.waitFor();
- val output = readString(process.getInputStream()).trim();
- val stderrOutput = readString(process.getErrorStream());
- if (exitCode != 0) {
- throw new RuntimeException("stderr was: " + stderrOutput);
- } else {
- return readPrimitive(output);
+ Path directoryPath = Files.createTempDirectory(null);
+ PythonCompiler compiler = new PythonCompiler(directoryPath, "couscous");
+ try {
+ compiler.compile(classNodes);
+ String argumentsString = Joiner.on(", ").join(arguments.stream().map(PythonCodeGenerator::generateCode).map(PythonSerializer::serialize).iterator());
+ String program = "from couscous." + className.getQualifiedName() + " import " + className.getSimpleName() + ";print(repr(" + className.getSimpleName() + "." + methodName + "(" + argumentsString + ")))";
+ Process process = new ProcessBuilder("python3.4", "-c", program).directory(directoryPath.toFile()).start();
+ int exitCode = process.waitFor();
+ String output = readString(process.getInputStream()).trim();
+ String stderrOutput = readString(process.getErrorStream());
+ if (exitCode != 0) {
+ throw new RuntimeException("stderr was: " + stderrOutput);
+ } else {
+ return readPrimitive(output);
+ }
+ } finally {
+ deleteRecursively(directoryPath.toFile());
}
- } finally {
- deleteRecursively(directoryPath.toFile());
+ } catch (final java.lang.Throwable $ex) {
+ throw new RuntimeException($ex);
}
}
-
+
private PrimitiveValue readPrimitive(String output) {
if (output.equals("None")) {
return PrimitiveValues.UNIT;
- } else if (output.startsWith("'")) {
+ } else if (output.startsWith("\'")) {
return value(output.substring(1, output.length() - 1));
} else if (output.equals("True")) {
return value(true);
@@ -76,8 +61,8 @@ public class PythonMethodRunner implements MethodRunner {
return value(parseInt(output));
}
}
-
+
private String readString(final InputStream stream) throws IOException {
return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
}
-}
+} \ No newline at end of file
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 46e0aa1..24b2e9a 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
@@ -3,6 +3,7 @@ package org.zwobble.couscous.tests.backends.python;
import org.junit.Test;
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;
@@ -22,19 +23,17 @@ import static org.zwobble.couscous.backends.python.ast.PythonReturnNode.pythonRe
import static org.zwobble.couscous.backends.python.ast.PythonStringLiteralNode.pythonStringLiteral;
import static org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode.pythonVariableReference;
-import lombok.val;
-
public class PythonSerializerTests {
@Test
public void integersAreNotBoxed() {
- val output = serialize(pythonIntegerLiteral(42));
+ String output = serialize(pythonIntegerLiteral(42));
assertEquals("42", output);
}
@Test
public void stringsAreNotBoxed() {
// TODO: escaping
- val output = serialize(pythonStringLiteral("blah"));
+ String output = serialize(pythonStringLiteral("blah"));
assertEquals("\"blah\"", output);
}
@@ -51,104 +50,82 @@ public class PythonSerializerTests {
@Test
public void conditionalExpressionIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(1) if (True) else (2)", serialize(
- pythonConditionalExpression(
- pythonBooleanLiteral(true),
- pythonIntegerLiteral(1),
- pythonIntegerLiteral(2))));
+ 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")));
+ assertEquals("(x).y", serialize(pythonAttributeAccess(pythonVariableReference("x"), "y")));
}
@Test
public void callIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(f)(x, y)", serialize(
- pythonCall(
- pythonVariableReference("f"),
- asList(
- pythonVariableReference("x"),
- pythonVariableReference("y")))));
+ assertEquals("(f)(x, y)", serialize(pythonCall(pythonVariableReference("f"), asList(pythonVariableReference("x"), pythonVariableReference("y")))));
}
@Test
public void getSliceIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(x)[y:z]", serialize(
- pythonGetSlice(
- pythonVariableReference("x"),
- asList(
- pythonVariableReference("y"),
- pythonVariableReference("z")))));
+ assertEquals("(x)[y:z]", serialize(pythonGetSlice(pythonVariableReference("x"), asList(pythonVariableReference("y"), pythonVariableReference("z")))));
}
@Test
public void returnKeywordIsUsedForReturns() {
- val output = serialize(pythonReturn(pythonIntegerLiteral(42)));
+ String output = serialize(pythonReturn(pythonIntegerLiteral(42)));
assertEquals("return 42\n", output);
}
@Test
public void passStatementIsSerializedAsPassKeyword() {
- val output = serialize(PASS);
+ final java.lang.Object output = serialize(PASS);
assertEquals("pass\n", output);
}
@Test
public void emptyFunctionHasPassStatement() {
- val function = PythonFunctionDefinitionNode.builder("empty").build();
- val output = serialize(function);
+ PythonFunctionDefinitionNode function = PythonFunctionDefinitionNode.builder("empty").build();
+ String output = serialize(function);
assertEquals("def empty():\n pass\n", output);
}
@Test
public void functionArgumentsAreSerializedWithName() {
- val function = PythonFunctionDefinitionNode.builder("empty")
+ PythonFunctionDefinitionNode function = PythonFunctionDefinitionNode.builder("empty")
.argument("one")
.argument("two")
.build();
- val output = serialize(function);
+ String output = serialize(function);
assertEquals("def empty(one, two):\n pass\n", output);
}
@Test
public void emptyClassHasPassStatement() {
- val classNode = PythonClassNode.builder("Empty")
- .build();
- val output = serialize(classNode);
+ PythonClassNode classNode = PythonClassNode.builder("Empty").build();
+ String output = serialize(classNode);
assertEquals("class Empty(object):\n pass\n", output);
}
@Test
public void bodiesOfBlocksAreIndented() {
- val classNode = PythonClassNode.builder("Foo")
+ PythonClassNode classNode = PythonClassNode.builder("Foo")
.statement(PythonFunctionDefinitionNode.builder("one").build())
.statement(PythonFunctionDefinitionNode.builder("two").build())
.build();
- val output = serialize(classNode);
- val expectedOutput =
- "class Foo(object):\n" +
- " def one():\n" +
- " pass\n" +
- " def two():\n" +
- " pass\n";
+ String output = serialize(classNode);
+ String expectedOutput = "class Foo(object):\n def one():\n pass\n def two():\n pass\n";
assertEquals(expectedOutput, output);
}
@Test
public void assignmentIsSerializedWithEqualsSymbolSeparatingTargetAndValue() {
- val output = serialize(pythonAssignment(
- pythonVariableReference("x"), pythonIntegerLiteral(42)));
+ String output = serialize(pythonAssignment(
+ pythonVariableReference("x"),
+ pythonIntegerLiteral(42)));
assertEquals("x = 42\n", output);
}
@Test
public void importIsSerializedUsingImportKeyword() {
- val output = serialize(pythonImport(
+ String output = serialize(pythonImport(
"com.example",
asList(pythonImportAlias("Program"))));
assertEquals("from com.example import Program\n", output);
@@ -156,16 +133,16 @@ public class PythonSerializerTests {
@Test
public void importAliasesAreSeparatedByCommas() {
- val output = serialize(pythonImport(
+ String output = serialize(pythonImport(
"com.example",
- asList(pythonImportAlias("Program"), pythonImportAlias("Runner"))));
+ asList(pythonImportAlias("Program"),pythonImportAlias("Runner"))));
assertEquals("from com.example import Program, Runner\n", output);
}
@Test
public void moduleIsSerializedStatements() {
- val classNode = pythonModule(asList(PASS));
- val output = serialize(classNode);
+ PythonModuleNode classNode = pythonModule(asList(PASS));
+ String output = serialize(classNode);
assertEquals("pass\n", output);
}
-}
+} \ No newline at end of file