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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
package org.zwobble.mammoth.tests.styles.parsing;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.styles.parsing.StyleMappingTokeniser;
import org.zwobble.mammoth.internal.styles.parsing.Token;
import org.zwobble.mammoth.internal.styles.parsing.TokenType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.zwobble.mammoth.tests.Matchers.isSameSequence;
public class StyleMappingTokeniserTests {
@Test
public void unknownTokensAreTokenised() {
assertTokens("~", isToken(TokenType.UNKNOWN, "~"));
}
@Test
public void emptyStringIsTokenisedToNoTokens() {
assertTokens("");
}
@Test
public void whitespaceIsTokenised() {
assertTokens(" \t\t ", isToken(TokenType.WHITESPACE, " \t\t "));
}
@Test
public void identifiersAreTokenised() {
assertTokens("Overture", isToken(TokenType.IDENTIFIER, "Overture"));
}
@Test
public void escapeSequencesInIdentifiersAreTokenised() {
assertTokens("\\:", isToken(TokenType.IDENTIFIER, "\\:"));
}
@Test
public void integersAreTokenised() {
assertTokens("123", isToken(TokenType.INTEGER, "123"));
}
@Test
public void stringsAreTokenised() {
assertTokens("'Tristan'", isToken(TokenType.STRING, "'Tristan'"));
}
@Test
public void escapedStringTerminatorsInStringsAreTokenised() {
assertTokens("'Tristan\\''", isToken(TokenType.STRING, "'Tristan\\''"));
}
@Test
public void escapeSequencesInStringsAreTokenised() {
assertTokens("'Tristan\\\\'", isToken(TokenType.STRING, "'Tristan\\\\'"));
}
@Test
public void unterminatedStringsAreTokenised() {
assertTokens("'Tristan", isToken(TokenType.UNTERMINATED_STRING, "'Tristan"));
}
@Test
public void unterminatedStringsEndingWithEscapedStringTerminatorAreTokenised() {
assertTokens("'Tristan\\'", isToken(TokenType.UNTERMINATED_STRING, "'Tristan\\'"));
}
@Test
public void unterminatedStringsWithUnterminatedEscapeAreTokenised() {
assertTokens("'Tristan\\", isToken(TokenType.UNTERMINATED_STRING, "'Tristan\\"));
}
@Test
public void unterminatedStringsWithManyEscapeSequencesAreTokenised() {
assertTokens("'" + repeatString("\\a", 50), isToken(TokenType.UNTERMINATED_STRING, "'" + repeatString("\\a", 50)));
}
@Test
public void arrowsAreTokenised() {
assertTokens("=>=>", isToken(TokenType.SYMBOL, "=>"), isToken(TokenType.SYMBOL, "=>"));
}
@Test
public void dotsAreTokenised() {
assertTokens(".", isToken(TokenType.SYMBOL, "."));
}
@Test
public void colonsAreTokenised() {
assertTokens("::", isToken(TokenType.SYMBOL, ":"), isToken(TokenType.SYMBOL, ":"));
}
@Test
public void greaterThansAreTokenised() {
assertTokens(">>", isToken(TokenType.SYMBOL, ">"), isToken(TokenType.SYMBOL, ">"));
}
@Test
public void equalsAreTokenised() {
assertTokens("==", isToken(TokenType.SYMBOL, "="), isToken(TokenType.SYMBOL, "="));
}
@Test
public void startsWithSymbolsAreTokenised() {
assertTokens("^=^=", isToken(TokenType.SYMBOL, "^="), isToken(TokenType.SYMBOL, "^="));
}
@Test
public void openParensAreTokenised() {
assertTokens("((", isToken(TokenType.SYMBOL, "("), isToken(TokenType.SYMBOL, "("));
}
@Test
public void closeParensAreTokenised() {
assertTokens("))", isToken(TokenType.SYMBOL, ")"), isToken(TokenType.SYMBOL, ")"));
}
@Test
public void openSquareBracketsAreTokenised() {
assertTokens("[[", isToken(TokenType.SYMBOL, "["), isToken(TokenType.SYMBOL, "["));
}
@Test
public void closeSquareBracketsAreTokenised() {
assertTokens("]]", isToken(TokenType.SYMBOL, "]"), isToken(TokenType.SYMBOL, "]"));
}
@Test
public void choicesAreTokenised() {
assertTokens("||", isToken(TokenType.SYMBOL, "|"), isToken(TokenType.SYMBOL, "|"));
}
@Test
public void bangsAreTokenised() {
assertTokens("!!", isToken(TokenType.SYMBOL, "!"), isToken(TokenType.SYMBOL, "!"));
}
@Test
public void canTokeniseMultipleTokens() {
assertTokens("The Magic Position",
isToken(TokenType.IDENTIFIER, "The"),
isToken(TokenType.WHITESPACE, " "),
isToken(TokenType.IDENTIFIER, "Magic"),
isToken(TokenType.WHITESPACE, " "),
isToken(TokenType.IDENTIFIER, "Position")
);
}
@SafeVarargs
private final void assertTokens(String input, Matcher<Token>... tokens) {
assertThat(StyleMappingTokeniser.tokeniseToList(input), isSameSequence(tokens));
}
private Matcher<Token> isToken(TokenType tokenType, String value) {
return allOf(
hasProperty("tokenType", equalTo(tokenType)),
hasProperty("value", equalTo(value))
);
}
private String repeatString(String value, int repeats) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < repeats; i++) {
builder.append(value);
}
return builder.toString();
}
}
|