Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_csv.d

raw
1 @safe unittest
2 {
3 import std.csv;
4
5 import std.exception : collectException;
6 import std.algorithm.searching : count;
7 string text = "a,b,c\nHello,65";
8 auto ex = collectException!CSVException(csvReader(text).count);
9 assert(ex.toString == "(Row: 0, Col: 0) Row 2's length 2 does not match previous length of 3.");
10 }
11
12 @safe unittest
13 {
14 import std.csv;
15
16 import std.exception : collectException;
17 import std.algorithm.searching : count;
18 import std.typecons : Tuple;
19 string text = "a,b\nHello,65";
20 auto ex = collectException!CSVException(csvReader!(Tuple!(string,int))(text).count);
21 assert(ex.toString == "(Row: 1, Col: 2) Unexpected 'b' when converting from type string to type int");
22 }
23
24 @safe unittest
25 {
26 import std.csv;
27
28 import std.exception : assertThrown;
29 string text = "a,\"b,c\nHello,65,2.5";
30 assertThrown!IncompleteCellException(text.csvReader(["a","b","c"]));
31 }
32
33 @safe unittest
34 {
35 import std.csv;
36
37 import std.exception : assertThrown;
38 string text = "a,b,c\nHello,65,2.5";
39 assertThrown!HeaderMismatchException(text.csvReader(["b","c","invalid"]));
40 }
41
42 @safe unittest
43 {
44 import std.csv;
45
46 import std.algorithm.comparison : equal;
47 import std.algorithm.searching : count;
48 import std.exception : assertThrown;
49
50 string text = "a,b,c\nHello,65,\"2.5";
51 assertThrown!IncompleteCellException(text.csvReader.count);
52
53 // ignore the exceptions and try to handle invalid CSV
54 auto firstLine = text.csvReader!(string, Malformed.ignore)(null).front;
55 assert(firstLine.equal(["Hello", "65", "2.5"]));
56 }
57
58 @safe unittest
59 {
60 import std.csv;
61
62 import std.algorithm.comparison : equal;
63 string text = "76,26,22";
64 auto records = text.csvReader!int;
65 assert(records.equal!equal([
66 [76, 26, 22],
67 ]));
68 }
69
70 @safe unittest
71 {
72 import std.csv;
73
74 import std.algorithm.comparison : equal;
75 string text = "Hello;65;2.5\nWorld;123;7.5";
76 struct Layout
77 {
78 string name;
79 int value;
80 double other;
81 }
82
83 auto records = text.csvReader!Layout(';');
84 assert(records.equal([
85 Layout("Hello", 65, 2.5),
86 Layout("World", 123, 7.5),
87 ]));
88 }
89
90 @safe unittest
91 {
92 import std.csv;
93
94 string text = "A \" is now part of the data";
95 auto records = text.csvReader!(string, Malformed.ignore);
96 auto record = records.front;
97
98 assert(record.front == text);
99 }
100
101 @safe unittest
102 {
103 import std.csv;
104
105 import std.algorithm.comparison : equal;
106 string text = "a,b,c\nHello,65,63.63\nWorld,123,3673.562";
107 auto records = text.csvReader!int(["b"]);
108
109 assert(records.equal!equal([
110 [65],
111 [123],
112 ]));
113 }
114
115 @safe unittest
116 {
117 import std.csv;
118
119 import std.algorithm.comparison : equal;
120 string text = "a,b,c\nHello,65,2.5\nWorld,123,7.5";
121 struct Layout
122 {
123 int value;
124 double other;
125 string name;
126 }
127
128 auto records = text.csvReader!Layout(["b","c","a"]);
129 assert(records.equal([
130 Layout(65, 2.5, "Hello"),
131 Layout(123, 7.5, "World")
132 ]));
133 }
134
135 @safe unittest
136 {
137 import std.csv;
138
139 string text = "a,b,c\nHello,65,63.63";
140 auto records = text.csvReader(null);
141
142 assert(records.header == ["a","b","c"]);
143 }
144
145 @safe unittest
146 {
147 import std.csv;
148
149 import std.algorithm.comparison : equal;
150
151 string text = "76,26,22\n1,2\n3,4,5,6";
152 auto records = text.csvReader!int(',', '"', true);
153
154 assert(records.equal!equal([
155 [76, 26, 22],
156 [1, 2],
157 [3, 4, 5, 6]
158 ]));
159 }
160
161 @safe unittest
162 {
163 import std.csv;
164
165 import std.algorithm.comparison : equal;
166
167 static struct Three
168 {
169 int a;
170 int b;
171 int c;
172 }
173
174 string text = "76,26,22\n1,2\n3,4,5,6";
175 auto records = text.csvReader!Three(',', '"', true);
176
177 assert(records.equal([
178 Three(76, 26, 22),
179 Three(1, 2, 0),
180 Three(3, 4, 5)
181 ]));
182 }
183
184 @safe unittest
185 {
186 import std.csv;
187
188 import std.algorithm.comparison : equal;
189
190 auto text = "Name,Occupation,Salary\r" ~
191 "Joe,Carpenter,300000\nFred,Blacksmith\r\n";
192
193 auto r = csvReader!(string[string])(text, null, ',', '"', true);
194
195 assert(r.equal([
196 [ "Name" : "Joe", "Occupation" : "Carpenter", "Salary" : "300000" ],
197 [ "Name" : "Fred", "Occupation" : "Blacksmith" ]
198 ]));
199 }
200
201 @safe unittest
202 {
203 import std.csv;
204
205 import std.array : appender;
206 import std.range.primitives : popFront;
207
208 string str = "65,63\n123,3673";
209
210 auto a = appender!(char[])();
211
212 csvNextToken(str,a,',','"');
213 assert(a.data == "65");
214 assert(str == ",63\n123,3673");
215
216 str.popFront();
217 a.shrinkTo(0);
218 csvNextToken(str,a,',','"');
219 assert(a.data == "63");
220 assert(str == "\n123,3673");
221
222 str.popFront();
223 a.shrinkTo(0);
224 csvNextToken(str,a,',','"');
225 assert(a.data == "123");
226 assert(str == ",3673");
227 }
228