summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-06-26 16:25:34 +0100
committerMichael Williamson <mike@zwobble.org>2026-06-26 16:25:34 +0100
commit9c2421d317068021c64a42be9f785e5ec73afd43 (patch)
treee5284a4ffa6a65117c3c84458fe1560ec0a713f5 /src/test/java
parent7e632842ecdc2a3b72b3e5f0b575166c6c5e8b4b (diff)
Treat variant as subtype of sum
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java40
1 files changed, 38 insertions, 2 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java
index 35779fe..e2b43d0 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java
@@ -1,7 +1,12 @@
package org.zwobble.hobgoblin.compiler.typechecker;
import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.types.NamespaceName;
import org.zwobble.hobgoblin.compiler.types.SimpleNativeType;
+import org.zwobble.hobgoblin.compiler.types.StructType;
+import org.zwobble.hobgoblin.compiler.types.SumType;
+
+import java.util.List;
import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerSubtyping.isSubtype;
import static org.zwobble.precisely.AssertThat.assertThat;
@@ -13,7 +18,7 @@ public class TypeCheckerSubtypingTests {
var nativeType1 = SimpleNativeType.builtin("Int32");
var nativeType2 = SimpleNativeType.builtin("Int32");
- var isSubtype = isSubtype(nativeType1, nativeType2);
+ var isSubtype = isSubtype(nativeType1, nativeType2, TypesInfo.EMPTY);
assertThat(isSubtype, equalTo(true));
}
@@ -23,7 +28,38 @@ public class TypeCheckerSubtypingTests {
var nativeType1 = SimpleNativeType.builtin("Int32");
var nativeType2 = SimpleNativeType.builtin("Int64");
- var isSubtype = isSubtype(nativeType1, nativeType2);
+ var isSubtype = isSubtype(nativeType1, nativeType2, TypesInfo.EMPTY);
+
+ assertThat(isSubtype, equalTo(false));
+ }
+
+ @Test
+ public void variantIsSubtypeOfSumType() {
+ var variantType1 = new StructType(NamespaceName.of(), "Rectangle");
+ var variantType2 = new StructType(NamespaceName.of(), "Circle");
+ var sumType = new SumType(NamespaceName.of(), "Shape");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(sumType, List.of(variantType1, variantType2), List.of());
+
+ var isSubtype = isSubtype(variantType1, sumType, typesInfo);
+
+ assertThat(isSubtype, equalTo(true));
+ }
+
+ @Test
+ public void variantIsNotSubtypeOfUnrelatedSumType() {
+ var typesInfo = TypesInfoInMemory.empty();
+
+ var variantType1 = new StructType(NamespaceName.of(), "Rectangle");
+ var variantType2 = new StructType(NamespaceName.of(), "Circle");
+ var sumType = new SumType(NamespaceName.of(), "Shape");
+ typesInfo.defineSumType(sumType, List.of(variantType1, variantType2), List.of());
+
+ var otherSumType = new SumType(NamespaceName.of(), "Shape3D");
+ var otherVariantType = new StructType(NamespaceName.of(), "Cube");
+ typesInfo.defineSumType(sumType, List.of(otherVariantType), List.of());
+
+ var isSubtype = isSubtype(variantType1, otherSumType, typesInfo);
assertThat(isSubtype, equalTo(false));
}