diff options
Diffstat (limited to 'src/test/java/org')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java | 83 |
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)); + } +} |
