diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-10-12 20:25:52 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-10-12 20:25:52 +0100 |
| commit | 07d4507eca4c950c65fac53fafa3f81d703167d1 (patch) | |
| tree | 9d13e4eccf100acd79c3513d479306ea6345d633 /src/test/java/org/zwobble | |
| parent | cb97484d4694c94946a109fd6d09252767fa1652 (diff) | |
Support reading Java this expressions
Diffstat (limited to 'src/test/java/org/zwobble')
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java | 42 |
1 files changed, 36 insertions, 6 deletions
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 cb2a707..ebfe068 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 @@ -3,6 +3,7 @@ package org.zwobble.couscous.tests.frontends.java; import java.nio.file.Files; import org.junit.Test; +import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.ExpressionNode; import org.zwobble.couscous.ast.ReturnNode; import org.zwobble.couscous.ast.TernaryConditionalNode; @@ -16,6 +17,7 @@ import static org.zwobble.couscous.ast.ConstructorCallNode.constructorCall; import static org.zwobble.couscous.ast.LiteralNode.literal; import static org.zwobble.couscous.ast.MethodCallNode.methodCall; import static org.zwobble.couscous.ast.StaticMethodCallNode.staticMethodCall; +import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference; import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively; import lombok.SneakyThrows; @@ -31,6 +33,13 @@ public class JavaReaderTests { } @Test + public void canReadThisReference() { + assertEquals( + thisReference(TypeName.of("com.example.Example")), + readExpressionInInstanceMethod("this")); + } + + @Test public void canReadInstanceMethodCalls() { assertEquals( methodCall(literal("hello"), "startsWith", asList(literal("h")), BooleanValue.REF), @@ -59,13 +68,36 @@ public class JavaReaderTests { } @SneakyThrows + private ExpressionNode readExpressionInInstanceMethod(String expressionSource) { + val javaClass = + "public Object main() {" + + " return " + expressionSource + ";" + + "}"; + + val classNode = readClass(javaClass); + val returnStatement = (ReturnNode) classNode.getMethods().get(0).getBody().get(0); + return returnStatement.getValue(); + } + + @SneakyThrows private ExpressionNode readExpression(String expressionSource) { val javaClass = + "public static Object main() {" + + " return " + expressionSource + ";" + + "}"; + + val classNode = readClass(javaClass); + val returnStatement = (ReturnNode) classNode.getMethods().get(0).getBody().get(0); + return returnStatement.getValue(); + } + + @SneakyThrows + private ClassNode readClass(String classBody) { + + val javaClass = "package com.example;" + "public class Example {" + - " public static Object main() {" + - " return " + expressionSource + ";" + - " }" + + classBody + "}"; val directoryPath = Files.createTempDirectory(null); @@ -75,9 +107,7 @@ public class JavaReaderTests { Files.write(sourcePath, asList(javaClass)); val reader = new JavaReader(); - val classNode = reader.readClassFromFile(directoryPath, sourcePath); - val returnStatement = (ReturnNode) classNode.getMethods().get(0).getBody().get(0); - return returnStatement.getValue(); + return reader.readClassFromFile(directoryPath, sourcePath); } finally { deleteRecursively(directoryPath.toFile()); } |
