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.assertNotSame;
15  import static org.junit.jupiter.api.Assertions.assertThrows;
16  
17  import org.csveed.report.CsvException;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * The Class SymbolMappingTest.
22   */
23  class SymbolMappingTest {
24  
25      /**
26       * Sets the row at zero.
27       */
28      @Test
29      void setRowAtZero() {
30          SymbolMapping mapping = new SymbolMapping();
31          assertThrows(CsvException.class, () -> mapping.setStartLine(0));
32      }
33  
34      /**
35       * Adds the mapping must empty delete mapping.
36       */
37      @Test
38      void addMappingMustEmptyDeleteMapping() {
39          SymbolMapping mapping = new SymbolMapping();
40          mapping.addMapping(EncounteredSymbol.QUOTE_SYMBOL, '\'');
41          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('\'', ParseState.START_OF_LINE));
42          assertNotSame(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.START_OF_LINE));
43      }
44  
45      /**
46       * End of line windows.
47       */
48      @Test
49      void endOfLineWindows() {
50          SymbolMapping mapping = new SymbolMapping();
51          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find(0x0d, ParseState.OUTSIDE_AFTER_FIELD));
52          assertEquals(EncounteredSymbol.EOL_SYMBOL_TRASH, mapping.find(0x0a, ParseState.START_OF_LINE));
53          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find(0x0d, ParseState.START_OF_LINE));
54          assertEquals(EncounteredSymbol.EOL_SYMBOL_TRASH, mapping.find(0x0a, ParseState.START_OF_LINE));
55      }
56  
57      /**
58       * Similar escape and quote.
59       */
60      @Test
61      void similarEscapeAndQuote() {
62          SymbolMapping mapping = new SymbolMapping();
63          mapping.addMapping(EncounteredSymbol.ESCAPE_SYMBOL, '"');
64          mapping.addMapping(EncounteredSymbol.QUOTE_SYMBOL, '"');
65          assertEquals(EncounteredSymbol.ESCAPE_SYMBOL, mapping.find('"', ParseState.INSIDE_QUOTED_FIELD));
66          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.OUTSIDE_BEFORE_FIELD));
67      }
68  
69      /**
70       * Dissimilar escape and quote.
71       */
72      @Test
73      void dissimilarEscapeAndQuote() {
74          SymbolMapping mapping = new SymbolMapping();
75          mapping.addMapping(EncounteredSymbol.ESCAPE_SYMBOL, '\\');
76          mapping.addMapping(EncounteredSymbol.QUOTE_SYMBOL, '"');
77          assertEquals(EncounteredSymbol.ESCAPE_SYMBOL, mapping.find('\\', ParseState.OUTSIDE_BEFORE_FIELD));
78          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.OUTSIDE_BEFORE_FIELD));
79          assertEquals(EncounteredSymbol.ESCAPE_SYMBOL, mapping.find('\\', ParseState.INSIDE_QUOTED_FIELD));
80          assertEquals(EncounteredSymbol.QUOTE_SYMBOL, mapping.find('"', ParseState.INSIDE_QUOTED_FIELD));
81      }
82  
83      /**
84       * Eol carriage return.
85       */
86      @Test
87      void eolCarriageReturn() {
88          SymbolMapping mapping = new SymbolMapping();
89          mapping.addMapping(EncounteredSymbol.EOL_SYMBOL, new char[] { '\r', '\n' });
90          assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find('\r', ParseState.ESCAPING));
91      }
92  
93      /**
94       * Eol line feed.
95       */
96      @Test
97      void eolLineFeed() {
98          SymbolMapping mapping = new SymbolMapping();
99          mapping.addMapping(EncounteredSymbol.EOL_SYMBOL, new char[] { '\r', '\n' });
100         assertEquals(EncounteredSymbol.EOL_SYMBOL, mapping.find('\n', ParseState.INSIDE_FIELD));
101     }
102 
103     /**
104      * Bom symbol.
105      */
106     @Test
107     void bomSymbol() {
108         SymbolMapping mapping = new SymbolMapping();
109         mapping.addMapping(EncounteredSymbol.BOM_SYMBOL, '\uFEFF');
110         assertEquals(EncounteredSymbol.BOM_SYMBOL, mapping.find('\uFEFF', ParseState.START_OF_LINE));
111     }
112 
113     /**
114      * Cannot find.
115      */
116     @Test
117     void cannotFind() {
118         SymbolMapping mapping = new SymbolMapping();
119         assertEquals(EncounteredSymbol.OTHER_SYMBOL, mapping.find('A', ParseState.FINISHED));
120     }
121 }