Debian Codegraph @

tests/all/simple_test.rs

raw symbols SCIP
1 use comfy_table::*;
2 use pretty_assertions::assert_eq;
3
4 #[test]
5 fn simple_table() {
6 let mut table = Table::new();
7 table
8 .set_header(vec!["Header1", "Header2", "Header3"])
9 .add_row(vec![
10 "This is a text",
11 "This is another text",
12 "This is the third text",
13 ])
14 .add_row(vec![
15 "This is another text",
16 "Now\nadd some\nmulti line stuff",
17 "This is awesome",
18 ]);
19
20 println!("{table}");
21 let expected = "
22 +----------------------+----------------------+------------------------+
23 | Header1 | Header2 | Header3 |
24 +======================================================================+
25 | This is a text | This is another text | This is the third text |
26 |----------------------+----------------------+------------------------|
27 | This is another text | Now | This is awesome |
28 | | add some | |
29 | | multi line stuff | |
30 +----------------------+----------------------+------------------------+";
31 assert_eq!(expected, "\n".to_string() + &table.to_string());
32 }
33
34 #[test]
35 fn missing_column_table() {
36 let mut table = Table::new();
37 table
38 .set_header(vec!["Header1", "Header2", "Header3"])
39 .add_row(vec!["One One", "One Two", "One Three"])
40 .add_row(vec!["Two One", "Two Two"])
41 .add_row(vec!["Three One"]);
42
43 println!("{table}");
44 let expected = "
45 +-----------+---------+-----------+
46 | Header1 | Header2 | Header3 |
47 +=================================+
48 | One One | One Two | One Three |
49 |-----------+---------+-----------|
50 | Two One | Two Two | |
51 |-----------+---------+-----------|
52 | Three One | | |
53 +-----------+---------+-----------+";
54 assert_eq!(expected, "\n".to_string() + &table.to_string());
55 }
56
57 #[test]
58 fn single_column_table() {
59 let mut table = Table::new();
60 table
61 .set_header(vec!["Header1"])
62 .add_row(vec!["One One"])
63 .add_row(vec!["Two One"])
64 .add_row(vec!["Three One"]);
65
66 println!("{table}");
67 let expected = "
68 +-----------+
69 | Header1 |
70 +===========+
71 | One One |
72 |-----------|
73 | Two One |
74 |-----------|
75 | Three One |
76 +-----------+";
77 assert_eq!(expected, "\n".to_string() + &table.to_string());
78 }
79
80 #[test]
81 fn lines() {
82 let mut t = Table::new();
83 t.set_header(["heading 1", "heading 2", "heading 3"]);
84 t.add_row(["test 1,1", "test 1,2", "test 1,3"]);
85 t.add_row(["test 2,1", "test 2,2", "test 2,3"]);
86 t.add_row(["test 3,1", "test 3,2", "test 3,3"]);
87
88 let actual = t.lines();
89 let expected = &[
90 "+-----------+-----------+-----------+",
91 "| heading 1 | heading 2 | heading 3 |",
92 "+===================================+",
93 "| test 1,1 | test 1,2 | test 1,3 |",
94 "|-----------+-----------+-----------|",
95 "| test 2,1 | test 2,2 | test 2,3 |",
96 "|-----------+-----------+-----------|",
97 "| test 3,1 | test 3,2 | test 3,3 |",
98 "+-----------+-----------+-----------+",
99 ];
100
101 assert_eq!(actual.collect::<Vec<String>>(), expected);
102 }