-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
504 lines (438 loc) · 18.6 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Author: Ozan Irsoy
#define CATCH_CONFIG_MAIN
#include <iostream>
#include <catch.hpp>
#include <tblr.h>
TEST_CASE("Basic Layout", "[basic]") {
SECTION("basic") {
using namespace tblr;
Table t;
t.widths({15, 6, 6, 7}).aligns({Left, Center, Center, Center});
// Disabling clang-format because of this bug:
// https://bugs.llvm.org/show_bug.cgi?id=45018
// clang-format off
t << "Model" << "Precision" << "Recall" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "Model Precis Recall F-score\n"
" ion \n"
"myModel1 0.1 0.2 0.3 \n"
"a model 3 0.4 0.5 0.6 \n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("multiline with space") {
using namespace tblr;
Table t;
t.widths({30, 15}).aligns({Left, Left}).multiline(Space);
t << "a function that is continuous everywhere and"
" differentiable nowhere"
<< "weierstrass function" << endr;
t << "an uncountable set of points that have zero length"
<< "cantor set" << endr;
t << "a continuous curve that covers the entire unit square"
<< "peano curve" << endr;
std::string expected = "a function that is continuous weierstrass \n"
"everywhere and differentiable function \n"
"nowhere \n"
"an uncountable set of points cantor set \n"
"that have zero length \n"
"a continuous curve that peano curve \n"
"covers the entire unit square \n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("multiline with space long") {
using namespace tblr;
Table t;
t.widths({2, 10}).aligns({Left, Left}).multiline(Space);
t << 1 << "short loooooong" << endr //
<< 2 << "short loooooooong" << endr //
<< 3 << "short loooooooong short" << endr //
<< 4 << "short loooooooong short short" << endr; //
std::string expected = "1 short \n"
" loooooong \n"
"2 short \n"
" loooooooon\n"
" g \n"
"3 short \n"
" loooooooon\n"
" g short \n"
"4 short \n"
" loooooooon\n"
" g short \n"
" short \n";
std::stringstream ss;
ss << t;
CHECK(ss.str() == expected);
}
}
TEST_CASE("Custom Layout", "[custom]") {
auto layout = tblr::simple_border("| ", " | ", " |", "^", "=", "-", "vV");
SECTION("single line") {
tblr::Table t;
t.widths({15, 5})
.aligns({tblr::Left, tblr::Right})
.multiline(tblr::SingleLine)
.layout(layout);
// clang-format off
t << "Model" << "Precision" << "Recall" << "F-score" << tblr::endr
<< (tblr::Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << tblr::endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << tblr::endr;
// clang-format on
std::string expected = "| ^^^^^^^^^^^^^^^ | ^^^^^ | ^^^^^^ | ^^^^^^^ |\n"
"| Model | Preci | Recall | F-score |\n"
"| =============== | ===== | ====== | ======= |\n"
"| myModel1 | 0.1 | 0.2 | 0.3 |\n"
"| --------------- | ----- | ------ | ------- |\n"
"| a model 3 | 0.4 | 0.5 | 0.6 |\n"
"| vVvVvVvVvVvVvVv | vVvVv | vVvVvV | vVvVvVv |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("multiline") {
using namespace tblr;
Table t;
t.widths({15, 5}).aligns({Left, Right}).layout(layout);
// clang-format off
t << "Model" << "Precision" << "Rec\nall\n" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "| ^^^^^^^^^^^^^^^ | ^^^^^ | ^^^ | ^^^^^^^ |\n"
"| Model | Preci | Rec | F-score |\n"
"| | sion | all | |\n"
"| =============== | ===== | === | ======= |\n"
"| myModel1 | 0.1 | 0.2 | 0.3 |\n"
"| --------------- | ----- | --- | ------- |\n"
"| a model 3 | 0.4 | 0.5 | 0.6 |\n"
"| vVvVvVvVvVvVvVv | vVvVv | vVv | vVvVvVv |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("extra line") {
using namespace tblr;
Table t;
t.widths({15, 5}).aligns({Left, Right}).layout(layout);
// clang-format off
t << "Model" << "Precision" << "Rec\nall\n\n" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "| ^^^^^^^^^^^^^^^ | ^^^^^ | ^^^ | ^^^^^^^ |\n"
"| Model | Preci | Rec | F-score |\n"
"| | sion | all | |\n"
"| | | | |\n"
"| =============== | ===== | === | ======= |\n"
"| myModel1 | 0.1 | 0.2 | 0.3 |\n"
"| --------------- | ----- | --- | ------- |\n"
"| a model 3 | 0.4 | 0.5 | 0.6 |\n"
"| vVvVvVvVvVvVvVv | vVvVv | vVv | vVvVvVv |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("extra line cr") {
using namespace tblr;
Table t;
t.widths({15, 5}).aligns({Left, Right}).layout(layout);
// clang-format off
t << "Model" << "Precision" << "Rec\rall\r\r" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "| ^^^^^^^^^^^^^^^ | ^^^^^ | ^^^ | ^^^^^^^ |\n"
"| Model | Preci | Rec | F-score |\n"
"| | sion | all | |\n"
"| | | | |\n"
"| =============== | ===== | === | ======= |\n"
"| myModel1 | 0.1 | 0.2 | 0.3 |\n"
"| --------------- | ----- | --- | ------- |\n"
"| a model 3 | 0.4 | 0.5 | 0.6 |\n"
"| vVvVvVvVvVvVvVv | vVvVv | vVv | vVvVvVv |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("extra line crlf") {
using namespace tblr;
Table t;
t.widths({15, 5}).aligns({Left, Right}).layout(layout);
// clang-format off
t << "Model" << "Precision" << "Rec\r\nall\r\n\r\n" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "| ^^^^^^^^^^^^^^^ | ^^^^^ | ^^^ | ^^^^^^^ |\n"
"| Model | Preci | Rec | F-score |\n"
"| | sion | all | |\n"
"| | | | |\n"
"| =============== | ===== | === | ======= |\n"
"| myModel1 | 0.1 | 0.2 | 0.3 |\n"
"| --------------- | ----- | --- | ------- |\n"
"| a model 3 | 0.4 | 0.5 | 0.6 |\n"
"| vVvVvVvVvVvVvVv | vVvVv | vVv | vVvVvVv |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("zero width") {
using namespace tblr;
Table t;
t.widths({0, 5}).aligns({Left, Right}).layout(layout);
// clang-format off
t << "Model" << "Precision" << "Rec\nall\n\n" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "| ^^^^^^^^^ | ^^^^^ | ^^^ | ^^^^^^^ |\n"
"| Model | Preci | Rec | F-score |\n"
"| | sion | all | |\n"
"| | | | |\n"
"| ========= | ===== | === | ======= |\n"
"| myModel1 | 0.1 | 0.2 | 0.3 |\n"
"| --------- | ----- | --- | ------- |\n"
"| a model 3 | 0.4 | 0.5 | 0.6 |\n"
"| vVvVvVvVv | vVvVv | vVv | vVvVvVv |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
}
TEST_CASE("Predefined Custom Layout", "[predefined]") {
using namespace tblr;
SECTION("markdown") {
Table t;
t.layout(markdown());
// clang-format off
t << "things to do" << "when" << endr
<< "give you up" << "never" << endr
<< "let you down" << "never" << endr
<< "run around and desert you" << "never" << endr;
// clang-format on
std::string expected = "| things to do | when |\n"
"| ------------------------- | ----- |\n"
"| give you up | never |\n"
"| let you down | never |\n"
"| run around and desert you | never |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("indented list") {
Table t;
t.layout(indented_list());
// clang-format off
t << "things to do" << "when" << endr
<< "give you up" << "never" << endr
<< "let you down" << "never" << endr
<< "run around and desert you" << "never" << endr;
// clang-format on
std::string expected = " things to do when \n"
" give you up never\n"
" let you down never\n"
" run around and desert you never\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("one column") {
Table t;
t.layout(markdown());
t << "things to do" << endr //
<< "give you up" << endr //
<< "let you down" << endr //
<< "run around and desert you" << endr; //
std::string expected = "| things to do |\n"
"| ------------------------- |\n"
"| give you up |\n"
"| let you down |\n"
"| run around and desert you |\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
SECTION("latex") {
Table t;
t.layout(latex()).aligns({Left, Right});
// clang-format off
t << "things to do" << "when" << endr
<< "give you up" << "never" << endr
<< "let you down" << "never" << endr
<< "run around and desert you" << "never" << endr;
// clang-format on
std::string expected = "\\begin{tabular}{lr}\n"
"\\hline\n"
"things to do & when \\\\\n"
"\\hline\n"
"give you up & never \\\\\n"
"let you down & never \\\\\n"
"run around and desert you & never \\\\\n"
"\\hline\n"
"\\end{tabular}\n";
std::stringstream ss;
t.print(ss);
CHECK(ss.str() == expected);
}
}
TEST_CASE("Edge Cases", "[edge]") {
using namespace tblr;
Table t;
SECTION("empty table") { CHECK(std::string(t) == ""); }
SECTION("multiple print") {
t.widths({15, 6, 6, 7}).aligns({Left, Center, Center, Center});
// clang-format off
t << "Model" << "Precision" << "Recall" << "F-score" << endr
<< (Cell() << "my" << "Model" << 1) << 0.1 << 0.2 << 0.3 << endr
<< "a model 3" << 0.4 << 0.5 << 0.6 << endr;
// clang-format on
std::string expected = "Model Precis Recall F-score\n"
" ion \n"
"myModel1 0.1 0.2 0.3 \n"
"a model 3 0.4 0.5 0.6 \n";
for (size_t i = 0; i < 3; i++) { CHECK(std::string(t) == expected); }
}
SECTION("empty column") {
t.layout(markdown());
t << endr << endr;
std::string expected = "| |\n"
"| |\n"
"| |\n";
CHECK(std::string(t) == expected);
}
SECTION("newlines only") {
auto layout = simple_border("|", "|", "|", "^", "=", "-", "v");
t.layout(layout);
t.widths({4});
t << "\n\n" << endr << "\n\n\n\n" << endr;
std::string expected = "|^^^^|\n"
"| |\n"
"| |\n"
"|====|\n"
"| |\n"
"| |\n"
"| |\n"
"| |\n"
"|vvvv|\n";
CHECK(std::string(t) == expected);
}
SECTION("length mismatch") {
t.layout(markdown());
t.widths({4}).aligns({Right, Left});
// clang-format off
t << "a" << endr
<< "b" << "c" << "d" << endr
<< endr
<< "e" << "f" << endr;
// clang-format on
std::string expected = "| a |\n"
"| ---- | - | - |\n"
"| b | c | d |\n"
"| |\n" // observe empty row (mis)alignment
"| e | f |\n";
CHECK(std::string(t) == expected);
}
SECTION("no row ending") {
// clang-format off
t << "a" << "b" << "c";
// clang-format on
// This effectively means that there are no rows in the table:
// nothing to print
CHECK(std::string(t) == "");
}
SECTION("table in table") {
Table mini;
mini.layout(markdown());
mini << "a"
<< "b" << endr << "c"
<< "d" << endr;
t.layout(extra_space());
t << mini << mini << endr << mini << mini << endr;
std::string expected = " \n"
" | a | b | | a | b | \n"
" | - | - | | - | - | \n"
" | c | d | | c | d | \n"
" \n"
" | a | b | | a | b | \n"
" | - | - | | - | - | \n"
" | c | d | | c | d | \n"
" \n";
CHECK(std::string(t) == expected);
}
}
std::string strip_ansi(const std::string& s) {
std::string rval;
size_t begin = 0;
auto [left, size] = tblr::find_ansi_esc(s);
while (left != std::string::npos) {
rval += s.substr(begin, left - begin);
begin = left + size;
std::tie(left, size) = tblr::find_ansi_esc(s, begin);
}
rval += s.substr(begin);
return rval;
}
TEST_CASE("ANSI color & styling", "[color]") {
using namespace tblr;
const auto green = "\033[1;32m";
const auto red = "\033[1;31m";
const auto blue = "\033[1;34m";
const auto underline = "\033[1;4m";
const auto clear = "\033[0m";
auto mlin = GENERATE(Space, Naive, SingleLine);
Table t;
t.layout(unicode_box_light());
t.widths({10, 10, 7}).multiline(mlin);
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows"
<< (Cell() << green << "in the cool breezy morning" << clear)
<< endr
<< "computer" << "crashes"
<< (Cell() << underline << red << "at warm co"
<< blue << "zy night" << clear)
<< endr;
// clang-format on
SECTION("Text content") {
Table plain;
plain.layout(unicode_box_light());
plain.widths({10, 10, 7}).multiline(mlin);
// clang-format off
plain << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows" << "in the cool breezy morning" << endr
<< "computer" << "crashes" << "at warm cozy night" << endr;
// clang-format on
// Check that raw text content of the styled table is the same as plain
// table
CHECK(strip_ansi(t) == std::string(plain));
}
SECTION("Output") {
if (mlin == Space) {
t.aligns({Left, Left, Right});
std::string expected =
"┌──────────┬──────────┬───────┐\n"
"│animal │does what │ when│\n"
"├──────────┼──────────┼───────┤\n"
"│dog │barks │ angry│\n"
"├──────────┼──────────┼───────┤\n"
"│rooster │crows │ [1;32min the[0m│\n"
"│ │ │ [1;32mcool[0m│\n"
"│ │ │ [1;32mbreezy[0m│\n"
"│ │ │[1;32mmorning[0m│\n"
"├──────────┼──────────┼───────┤\n"
"│computer │crashes │ [1;4m[1;31mat[0m│\n"
"│ │ │ [1;4m[1;31mwarm[0m│\n"
"│ │ │ [1;4m[1;31mco[1;34mzy[0m│\n"
"│ │ │ [1;4m[1;31m[1;34mnight[0m│\n"
"└──────────┴──────────┴───────┘\n";
CHECK(expected == std::string(t));
}
}
}