blob: 513287350e4848605b90a0ef5e18854cc9d402fd [file] [log] [blame]
Stephen Hines51393642024-02-02 00:10:59 -08001#![cfg(feature = "std")]
2
3use tabled::settings::{object::Cell, Border, Highlight, Margin, Modify, Span, Style, Width};
4
5use crate::matrix::Matrix;
6use testing_table::{is_lines_equal, static_table, test_table};
7
8#[cfg(feature = "color")]
9use ::{owo_colors::OwoColorize, std::convert::TryFrom, tabled::settings::Color};
10
11test_table!(
12 margin_with_table_based_on_grid_borders,
13 Matrix::new(3, 3)
14 .with(Style::extended())
15 .with(Highlight::new(Cell::new(0, 0), Border::filled('+')))
16 .with(Highlight::new(Cell::new(1, 1), Border::filled('*')))
17 .with(Margin::new(1, 2, 1, 2).fill('>', '<', 'V', '^')),
18 "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV"
19 ">+++++══════════╦══════════╦══════════╗<<"
20 ">+ N + column 0 ║ column 1 ║ column 2 ║<<"
21 ">++++************══════════╬══════════╣<<"
22 ">║ 0 * 0-0 * 0-1 ║ 0-2 ║<<"
23 ">╠═══************══════════╬══════════╣<<"
24 ">║ 1 ║ 1-0 ║ 1-1 ║ 1-2 ║<<"
25 ">╠═══╬══════════╬══════════╬══════════╣<<"
26 ">║ 2 ║ 2-0 ║ 2-1 ║ 2-2 ║<<"
27 ">╚═══╩══════════╩══════════╩══════════╝<<"
28 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
29 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
30);
31
32test_table!(
33 margin_without_table_based_on_grid_borders,
34 Matrix::new(3, 3)
35 .insert((3, 2), "https://\nwww\n.\nredhat\n.com\n/en")
36 .with(Style::psql())
37 .with(Modify::new(Cell::new(3, 2)).with(Span::column(2)))
38 .with(Margin::new(1, 1, 1, 1).fill('>', '<', 'V', '^')),
39 "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV"
40 "> N | column 0 | column 1 | column 2 <"
41 ">---+----------+----------+----------<"
42 "> 0 | 0-0 | 0-1 | 0-2 <"
43 "> 1 | 1-0 | 1-1 | 1-2 <"
44 "> 2 | 2-0 | https:// <"
45 "> | | www <"
46 "> | | . <"
47 "> | | redhat <"
48 "> | | .com <"
49 "> | | /en <"
50 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
51);
52
53test_table!(
54 table_with_empty_margin,
55 Matrix::new(3, 3)
56 .insert((3, 2), "https://\nwww\n.\nredhat\n.com\n/en")
57 .with(Style::psql())
58 .with(Modify::new(Cell::new(3, 2)).with(Span::column(2)))
59 .with(Margin::new(0, 0, 0, 0).fill('>', '<', 'V', '^')),
60 " N | column 0 | column 1 | column 2 "
61 "---+----------+----------+----------"
62 " 0 | 0-0 | 0-1 | 0-2 "
63 " 1 | 1-0 | 1-1 | 1-2 "
64 " 2 | 2-0 | https:// "
65 " | | www "
66 " | | . "
67 " | | redhat "
68 " | | .com "
69 " | | /en "
70);
71
72#[test]
73fn table_with_margin_and_min_width() {
74 let table = Matrix::new(3, 3)
75 .with(Style::psql())
76 .with(Modify::new(Cell::new(1, 1)).with(Span::column(2)))
77 .with(Margin::new(1, 1, 1, 1).fill('>', '<', 'V', '^'))
78 .with(Width::truncate(20))
79 .to_string();
80
81 assert_eq!(
82 table,
83 static_table!(
84 "VVVVVVVVVVVVVVVVVVVV"
85 "> | co | co | col <"
86 ">--+----+----+-----<"
87 "> | 0-0 | 0-2 <"
88 "> | 1- | 1- | 1-2 <"
89 "> | 2- | 2- | 2-2 <"
90 "^^^^^^^^^^^^^^^^^^^^"
91 )
92 );
93 assert!(is_lines_equal(&table, 20));
94}
95
96#[test]
97fn table_with_margin_and_max_width() {
98 let table = Matrix::new(3, 3)
99 .with(Style::psql())
100 .with(Modify::new(Cell::new(1, 1)).with(Span::column(2)))
101 .with(Margin::new(1, 1, 1, 1).fill('>', '<', 'V', '^'))
102 .with(Width::increase(50))
103 .to_string();
104
105 assert_eq!(papergrid::util::string::string_width_multiline(&table), 50);
106 assert_eq!(
107 table,
108 static_table!(
109 "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV"
110 "> N | column 0 | column 1 | column 2 <"
111 ">------+-------------+-------------+-------------<"
112 "> 0 | 0-0 | 0-2 <"
113 "> 1 | 1-0 | 1-1 | 1-2 <"
114 "> 2 | 2-0 | 2-1 | 2-2 <"
115 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
116 )
117 );
118}
119
120#[test]
121#[ignore = "It's not yet clear what to do with such spans"]
122fn table_0_spanned_with_width() {
123 let table = Matrix::table(0, 0)
124 .with(Modify::new(Cell::new(0, 0)).with(Span::column(0)))
125 .with(Width::increase(50))
126 .to_string();
127
128 assert_eq!(table, "++\n|\n++\n");
129
130 let table = Matrix::table(0, 0)
131 .with(Modify::new(Cell::new(0, 0)).with(Span::column(0)))
132 .with(Width::truncate(50))
133 .to_string();
134
135 assert_eq!(table, "++\n|\n++\n");
136}
137
138#[test]
139fn margin_color_test_not_colored_feature() {
140 use tabled::settings::Color;
141
142 let table = Matrix::new(3, 3)
143 .with(Style::psql())
144 .with(Margin::new(2, 2, 2, 2).fill('>', '<', 'V', '^').colorize(
145 Color::BG_GREEN,
146 Color::BG_YELLOW,
147 Color::BG_RED,
148 Color::BG_BLUE,
149 ))
150 .to_string();
151
152 assert_eq!(
153 table,
154 static_table!(
155 "\u{1b}[41mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[49m"
156 "\u{1b}[41mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[49m"
157 "\u{1b}[42m>>\u{1b}[49m N | column 0 | column 1 | column 2 \u{1b}[43m<<\u{1b}[49m"
158 "\u{1b}[42m>>\u{1b}[49m---+----------+----------+----------\u{1b}[43m<<\u{1b}[49m"
159 "\u{1b}[42m>>\u{1b}[49m 0 | 0-0 | 0-1 | 0-2 \u{1b}[43m<<\u{1b}[49m"
160 "\u{1b}[42m>>\u{1b}[49m 1 | 1-0 | 1-1 | 1-2 \u{1b}[43m<<\u{1b}[49m"
161 "\u{1b}[42m>>\u{1b}[49m 2 | 2-0 | 2-1 | 2-2 \u{1b}[43m<<\u{1b}[49m"
162 "\u{1b}[44m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[49m"
163 "\u{1b}[44m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[49m"
164 )
165 );
166}
167
168#[cfg(feature = "color")]
169#[test]
170fn margin_color_test() {
171 let table = Matrix::new(3, 3)
172 .with(Style::psql())
173 .with(Margin::new(2, 2, 2, 2).fill('>', '<', 'V', '^').colorize(
174 Color::try_from(" ".red().bold().to_string()).unwrap(),
175 Color::try_from(" ".green().to_string()).unwrap(),
176 Color::try_from(" ".on_blue().red().bold().to_string()).unwrap(),
177 Color::try_from(" ".on_yellow().blue().to_string()).unwrap(),
178 ))
179 .to_string();
180
181 assert_eq!(
182 table,
183 static_table!(
184 "\u{1b}[1m\u{1b}[31m\u{1b}[44mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[22m\u{1b}[39m\u{1b}[49m"
185 "\u{1b}[1m\u{1b}[31m\u{1b}[44mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[22m\u{1b}[39m\u{1b}[49m"
186 "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m N | column 0 | column 1 | column 2 \u{1b}[32m<<\u{1b}[39m"
187 "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m---+----------+----------+----------\u{1b}[32m<<\u{1b}[39m"
188 "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m 0 | 0-0 | 0-1 | 0-2 \u{1b}[32m<<\u{1b}[39m"
189 "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m 1 | 1-0 | 1-1 | 1-2 \u{1b}[32m<<\u{1b}[39m"
190 "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m 2 | 2-0 | 2-1 | 2-2 \u{1b}[32m<<\u{1b}[39m"
191 "\u{1b}[34m\u{1b}[43m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[39m\u{1b}[49m"
192 "\u{1b}[34m\u{1b}[43m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[39m\u{1b}[49m"
193 )
194 );
195}