View Javadoc
1   package org.csveed.token;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNull;
5   import static org.junit.jupiter.api.Assertions.assertThrows;
6   import static org.junit.jupiter.api.Assertions.assertTrue;
7   
8   import org.junit.jupiter.api.Test;
9   
10  public class ParseStateMachineTest {
11  
12      @Test
13      public void columns() throws ParseException {
14          ParseStateMachine machine = new ParseStateMachine();
15          assertEquals(1, machine.getCurrentColumn());
16          machine.offerSymbol(';');
17          assertEquals(2, machine.getCurrentColumn());
18          machine.offerSymbol(';');
19          assertEquals(3, machine.getCurrentColumn());
20      }
21  
22      @Test
23      public void windowsCrLf() throws ParseException {
24          ParseStateMachine machine = new ParseStateMachine();
25          machine.offerSymbol(';');
26          machine.offerSymbol(0x0d);
27          machine.offerSymbol(0x0a);
28          machine.offerSymbol(';');
29          assertEquals(2, machine.getCurrentLine());
30          machine.offerSymbol(0x0d);
31          machine.offerSymbol(0x0a);
32          machine.offerSymbol(-1);
33          assertEquals(3, machine.getCurrentLine());
34      }
35  
36      @Test
37      public void commentLine() throws ParseException {
38          ParseStateMachine machine = new ParseStateMachine();
39          assertNull(machine.offerSymbol('#'));
40          assertNull(machine.offerSymbol('-'));
41          assertNull(machine.offerSymbol('#'));
42          assertNull(machine.offerSymbol('\n'));
43          assertEquals("", machine.offerSymbol(';'));
44      }
45  
46      @Test
47      public void simpleTest() throws ParseException {
48          ParseStateMachine machine = new ParseStateMachine();
49          assertNull(machine.offerSymbol('"'));
50          assertNull(machine.offerSymbol('a'));
51          assertNull(machine.offerSymbol('"'));
52          assertEquals("a", machine.offerSymbol(-1));
53      }
54  
55      @Test
56      public void emptyColumns() throws ParseException {
57          ParseStateMachine machine = new ParseStateMachine();
58          assertEquals("", machine.offerSymbol(';'));
59          assertEquals("", machine.offerSymbol(';'));
60          assertEquals("", machine.offerSymbol(';'));
61          assertEquals("", machine.offerSymbol(';'));
62          assertEquals("", machine.offerSymbol(-1));
63      }
64  
65      @Test
66      public void illegalState() throws ParseException {
67          ParseStateMachine machine = new ParseStateMachine();
68          machine.offerSymbol(-1);
69          assertThrows(ParseException.class, () ->  {
70              machine.offerSymbol(-1);
71          });
72      }
73  
74      @Test
75      public void illegalCharactersAfterQuotedContent() {
76          ParseStateMachine machine = new ParseStateMachine();
77          assertThrows(ParseException.class, () ->  {
78              feedStateMachine(machine, "    \"alpha\"  ; \"beta\"   x; \"gamma\" ");
79          });
80      }
81  
82      @Test
83      public void beforeFieldWithEOL() throws ParseException {
84          ParseStateMachine machine = new ParseStateMachine();
85          machine.offerSymbol(' ');
86          machine.offerSymbol('\n');
87          assertTrue(machine.isLineFinished());
88      }
89  
90      @Test
91      public void beforeFieldWithEOF() throws ParseException {
92          ParseStateMachine machine = new ParseStateMachine();
93          machine.offerSymbol(' ');
94          machine.offerSymbol(-1);
95          assertTrue(machine.isLineFinished());
96          assertTrue(machine.isFinished());
97      }
98  
99      @Test
100     public void beforeFieldWithSeparator() throws ParseException {
101         ParseStateMachine machine = new ParseStateMachine();
102         assertNull(machine.offerSymbol(' '));
103         assertEquals("", machine.offerSymbol(';'));
104     }
105 
106     @Test
107     public void cellNotFinished() {
108         ParseStateMachine machine = new ParseStateMachine();
109         assertThrows(ParseException.class, () ->  {
110             feedStateMachine(machine, "\"alpha\";\"beta\";\"ga");
111         });
112     }
113 
114     @Test
115     public void doubleQuotesAfterFieldInfoStarted() {
116         ParseStateMachine machine = new ParseStateMachine();
117         assertThrows(ParseException.class, () ->  {
118             feedStateMachine(machine, "some text and... \"double quote\"... WAT?;\"beta\";\"ga\"");
119         });
120     }
121 
122     protected void feedStateMachine(ParseStateMachine machine, String symbols) throws ParseException {
123         for (char symbol : symbols.toCharArray()) {
124             machine.offerSymbol(symbol);
125         }
126         machine.offerSymbol(-1);
127     }
128 }