aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java
blob: bf7621e536f639429a8ff0c4be7c55f031b85427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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(
                    isUntypedFunctionTypeNode()
                        .withParams(isUntypedFunctionTypeParamsNode(
                            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)
        );
    }
}