aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-08 11:39:35 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-08 11:39:35 +0000
commit3fcd30f12e7ef31017603f4f954540e6696d77b1 (patch)
tree9724a46a51d646cfb91abfe0141d7614d1ef09b3 /src/test/java
parent2d03ff53b4f1c73bfdb02045d5b632a80f8caeeb (diff)
Compile native type methods from Shed to Shid
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java45
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java2
2 files changed, 45 insertions, 2 deletions
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java
index 6a4070c5..d49d9762 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java
@@ -1,12 +1,15 @@
package org.shedlang.compiler.shid;
import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.TypedNativeFunctionDeclarationNode;
import org.shedlang.compiler.ast.typed.TypedNativeTypeDeclarationNode;
+import org.shedlang.compiler.shid.instructions.ShidDeclareNativeFunction;
import org.shedlang.compiler.shid.instructions.ShidDeclareNativeType;
+import org.shedlang.compiler.types.FunctionType;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;
-import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalStore;
+import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -30,4 +33,44 @@ public class ShidCodeGenNativeTypeDeclarationTests {
isShidLocalStore("Array__0")
));
}
+
+ @Test
+ public void nativeTypeMethodsAreCompiledToFreeFunctionDeclarations() {
+ var nativeType = Types.nativeType(NamespaceName.of("Example", "Project"), "User");
+ var node = TypedNativeTypeDeclarationNode.builder()
+ .withType(nativeType)
+ .addMethod(
+ TypedNativeFunctionDeclarationNode.builder()
+ .withName("name")
+ .withSignature(
+ FunctionType.builder()
+ .addPositionalParam(nativeType)
+ .addPositionalParam(Types.BOOL)
+ .addNamedParam("b", Types.INT)
+ .withReturnType(Types.STRING)
+ .build()
+ )
+ .build()
+ )
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("User");
+
+ var result = ShidCodeGen.compileNativeTypeDeclarationMethods(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDeclareNativeFunction.class,
+ has("name", x -> x.name(), equalTo("method__User__name")),
+ has("positionalParams", x -> x.positionalParams(), isSequence(
+ isShidDefineFunctionParam("arg0"),
+ isShidDefineFunctionParam("arg1")
+ )),
+ has("namedParams", x -> x.namedParams(), isSequence(
+ isShidDefineFunctionParam("b")
+ ))
+ ),
+ isShidMethodStore(nativeType, "name")
+ ));
+ }
}
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java
index 96306fd2..c41858c6 100644
--- a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java
@@ -167,7 +167,7 @@ public class ShidMatchers {
}
public static Matcher<ShidInstruction> isShidMethodStore(
- SimpleStructType type,
+ SimpleConcreteType type,
String methodName
) {
return instanceOf(