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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package org.zwobble.hobgoblin.compiler.parser;
import org.junit.jupiter.api.Test;
import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode;
import org.zwobble.hobgoblin.compiler.types.NamespaceName;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNodeMatcher.isUntypedImportNode;
import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceNodeMatcher.isUntypedNamespaceNode;
import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
public class ParserNamespaceTests {
@Test
public void emptyNamespaceHasNoImports() {
var source = "";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, isUntypedNamespaceNode()
.withImports(isSequence())
);
}
@Test
public void importsAreParsed() {
var source = """
import {Point} from point;
import {Square, Rectangle} from ./shapes;
import {Line} from ../../line;
""";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, isUntypedNamespaceNode()
.withImports(isSequence(
isUntypedImportNode()
.withImportedNames(isSequence(equalTo("Point")))
.withAncestorDepth(isOptionalEmpty())
.withNamespaceName(equalTo(NamespaceName.of("point"))),
isUntypedImportNode()
.withImportedNames(isSequence(equalTo("Square"), equalTo("Rectangle")))
.withAncestorDepth(isOptionalOf(equalTo(0)))
.withNamespaceName(equalTo(NamespaceName.of("shapes"))),
isUntypedImportNode()
.withImportedNames(isSequence(equalTo("Line")))
.withAncestorDepth(isOptionalOf(equalTo(2)))
.withNamespaceName(equalTo(NamespaceName.of("line")))
))
);
}
@Test
public void emptyNamespaceHasEmptyBody() {
var source = "";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, isUntypedNamespaceNode()
.withNamespaceName(equalTo(NamespaceName.of("Example", "Project")))
.withBody(isSequence())
);
}
@Test
public void bodyContainsStatements() {
var source = """
struct One {}
struct Two {}""";
var node = parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
);
assertThat(node, isUntypedNamespaceNode()
.withBody(isSequence(
instanceOf(
UntypedStructDefinitionNode.class,
has("name", UntypedStructDefinitionNode::name, equalTo("One"))
),
instanceOf(
UntypedStructDefinitionNode.class,
has("name", UntypedStructDefinitionNode::name, equalTo("Two"))
)
))
);
}
@Test
public void whenNamespaceStatementIsNotRecognisedThenErrorIsThrown() {
var source = "blah";
var error = assertThrows(
UnexpectedTokenError.class,
() -> parseString(
source,
tokens -> Parser.parseNamespace(tokens, NamespaceName.of("Example", "Project"))
)
);
assertThat(error.getExpected(), equalTo("namespace statement"));
assertThat(error.getActual().charSequence().toString(), equalTo("blah"));
}
}
|