| 1 | use comfy_table::{ColumnConstraint, ContentArrangement, Table, Width}; |
| 2 | use pretty_assertions::assert_eq; |
| 3 | |
| 4 | use super::assert_table_line_width; |
| 5 | |
| 6 | /// Test the robustness of the dynamic table arrangement. |
| 7 | #[test] |
| 8 | fn simple_dynamic_table() { |
| 9 | let mut table = Table::new(); |
| 10 | table.set_header(vec!["Header1", "Header2", "Head"]) |
| 11 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 12 | .set_width(25) |
| 13 | .add_row(vec![ |
| 14 | "This is a very long line with a lot of text", |
| 15 | "This is anotherverylongtextwithlongwords text", |
| 16 | "smol", |
| 17 | ]) |
| 18 | .add_row(vec![ |
| 19 | "This is another text", |
| 20 | "Now let's\nadd a really long line in the middle of the cell \n and add more multi line stuff", |
| 21 | "smol", |
| 22 | ]); |
| 23 | |
| 24 | println!("{table}"); |
| 25 | let expected = " |
| 26 | +--------+-------+------+ |
| 27 | | Header | Heade | Head | |
| 28 | | 1 | r2 | | |
| 29 | +=======================+ |
| 30 | | This | This | smol | |
| 31 | | is a | is | | |
| 32 | | very | anoth | | |
| 33 | | long | erver | | |
| 34 | | line | ylong | | |
| 35 | | with a | textw | | |
| 36 | | lot of | ithlo | | |
| 37 | | text | ngwor | | |
| 38 | | | ds | | |
| 39 | | | text | | |
| 40 | |--------+-------+------| |
| 41 | | This | Now | smol | |
| 42 | | is ano | let's | | |
| 43 | | ther | add a | | |
| 44 | | text | reall | | |
| 45 | | | y | | |
| 46 | | | long | | |
| 47 | | | line | | |
| 48 | | | in | | |
| 49 | | | the | | |
| 50 | | | middl | | |
| 51 | | | e of | | |
| 52 | | | the | | |
| 53 | | | cell | | |
| 54 | | | and | | |
| 55 | | | add | | |
| 56 | | | more | | |
| 57 | | | multi | | |
| 58 | | | line | | |
| 59 | | | stuff | | |
| 60 | +--------+-------+------+"; |
| 61 | println!("{expected}"); |
| 62 | assert_table_line_width(&table, 25); |
| 63 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 64 | } |
| 65 | |
| 66 | /// This table checks the scenario, where a column has a big max_width, but a lot of the assigned |
| 67 | /// space doesn't get used after splitting the lines. This happens mostly when there are |
| 68 | /// many long words in a single column. |
| 69 | /// The remaining space should rather be distributed to other cells. |
| 70 | #[test] |
| 71 | fn distribute_space_after_split() { |
| 72 | let mut table = Table::new(); |
| 73 | table |
| 74 | .set_header(vec!["Header1", "Header2", "Head"]) |
| 75 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 76 | .set_width(80) |
| 77 | .add_row(vec![ |
| 78 | "This is a very long line with a lot of text", |
| 79 | "This is text with a anotherverylongtexttesttest", |
| 80 | "smol", |
| 81 | ]); |
| 82 | |
| 83 | println!("{table}"); |
| 84 | let expected = " |
| 85 | +-----------------------------------------+-----------------------------+------+ |
| 86 | | Header1 | Header2 | Head | |
| 87 | +==============================================================================+ |
| 88 | | This is a very long line with a lot of | This is text with a | smol | |
| 89 | | text | anotherverylongtexttesttest | | |
| 90 | +-----------------------------------------+-----------------------------+------+"; |
| 91 | println!("{expected}"); |
| 92 | |
| 93 | assert_table_line_width(&table, 80); |
| 94 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 95 | } |
| 96 | |
| 97 | /// A single column get's split and a lot of the available isn't used afterward. |
| 98 | /// The remaining space should be cut away, making the table more compact. |
| 99 | #[test] |
| 100 | fn unused_space_after_split() { |
| 101 | let mut table = Table::new(); |
| 102 | table |
| 103 | .set_header(vec!["Header1"]) |
| 104 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 105 | .set_width(30) |
| 106 | .add_row(vec!["This is text with a anotherverylongtext"]); |
| 107 | |
| 108 | println!("{table}"); |
| 109 | let expected = " |
| 110 | +---------------------+ |
| 111 | | Header1 | |
| 112 | +=====================+ |
| 113 | | This is text with a | |
| 114 | | anotherverylongtext | |
| 115 | +---------------------+"; |
| 116 | println!("{expected}"); |
| 117 | assert_table_line_width(&table, 23); |
| 118 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 119 | } |
| 120 | |
| 121 | /// The full width of a table should be used, even if the space isn't used. |
| 122 | #[test] |
| 123 | fn dynamic_full_width_after_split() { |
| 124 | let mut table = Table::new(); |
| 125 | table |
| 126 | .set_header(vec!["Header1"]) |
| 127 | .set_content_arrangement(ContentArrangement::DynamicFullWidth) |
| 128 | .set_width(50) |
| 129 | .add_row(vec!["This is text with a anotherverylongtexttesttestaa"]); |
| 130 | |
| 131 | println!("{table}"); |
| 132 | let expected = " |
| 133 | +------------------------------------------------+ |
| 134 | | Header1 | |
| 135 | +================================================+ |
| 136 | | This is text with a | |
| 137 | | anotherverylongtexttesttestaa | |
| 138 | +------------------------------------------------+"; |
| 139 | println!("{expected}"); |
| 140 | assert_table_line_width(&table, 50); |
| 141 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 142 | } |
| 143 | |
| 144 | /// This table checks the scenario, where a column has a big max_width, but a lot of the assigned |
| 145 | /// space isn't used after splitting the lines. |
| 146 | /// The remaining space should rather distributed between all cells. |
| 147 | #[test] |
| 148 | fn dynamic_full_width() { |
| 149 | let mut table = Table::new(); |
| 150 | table |
| 151 | .set_header(vec!["Header1", "Header2", "smol"]) |
| 152 | .set_content_arrangement(ContentArrangement::DynamicFullWidth) |
| 153 | .set_width(80) |
| 154 | .add_row(vec!["This is a short line", "small", "smol"]); |
| 155 | |
| 156 | println!("{table}"); |
| 157 | let expected = " |
| 158 | +-----------------------------------+----------------------+-------------------+ |
| 159 | | Header1 | Header2 | smol | |
| 160 | +==============================================================================+ |
| 161 | | This is a short line | small | smol | |
| 162 | +-----------------------------------+----------------------+-------------------+"; |
| 163 | println!("{expected}"); |
| 164 | assert_table_line_width(&table, 80); |
| 165 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 166 | } |
| 167 | |
| 168 | /// Test that a table is displayed in its full width, if the `table.width` is set to the exact |
| 169 | /// width the table has, if it's fully expanded. |
| 170 | /// |
| 171 | /// The same should be the case for values that are larger than this width. |
| 172 | #[test] |
| 173 | fn dynamic_exact_width() { |
| 174 | let header = vec!["a\n---\ni64", "b\n---\ni64", "b_squared\n---\nf64"]; |
| 175 | let rows = vec![ |
| 176 | vec!["1", "2", "4.0"], |
| 177 | vec!["3", "4", "16.0"], |
| 178 | vec!["5", "6", "36.0"], |
| 179 | ]; |
| 180 | |
| 181 | for width in 25..40 { |
| 182 | let mut table = Table::new(); |
| 183 | let table = table |
| 184 | .load_preset(comfy_table::presets::UTF8_FULL) |
| 185 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 186 | .set_width(width); |
| 187 | |
| 188 | table.set_header(header.clone()).add_rows(rows.clone()); |
| 189 | |
| 190 | println!("{table}"); |
| 191 | let expected = " |
| 192 | ┌─────┬─────┬───────────┐ |
| 193 | │ a ┆ b ┆ b_squared │ |
| 194 | │ --- ┆ --- ┆ --- │ |
| 195 | │ i64 ┆ i64 ┆ f64 │ |
| 196 | ╞═════╪═════╪═══════════╡ |
| 197 | │ 1 ┆ 2 ┆ 4.0 │ |
| 198 | ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤ |
| 199 | │ 3 ┆ 4 ┆ 16.0 │ |
| 200 | ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤ |
| 201 | │ 5 ┆ 6 ┆ 36.0 │ |
| 202 | └─────┴─────┴───────────┘"; |
| 203 | println!("{expected}"); |
| 204 | assert_table_line_width(table, 25); |
| 205 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /// Test that the formatting works as expected, if the table is slightly smaller than the max width |
| 210 | /// of the table. |
| 211 | #[test] |
| 212 | fn dynamic_slightly_smaller() { |
| 213 | let header = vec!["a\n---\ni64", "b\n---\ni64", "b_squared\n---\nf64"]; |
| 214 | let rows = vec![ |
| 215 | vec!["1", "2", "4.0"], |
| 216 | vec!["3", "4", "16.0"], |
| 217 | vec!["5", "6", "36.0"], |
| 218 | ]; |
| 219 | |
| 220 | let mut table = Table::new(); |
| 221 | let table = table |
| 222 | .load_preset(comfy_table::presets::UTF8_FULL) |
| 223 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 224 | .set_width(24); |
| 225 | |
| 226 | table.set_header(header.clone()).add_rows(rows.clone()); |
| 227 | |
| 228 | println!("{table}"); |
| 229 | let expected = " |
| 230 | ┌─────┬─────┬──────────┐ |
| 231 | │ a ┆ b ┆ b_square │ |
| 232 | │ --- ┆ --- ┆ d │ |
| 233 | │ i64 ┆ i64 ┆ --- │ |
| 234 | │ ┆ ┆ f64 │ |
| 235 | ╞═════╪═════╪══════════╡ |
| 236 | │ 1 ┆ 2 ┆ 4.0 │ |
| 237 | ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤ |
| 238 | │ 3 ┆ 4 ┆ 16.0 │ |
| 239 | ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤ |
| 240 | │ 5 ┆ 6 ┆ 36.0 │ |
| 241 | └─────┴─────┴──────────┘"; |
| 242 | println!("{expected}"); |
| 243 | assert_table_line_width(table, 24); |
| 244 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 245 | } |
| 246 | |
| 247 | /// This failed on a python integration test case in the polars project. |
| 248 | /// This a regression test. |
| 249 | #[test] |
| 250 | fn polar_python_test_tbl_width_chars() { |
| 251 | let header = vec![ |
| 252 | "a really long col\n---\ni64", |
| 253 | "b\n---\nstr", |
| 254 | "this is 10\n---\ni64", |
| 255 | ]; |
| 256 | let rows = vec![ |
| 257 | vec!["1", "", "4"], |
| 258 | vec!["2", "this is a string value that will...", "5"], |
| 259 | vec!["3", "null", "6"], |
| 260 | ]; |
| 261 | |
| 262 | let mut table = Table::new(); |
| 263 | let table = table |
| 264 | .load_preset(comfy_table::presets::UTF8_FULL) |
| 265 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 266 | .set_width(100) |
| 267 | .set_header(header) |
| 268 | .add_rows(rows) |
| 269 | .set_constraints(vec![ |
| 270 | ColumnConstraint::LowerBoundary(Width::Fixed(12)), |
| 271 | ColumnConstraint::LowerBoundary(Width::Fixed(5)), |
| 272 | ColumnConstraint::LowerBoundary(Width::Fixed(10)), |
| 273 | ]); |
| 274 | |
| 275 | println!("{table}"); |
| 276 | let expected = " |
| 277 | ┌───────────────────┬─────────────────────────────────────┬────────────┐ |
| 278 | │ a really long col ┆ b ┆ this is 10 │ |
| 279 | │ --- ┆ --- ┆ --- │ |
| 280 | │ i64 ┆ str ┆ i64 │ |
| 281 | ╞═══════════════════╪═════════════════════════════════════╪════════════╡ |
| 282 | │ 1 ┆ ┆ 4 │ |
| 283 | ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤ |
| 284 | │ 2 ┆ this is a string value that will... ┆ 5 │ |
| 285 | ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤ |
| 286 | │ 3 ┆ null ┆ 6 │ |
| 287 | └───────────────────┴─────────────────────────────────────┴────────────┘"; |
| 288 | println!("{expected}"); |
| 289 | assert_table_line_width(table, 72); |
| 290 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 291 | } |