diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-11-07 15:15:21 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-11-07 15:15:21 +0000 |
| commit | 5579d7b84fbfbaecb953d0a7a720eafe521f8bc7 (patch) | |
| tree | 2624d0778580395407084f3fd3d637efb9f9d5bf /src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java | |
| parent | d8e0031c6d73ba960505601d8e4b3c1933e584ea (diff) | |
Add WIP test for recursive factorial program
Diffstat (limited to 'src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java')
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java index b639c6f..e5d810e 100644 --- a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java +++ b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java @@ -1,12 +1,19 @@ package org.zwobble.couscous.tests; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import org.junit.Ignore; import org.junit.Test; +import org.zwobble.couscous.CouscousCompiler; import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.backends.python.PythonCompiler; import org.zwobble.couscous.frontends.java.JavaFrontend; import org.zwobble.couscous.tests.backends.python.PythonMethodRunner; import org.zwobble.couscous.values.PrimitiveValue; @@ -42,6 +49,45 @@ public class JavaToPythonTests { assertEquals(value(false), exec("Object x = 1; return x.equals(new Object());")); } + @Test + @Ignore + public void recursiveFactorial() throws Exception { + assertEquals( + value(120), + execProgram( + "recursive-factorial", + TypeName.of("com.example.RecursiveFactorial"), + "factorial", + asList(value(6)))); + } + + private PrimitiveValue execProgram( + String directory, + TypeName type, + String methodName, + List<PrimitiveValue> arguments) throws IOException, InterruptedException { + + Path path = pathForResource( + "/java/" + directory + "/" + type.getQualifiedName().replace(".", "/") + ".java"); + + Path directoryPath = Files.createTempDirectory(null); + try { + CouscousCompiler compiler = new CouscousCompiler( + new PythonCompiler(directoryPath, "couscous")); + compiler.compileDirectory(directoryName(path, type.getQualifiedName().split(".").length)); + return PythonMethodRunner.runFunction(directoryPath, type, methodName, arguments); + } finally { + deleteRecursively(directoryPath.toFile()); + } + } + + private Path directoryName(Path path, int length) { + for (int i = 0; i < length; i++) { + path = path.getParent(); + } + return path; + } + private PrimitiveValue exec(String source) { try { String javaClass = @@ -66,4 +112,13 @@ public class JavaToPythonTests { private PrimitiveValue evalExpression(String expressionSource) { return exec("return " + expressionSource + ";"); } + + private static Path pathForResource(String name) { + try { + URI uri = JavaToPythonTests.class.getResource(name).toURI(); + return new File(uri).toPath(); + } catch (URISyntaxException exception) { + throw new RuntimeException(exception); + } + } }
\ No newline at end of file |
