aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-21 10:40:28 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-21 10:40:28 +0000
commit2dba29dfa74c32a3102a9317455e16cd9a5a35a7 (patch)
tree6cee1e7886c35305ae24df5ba00c759dfe828d01 /src/test/java
parentf570b54bd3e0fe6ebc134101e3c54be5bc49253d (diff)
Compile methods into free functions
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java44
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java18
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/Shid.java1
3 files changed, 52 insertions, 11 deletions
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
index 1e6a1000..e760bdae 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
@@ -7,6 +7,7 @@ import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;
import java.util.List;
+import java.util.Optional;
import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
@@ -115,4 +116,47 @@ public class ShidCodeGenNamespaceTests {
isShidNamespaceFieldStore(namespaceName, "None")
)));
}
+
+ @Test
+ public void structMethodsAreExported() {
+ var namespaceName = new NamespaceName(List.of("Example", "Project"));
+ var structType = Types.struct(namespaceName, "User");
+ var node = Typed.namespace(
+ namespaceName,
+ List.of(
+ TypedStructDefinitionNode.builder()
+ .withType(structType)
+ .addMethod(
+ TypedFunctionDefinitionNode.builder()
+ .withName("name")
+ .addPositionalParam("this", Optional.empty())
+ .build()
+ )
+ .build()
+ ),
+ List.of(BoundVariable.val("User")),
+ List.of()
+ );
+
+ var shidNamespace = ShidCodeGen.compileNamespace(node);
+
+ assertThat(shidNamespace.body(), isShidBlock(isSequence(
+ instanceOf(ShidNamespaceImport.class),
+ instanceOf(ShidFieldLoad.class),
+ instanceOf(ShidLocalStore.class),
+
+ instanceOf(
+ ShidDefineStruct.class,
+ has("type", x -> x.type(), equalTo(structType))
+ ),
+ isShidLocalStore("User__0"),
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("$methods__Example_Project__User__name__0"))
+ ),
+ isShidLocalStore("$methods__Example_Project__User__name__0"),
+ isShidLocalLoad("$methods__Example_Project__User__name__0"),
+ isShidNamespaceFieldStore(namespaceName, "$methods__Example_Project__User__name__0")
+ )));
+ }
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java
index 7db25429..0ffea260 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java
@@ -61,12 +61,12 @@ public class ShidCodeGenStructDefinitionTests {
var context = ShidCodeGen.Context.EMPTY
.addShedVal("User");
- var result = ShidCodeGen.compileNamespaceStatement(node, context);
+ var result = ShidCodeGen.compileStructDefinitionMethods(node, context);
assertThat(result.instructions(), isSequence(
instanceOf(
ShidDefineFunction.class,
- has("name", x -> x.name(), equalTo("User__name__0")),
+ has("name", x -> x.name(), equalTo("$methods__Example_Project__User__name__0")),
has("positionalParams", x -> x.positionalParams(), isSequence(
isShidDefineFunctionParam("this__0"),
isShidDefineFunctionParam("a__0")
@@ -80,14 +80,12 @@ public class ShidCodeGenStructDefinitionTests {
isShidReturn()
)))
),
- instanceOf(
- ShidDefineStruct.class,
- has("type", x -> x.type(), equalTo(structType)),
- has("methods", x -> x.methods(), isSequence(
- equalTo(new ShidDefineStruct.Method("name", "User__name__0"))
- ))
- ),
- isShidLocalStore("User__0")
+ isShidLocalStore("$methods__Example_Project__User__name__0"),
+ isShidLocalLoad("$methods__Example_Project__User__name__0"),
+ isShidNamespaceFieldStore(
+ NamespaceName.of("Example", "Project"),
+ "$methods__Example_Project__User__name__0"
+ )
));
}
}
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
index 1a547eea..ee78a3e3 100644
--- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
@@ -35,7 +35,6 @@ public class Shid {
return new ShidDefineStruct(
type,
fields,
- List.of(),
Sources.NULL
);
}