From f818776dba8bda2bf6e423da991190f3f4b4ff3f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 19 Jun 2026 17:34:46 +0100 Subject: Handle relative import with invalid ancestor depth --- .../typechecker/NamespaceNameResolutionTests.java | 37 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/test/java/org/zwobble') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/NamespaceNameResolutionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/NamespaceNameResolutionTests.java index 7929e7d..da0eb90 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/NamespaceNameResolutionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/NamespaceNameResolutionTests.java @@ -1,10 +1,12 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.NamespaceName; import java.util.Optional; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.zwobble.hobgoblin.compiler.typechecker.NamespaceNameResolution.resolveNamespaceName; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; @@ -16,7 +18,12 @@ public class NamespaceNameResolutionTests { var importedAncestorDepth = Optional.empty(); var importedNamespaceName = NamespaceName.of("x", "y"); - var result = resolveNamespaceName(current, importedAncestorDepth, importedNamespaceName); + var result = resolveNamespaceName( + current, + importedAncestorDepth, + importedNamespaceName, + NullSource.INSTANCE + ); assertThat(result, equalTo(NamespaceName.of("x", "y"))); } @@ -27,7 +34,12 @@ public class NamespaceNameResolutionTests { var importedAncestorDepth = Optional.of(0); var importedNamespaceName = NamespaceName.of("x", "y"); - var result = resolveNamespaceName(current, importedAncestorDepth, importedNamespaceName); + var result = resolveNamespaceName( + current, + importedAncestorDepth, + importedNamespaceName, + NullSource.INSTANCE + ); assertThat(result, equalTo(NamespaceName.of("a", "b", "x", "y"))); } @@ -38,8 +50,27 @@ public class NamespaceNameResolutionTests { var importedAncestorDepth = Optional.of(2); var importedNamespaceName = NamespaceName.of("x", "y"); - var result = resolveNamespaceName(current, importedAncestorDepth, importedNamespaceName); + var result = resolveNamespaceName( + current, + importedAncestorDepth, + importedNamespaceName, + NullSource.INSTANCE + ); assertThat(result, equalTo(NamespaceName.of("a", "x", "y"))); } + + @Test + public void whenAncestorDepthIsGreaterThanPartsThenErrorIsThrown() { + var current = NamespaceName.of("a", "b", "c"); + var importedAncestorDepth = Optional.of(4); + var importedNamespaceName = NamespaceName.of("x", "y"); + + assertThrows(RootNamespaceHasNoParentError.class, () -> resolveNamespaceName( + current, + importedAncestorDepth, + importedNamespaceName, + NullSource.INSTANCE + )); + } } -- cgit v1.2.3