aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/couscous/tests/backends/python
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-11-07 15:15:21 +0000
committerMichael Williamson <mike@zwobble.org>2015-11-07 15:15:21 +0000
commit5579d7b84fbfbaecb953d0a7a720eafe521f8bc7 (patch)
tree2624d0778580395407084f3fd3d637efb9f9d5bf /src/test/java/org/zwobble/couscous/tests/backends/python
parentd8e0031c6d73ba960505601d8e4b3c1933e584ea (diff)
Add WIP test for recursive factorial program
Diffstat (limited to 'src/test/java/org/zwobble/couscous/tests/backends/python')
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java36
1 files changed, 23 insertions, 13 deletions
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 1fc0d03..d559e09 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
@@ -29,17 +29,7 @@ public class PythonMethodRunner implements MethodRunner {
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);
- }
+ return runFunction(directoryPath, className, methodName, arguments);
} finally {
deleteRecursively(directoryPath.toFile());
}
@@ -47,8 +37,28 @@ public class PythonMethodRunner implements MethodRunner {
throw new RuntimeException($ex);
}
}
+
+ public static PrimitiveValue runFunction(
+ Path directoryPath,
+ TypeName className,
+ String methodName,
+ List<PrimitiveValue> arguments)
+ throws IOException, InterruptedException {
+
+ 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);
+ }
+ }
- private PrimitiveValue readPrimitive(String output) {
+ private static PrimitiveValue readPrimitive(String output) {
if (output.equals("None")) {
return PrimitiveValues.UNIT;
} else if (output.startsWith("\'")) {
@@ -62,7 +72,7 @@ public class PythonMethodRunner implements MethodRunner {
}
}
- private String readString(final InputStream stream) throws IOException {
+ private static String readString(final InputStream stream) throws IOException {
return CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
}
} \ No newline at end of file