summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-02 11:54:31 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-02 11:54:31 +0100
commit7f0b4097352f429c31526c31b281c78b7ba4d8c9 (patch)
treee9f846a509699fb8eabffb9900187f89704e4e8e /src/test/java
parent64ed8324cc107429cd0d9cf39de84cda18f8aad1 (diff)
Generate proper errors when arbitrary value cannot be found
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java83
1 files changed, 83 insertions, 0 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
new file mode 100644
index 0000000..30dbc7b
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
@@ -0,0 +1,83 @@
+package org.zwobble.hobgoblin.compiler.analysis;
+
+import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.analysis.errors.SumTypeHasNoVariantsError;
+import org.zwobble.hobgoblin.compiler.analysis.errors.TypeIsInfiniteError;
+import org.zwobble.hobgoblin.compiler.builtins.NativeTypes;
+import org.zwobble.hobgoblin.compiler.sources.NullSource;
+import org.zwobble.hobgoblin.compiler.typechecker.TypesInfoInMemory;
+import org.zwobble.hobgoblin.compiler.types.*;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.zwobble.hobgoblin.compiler.analysis.ArbitraryValueAnalysis.selectArbitraryVariant;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class ArbitraryValueAnalysisTests {
+ @Test
+ public void whenTypeHasNoVariantsThenErrorIsThrown() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(sumType, List.of(), List.of());
+
+ var error = assertThrows(
+ SumTypeHasNoVariantsError.class,
+ () -> selectArbitraryVariant(sumType, typesInfo)
+ );
+
+ assertThat(error.type(), equalTo(sumType));
+ }
+
+ @Test
+ public void whenTypeIsInfiniteThenErrorIsThrown() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var variantType = new StructType(NamespaceName.of(), "Y");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, variantType, variantType)
+ ),
+ List.of()
+ );
+ typesInfo.defineStructType(variantType, Optional.of(List.of(
+ new Field("x", sumType, NullSource.INSTANCE)
+ )));
+
+ var error = assertThrows(
+ TypeIsInfiniteError.class,
+ () -> selectArbitraryVariant(sumType, typesInfo)
+ );
+
+ assertThat(error.type(), equalTo(sumType));
+ }
+
+ @Test
+ public void firstNonRecursiveVariantIsChosen() {
+ 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()
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", sumType, NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.INT_64, NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(nonRecursiveVariantType));
+ }
+}