diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
| commit | 20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 (patch) | |
| tree | 47914e8b0356b2f40e3723eae650d3bb6f100dd4 /src/test/java | |
| parent | 8ac6cb16b727b5e3d28ab89fee1d169a4f74d9a1 (diff) | |
Introduce Type interface
Diffstat (limited to 'src/test/java')
14 files changed, 139 insertions, 115 deletions
diff --git a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java index 2544c4a..10c550b 100644 --- a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java +++ b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java @@ -2,6 +2,7 @@ package org.zwobble.couscous.tests; import org.junit.Test; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.PrimitiveValue; import org.zwobble.couscous.values.StringValue; @@ -42,8 +43,8 @@ public abstract class BackendEvalTests { assertEquals( value(false), evalExpression(same( - constructorCall(TypeName.of("java.lang.Object"), Collections.emptyList()), - constructorCall(TypeName.of("java.lang.Object"), Collections.emptyList())))); + constructorCall(ScalarType.of("java.lang.Object"), Collections.emptyList()), + constructorCall(ScalarType.of("java.lang.Object"), Collections.emptyList())))); } @Test @@ -228,7 +229,7 @@ public abstract class BackendEvalTests { @Test public void staticConstructorIsExecutedOnReference() { - TypeName type = TypeName.of("com.example.Example"); + ScalarType type = ScalarType.of("com.example.Example"); ClassNode classNode = ClassNode.builder(type) .staticField("value", IntegerValue.REF) .staticConstructor(list( diff --git a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java index 059130c..629f288 100644 --- a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java @@ -1,7 +1,7 @@ package org.zwobble.couscous.tests; import org.junit.Test; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.PrimitiveValue; @@ -56,7 +56,7 @@ public abstract class CompilerTests { value(720), execTestProgram( "recursive-factorial", - TypeName.of("com.example.RecursiveFactorial"), + ScalarType.of("com.example.RecursiveFactorial"), "factorial", list(value(6)), IntegerValue.REF)); @@ -68,7 +68,7 @@ public abstract class CompilerTests { value(720), execTestProgram( "while-factorial", - TypeName.of("com.example.WhileFactorial"), + ScalarType.of("com.example.WhileFactorial"), "factorial", list(value(6)), IntegerValue.REF)); @@ -80,7 +80,7 @@ public abstract class CompilerTests { value(720), execTestProgram( "for-factorial", - TypeName.of("com.example.ForFactorial"), + ScalarType.of("com.example.ForFactorial"), "factorial", list(value(6)), IntegerValue.REF)); @@ -92,7 +92,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "static-nested-class", - TypeName.of("com.example.StaticNestedClass"), + ScalarType.of("com.example.StaticNestedClass"), "value", list(), IntegerValue.REF)); @@ -104,7 +104,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "inner-class", - TypeName.of("com.example.InnerClass"), + ScalarType.of("com.example.InnerClass"), "run", list(), IntegerValue.REF)); @@ -116,7 +116,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "inner-class-with-constructor-args", - TypeName.of("com.example.InnerClass"), + ScalarType.of("com.example.InnerClass"), "run", list(), IntegerValue.REF)); @@ -128,7 +128,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "implement-user-interface", - TypeName.of("com.example.ConstantIntSupplier"), + ScalarType.of("com.example.ConstantIntSupplier"), "value", list(), IntegerValue.REF)); @@ -140,7 +140,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "anonymous-class", - TypeName.of("com.example.AnonymousClass"), + ScalarType.of("com.example.AnonymousClass"), "value", list(), IntegerValue.REF)); @@ -152,7 +152,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "anonymous-class-capture", - TypeName.of("com.example.AnonymousClass"), + ScalarType.of("com.example.AnonymousClass"), "value", list(value(42)), IntegerValue.REF)); @@ -164,7 +164,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "lambda", - TypeName.of("com.example.Lambda"), + ScalarType.of("com.example.Lambda"), "value", list(), IntegerValue.REF)); @@ -176,7 +176,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "lambda-capture", - TypeName.of("com.example.Lambda"), + ScalarType.of("com.example.Lambda"), "value", list(value(2)), IntegerValue.REF)); @@ -188,7 +188,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "lambda-this-capture", - TypeName.of("com.example.Lambda"), + ScalarType.of("com.example.Lambda"), "value", list(value(2)), IntegerValue.REF)); @@ -200,7 +200,7 @@ public abstract class CompilerTests { value("Hello"), execTestProgram( "generic-interface", - TypeName.of("com.example.GenericInterface"), + ScalarType.of("com.example.GenericInterface"), "value", list(), StringValue.REF)); @@ -212,7 +212,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "static-method-overloads", - TypeName.of("com.example.StaticMethodOverloads"), + ScalarType.of("com.example.StaticMethodOverloads"), "value", list(), IntegerValue.REF)); @@ -237,10 +237,10 @@ public abstract class CompilerTests { private PrimitiveValue execTestProgram( String directoryName, - TypeName type, + ScalarType type, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) throws IOException, InterruptedException { + ScalarType returnType) throws IOException, InterruptedException { Path path = pathForResource( "/java/" + directoryName + "/" + type.getQualifiedName().replace(".", "/") + ".java"); @@ -255,10 +255,10 @@ public abstract class CompilerTests { protected abstract PrimitiveValue execProgram( Path directory, - TypeName type, + ScalarType type, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) throws IOException, InterruptedException; + ScalarType returnType) throws IOException, InterruptedException; private Path directoryName(Path path, int length) { for (int i = 0; i < length; i++) { @@ -267,7 +267,7 @@ public abstract class CompilerTests { return path; } - private PrimitiveValue exec(TypeName returnType, String source) { + private PrimitiveValue exec(ScalarType returnType, String source) { try { String javaClass = "package com.example;" + @@ -280,7 +280,7 @@ public abstract class CompilerTests { Files.write(directoryPath.resolve("com/example/Example.java"), list(javaClass)); return execProgram( directoryPath, - TypeName.of("com.example.Example"), + ScalarType.of("com.example.Example"), "main", list(), returnType); @@ -300,7 +300,7 @@ public abstract class CompilerTests { return evalExpression(IntegerValue.REF, expressionSource); } - private PrimitiveValue evalExpression(TypeName type, String expressionSource) { + private PrimitiveValue evalExpression(ScalarType type, String expressionSource) { return exec(type, "return " + expressionSource + ";"); } diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java index 67ce611..e0cd72e 100644 --- a/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java +++ b/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java @@ -1,7 +1,7 @@ package org.zwobble.couscous.tests; import com.google.common.collect.ImmutableList; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.TypeNode; import org.zwobble.couscous.frontends.java.JavaFrontend; import org.zwobble.couscous.interpreter.Interpreter; @@ -22,10 +22,10 @@ public class JavaToInterpreterTests extends CompilerTests { @Override protected PrimitiveValue execProgram( Path directory, - TypeName type, + ScalarType type, String methodName, List<PrimitiveValue> arguments, - TypeName returnType + ScalarType returnType ) throws IOException, InterruptedException { JavaFrontend frontend = new JavaFrontend(); List<TypeNode> classNodes = frontend.readSourceDirectory(list(directory), directory); diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java index 6b9f6ed..ecc1fc9 100644 --- a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java +++ b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java @@ -1,7 +1,7 @@ package org.zwobble.couscous.tests; import org.zwobble.couscous.CouscousCompiler; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.backends.python.PythonBackend; import org.zwobble.couscous.frontends.java.JavaFrontend; import org.zwobble.couscous.tests.backends.python.PythonMethodRunner; @@ -18,10 +18,10 @@ import static org.zwobble.couscous.util.ExtraLists.list; public class JavaToPythonTests extends CompilerTests { protected PrimitiveValue execProgram( Path directory, - TypeName type, + ScalarType type, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) throws IOException, InterruptedException { + ScalarType returnType) throws IOException, InterruptedException { Path directoryPath = Files.createTempDirectory(null); try { diff --git a/src/test/java/org/zwobble/couscous/tests/MethodRunner.java b/src/test/java/org/zwobble/couscous/tests/MethodRunner.java index 8fde77f..cf7b884 100644 --- a/src/test/java/org/zwobble/couscous/tests/MethodRunner.java +++ b/src/test/java/org/zwobble/couscous/tests/MethodRunner.java @@ -3,6 +3,8 @@ package org.zwobble.couscous.tests; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.values.PrimitiveValue; import java.util.List; @@ -12,9 +14,10 @@ import static org.zwobble.couscous.util.ExtraLists.list; public interface MethodRunner { PrimitiveValue runMethod( List<TypeNode> classNodes, - TypeName className, + ScalarType className, String methodName, - List<PrimitiveValue> arguments, TypeName returnType); + List<PrimitiveValue> arguments, + Type returnType); default PrimitiveValue evalExpression(List<TypeNode> classes, ExpressionNode expression) { ClassNode programNode = ClassNode.builder("Program") diff --git a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java index 8e5d7c8..f0780d5 100644 --- a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java +++ b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java @@ -7,6 +7,8 @@ import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.util.ExtraArrays; import org.zwobble.couscous.values.PrimitiveValue; import org.zwobble.couscous.values.StringValue; @@ -60,11 +62,12 @@ public class ValueObjectTests { {MethodNode.class}, {OperationNode.class}, {ReturnNode.class}, + {ScalarType.class}, {StaticReceiver.class}, {TernaryConditionalNode.class}, {ThisReferenceNode.class}, {TypeCoercionNode.class}, - {TypeName.class}, + {ScalarType.class}, {VariableDeclaration.class}, {VariableReferenceNode.class}, {WhileNode.class} @@ -153,6 +156,8 @@ public class ValueObjectTests { return generateFirstInstance(InstanceReceiver.class); } else if (type.equals(AssignableExpressionNode.class)) { return generateFirstInstance(VariableReferenceNode.class); + } else if (type.equals(Type.class)) { + return generateFirstInstance(ScalarType.class); } else { return generateValue(type, ValueObjectTests::generateFirstInstance).instance; } @@ -181,6 +186,8 @@ public class ValueObjectTests { return generateSecondInstance(InstanceReceiver.class); } else if (type.equals(AssignableExpressionNode.class)) { return generateSecondInstance(VariableReferenceNode.class); + } else if (type.equals(Type.class)) { + return generateSecondInstance(ScalarType.class); } else { return generateValue(type, ValueObjectTests::generateSecondInstance).instance; } @@ -209,6 +216,8 @@ public class ValueObjectTests { return generateInstance(InstanceReceiver.class); } else if (type.equals(AssignableExpressionNode.class)) { return generateInstance(VariableReferenceNode.class); + } else if (type.equals(Type.class)) { + return generateInstance(ScalarType.class); } else { return generateValue(type, ValueObjectTests::generateInstance).instance; } diff --git a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java index 4e05266..a73a5ee 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java @@ -4,7 +4,11 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import org.hamcrest.Matchers; import org.zwobble.couscous.Backend; -import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.LiteralNode; +import org.zwobble.couscous.ast.MethodSignature; +import org.zwobble.couscous.ast.TypeNode; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.backends.Names; import org.zwobble.couscous.backends.csharp.CsharpBackend; import org.zwobble.couscous.backends.csharp.CsharpSerializer; @@ -33,7 +37,7 @@ public class CsharpMethodRunner implements MethodRunner { private static final String NAMESPACE = "Couscous"; @Override - public PrimitiveValue runMethod(List<TypeNode> classNodes, TypeName className, String methodName, List<PrimitiveValue> arguments, TypeName returnType) { + public PrimitiveValue runMethod(List<TypeNode> classNodes, ScalarType className, String methodName, List<PrimitiveValue> arguments, Type returnType) { try { Path directoryPath = Files.createTempDirectory(null); Backend compiler = new CsharpBackend(directoryPath, NAMESPACE); @@ -50,10 +54,10 @@ public class CsharpMethodRunner implements MethodRunner { public static PrimitiveValue runFunction( Path directoryPath, - TypeName className, + ScalarType className, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) + Type returnType) throws IOException, InterruptedException { MethodSignature signature = new MethodSignature( @@ -63,7 +67,7 @@ public class CsharpMethodRunner implements MethodRunner { String csharpMethodName = Names.toUniqueName(signature); String value = CsharpSerializer.serialize( staticMethodCall( - TypeName.of(NAMESPACE + "." + className.getQualifiedName()), + ScalarType.of(NAMESPACE + "." + className.getQualifiedName()), csharpMethodName, eagerMap(arguments, LiteralNode::literal), returnType)); @@ -122,7 +126,7 @@ public class CsharpMethodRunner implements MethodRunner { return value(StringValue.REF); } else { assertThat(value, Matchers.startsWith(NAMESPACE_PREFIX)); - return value(TypeName.of(value.substring(NAMESPACE_PREFIX.length()))); + return value(ScalarType.of(value.substring(NAMESPACE_PREFIX.length()))); } default: throw new RuntimeException("Unhandled type: " + type); diff --git a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java index 493ba2b..d0a8c77 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java @@ -2,6 +2,7 @@ package org.zwobble.couscous.tests.backends.csharp; import org.junit.Test; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.backends.csharp.CsharpSerializer; import org.zwobble.couscous.tests.TestIds; import org.zwobble.couscous.values.BooleanValue; @@ -51,19 +52,19 @@ public class CsharpSerializerTests { @Test public void typeLiteralUsesTypeOfOperator() { - String output = serialize(literal(TypeName.of("com.example.Example"))); + String output = serialize(literal(ScalarType.of("com.example.Example"))); assertEquals("typeof(com.example.Example)", output); } @Test public void variableReferenceWritesIdentifier() { - String output = serialize(reference(var(TestIds.ANY_ID, "x", TypeName.of("X")))); + String output = serialize(reference(var(TestIds.ANY_ID, "x", ScalarType.of("X")))); assertEquals("x", output); } @Test public void thisReferenceUsesThisKeyword() { - String output = serialize(thisReference(TypeName.of("X"))); + String output = serialize(thisReference(ScalarType.of("X"))); assertEquals("this", output); } @@ -84,37 +85,37 @@ public class CsharpSerializerTests { @Test public void staticMethodCallWithNoArgumentsWritesStaticReceiver() { String output = serialize(staticMethodCall( - TypeName.of("X"), + ScalarType.of("X"), "y", list(), - TypeName.of("Y"))); + ScalarType.of("Y"))); assertEquals("X.y()", output); } @Test public void methodCallWithNoArgumentsWritesReceiver() { String output = serialize(methodCall( - reference(var(TestIds.ANY_ID, "x", TypeName.of("X"))), + reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), "y", list(), - TypeName.of("Y"))); + ScalarType.of("Y"))); assertEquals("x.y()", output); } @Test public void methodCallWithArguments() { String output = serialize(methodCall( - reference(var(TestIds.ANY_ID, "x", TypeName.of("X"))), + reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), "y", list(literal(1), literal(2)), - TypeName.of("Y"))); + ScalarType.of("Y"))); assertEquals("x.y(1, 2)", output); } @Test public void constructorCallWithNoArguments() { String output = serialize(constructorCall( - TypeName.of("X"), + ScalarType.of("X"), list())); assertEquals("new X()", output); } @@ -122,7 +123,7 @@ public class CsharpSerializerTests { @Test public void constructorCallWithArguments() { String output = serialize(constructorCall( - TypeName.of("X"), + ScalarType.of("X"), list(literal(1), literal(2)))); assertEquals("new X(1, 2)", output); } @@ -143,18 +144,18 @@ public class CsharpSerializerTests { @Test public void fieldAccessSeparatesReceiverAndNameWithDot() { String output = serialize(fieldAccess( - reference(var(TestIds.ANY_ID, "x", TypeName.of("X"))), + reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), "y", - TypeName.of("Y"))); + ScalarType.of("Y"))); assertEquals("x.y", output); } @Test public void staticFieldAccessSeparatesReceiverAndNameWithDot() { String output = serialize(fieldAccess( - TypeName.of("X"), + ScalarType.of("X"), "y", - TypeName.of("Y"))); + ScalarType.of("Y"))); assertEquals("X.y", output); } @@ -181,7 +182,7 @@ public class CsharpSerializerTests { String output = serialize(localVariableDeclaration( TestIds.ANY_ID, "x", - TypeName.of("string"), + ScalarType.of("string"), literal("[value]"))); assertEquals("string x = \"[value]\";\n", output); } @@ -206,7 +207,7 @@ public class CsharpSerializerTests { @Test public void methodCanHaveVoidReturnType() { String output = serialize(MethodNode.staticMethod("nothing") - .returns(TypeName.of("void")) + .returns(ScalarType.of("void")) .build()); assertEquals("public static void nothing() {\n}\n", output); } @@ -214,7 +215,7 @@ public class CsharpSerializerTests { @Test public void instanceMethodHasNoStaticKeword() { String output = serialize(MethodNode.builder("nothing") - .returns(TypeName.of("void")) + .returns(ScalarType.of("void")) .build()); assertEquals("public void nothing() {\n}\n", output); } @@ -222,7 +223,7 @@ public class CsharpSerializerTests { @Test public void methodWithReturnType() { MethodNode methodNode = MethodNode.staticMethod("nothing") - .returns(TypeName.of("X")) + .returns(ScalarType.of("X")) .build(); String output = serialize(methodNode); @@ -233,9 +234,9 @@ public class CsharpSerializerTests { @Test public void methodWithArguments() { MethodNode methodNode = MethodNode.staticMethod("nothing") - .argument(formalArg(var(TestIds.id("x"), "x", TypeName.of("X")))) - .argument(formalArg(var(TestIds.id("y"), "y", TypeName.of("Y")))) - .returns(TypeName.of("void")) + .argument(formalArg(var(TestIds.id("x"), "x", ScalarType.of("X")))) + .argument(formalArg(var(TestIds.id("y"), "y", ScalarType.of("Y")))) + .returns(ScalarType.of("void")) .build(); String output = serialize(methodNode); @@ -246,7 +247,7 @@ public class CsharpSerializerTests { @Test public void methodHasSerializedBody() { MethodNode method = MethodNode.staticMethod("nothing") - .returns(TypeName.of("void")) + .returns(ScalarType.of("void")) .statement(returns(literal(true))) .build(); String output = serialize(method); @@ -301,7 +302,7 @@ public class CsharpSerializerTests { @Test public void constructorHasSerializedBody() { ConstructorNode constructor = ConstructorNode.constructor( - list(formalArg(var(TestIds.ANY_ID, "x", TypeName.of("X")))), + list(formalArg(var(TestIds.ANY_ID, "x", ScalarType.of("X")))), list(expressionStatement(literal(true)))); ClassNode classNode = ClassNode.builder("com.example.Example") .constructor(constructor) @@ -316,8 +317,8 @@ public class CsharpSerializerTests { @Test public void classWithFields() { ClassNode classNode = ClassNode.builder("com.example.Example") - .staticField("x", TypeName.of("X")) - .field("y", TypeName.of("Y")) + .staticField("x", ScalarType.of("X")) + .field("y", ScalarType.of("Y")) .build(); String output = serialize(classNode); @@ -327,7 +328,7 @@ public class CsharpSerializerTests { @Test public void interfaceIsInNamespace() { - Node node = new ClassNodeBuilder(TypeName.of("com.example.Example")).buildInterface(); + Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")).buildInterface(); String output = serialize(node); @@ -336,7 +337,7 @@ public class CsharpSerializerTests { @Test public void interfaceWithTypeParameters() { - Node node = new ClassNodeBuilder(TypeName.of("com.example.Example")) + Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) .addTypeParameter("T") .addTypeParameter("U") .buildInterface(); @@ -348,7 +349,7 @@ public class CsharpSerializerTests { @Test public void interfaceWithSuperTypes() { - Node node = new ClassNodeBuilder(TypeName.of("com.example.Example")) + Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) .addSuperType("com.example.Base") .buildInterface(); @@ -360,7 +361,7 @@ public class CsharpSerializerTests { @Test public void interfaceWithMethod() { MethodNode method = MethodNode.builder("get").isAbstract().returns(IntegerValue.REF).build(); - Node node = new ClassNodeBuilder(TypeName.of("com.example.Example")) + Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) .method(method) .buildInterface(); diff --git a/src/test/java/org/zwobble/couscous/tests/backends/csharp/JavaToCsharpTests.java b/src/test/java/org/zwobble/couscous/tests/backends/csharp/JavaToCsharpTests.java index 4c7e56b..68d270d 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/csharp/JavaToCsharpTests.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/csharp/JavaToCsharpTests.java @@ -3,7 +3,7 @@ package org.zwobble.couscous.tests.backends.csharp; import org.junit.Ignore; import org.junit.Test; import org.zwobble.couscous.CouscousCompiler; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.backends.csharp.CsharpBackend; import org.zwobble.couscous.frontends.java.JavaFrontend; import org.zwobble.couscous.tests.CompilerTests; @@ -25,10 +25,10 @@ public class JavaToCsharpTests extends CompilerTests { protected PrimitiveValue execProgram( Path directory, - TypeName type, + ScalarType type, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) throws IOException, InterruptedException + ScalarType returnType) throws IOException, InterruptedException { Path directoryPath = Files.createTempDirectory(null); try { 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 2b0d2ce..ce91549 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 @@ -3,8 +3,9 @@ package org.zwobble.couscous.tests.backends.python; import com.google.common.base.Joiner; import org.hamcrest.Matchers; import org.zwobble.couscous.ast.MethodSignature; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.TypeNode; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.backends.Names; import org.zwobble.couscous.backends.python.PythonBackend; import org.zwobble.couscous.backends.python.PythonCodeGenerator; @@ -28,7 +29,7 @@ import static org.zwobble.couscous.values.PrimitiveValues.value; public class PythonMethodRunner implements MethodRunner { @Override - public PrimitiveValue runMethod(List<TypeNode> classNodes, TypeName className, String methodName, List<PrimitiveValue> arguments, TypeName returnType) { + public PrimitiveValue runMethod(List<TypeNode> classNodes, ScalarType className, String methodName, List<PrimitiveValue> arguments, Type returnType) { try { Path directoryPath = Files.createTempDirectory(null); PythonBackend compiler = new PythonBackend(directoryPath, "couscous"); @@ -45,10 +46,10 @@ public class PythonMethodRunner implements MethodRunner { public static PrimitiveValue runFunction( Path directoryPath, - TypeName className, + ScalarType className, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) + Type returnType) throws IOException, InterruptedException { String argumentsString = Joiner.on(", ").join(arguments.stream().map(PythonCodeGenerator::generateCode).map(PythonSerializer::serialize).iterator()); @@ -82,7 +83,7 @@ public class PythonMethodRunner implements MethodRunner { case "<class 'type'>": String typeName = value.substring(value.indexOf("'") + 1, value.lastIndexOf("'")); assertThat(typeName, Matchers.startsWith(PACKAGE_PREFIX)); - return value(TypeName.of(typeName.substring(PACKAGE_PREFIX.length(), typeName.lastIndexOf(".")))); + return value(ScalarType.of(typeName.substring(PACKAGE_PREFIX.length(), typeName.lastIndexOf(".")))); default: throw new RuntimeException("Unhandled type: " + type); } diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java index 2f59e57..e73b63b 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java @@ -2,6 +2,7 @@ package org.zwobble.couscous.tests.frontends.java; import org.junit.Test; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; @@ -43,7 +44,7 @@ public class ExpressionReadingTests { @Test public void canReadThisReference() { assertEquals( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), readExpressionInInstanceMethod("com.example.Example", "this")); } @@ -59,7 +60,7 @@ public class ExpressionReadingTests { "private String name;"); assertEquals( - list(field("name", TypeName.of("java.lang.String"))), + list(field("name", ScalarType.of("java.lang.String"))), classNode.getFields()); } @@ -69,7 +70,7 @@ public class ExpressionReadingTests { "private static String name;"); assertEquals( - list(staticField("name", TypeName.of("java.lang.String"))), + list(staticField("name", ScalarType.of("java.lang.String"))), classNode.getFields()); } @@ -93,7 +94,7 @@ public class ExpressionReadingTests { ReturnNode returnNode = (ReturnNode) classNode.getMethods().get(0).getBody().get().get(0); assertEquals( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "name", StringValue.REF), returnNode.getValue()); @@ -119,7 +120,7 @@ public class ExpressionReadingTests { ReturnNode returnNode = (ReturnNode) classNode.getMethods().get(0).getBody().get().get(0); assertEquals( fieldAccess( - TypeName.of("com.example.Example"), + ScalarType.of("com.example.Example"), "name", StringValue.REF), returnNode.getValue()); @@ -149,7 +150,7 @@ public class ExpressionReadingTests { assertEquals( methodCall( - ThisReferenceNode.thisReference(TypeName.of("com.example.Example")), + ThisReferenceNode.thisReference(ScalarType.of("com.example.Example")), "loop", list(), StringValue.REF), @@ -160,7 +161,7 @@ public class ExpressionReadingTests { public void canReadStaticMethodCalls() { assertEquals( staticMethodCall( - TypeName.of("java.lang.Integer"), + ScalarType.of("java.lang.Integer"), "parseInt", list(literal("42")), IntegerValue.REF), @@ -177,7 +178,7 @@ public class ExpressionReadingTests { assertEquals( staticMethodCall( - TypeName.of("com.example.Example"), + ScalarType.of("com.example.Example"), "loop", list(), StringValue.REF), @@ -187,7 +188,7 @@ public class ExpressionReadingTests { @Test public void canReadConstructorCalls() { assertEquals( - constructorCall(TypeName.of("java.lang.String"), list(literal("_"))), + constructorCall(ScalarType.of("java.lang.String"), list(literal("_"))), readExpression("String", "new String(\"_\")")); } @@ -205,14 +206,14 @@ public class ExpressionReadingTests { public void canUseOperatorsOnReferences() { assertEquals( same( - constructorCall(TypeName.of("java.lang.Object"), emptyList()), - constructorCall(TypeName.of("java.lang.Object"), emptyList())), + constructorCall(ScalarType.of("java.lang.Object"), emptyList()), + constructorCall(ScalarType.of("java.lang.Object"), emptyList())), readBooleanExpression("new Object() == new Object()")); assertEquals( not(same( - constructorCall(TypeName.of("java.lang.Object"), emptyList()), - constructorCall(TypeName.of("java.lang.Object"), emptyList()))), + constructorCall(ScalarType.of("java.lang.Object"), emptyList()), + constructorCall(ScalarType.of("java.lang.Object"), emptyList()))), readBooleanExpression("new Object() != new Object()")); } @@ -270,13 +271,13 @@ public class ExpressionReadingTests { assertEquals( equal( literal(1), - unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(1))))), + unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(1))))), readBooleanExpression("1 == new Integer(1)")); assertEquals( notEqual( literal(1), - unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(1))))), + unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(1))))), readBooleanExpression("1 != new Integer(1)")); } @@ -299,8 +300,8 @@ public class ExpressionReadingTests { public void integerOperatorUnboxesWhenBothOperandsAreBoxed() { assertEquals( integerAdd( - unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(1)))), - unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(2))))), + unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(1)))), + unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(2))))), readIntExpression("new Integer(1) + new Integer(2)")); } @@ -323,7 +324,7 @@ public class ExpressionReadingTests { assertEquals( assign( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "name", StringValue.REF), literal("blah")), @@ -342,7 +343,7 @@ public class ExpressionReadingTests { assertEquals( assign( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "value", ObjectValues.OBJECT), typeCoercion(literal(4), ObjectValues.OBJECT)), 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 4694f85..5745adb 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 @@ -2,6 +2,7 @@ package org.zwobble.couscous.tests.frontends.java; import org.junit.Test; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; import org.zwobble.couscous.values.StringValue; @@ -77,7 +78,7 @@ public class JavaReaderTests { MethodNode method = classNode.getMethods().get(0); FormalArgumentNode argument = method.getArguments().get(0); assertEquals("value", argument.getName()); - assertEquals(TypeName.of("int"), argument.getType()); + assertEquals(ScalarType.of("int"), argument.getType()); assertEquals(1, method.getArguments().size()); ReturnNode returnNode = (ReturnNode)method.getBody().get().get(0); @@ -89,7 +90,7 @@ public class JavaReaderTests { assertEquals( expressionStatement( staticMethodCall( - TypeName.of("java.lang.Integer"), + ScalarType.of("java.lang.Integer"), "parseInt", list(literal("42")), IntegerValue.REF)), @@ -179,7 +180,7 @@ public class JavaReaderTests { assertEquals( list(assignStatement( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "name", StringValue.REF), literal("Flaws"))), @@ -202,13 +203,13 @@ public class JavaReaderTests { list( assignStatement( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "year", IntegerValue.REF), literal(2013)), assignStatement( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "name", StringValue.REF), literal("Flaws"))), @@ -230,13 +231,13 @@ public class JavaReaderTests { list( assignStatement( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "year", IntegerValue.REF), literal(2013)), assignStatement( fieldAccess( - thisReference(TypeName.of("com.example.Example")), + thisReference(ScalarType.of("com.example.Example")), "name", StringValue.REF), literal("Flaws"))), @@ -256,7 +257,7 @@ public class JavaReaderTests { assertEquals( list(assignStatement( fieldAccess( - TypeName.of("com.example.Example"), + ScalarType.of("com.example.Example"), "name", StringValue.REF), literal("Flaws"))), @@ -273,7 +274,7 @@ public class JavaReaderTests { assertEquals( list(assignStatement( fieldAccess( - TypeName.of("com.example.Example"), + ScalarType.of("com.example.Example"), "name", StringValue.REF), literal("Flaws"))), @@ -287,7 +288,7 @@ public class JavaReaderTests { MethodNode method = classNode.getMethods().get(0); assertEquals( - list(annotation(TypeName.of("java.lang.Deprecated"))), + list(annotation(ScalarType.of("java.lang.Deprecated"))), method.getAnnotations()); } @@ -309,7 +310,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); InterfaceNode classNode = (InterfaceNode) classes.get(0); - assertEquals(TypeName.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); assertEquals(list(), classNode.getMethods()); assertEquals(set(), classNode.getSuperTypes()); assertEquals(list(), classNode.getTypeParameters()); @@ -322,7 +323,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); ClassNode classNode = (ClassNode) classes.get(0); - assertEquals(TypeName.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); assertEquals(list(formalTypeParameter("T")), classNode.getTypeParameters()); } @@ -333,7 +334,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); InterfaceNode classNode = (InterfaceNode) classes.get(0); - assertEquals(TypeName.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); assertEquals(list(formalTypeParameter("T")), classNode.getTypeParameters()); } @@ -344,9 +345,9 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); InterfaceNode classNode = (InterfaceNode) classes.get(0); - assertEquals(TypeName.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); assertEquals(list(), classNode.getMethods()); - assertThat(classNode.getSuperTypes(), contains(TypeName.of("java.util.function.IntSupplier"))); + assertThat(classNode.getSuperTypes(), contains(ScalarType.of("java.util.function.IntSupplier"))); } @Test diff --git a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java index 902febd..73576dd 100644 --- a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java +++ b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.tests.interpreter; import com.google.common.collect.ImmutableMap; import org.junit.Test; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.JavaProject; import org.zwobble.couscous.interpreter.MapBackedProject; @@ -171,7 +172,7 @@ public class EvaluatorTests extends BackendEvalTests { @Test public void cannotSetValueOfInstanceFieldAsThoughItIsAStaticField() { - TypeName type = TypeName.of("com.example.Example"); + ScalarType type = ScalarType.of("com.example.Example"); ClassNode classNode = ClassNode.builder(type) .field("value", IntegerValue.REF) .constructor(constructor -> constructor diff --git a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java index c8dd2fb..d9cef51 100644 --- a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java +++ b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.tests.interpreter; import org.junit.Test; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Interpreter; import org.zwobble.couscous.interpreter.JavaProject; import org.zwobble.couscous.interpreter.Project; @@ -106,10 +108,10 @@ public class InterpreterMethodTests extends BackendMethodTests { @Override public PrimitiveValue runMethod( List<TypeNode> classNodes, - TypeName className, + ScalarType className, String methodName, List<PrimitiveValue> arguments, - TypeName returnType) { + Type returnType) { Project project = JavaProject.of(classNodes); Interpreter interpreter = new Interpreter(project); |
