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
64
65
66
67
68
69
70
71
72
73
|
package org.shedlang.compiler.parser;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.untyped.UntypedNamespaceNode;
import org.shedlang.compiler.types.NamespaceName;
import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedFunctionDefinitionNode;
import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedImportNode;
import static org.shedlang.compiler.parser.ParserTesting.parseString;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
public class ParserNamespaceTests {
@Test
public void canParseEmptyNamespace() {
var source = "";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, instanceOf(
UntypedNamespaceNode.class,
has("namespaceName", x -> x.namespaceName(), equalTo(NamespaceName.of("Example", "Project"))),
has("body", x -> x.body(), isSequence())
));
}
@Test
public void canParseNamespaceWithStatements() {
var source = """
fun f() -> Unit {
}
fun g() -> Unit {
}
""";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, instanceOf(
UntypedNamespaceNode.class,
has("body", x -> x.body(), isSequence(
isUntypedFunctionDefinitionNode().withName("f"),
isUntypedFunctionDefinitionNode().withName("g")
))
));
}
@Test
public void canParseNamespaceWithImports() {
var source = """
import One;
import Two/Three;""";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, instanceOf(
UntypedNamespaceNode.class,
has("imports", x -> x.imports(), isSequence(
isUntypedImportNode(NamespaceName.of("One")),
isUntypedImportNode(NamespaceName.of("Two", "Three"))
))
));
}
}
|