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.row;
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  
17  import org.csveed.common.Column;
18  import org.csveed.report.CsvException;
19  import org.csveed.report.RowReport;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * The Class LineWithInfoTest.
24   */
25  class LineWithInfoTest {
26  
27      /**
28       * Cell is null.
29       */
30      @Test
31      void cellIsNull() {
32          LineWithInfo row = new LineWithInfo();
33          row.addCell(null);
34          assertEquals(0, row.getCellPosition(new Column()).getStart());
35          assertEquals(0, row.getCellPosition(new Column()).getEnd());
36      }
37  
38      /**
39       * Cell is empty.
40       */
41      @Test
42      void cellIsEmpty() {
43          LineWithInfo row = new LineWithInfo();
44          row.addCell("");
45          assertEquals(0, row.getCellPosition(new Column()).getStart());
46          assertEquals(0, row.getCellPosition(new Column()).getEnd());
47      }
48  
49      /**
50       * Convert characters.
51       */
52      @Test
53      void convertCharacters() {
54          LineWithInfo row = new LineWithInfo();
55          assertEquals("\\b", row.convertToPrintable('\b'));
56          assertEquals("\\f", row.convertToPrintable('\f'));
57      }
58  
59      /**
60       * Non existing cell.
61       */
62      @Test
63      void nonExistingCell() {
64          LineWithInfo row = new LineWithInfo();
65          assertNull(row.reportOnColumn(new Column()));
66      }
67  
68      /**
69       * Gets the report on column index 0.
70       */
71      @Test
72      void getReportOnColumnIndex0() {
73          LineWithInfo row = addString(new LineWithInfo(), "Hello");
74          assertThrows(CsvException.class, () -> row.reportOnColumn(new Column(0)));
75      }
76  
77      /**
78       * Simple word.
79       */
80      @Test
81      void simpleWord() {
82          LineWithInfo row = new LineWithInfo();
83          row = addString(row, "Hello");
84          RowReport report = row.reportOnColumn(new Column());
85          assertEquals("Hello", report.getRow());
86          assertEquals(0, report.getStart());
87          assertEquals(5, report.getEnd());
88      }
89  
90      /**
91       * Couple of words.
92       */
93      @Test
94      void coupleOfWords() {
95          LineWithInfo row = new LineWithInfo();
96          row = addString(row, "Alpha");
97          row.addCharacter(';');
98          row = addString(row, "Beta");
99          row.addCharacter(';');
100         row = addString(row, "Gamma");
101         RowReport report = row.reportOnColumn(new Column(3));
102         assertEquals("Alpha;Beta;Gamma", report.getRow());
103         assertEquals(11, report.getStart());
104         assertEquals(16, report.getEnd());
105     }
106 
107     /**
108      * Various non printables.
109      */
110     @Test
111     void variousNonPrintables() {
112         LineWithInfo row = new LineWithInfo();
113         row = addString(row, "Alpha");
114         row.addCharacter('\t');
115         row = addString(row, "Beta");
116         row.addCharacter('\t');
117         row = addString(row, "Gamma");
118         RowReport report = row.reportOnColumn(new Column(3));
119         assertEquals("Alpha\\tBeta\\tGamma", report.getRow());
120         assertEquals(13, report.getStart());
121         assertEquals(18, report.getEnd());
122     }
123 
124     /**
125      * Report on end of line.
126      */
127     @Test
128     void reportOnEndOfLine() {
129         LineWithInfo row = new LineWithInfo();
130         row = addString(row, "Alpha");
131         row.addCharacter('\t');
132         row = addString(row, "Beta");
133         row.addCharacter('@');
134         RowReport report = row.reportOnEndOfLine();
135         assertEquals("Alpha\\tBeta@", report.getRow());
136         assertEquals(12, report.getStart());
137         assertEquals(12, report.getEnd());
138     }
139 
140     /**
141      * Adds the string.
142      *
143      * @param row
144      *            the row
145      * @param text
146      *            the text
147      *
148      * @return the line with info
149      */
150     protected LineWithInfo addString(LineWithInfo row, String text) {
151         row.markStartOfColumn();
152         for (int i = 0; i < text.length(); i++) {
153             char character = text.charAt(i);
154             row.addCharacter(character);
155         }
156         row.addCell(text);
157         return row;
158     }
159 
160 }