aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-02 19:46:06 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-02 19:46:06 +0000
commit089f1dfd9a3972fe8d399fa5797d75e9c69b5dac (patch)
tree12ef31cafa24169773cc702b0c37583b3bee8d61 /src/test/java
parentd75f1c798a226861297a18da9a5afaa756e1ba91 (diff)
Handle This type for generic native types
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeTypeDeclarationTests.java65
1 files changed, 59 insertions, 6 deletions
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeTypeDeclarationTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeTypeDeclarationTests.java
index 029fc034..be001569 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeTypeDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeTypeDeclarationTests.java
@@ -5,10 +5,7 @@ import org.shedlang.compiler.ast.untyped.Untyped;
import org.shedlang.compiler.ast.untyped.UntypedNativeFunctionDeclarationNode;
import org.shedlang.compiler.ast.untyped.UntypedNativeTypeDeclarationNode;
import org.shedlang.compiler.typechecker.errors.CannotDefineNativeTypeOutsideOfNamespaceError;
-import org.shedlang.compiler.types.FunctionType;
-import org.shedlang.compiler.types.MethodInfo;
-import org.shedlang.compiler.types.NamespaceName;
-import org.shedlang.compiler.types.Types;
+import org.shedlang.compiler.types.*;
import org.shedlang.compiler.util.P;
import java.util.List;
@@ -132,8 +129,8 @@ public class TypeCheckerNativeTypeDeclarationTests {
.withName("ExampleType")
.addMethod(
UntypedNativeFunctionDeclarationNode.builder()
- .addPositionalParam(Untyped.typeLevelReference("This"))
.withName("a")
+ .addPositionalParam(Untyped.typeLevelReference("This"))
.withReturnType(Untyped.typeLevelReference("Unit"))
.build()
)
@@ -179,10 +176,10 @@ public class TypeCheckerNativeTypeDeclarationTests {
.withName("ExampleType")
.addMethod(
UntypedNativeFunctionDeclarationNode.builder()
+ .withName("a")
.addPositionalParam(Untyped.typeLevelReference("This"))
.addPositionalParam(Untyped.typeLevelReference("Bool"))
.addNamedParam("b", Untyped.typeLevelReference("Int"))
- .withName("a")
.withReturnType(Untyped.typeLevelReference("Unit"))
.build()
)
@@ -228,4 +225,60 @@ public class TypeCheckerNativeTypeDeclarationTests {
))
);
}
+
+ @Test
+ public void whenNativeTypeIsGenericThenThisIsConstructedType() {
+ var untyped = UntypedNativeTypeDeclarationNode.builder()
+ .withName("ExampleType")
+ .withTypeLevelParams(List.of(Untyped.typeParam("T")))
+ .addMethod(
+ UntypedNativeFunctionDeclarationNode.builder()
+ .withName("a")
+ .addPositionalParam(Untyped.typeLevelReference("This"))
+ .withReturnType(Untyped.typeLevelReference("Unit"))
+ .build()
+ )
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .enterGlobal(P.map(
+ Map.entry("Unit", Types.metaType(Types.UNIT))
+ ));
+
+ var typed = typeCheckNamespaceStatement(untyped, context);
+
+ assertThat(
+ typed.value(),
+ isTypedNativeTypeDeclarationNode()
+ .withMethods(isSequence(
+ isTypedNativeFunctionDeclarationNode()
+ .withSignature(
+ FunctionType.builder()
+ .addPositionalParam(Types.constructedType(
+ Types.typeConstructor(
+ List.of(
+ TypeParam.forNativeType(Types.nativeType(NAMESPACE_NAME, "ExampleType"), "T")
+ ),
+ Types.nativeType(NAMESPACE_NAME, "ExampleType")
+ ),
+ List.of(TypeParam.forNativeType(Types.nativeType(NAMESPACE_NAME, "ExampleType"), "T"))
+ ))
+ .build()
+ )
+ ))
+ );
+ var nativeTypeInfo = typed.context().toTypeInfos()
+ .nativeTypeInfo(Types.nativeType(NAMESPACE_NAME, "ExampleType"))
+ .orElseThrow();
+ assertThat(
+ nativeTypeInfo,
+ has("methods", x -> x.methods(), isSequence(
+ equalTo(new MethodInfo(
+ "a",
+ FunctionType.builder()
+ .withReturnType(Types.UNIT)
+ .build()
+ ))
+ ))
+ );
+ }
}