From 07d4507eca4c950c65fac53fafa3f81d703167d1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 12 Oct 2015 20:25:52 +0100 Subject: Support reading Java this expressions --- .../tests/frontends/java/JavaReaderTests.java | 42 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'src/test') 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; @@ -30,6 +32,13 @@ public class JavaReaderTests { assertEquals(literal(42), readExpression("42")); } + @Test + public void canReadThisReference() { + assertEquals( + thisReference(TypeName.of("com.example.Example")), + readExpressionInInstanceMethod("this")); + } + @Test public void canReadInstanceMethodCalls() { assertEquals( @@ -58,14 +67,37 @@ public class JavaReaderTests { readExpression("true ? 1 : 2")); } + @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()); } -- cgit v1.2.3