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.assertNotSame;
5   import static org.junit.jupiter.api.Assertions.assertThrows;
6   
7   import org.csveed.report.CsvException;
8   import org.junit.jupiter.api.Test;
9   
10  public class SymbolMappingTest {
11  
12      @Test
13      public void setRowAtZero() {
14          SymbolMapping mapping = new SymbolMapping();
15          assertThrows(CsvException.class, () ->  {
16              mapping.setStartLine(0);
17          });
18      }
19  
20      @Test
21      public void addMappingMustEmptyDeleteMapping() {
22          SymbolMapping mapping = new SymbolMapping();
23          mapping.addMapping(EncounteredSymbol.QUOTE_SYMBOL, '\'');
24          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('\'', ParseState.START_OF_LINE));
25          assertNotSame(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.START_OF_LINE));
26      }
27  
28      @Test
29      public void endOfLineWindows() {
30          SymbolMapping mapping = new SymbolMapping();
31          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find(0x0d, ParseState.OUTSIDE_AFTER_FIELD));
32          assertEquals(EncounteredSymbol.EOL_SYMBOL_TRASH, mapping.find(0x0a, ParseState.START_OF_LINE));
33          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find(0x0d, ParseState.START_OF_LINE));
34          assertEquals(EncounteredSymbol.EOL_SYMBOL_TRASH, mapping.find(0x0a, ParseState.START_OF_LINE));
35      }
36  
37      @Test
38      public void similarEscapeAndQuote() {
39          SymbolMapping mapping = new SymbolMapping();
40          mapping.addMapping(EncounteredSymbol.ESCAPE_SYMBOL, '"');
41          mapping.addMapping(EncounteredSymbol.QUOTE_SYMBOL, '"');
42          assertEquals(EncounteredSymbol.ESCAPE_SYMBOL, mapping.find('"', ParseState.INSIDE_QUOTED_FIELD));
43          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.OUTSIDE_BEFORE_FIELD));
44      }
45  
46      @Test
47      public void dissimilarEscapeAndQuote() {
48          SymbolMapping mapping = new SymbolMapping();
49          mapping.addMapping(EncounteredSymbol.ESCAPE_SYMBOL, '\\');
50          mapping.addMapping(EncounteredSymbol.QUOTE_SYMBOL, '"');
51          assertEquals(EncounteredSymbol.ESCAPE_SYMBOL, mapping.find('\\', ParseState.OUTSIDE_BEFORE_FIELD));
52          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.OUTSIDE_BEFORE_FIELD));
53          assertEquals(EncounteredSymbol.ESCAPE_SYMBOL, mapping.find('\\', ParseState.INSIDE_QUOTED_FIELD));
54          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.INSIDE_QUOTED_FIELD));
55      }
56  
57      @Test
58      public void eolCarriageReturn() {
59          SymbolMapping mapping = new SymbolMapping();
60          mapping.addMapping(EncounteredSymbol.EOL_SYMBOL, new char[] { '\r', '\n' });
61          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find('\r', ParseState.ESCAPING));
62      }
63  
64      @Test
65      public void eolLineFeed() {
66          SymbolMapping mapping = new SymbolMapping();
67          mapping.addMapping(EncounteredSymbol.EOL_SYMBOL, new char[] { '\r', '\n' });
68          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find('\n', ParseState.INSIDE_FIELD));
69      }
70  
71      @Test
72      public void cannotFind() {
73          SymbolMapping mapping = new SymbolMapping();
74          assertEquals(EncounteredSymbol.OTHER_SYMBOL, mapping.find('A', ParseState.FINISHED));
75      }
76  
77  }