-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexerTests.java
More file actions
207 lines (183 loc) · 7.84 KB
/
Copy pathLexerTests.java
File metadata and controls
207 lines (183 loc) · 7.84 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package plc.project;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import plc.project.ParseException;
import plc.project.Token;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class LexerTests {
@ParameterizedTest
@MethodSource
void testIdentifier(String test, String input, boolean success) {
test(input, Token.Type.IDENTIFIER, success);
}
private static Stream<Arguments> testIdentifier() {
return Stream.of(
Arguments.of("Alphabetic", "getName", true),
Arguments.of("Alphanumeric", "thelegend27", true),
Arguments.of("_at the front", "_adfasdf", false),
Arguments.of("@in the beginning","@adfwef123",true),
Arguments.of("dash in middle","AfawD23-_adfweC0",true),
Arguments.of("contain @","@adsf@adf",false),
Arguments.of("Leading Hyphen", "-five", false),
Arguments.of("Leading Digit", "1fish2fish3fishbluefish", false),
Arguments.of("single","@",true),
Arguments.of("single","S",true),
Arguments.of("empty","",false),
Arguments.of("single character","a",true),
Arguments.of("Underscores","___",false)
);
}
@ParameterizedTest
@MethodSource
void testInteger(String test, String input, boolean success) {
test(input, Token.Type.INTEGER, success);
}
private static Stream<Arguments> testInteger() {
return Stream.of(
Arguments.of("Single Digit", "1", true),
Arguments.of("Multiple Digits", "12345", true),
Arguments.of("Negative", "-1", true),
Arguments.of("Leading Zero", "01", false),
Arguments.of("single zero","0",true),
Arguments.of("single number","1",true),
Arguments.of("float number", "1.23",false),
Arguments.of("empty","",false),
Arguments.of("Decimal","123.456",false),
Arguments.of("Comma Seperated","1,234",false),
Arguments.of("leading zero","007",false)
);
}
@ParameterizedTest
@MethodSource
void testDecimal(String test, String input, boolean success) {
test(input, Token.Type.DECIMAL, success);
}
private static Stream<Arguments> testDecimal() {
return Stream.of(
Arguments.of("Multiple Digits", "123.456", true),
Arguments.of("Negative Decimal", "-1.0", true),
Arguments.of("Trailing Decimal", "1.", false),
Arguments.of("Leading Decimal", ".5", false),
Arguments.of("other Characters", "123.baf",false),
Arguments.of("single 0", "0",false),
Arguments.of("single 0 point number", "0.123",true),
Arguments.of("01.123","01.123",false),
Arguments.of("0.","0.",false),
Arguments.of("single digit","1",false),
Arguments.of("trailing zero","7.000156",true),
Arguments.of("double decimal","1..0",false)
);
}
@ParameterizedTest
@MethodSource
void testCharacter(String test, String input, boolean success) {
test(input, Token.Type.CHARACTER, success);
}
private static Stream<Arguments> testCharacter() {
return Stream.of(
Arguments.of("Alphabetic", "\'c\'", true),
Arguments.of("Newline Escape", "\'\\n\'", true),
Arguments.of("Empty", "\'\'", false),
Arguments.of("Multiple", "\'abc\'", false),
Arguments.of("symbol","\'$\'",true),
Arguments.of("'\\'","\'\\\'",true),
Arguments.of("'\\a'","\'\\a\'",false),
Arguments.of("Unterminated:","\'\\",false),
Arguments.of("Newline:","\'\r\'",true)
);
}
@ParameterizedTest
@MethodSource
void testString(String test, String input, boolean success) {
test(input, Token.Type.STRING, success);
}
private static Stream<Arguments> testString() {
return Stream.of(
Arguments.of("Empty", "\"\"", true),
Arguments.of("Alphabetic", "\"abc\"", true),
Arguments.of("Newline Escape", "\"Hello,\\nWorld\"", true),
Arguments.of("Unterminated", "\"unterminated", false),
Arguments.of("Invalid Escape", "\"invalid\\escape\"", false),
Arguments.of("slash","\"adsf\\\\adsf\"",true)
);
}
@ParameterizedTest
@MethodSource
void testOperator(String test, String input, boolean success) {
//this test requires our lex() method, since that's where whitespace is handled.
test(input, Arrays.asList(new Token(Token.Type.OPERATOR, input, 0)), success);
}
private static Stream<Arguments> testOperator() {
return Stream.of(
Arguments.of("Character", "(", true),
Arguments.of("Comparison", "!=", true),
Arguments.of("Space", " ", false),
Arguments.of("Tab", "\t", false),
Arguments.of(")", ")", true)
);
}
@ParameterizedTest
@MethodSource
void testExamples(String test, String input, List<Token> expected) {
test(input, expected, true);
}
private static Stream<Arguments> testExamples() {
return Stream.of(
Arguments.of("Example 1", "LET x = 5;", Arrays.asList(
new Token(Token.Type.IDENTIFIER, "LET", 0),
new Token(Token.Type.IDENTIFIER, "x", 4),
new Token(Token.Type.OPERATOR, "=", 6),
new Token(Token.Type.INTEGER, "5", 8),
new Token(Token.Type.OPERATOR, ";", 9)
)),
Arguments.of("Example 2", "print(\"Hello, World!\");", Arrays.asList(
new Token(Token.Type.IDENTIFIER, "print", 0),
new Token(Token.Type.OPERATOR, "(", 5),
new Token(Token.Type.STRING, "\"Hello, World!\"", 6),
new Token(Token.Type.OPERATOR, ")", 21),
new Token(Token.Type.OPERATOR, ";", 22)
))
);
}
@Test
void testException() {
ParseException exception = Assertions.assertThrows(ParseException.class,
() -> new Lexer("\"unterminated").lex());
Assertions.assertEquals(13, exception.getIndex());
}
/**
* Tests that lexing the input through {@link Lexer#lexToken()} produces a
* single token with the expected type and literal matching the input.
*/
private static void test(String input, Token.Type expected, boolean success) {
try {
if (success) {
Assertions.assertEquals(new Token(expected, input, 0), new Lexer(input).lexToken());
} else {
Assertions.assertNotEquals(new Token(expected, input, 0), new Lexer(input).lexToken());
}
} catch (ParseException e) {
Assertions.assertFalse(success, e.getMessage());
}
}
/**
* Tests that lexing the input through {@link Lexer#lex()} matches the
* expected token list.
*/
private static void test(String input, List<Token> expected, boolean success) {
try {
if (success) {
Assertions.assertEquals(expected, new Lexer(input).lex());
} else {
Assertions.assertNotEquals(expected, new Lexer(input).lex());
}
} catch (ParseException e) {
Assertions.assertFalse(success, e.getMessage());
}
}
}