View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
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   * The Class ParseStateMachineTest.
22   */
23  class ParseStateMachineTest {
24  
25      /**
26       * Columns.
27       *
28       * @throws ParseException
29       *             the parse exception
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       * Windows cr lf.
43       *
44       * @throws ParseException
45       *             the parse exception
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       * Comment line.
63       *
64       * @throws ParseException
65       *             the parse exception
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       * Simple test.
79       *
80       * @throws ParseException
81       *             the parse exception
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       * Empty columns.
94       *
95       * @throws ParseException
96       *             the parse exception
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      * Illegal state.
110      *
111      * @throws ParseException
112      *             the parse exception
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      * Illegal characters after quoted content.
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      * Before field with EOL.
133      *
134      * @throws ParseException
135      *             the parse exception
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      * Before field with EOF.
147      *
148      * @throws ParseException
149      *             the parse exception
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      * Before field with separator.
162      *
163      * @throws ParseException
164      *             the parse exception
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      * Cell not finished.
175      */
176     @Test
177     void cellNotFinished() {
178         ParseStateMachine machine = new ParseStateMachine();
179         assertThrows(ParseException.class, () -> feedStateMachine(machine, "\"alpha\";\"beta\";\"ga"));
180     }
181 
182     /**
183      * Double quotes after field info started.
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      * Feed state machine.
194      *
195      * @param machine
196      *            the machine
197      * @param symbols
198      *            the symbols
199      *
200      * @throws ParseException
201      *             the parse exception
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 }