diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-01-12 21:25:32 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-01-12 21:25:32 +0000 |
| commit | 6f554d8e265ad2664c97d9d8d6efef2f53e9eb5f (patch) | |
| tree | 8d2453bd70ce1143be425448a16c4a93d8767461 /src/test/java | |
| parent | eef0470389d88de6169a6962f14a9d422175e4c2 (diff) | |
Parse native function declarations
Diffstat (limited to 'src/test/java')
3 files changed, 116 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java new file mode 100644 index 00000000..9eb338fd --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java @@ -0,0 +1,52 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.Empty; +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.shedlang.compiler.types.Visibility; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedNativeFunctionDeclarationNodeMatcher extends MatcherBuilder< + UntypedNamespaceStatementNode, + UntypedNativeFunctionDeclarationNode, + UntypedNativeFunctionDeclarationNodeMatcher +> { + public static UntypedNativeFunctionDeclarationNodeMatcher empty() { + return new UntypedNativeFunctionDeclarationNodeMatcher(Empty.vector()); + } + + public UntypedNativeFunctionDeclarationNodeMatcher( + PVector<Matcher<? super UntypedNativeFunctionDeclarationNode>> submatchers + ) { + super(submatchers); + } + + @Override + protected Class<UntypedNativeFunctionDeclarationNode> expectedType() { + return UntypedNativeFunctionDeclarationNode.class; + } + + @Override + protected UntypedNativeFunctionDeclarationNodeMatcher create( + PVector<Matcher<? super UntypedNativeFunctionDeclarationNode>> submatchers + ) { + return new UntypedNativeFunctionDeclarationNodeMatcher(submatchers); + } + + public UntypedNativeFunctionDeclarationNodeMatcher withSignature( + Matcher<? super UntypedFunctionSignatureNode> signature + ) { + return with(has("signature", x -> x.signature(), signature)); + } + + public UntypedNativeFunctionDeclarationNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedNativeFunctionDeclarationNodeMatcher withVisibility(Visibility visibility) { + return with(has("visibility", x -> x.visibility(), equalTo(visibility))); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java index 0e11231b..94fc68e1 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -235,6 +235,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedNativeFunctionDeclarationNodeMatcher isUntypedNativeFunctionDeclarationNode() { + return UntypedNativeFunctionDeclarationNodeMatcher.empty(); + } + public static Matcher<UntypedExpressionNode> isUntypedNotEqualNode( Matcher<UntypedExpressionNode> left, Matcher<UntypedExpressionNode> right diff --git a/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java b/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java new file mode 100644 index 00000000..f11a673c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java @@ -0,0 +1,60 @@ +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.isOptionalEmpty; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ParserNativeFunctionDeclarationTests { + @Test + public void canParseMinimalFunction() { + var source = """ + native fun f() -> Unit;"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeFunctionDeclarationNode() + .withName("f") + .withSignature( + isUntypedFunctionSignatureNode() + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ) + ); + } + + @Test + public void defaultVisibilityIsPrivate() { + var source = """ + native fun f() -> Unit;"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeFunctionDeclarationNode() + .withVisibility(Visibility.PRIVATE) + ); + } + + @Test + public void whenFunIsPrefixedWithPubThenVisibilityIsPublic() { + var source = """ + pub native fun f() -> Unit;"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedNativeFunctionDeclarationNode() + .withVisibility(Visibility.PUBLIC) + ); + } +} |
