View Javadoc
1   package org.csveed.row;
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   
7   import org.csveed.common.Column;
8   import org.csveed.report.CsvException;
9   import org.csveed.report.RowReport;
10  import org.junit.jupiter.api.Test;
11  
12  public class LineWithInfoTest {
13  
14      @Test
15      public void cellIsNull() {
16          LineWithInfo row = new LineWithInfo();
17          row.addCell(null);
18          assertEquals(0, row.getCellPosition(new Column()).getStart());
19          assertEquals(0, row.getCellPosition(new Column()).getEnd());
20      }
21  
22      @Test
23      public void cellIsEmpty() {
24          LineWithInfo row = new LineWithInfo();
25          row.addCell("");
26          assertEquals(0, row.getCellPosition(new Column()).getStart());
27          assertEquals(0, row.getCellPosition(new Column()).getEnd());
28      }
29  
30      @Test
31      public void convertCharacters() {
32          LineWithInfo row = new LineWithInfo();
33          assertEquals("\\b", row.convertToPrintable('\b'));
34          assertEquals("\\f", row.convertToPrintable('\f'));
35      }
36  
37      @Test
38      public void nonExistingCell() {
39          LineWithInfo row = new LineWithInfo();
40          assertNull(row.reportOnColumn(new Column()));
41      }
42  
43      @Test
44      public void getReportOnColumnIndex0() {
45          LineWithInfo row = addString(new LineWithInfo(), "Hello");
46          assertThrows(CsvException.class, () ->  {
47              row.reportOnColumn(new Column(0));
48          });
49      }
50  
51      @Test
52      public void simpleWord() {
53          LineWithInfo row = new LineWithInfo();
54          row = addString(row, "Hello");
55          RowReport report = row.reportOnColumn(new Column());
56          assertEquals("Hello", report.getRow());
57          assertEquals(0, report.getStart());
58          assertEquals(5, report.getEnd());
59      }
60  
61      @Test
62      public void coupleOfWords() {
63          LineWithInfo row = new LineWithInfo();
64          row = addString(row, "Alpha");
65          row.addCharacter(';');
66          row = addString(row, "Beta");
67          row.addCharacter(';');
68          row = addString(row, "Gamma");
69          RowReport report = row.reportOnColumn(new Column(3));
70          assertEquals("Alpha;Beta;Gamma", report.getRow());
71          assertEquals(11, report.getStart());
72          assertEquals(16, report.getEnd());
73      }
74  
75      @Test
76      public void variousNonPrintables() {
77          LineWithInfo row = new LineWithInfo();
78          row = addString(row, "Alpha");
79          row.addCharacter('\t');
80          row = addString(row, "Beta");
81          row.addCharacter('\t');
82          row = addString(row, "Gamma");
83          RowReport report = row.reportOnColumn(new Column(3));
84          assertEquals("Alpha\\tBeta\\tGamma", report.getRow());
85          assertEquals(13, report.getStart());
86          assertEquals(18, report.getEnd());
87      }
88  
89      @Test
90      public void reportOnEndOfLine() {
91          LineWithInfo row = new LineWithInfo();
92          row = addString(row, "Alpha");
93          row.addCharacter('\t');
94          row = addString(row, "Beta");
95          row.addCharacter('@');
96          RowReport report = row.reportOnEndOfLine();
97          assertEquals("Alpha\\tBeta@", report.getRow());
98          assertEquals(12, report.getStart());
99          assertEquals(12, report.getEnd());
100     }
101 
102     protected LineWithInfo addString(LineWithInfo row, String text) {
103         row.markStartOfColumn();
104         for (char character : text.toCharArray()) {
105             row.addCharacter(character);
106         }
107         row.addCell(text);
108         return row;
109     }
110 
111 }