-
Notifications
You must be signed in to change notification settings - Fork 29
/
content_arrangement_test.rs
352 lines (322 loc) · 13.4 KB
/
content_arrangement_test.rs
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
use comfy_table::ColumnConstraint;
use comfy_table::Width;
use pretty_assertions::assert_eq;
use comfy_table::ColumnConstraint::*;
use comfy_table::Width::*;
use comfy_table::{ContentArrangement, Row, Table};
use super::assert_table_line_width;
/// Test the robustnes of the dynamic table arangement.
#[test]
fn simple_dynamic_table() {
let mut table = Table::new();
table.set_header(&vec!["Header1", "Header2", "Head"])
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(25)
.add_row(&vec![
"This is a very long line with a lot of text",
"This is anotherverylongtextwithlongwords text",
"smol",
])
.add_row(&vec![
"This is another text",
"Now let's\nadd a really long line in the middle of the cell \n and add more multi line stuff",
"smol",
]);
println!("{table}");
let expected = "
+--------+-------+------+
| Header | Heade | Head |
| 1 | r2 | |
+=======================+
| This | This | smol |
| is a | is | |
| very | anoth | |
| long | erver | |
| line | ylong | |
| with a | textw | |
| lot of | ithlo | |
| text | ngwor | |
| | ds | |
| | text | |
|--------+-------+------|
| This | Now | smol |
| is ano | let's | |
| ther | add a | |
| text | reall | |
| | y | |
| | long | |
| | line | |
| | in | |
| | the | |
| | middl | |
| | e of | |
| | the | |
| | cell | |
| | and | |
| | add | |
| | more | |
| | multi | |
| | line | |
| | stuff | |
+--------+-------+------+";
println!("{expected}");
assert_table_line_width(&table, 25);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// Individual rows can be configured to have a max height.
/// Everything beyond that line height should be truncated.
#[test]
fn table_with_truncate() {
let mut table = Table::new();
let mut first_row: Row = Row::from(vec![
"This is a very long line with a lot of text",
"This is anotherverylongtextwithlongwords text",
"smol",
]);
first_row.max_height(4);
let mut second_row = Row::from(vec![
"Now let's\nadd a really long line in the middle of the cell \n and add more multi line stuff",
"This is another text",
"smol",
]);
second_row.max_height(4);
table
.set_header(&vec!["Header1", "Header2", "Head"])
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(35)
.add_row(first_row)
.add_row(second_row);
// The first column will be wider than 6 chars.
// The second column's content is wider than 6 chars. There should be a '...'.
let second_column = table.column_mut(1).unwrap();
second_column.set_constraint(Absolute(Fixed(8)));
// The third column's content is less than 6 chars width. There shouldn't be a '...'.
let third_column = table.column_mut(2).unwrap();
third_column.set_constraint(Absolute(Fixed(7)));
println!("{table}");
let expected = "
+----------------+--------+-------+
| Header1 | Header | Head |
| | 2 | |
+=================================+
| This is a very | This | smol |
| long line with | is ano | |
| a lot of text | therve | |
| | ryl... | |
|----------------+--------+-------|
| Now let's | This | smol |
| add a really | is ano | |
| long line in | ther | |
| the middle ... | text | |
+----------------+--------+-------+";
println!("{expected}");
assert_table_line_width(&table, 35);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// This table checks the scenario, where a column has a big max_width, but a lot of the assigned
/// space doesn't get used after splitting the lines. This happens mostly when there are
/// many long words in a single column.
/// The remaining space should rather be distributed to other cells.
#[test]
fn distribute_space_after_split() {
let mut table = Table::new();
table
.set_header(&vec!["Header1", "Header2", "Head"])
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(80)
.add_row(&vec![
"This is a very long line with a lot of text",
"This is text with a anotherverylongtexttesttest",
"smol",
]);
println!("{table}");
let expected = "
+-----------------------------------------+-----------------------------+------+
| Header1 | Header2 | Head |
+==============================================================================+
| This is a very long line with a lot of | This is text with a | smol |
| text | anotherverylongtexttesttest | |
+-----------------------------------------+-----------------------------+------+";
println!("{expected}");
assert_table_line_width(&table, 80);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// A single column get's split and a lot of the available isn't used afterward.
/// The remaining space should be cut away, making the table more compact.
#[test]
fn unused_space_after_split() {
let mut table = Table::new();
table
.set_header(&vec!["Header1"])
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(30)
.add_row(&vec!["This is text with a anotherverylongtext"]);
println!("{table}");
let expected = "
+---------------------+
| Header1 |
+=====================+
| This is text with a |
| anotherverylongtext |
+---------------------+";
println!("{expected}");
assert_table_line_width(&table, 23);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// The full width of a table should be used, even if the space isn't used.
#[test]
fn dynamic_full_width_after_split() {
let mut table = Table::new();
table
.set_header(&vec!["Header1"])
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_width(50)
.add_row(&vec!["This is text with a anotherverylongtexttesttestaa"]);
println!("{table}");
let expected = "
+------------------------------------------------+
| Header1 |
+================================================+
| This is text with a |
| anotherverylongtexttesttestaa |
+------------------------------------------------+";
println!("{expected}");
assert_table_line_width(&table, 50);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// This table checks the scenario, where a column has a big max_width, but a lot of the assigned
/// space isn't used after splitting the lines.
/// The remaining space should rather distributed between all cells.
#[test]
fn dynamic_full_width() {
let mut table = Table::new();
table
.set_header(&vec!["Header1", "Header2", "smol"])
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_width(80)
.add_row(&vec!["This is a short line", "small", "smol"]);
println!("{table}");
let expected = "
+-----------------------------------+----------------------+-------------------+
| Header1 | Header2 | smol |
+==============================================================================+
| This is a short line | small | smol |
+-----------------------------------+----------------------+-------------------+";
println!("{expected}");
assert_table_line_width(&table, 80);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// Test that a table is displayed in its full width, if the `table.width` is set to the exact
/// width the table has, if it's fully expanded.
///
/// The same should be the case for values that're larget than this width.
#[test]
fn dynamic_exact_width() {
let header = vec!["a\n---\ni64", "b\n---\ni64", "b_squared\n---\nf64"];
let rows = vec![
vec!["1", "2", "4.0"],
vec!["3", "4", "16.0"],
vec!["5", "6", "36.0"],
];
for width in 25..40 {
let mut table = Table::new();
let table = table
.load_preset(comfy_table::presets::UTF8_FULL)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(width);
table.set_header(header.clone()).add_rows(rows.clone());
println!("{table}");
let expected = "
┌─────┬─────┬───────────┐
│ a ┆ b ┆ b_squared │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 │
╞═════╪═════╪═══════════╡
│ 1 ┆ 2 ┆ 4.0 │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 3 ┆ 4 ┆ 16.0 │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
│ 5 ┆ 6 ┆ 36.0 │
└─────┴─────┴───────────┘";
println!("{expected}");
assert_table_line_width(&table, 25);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
}
/// Test that the formatting works as expected, if the table is slightly smaller than the max width
/// of the table.
#[test]
fn dynamic_slightly_smaller() {
let header = vec!["a\n---\ni64", "b\n---\ni64", "b_squared\n---\nf64"];
let rows = vec![
vec!["1", "2", "4.0"],
vec!["3", "4", "16.0"],
vec!["5", "6", "36.0"],
];
let mut table = Table::new();
let table = table
.load_preset(comfy_table::presets::UTF8_FULL)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(24);
table.set_header(header.clone()).add_rows(rows.clone());
println!("{table}");
let expected = "
┌─────┬─────┬──────────┐
│ a ┆ b ┆ b_square │
│ --- ┆ --- ┆ d │
│ i64 ┆ i64 ┆ --- │
│ ┆ ┆ f64 │
╞═════╪═════╪══════════╡
│ 1 ┆ 2 ┆ 4.0 │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 3 ┆ 4 ┆ 16.0 │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 5 ┆ 6 ┆ 36.0 │
└─────┴─────┴──────────┘";
println!("{expected}");
assert_table_line_width(&table, 24);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
/// This failed on a python integration test case in the polars project.
/// This a regression test.
#[test]
fn polar_python_test_tbl_width_chars() {
let header = vec![
"a really long col\n---\ni64",
"b\n---\nstr",
"this is 10\n---\ni64",
];
let rows = vec![
vec!["1", "", "4"],
vec!["2", "this is a string value that will...", "5"],
vec!["3", "null", "6"],
];
let mut table = Table::new();
let table = table
.load_preset(comfy_table::presets::UTF8_FULL)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(100)
.set_header(header)
.add_rows(rows)
.set_constraints(vec![
ColumnConstraint::LowerBoundary(Width::Fixed(12)),
ColumnConstraint::LowerBoundary(Width::Fixed(5)),
ColumnConstraint::LowerBoundary(Width::Fixed(10)),
]);
println!("{table}");
let expected = "
┌───────────────────┬─────────────────────────────────────┬────────────┐
│ a really long col ┆ b ┆ this is 10 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞═══════════════════╪═════════════════════════════════════╪════════════╡
│ 1 ┆ ┆ 4 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2 ┆ this is a string value that will... ┆ 5 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3 ┆ null ┆ 6 │
└───────────────────┴─────────────────────────────────────┴────────────┘";
println!("{expected}");
assert_table_line_width(&table, 72);
assert_eq!("\n".to_string() + &table.to_string(), expected);
}