1
2
3
4
5
6
7
8
9
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
24
25 class LineWithInfoTest {
26
27
28
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
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
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
61
62 @Test
63 void nonExistingCell() {
64 LineWithInfo row = new LineWithInfo();
65 assertNull(row.reportOnColumn(new Column()));
66 }
67
68
69
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
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
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
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
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
142
143
144
145
146
147
148
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 }