From 2e7d1bb669e3ee26efe060ca5ea92ef73cebff83 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 6 Feb 2016 17:38:26 +0000 Subject: Add return type to MethodSignature --- .../org/zwobble/couscous/tests/CompilerTests.java | 74 +++++++++++++--------- 1 file changed, 43 insertions(+), 31 deletions(-) (limited to 'src/test/java/org/zwobble/couscous/tests/CompilerTests.java') diff --git a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java index 5b63a66..f03f6ce 100644 --- a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java @@ -2,7 +2,10 @@ package org.zwobble.couscous.tests; import org.junit.Test; import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.values.BooleanValue; +import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.PrimitiveValue; +import org.zwobble.couscous.values.StringValue; import java.io.File; import java.io.IOException; @@ -20,7 +23,7 @@ import static org.zwobble.couscous.values.PrimitiveValues.value; public abstract class CompilerTests { @Test public void canEvaluateLiterals() { - assertEquals(value("hello"), evalExpression("String", "\"hello\"")); + assertEquals(value("hello"), evalExpression(StringValue.REF, "\"hello\"")); assertEquals(value(42), evalIntExpression("42")); } @@ -38,13 +41,13 @@ public abstract class CompilerTests { @Test public void equalityOnReferenceTypesChecksForIdentity() { assertEquals(value(false), evalBooleanExpression("new Object() == new Object()")); - assertEquals(value(true), exec("boolean", "Object x = new Object(); return x == x;")); + assertEquals(value(true), exec(BooleanValue.REF, "Object x = new Object(); return x == x;")); } @Test public void integersCanBeAssignedToObjectVariable() { - assertEquals(value(false), exec("boolean", "Object x = 1; return x.equals(new Object());")); - assertEquals(value(true), exec("boolean", "Object x = 1; return x.equals(1);")); + assertEquals(value(false), exec(BooleanValue.REF, "Object x = 1; return x.equals(new Object());")); + assertEquals(value(true), exec(BooleanValue.REF, "Object x = 1; return x.equals(1);")); } @Test @@ -55,7 +58,8 @@ public abstract class CompilerTests { "recursive-factorial", TypeName.of("com.example.RecursiveFactorial"), "factorial", - list(value(6)))); + list(value(6)), + IntegerValue.REF)); } @Test @@ -66,7 +70,8 @@ public abstract class CompilerTests { "while-factorial", TypeName.of("com.example.WhileFactorial"), "factorial", - list(value(6)))); + list(value(6)), + IntegerValue.REF)); } @Test @@ -77,7 +82,8 @@ public abstract class CompilerTests { "for-factorial", TypeName.of("com.example.ForFactorial"), "factorial", - list(value(6)))); + list(value(6)), + IntegerValue.REF)); } @Test @@ -88,7 +94,8 @@ public abstract class CompilerTests { "anonymous-class", TypeName.of("com.example.AnonymousClass"), "value", - list())); + list(), + IntegerValue.REF)); } @Test @@ -99,7 +106,8 @@ public abstract class CompilerTests { "anonymous-class-capture", TypeName.of("com.example.AnonymousClass"), "value", - list(value(42)))); + list(value(42)), + IntegerValue.REF)); } @Test @@ -110,7 +118,8 @@ public abstract class CompilerTests { "lambda", TypeName.of("com.example.Lambda"), "value", - list())); + list(), + IntegerValue.REF)); } @Test @@ -121,7 +130,8 @@ public abstract class CompilerTests { "lambda-capture", TypeName.of("com.example.Lambda"), "value", - list(value(2)))); + list(value(2)), + IntegerValue.REF)); } @Test @@ -132,7 +142,8 @@ public abstract class CompilerTests { "lambda-this-capture", TypeName.of("com.example.Lambda"), "value", - list(value(2)))); + list(value(2)), + IntegerValue.REF)); } @Test @@ -143,31 +154,33 @@ public abstract class CompilerTests { "static-method-overloads", TypeName.of("com.example.StaticMethodOverloads"), "value", - list())); + list(), + IntegerValue.REF)); } @Test public void stringAdd() throws Exception { - assertEquals(value("42"), evalExpression("String", "\"4\" + \"2\"")); + assertEquals(value("42"), evalExpression(StringValue.REF, "\"4\" + \"2\"")); } @Test public void stringToLowerCase() throws Exception { - assertEquals(value("patrick wolf"), evalExpression("String", "\"Patrick Wolf\".toLowerCase()")); + assertEquals(value("patrick wolf"), evalExpression(StringValue.REF, "\"Patrick Wolf\".toLowerCase()")); } @Test public void stringEquals() throws Exception { - assertEquals(value(true), evalExpression("boolean", "\"a\".equals(\"a\")")); - assertEquals(value(false), evalExpression("boolean", "\"a\".equals(\"b\")")); - assertEquals(value(false), evalExpression("boolean", "\"a\".equals(42)")); + assertEquals(value(true), evalBooleanExpression("\"a\".equals(\"a\")")); + assertEquals(value(false), evalBooleanExpression("\"a\".equals(\"b\")")); + assertEquals(value(false), evalBooleanExpression("\"a\".equals(42)")); } private PrimitiveValue execTestProgram( String directoryName, TypeName type, String methodName, - List arguments) throws IOException, InterruptedException { + List arguments, + TypeName returnType) throws IOException, InterruptedException { Path path = pathForResource( "/java/" + directoryName + "/" + type.getQualifiedName().replace(".", "/") + ".java"); @@ -176,14 +189,16 @@ public abstract class CompilerTests { directoryName(path, type.getQualifiedName().split("\\.").length), type, methodName, - arguments); + arguments, + returnType); } protected abstract PrimitiveValue execProgram( Path directory, TypeName type, String methodName, - List arguments) throws IOException, InterruptedException; + List arguments, + TypeName returnType) throws IOException, InterruptedException; private Path directoryName(Path path, int length) { for (int i = 0; i < length; i++) { @@ -192,12 +207,12 @@ public abstract class CompilerTests { return path; } - private PrimitiveValue exec(String returnType, String source) { + private PrimitiveValue exec(TypeName returnType, String source) { try { String javaClass = "package com.example;" + "public class Example {" + - " public static " + returnType + " main() {" + source + "}" + + " public static " + returnType.getQualifiedName() + " main() {" + source + "}" + "}"; Path directoryPath = Files.createTempDirectory(null); try { @@ -207,7 +222,8 @@ public abstract class CompilerTests { directoryPath, TypeName.of("com.example.Example"), "main", - list()); + list(), + returnType); } finally { deleteRecursively(directoryPath.toFile()); } @@ -217,18 +233,14 @@ public abstract class CompilerTests { } private PrimitiveValue evalBooleanExpression(String expressionSource) { - return evalExpression("boolean", expressionSource); + return evalExpression(BooleanValue.REF, expressionSource); } private PrimitiveValue evalIntExpression(String expressionSource) { - return evalExpression("int", expressionSource); + return evalExpression(IntegerValue.REF, expressionSource); } - private PrimitiveValue evalObjectExpression(String expressionSource) { - return evalExpression("Object", expressionSource); - } - - private PrimitiveValue evalExpression(String type, String expressionSource) { + private PrimitiveValue evalExpression(TypeName type, String expressionSource) { return exec(type, "return " + expressionSource + ";"); } -- cgit v1.2.3