From 1c88d4563baea119c86f90ba2ba0c1f9f2e37b91 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 2 Feb 2025 13:09:06 +0000 Subject: Parse native type declarations --- .../parser/ParserNativeTypeDeclarationTests.java | 180 +++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java (limited to 'src/test/java/org/shedlang/compiler/parser') diff --git a/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java b/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java new file mode 100644 index 00000000..900ac1aa --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java @@ -0,0 +1,180 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.types.Visibility; + +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; +import static org.shedlang.compiler.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ParserNativeTypeDeclarationTests { + @Test + public void canParseNameOfNativeType() { + var source = """ + native type X {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withName("X") + ); + } + + @Test + public void defaultVisibilityIsPrivate() { + var source = "native type X {}"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withVisibility(Visibility.PRIVATE) + ); + } + + @Test + public void whenHasPubPrefixThenVisibilityIsPublic() { + var source = "pub native type X {}"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withVisibility(Visibility.PUBLIC) + ); + } + + @Test + public void canParseNativeTypeWithNoTypeLevelParams() { + var source = """ + native type X {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withTypeLevelParams(isOptionalEmpty()) + ); + } + + @Test + public void canParseNativeTypeWithSingleTypeLevelParam() { + var source = """ + native type X[T1] {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeParamNode("T1") + ))) + ); + } + + @Test + public void canParseNativeTypeWithMultipleTypeLevelParams() { + var source = """ + native type X[T1, T2, T3] {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeParamNode("T1"), + isUntypedTypeParamNode("T2"), + isUntypedTypeParamNode("T3") + ))) + ); + } + + @Test + public void typeLevelParamsCanHaveTrailingComma() { + var source = """ + native type X[T1,] {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeParamNode("T1") + ))) + ); + } + + @Test + public void canParseNativeTypeWithNoMethods() { + var source = """ + native type X {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withMethods(isSequence()) + ); + } + + @Test + public void canParseNativeTypeWithSingleMethod() { + var source = """ + native type X { + native fun a(this); + } + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withMethods(isSequence( + isUntypedNativeFunctionDeclarationNode().withName("a") + )) + ); + } + + @Test + public void canParseNativeTypeWithMultipleMethods() { + var source = """ + native type X { + native fun a(this); + + native fun b(this); + + native fun c(this); + + } + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeTypeDeclarationNode() + .withMethods(isSequence( + isUntypedNativeFunctionDeclarationNode().withName("a"), + isUntypedNativeFunctionDeclarationNode().withName("b"), + isUntypedNativeFunctionDeclarationNode().withName("c") + )) + ); + } +} -- cgit v1.2.3