summaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-02 12:39:49 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-02 12:39:49 +0100
commitdf914fb9c7eeee62b050164d95f99bb81b395bc9 (patch)
treebc1bf21f86c9c3d2116784173d3961b736d1118d /src/test/java/org/zwobble
parentdec0f4467d4089c251b17d2cc6b4a0a8fed18f82 (diff)
Handle Box and Shared when determining constructibility
Diffstat (limited to 'src/test/java/org/zwobble')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java110
1 files changed, 109 insertions, 1 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
index 526f6bb..b802386 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
@@ -57,7 +57,7 @@ public class ArbitraryValueAnalysisTests {
}
@Test
- public void firstNonRecursiveVariantIsChosen() {
+ public void firstNonRecursivelyConstructibleVariantIsChosen() {
var sumType = new SumType(NamespaceName.of(), "X");
var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
@@ -82,4 +82,112 @@ public class ArbitraryValueAnalysisTests {
assertThat(variant, equalTo(nonRecursiveVariantType));
}
+
+ @Test
+ public void boxIsRecursivelyConstructibleIffInnerTypeIsRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.box(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.box(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(nonRecursiveVariantType));
+ }
+
+ @Test
+ public void listIsAlwaysNonRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.list(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.list(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(recursiveVariantType));
+ }
+
+ @Test
+ public void optionIsAlwaysNonRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.option(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.option(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(recursiveVariantType));
+ }
+
+ @Test
+ public void sharedIsRecursivelyConstructibleIffInnerTypeIsRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.shared(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.shared(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(nonRecursiveVariantType));
+ }
}