Debian Codegraph @

debian/rust-vendor/uutils_term_grid/tests/test.rs

raw
1 // For the full copyright and license information, please view the LICENSE
2 // file that was distributed with this source code.
3
4 // spell-checker:ignore underflowed
5
6 use term_grid::{Direction, Filling, Grid, GridOptions, DEFAULT_SEPARATOR_SIZE, SPACES_IN_TAB};
7
8 #[test]
9 fn no_items() {
10 let grid = Grid::new(
11 Vec::<String>::new(),
12 GridOptions {
13 direction: Direction::TopToBottom,
14 filling: Filling::Spaces(2),
15 width: 40,
16 },
17 );
18
19 assert_eq!("", grid.to_string());
20 }
21
22 #[test]
23 fn one_item() {
24 let grid = Grid::new(
25 vec!["1"],
26 GridOptions {
27 direction: Direction::TopToBottom,
28 filling: Filling::Spaces(2),
29 width: 40,
30 },
31 );
32 assert_eq!("1\n", grid.to_string());
33 }
34
35 #[test]
36 fn one_item_exact_width() {
37 let grid = Grid::new(
38 vec!["1234567890"],
39 GridOptions {
40 direction: Direction::TopToBottom,
41 filling: Filling::Spaces(2),
42 width: 10,
43 },
44 );
45
46 assert_eq!("1234567890\n", grid.to_string());
47 }
48
49 #[test]
50 fn one_item_just_over() {
51 let grid = Grid::new(
52 vec!["1234567890!"],
53 GridOptions {
54 direction: Direction::TopToBottom,
55 filling: Filling::Spaces(2),
56 width: 10,
57 },
58 );
59
60 assert_eq!(grid.row_count(), 1);
61 }
62
63 #[test]
64 fn two_small_items() {
65 let grid = Grid::new(
66 vec!["1", "2"],
67 GridOptions {
68 direction: Direction::TopToBottom,
69 filling: Filling::Spaces(2),
70 width: 40,
71 },
72 );
73
74 assert_eq!(grid.width(), 1 + 2 + 1);
75 assert_eq!("1 2\n", grid.to_string());
76 }
77
78 #[test]
79 fn two_medium_size_items() {
80 let grid = Grid::new(
81 vec!["hello there", "how are you today?"],
82 GridOptions {
83 direction: Direction::TopToBottom,
84 filling: Filling::Spaces(2),
85 width: 40,
86 },
87 );
88
89 assert_eq!(grid.width(), 11 + 2 + 18);
90 assert_eq!("hello there how are you today?\n", grid.to_string());
91 }
92
93 #[test]
94 fn two_big_items() {
95 let grid = Grid::new(
96 vec![
97 "nuihuneihsoenhisenouiuteinhdauisdonhuisudoiosadiuohnteihaosdinhteuieudi",
98 "oudisnuthasuouneohbueobaugceoduhbsauglcobeuhnaeouosbubaoecgueoubeohubeo",
99 ],
100 GridOptions {
101 direction: Direction::TopToBottom,
102 filling: Filling::Spaces(2),
103 width: 40,
104 },
105 );
106
107 assert_eq!(grid.row_count(), 2);
108 }
109
110 #[test]
111 fn that_example_from_earlier() {
112 let grid = Grid::new(
113 vec![
114 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
115 "eleven", "twelve",
116 ],
117 GridOptions {
118 filling: Filling::Spaces(1),
119 direction: Direction::LeftToRight,
120 width: 24,
121 },
122 );
123
124 let bits = "one two three four\nfive six seven eight\nnine ten eleven twelve\n";
125 assert_eq!(grid.to_string(), bits);
126 assert_eq!(grid.row_count(), 3);
127 }
128
129 #[test]
130 fn number_grid_with_pipe() {
131 let grid = Grid::new(
132 vec![
133 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
134 "eleven", "twelve",
135 ],
136 GridOptions {
137 filling: Filling::Text("|".into()),
138 direction: Direction::LeftToRight,
139 width: 24,
140 },
141 );
142
143 let bits = "one |two|three |four\nfive|six|seven |eight\nnine|ten|eleven|twelve\n";
144 assert_eq!(grid.to_string(), bits);
145 assert_eq!(grid.row_count(), 3);
146 }
147
148 #[test]
149 fn huge_separator() {
150 let grid = Grid::new(
151 vec!["a", "b"],
152 GridOptions {
153 filling: Filling::Spaces(100),
154 direction: Direction::LeftToRight,
155 width: 99,
156 },
157 );
158 assert_eq!(grid.row_count(), 2);
159 }
160
161 #[test]
162 fn huge_yet_unused_separator() {
163 let grid = Grid::new(
164 vec!["abcd"],
165 GridOptions {
166 filling: Filling::Spaces(100),
167 direction: Direction::LeftToRight,
168 width: 99,
169 },
170 );
171
172 assert_eq!(grid.width(), 4);
173 assert_eq!("abcd\n", grid.to_string());
174 }
175
176 // Note: This behaviour is right or wrong depending on your terminal
177 // This test is mostly added so that we don't change our current
178 // behaviour, unless we explicitly want to do that.
179 #[test]
180 fn emoji() {
181 let grid = Grid::new(
182 vec!["šŸ¦€", "hello", "šŸ‘©ā€šŸ”¬", "hello"],
183 GridOptions {
184 direction: Direction::LeftToRight,
185 filling: Filling::Spaces(2),
186 width: 12,
187 },
188 );
189 assert_eq!("šŸ¦€ hello\nšŸ‘©ā€šŸ”¬ hello\n", grid.to_string());
190 }
191
192 // This test once underflowed, which should never happen. The test is just
193 // checking that we do not get a panic.
194 #[test]
195 fn possible_underflow() {
196 let cells: Vec<_> = (0..48).map(|i| 2_i64.pow(i).to_string()).collect();
197
198 let grid = Grid::new(
199 cells,
200 GridOptions {
201 direction: Direction::TopToBottom,
202 filling: Filling::Text(" | ".into()),
203 width: 15,
204 },
205 );
206
207 println!("{grid}");
208 }
209
210 #[test]
211 fn exact_fit() {
212 let grid = Grid::new(
213 vec!["a", "b", "c", "d"],
214 GridOptions {
215 direction: Direction::TopToBottom,
216 filling: Filling::Spaces(2),
217 width: 4,
218 },
219 );
220
221 assert_eq!(grid.row_count(), 2);
222 }
223
224 // This is a reproduction of https://github.com/eza-community/eza/issues/845
225 #[test]
226 fn eza_many_folders() {
227 let cells: Vec<_> = (100_000_i32..=100_401).map(|i| i.to_string()).collect();
228
229 let grid = Grid::new(
230 cells,
231 GridOptions {
232 direction: Direction::TopToBottom,
233 filling: Filling::Spaces(2),
234 width: 166,
235 },
236 );
237
238 assert_eq!(grid.row_count(), 20);
239 }
240
241 #[test]
242 fn filling_with_tabs() {
243 let grid = Grid::new(
244 vec![
245 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
246 "eleven", "twelve",
247 ],
248 GridOptions {
249 direction: Direction::LeftToRight,
250 filling: Filling::Tabs {
251 spaces: DEFAULT_SEPARATOR_SIZE,
252 tab_size: 2,
253 },
254 width: 24,
255 },
256 );
257
258 let bits = "one\t\t two\t\t three\nfour\t five\t\t six\nseven\t eight\t nine\nten\t\t eleven\t twelve\n";
259 assert_eq!(grid.to_string(), bits);
260 assert_eq!(grid.row_count(), 4);
261 }
262
263 #[test]
264 fn padding_bigger_than_widest() {
265 let grid = Grid::new(
266 vec!["1", "2", "3"],
267 GridOptions {
268 direction: Direction::LeftToRight,
269 filling: Filling::Tabs {
270 spaces: DEFAULT_SEPARATOR_SIZE,
271 tab_size: SPACES_IN_TAB,
272 },
273 width: 20,
274 },
275 );
276
277 let bits = "1 2 3\n";
278 assert_eq!(grid.to_string(), bits);
279 }
280
281 #[test]
282 fn odd_number_of_entries() {
283 let cells = vec!["one", "two", "three", "four", "five"];
284 let grid = Grid::new(
285 cells.clone(),
286 GridOptions {
287 direction: Direction::LeftToRight,
288 filling: Filling::Spaces(2),
289 width: 15,
290 },
291 );
292
293 assert_eq!(grid.to_string(), "one two\nthree four\nfive\n");
294
295 let grid = Grid::new(
296 cells.clone(),
297 GridOptions {
298 direction: Direction::TopToBottom,
299 filling: Filling::Spaces(2),
300 width: 15,
301 },
302 );
303
304 assert_eq!(grid.to_string(), "one four\ntwo five\nthree\n");
305 }
306
307 #[test]
308 fn different_size_separator_with_tabs() {
309 let grid = Grid::new(
310 vec![
311 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
312 "eleven", "twelve",
313 ],
314 GridOptions {
315 direction: Direction::LeftToRight,
316 filling: Filling::Tabs {
317 spaces: 4,
318 tab_size: 2,
319 },
320 width: 40,
321 },
322 );
323
324 let bits = "one\t\t\ttwo\t\t three\t\t four\nfive\t\tsix\t\t seven\t\t eight\nnine\t\tten\t\t eleven\t\t twelve\n";
325 assert_eq!(grid.to_string(), bits);
326 }
327
328 #[test]
329 fn use_max_possible_width() {
330 let grid = Grid::new(
331 vec![
332 "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9",
333 "test10", "test11",
334 ],
335 GridOptions {
336 filling: Filling::Text("||".to_string()),
337 direction: Direction::LeftToRight,
338 width: 69,
339 },
340 );
341
342 let bits = "test1 ||test2 ||test3||test4||test5||test6||test7||test8||test9\ntest10||test11\n";
343
344 assert_eq!(grid.to_string(), bits);
345 assert_eq!(grid.row_count(), 2);
346 }
347
348 #[test]
349 fn dont_use_max_possible_width() {
350 let grid = Grid::new(
351 vec![
352 "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9",
353 "test10", "test11",
354 ],
355 GridOptions {
356 filling: Filling::Text("||".to_string()),
357 direction: Direction::TopToBottom,
358 width: 69,
359 },
360 );
361
362 let bits = "test1||test3||test5||test7||test9 ||test11\ntest2||test4||test6||test8||test10\n";
363
364 assert_eq!(grid.to_string(), bits);
365 assert_eq!(grid.row_count(), 2);
366 }
367
368 #[test]
369 fn use_minimal_optimal_lines() {
370 let grid = Grid::new(
371 vec!["a", "b", "ccc", "ddd"],
372 GridOptions {
373 direction: Direction::TopToBottom,
374 filling: Filling::Spaces(2),
375 width: 6,
376 },
377 );
378
379 let expected = "a ccc\nb ddd\n";
380 assert_eq!(grid.to_string(), expected);
381 }
382
383 #[test]
384 fn weird_column_edge_case() {
385 // Here, 5 columns fit while fewer columns don't. So if we exit too early
386 // while increasing columns, we don't find the optimal solution.
387 let grid = Grid::new(
388 vec!["0", "1", "222222222", "333333333", "4", "5", "6", "7", "8"],
389 GridOptions {
390 direction: Direction::TopToBottom,
391 filling: Filling::Spaces(2),
392 width: 21,
393 },
394 );
395
396 let expected = "\
397 0 222222222 4 6 8\n\
398 1 333333333 5 7\n\
399 ";
400
401 println!("{grid}");
402 assert_eq!(grid.to_string(), expected);
403 }
404
405 // Regression test for the off-by-one in the column-search loop: when cells
406 // have varying widths, all cells can sometimes fit on a single row even though
407 // the worst-case (widest cell) estimate says they can't. The loop must try
408 // num_columns == cells.len() (..= rather than ..).
409 //
410 // Setup: cells ["aaa","b","c","d"], separator 1, width 9.
411 // widest_column = 3+1 = 4 → min_columns = (9+1)/4 = 2
412 // Loop with bug (2+1 .. 4): tries only 3 cols → 2 rows
413 // Loop with fix (2+1 ..= 4): also tries 4 cols → sum 3+1+1+1=6 ≤ 9-3=6 → 1 row āœ“
414 #[test]
415 fn all_cells_fit_on_one_row_when_widths_vary() {
416 let grid = Grid::new(
417 vec!["aaa", "b", "c", "d"],
418 GridOptions {
419 direction: Direction::LeftToRight,
420 filling: Filling::Spaces(1),
421 width: 9,
422 },
423 );
424
425 assert_eq!(grid.row_count(), 1);
426 assert_eq!(grid.to_string(), "aaa b c d\n");
427 }
428
429 // These test are based on the tests in uutils ls, to ensure we won't break
430 // it while editing this library.
431 mod uutils_ls {
432 use super::*;
433
434 #[test]
435 fn different_widths() {
436 for (width, expected) in [
437 (
438 100,
439 "test-width-1 test-width-2 test-width-3 test-width-4\n",
440 ),
441 (
442 50,
443 "test-width-1 test-width-3\ntest-width-2 test-width-4\n",
444 ),
445 (
446 25,
447 "test-width-1\ntest-width-2\ntest-width-3\ntest-width-4\n",
448 ),
449 ] {
450 let grid = Grid::new(
451 vec![
452 "test-width-1",
453 "test-width-2",
454 "test-width-3",
455 "test-width-4",
456 ],
457 GridOptions {
458 direction: Direction::TopToBottom,
459 filling: Filling::Spaces(2),
460 width,
461 },
462 );
463 assert_eq!(expected, grid.to_string());
464 }
465 }
466
467 #[test]
468 fn across_width_30() {
469 let grid = Grid::new(
470 vec![
471 "test-across1",
472 "test-across2",
473 "test-across3",
474 "test-across4",
475 ],
476 GridOptions {
477 direction: Direction::LeftToRight,
478 filling: Filling::Spaces(2),
479 width: 30,
480 },
481 );
482
483 assert_eq!(
484 "test-across1 test-across2\ntest-across3 test-across4\n",
485 grid.to_string()
486 );
487 }
488
489 #[test]
490 fn columns_width_30() {
491 let grid = Grid::new(
492 vec![
493 "test-columns1",
494 "test-columns2",
495 "test-columns3",
496 "test-columns4",
497 ],
498 GridOptions {
499 direction: Direction::TopToBottom,
500 filling: Filling::Spaces(2),
501 width: 30,
502 },
503 );
504
505 assert_eq!(
506 "test-columns1 test-columns3\ntest-columns2 test-columns4\n",
507 grid.to_string()
508 );
509 }
510
511 #[test]
512 fn three_short_one_long() {
513 let grid = Grid::new(
514 vec!["a", "b", "a-long-name", "z"],
515 GridOptions {
516 direction: Direction::TopToBottom,
517 filling: Filling::Spaces(2),
518 width: 15,
519 },
520 );
521
522 assert_eq!("a a-long-name\nb z\n", grid.to_string());
523 }
524 }