1
2
3
4
5
6
7
8
9
10
11 package org.csveed.token;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertNull;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
17
18 import org.junit.jupiter.api.Test;
19
20
21
22
23 class ParseStateMachineTest {
24
25
26
27
28
29
30
31 @Test
32 void columns() throws ParseException {
33 ParseStateMachine machine = new ParseStateMachine();
34 assertEquals(1, machine.getCurrentColumn());
35 machine.offerSymbol(';');
36 assertEquals(2, machine.getCurrentColumn());
37 machine.offerSymbol(';');
38 assertEquals(3, machine.getCurrentColumn());
39 }
40
41
42
43
44
45
46
47 @Test
48 void windowsCrLf() throws ParseException {
49 ParseStateMachine machine = new ParseStateMachine();
50 machine.offerSymbol(';');
51 machine.offerSymbol(0x0d);
52 machine.offerSymbol(0x0a);
53 machine.offerSymbol(';');
54 assertEquals(2, machine.getCurrentLine());
55 machine.offerSymbol(0x0d);
56 machine.offerSymbol(0x0a);
57 machine.offerSymbol(-1);
58 assertEquals(3, machine.getCurrentLine());
59 }
60
61
62
63
64
65
66
67 @Test
68 void commentLine() throws ParseException {
69 ParseStateMachine machine = new ParseStateMachine();
70 assertNull(machine.offerSymbol('#'));
71 assertNull(machine.offerSymbol('-'));
72 assertNull(machine.offerSymbol('#'));
73 assertNull(machine.offerSymbol('\n'));
74 assertEquals("", machine.offerSymbol(';'));
75 }
76
77
78
79
80
81
82
83 @Test
84 void simpleTest() throws ParseException {
85 ParseStateMachine machine = new ParseStateMachine();
86 assertNull(machine.offerSymbol('"'));
87 assertNull(machine.offerSymbol('a'));
88 assertNull(machine.offerSymbol('"'));
89 assertEquals("a", machine.offerSymbol(-1));
90 }
91
92
93
94
95
96
97
98 @Test
99 void emptyColumns() throws ParseException {
100 ParseStateMachine machine = new ParseStateMachine();
101 assertEquals("", machine.offerSymbol(';'));
102 assertEquals("", machine.offerSymbol(';'));
103 assertEquals("", machine.offerSymbol(';'));
104 assertEquals("", machine.offerSymbol(';'));
105 assertEquals("", machine.offerSymbol(-1));
106 }
107
108
109
110
111
112
113
114 @Test
115 void illegalState() throws ParseException {
116 ParseStateMachine machine = new ParseStateMachine();
117 machine.offerSymbol(-1);
118 assertThrows(ParseException.class, () -> machine.offerSymbol(-1));
119 }
120
121
122
123
124 @Test
125 void illegalCharactersAfterQuotedContent() {
126 ParseStateMachine machine = new ParseStateMachine();
127 assertThrows(ParseException.class,
128 () -> feedStateMachine(machine, " \"alpha\" ; \"beta\" x; \"gamma\" "));
129 }
130
131
132
133
134
135
136
137 @Test
138 void beforeFieldWithEOL() throws ParseException {
139 ParseStateMachine machine = new ParseStateMachine();
140 machine.offerSymbol(' ');
141 machine.offerSymbol('\n');
142 assertTrue(machine.isLineFinished());
143 }
144
145
146
147
148
149
150
151 @Test
152 void beforeFieldWithEOF() throws ParseException {
153 ParseStateMachine machine = new ParseStateMachine();
154 machine.offerSymbol(' ');
155 machine.offerSymbol(-1);
156 assertTrue(machine.isLineFinished());
157 assertTrue(machine.isFinished());
158 }
159
160
161
162
163
164
165
166 @Test
167 void beforeFieldWithSeparator() throws ParseException {
168 ParseStateMachine machine = new ParseStateMachine();
169 assertNull(machine.offerSymbol(' '));
170 assertEquals("", machine.offerSymbol(';'));
171 }
172
173
174
175
176 @Test
177 void cellNotFinished() {
178 ParseStateMachine machine = new ParseStateMachine();
179 assertThrows(ParseException.class, () -> feedStateMachine(machine, "\"alpha\";\"beta\";\"ga"));
180 }
181
182
183
184
185 @Test
186 void doubleQuotesAfterFieldInfoStarted() {
187 ParseStateMachine machine = new ParseStateMachine();
188 assertThrows(ParseException.class,
189 () -> feedStateMachine(machine, "some text and... \"double quote\"... WAT?;\"beta\";\"ga\""));
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203 protected void feedStateMachine(ParseStateMachine machine, String symbols) throws ParseException {
204 for (int i = 0; i < symbols.length(); i++) {
205 char symbol = symbols.charAt(i);
206 machine.offerSymbol(symbol);
207 }
208 machine.offerSymbol(-1);
209 }
210 }