From fccc76e339651d6451bb8fc7394f21eed96e3318 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Sep 2023 12:07:09 +0100 Subject: Check for namespace name when defining struct --- .../typechecker/TypeCheckerStructDefinitionTests.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java index 325f7ea7..818c026f 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -5,12 +5,14 @@ import org.shedlang.compiler.ast.typed.TypedNamespaceStatementNode; import org.shedlang.compiler.ast.untyped.Untyped; import org.shedlang.compiler.ast.untyped.UntypedNamespaceStatementNode; import org.shedlang.compiler.ast.untyped.UntypedStructDefinitionNode; +import org.shedlang.compiler.typechecker.errors.CannotDefineStructOutsideOfNamespace; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -18,6 +20,19 @@ import static org.zwobble.precisely.Matchers.*; public class TypeCheckerStructDefinitionTests { private static final NamespaceName NAMESPACE_NAME = NamespaceName.of("Example"); + @Test + public void whenOutsideOfNamespaceThenErrorIsThrown() { + var untyped = UntypedStructDefinitionNode.builder() + .withName("ExampleStruct") + .build(); + var context = TypeCheckerContext.EMPTY; + + assertThrows( + CannotDefineStructOutsideOfNamespace.class, + () -> TypeChecker.typeCheckNamespaceStatement(untyped).typeCheckAllPhases(context) + ); + } + @Test public void canTypeCheckStructDefinitionWithNoFields() { var untyped = UntypedStructDefinitionNode.builder() -- cgit v1.2.3