| 1 | use comfy_table::{ColumnConstraint::*, Width::*, *}; |
| 2 | use pretty_assertions::assert_eq; |
| 3 | |
| 4 | use super::assert_table_line_width; |
| 5 | |
| 6 | fn get_constraint_table() -> Table { |
| 7 | let mut table = Table::new(); |
| 8 | table |
| 9 | .set_header(vec!["smol", "Header2", "Header3"]) |
| 10 | .add_row(vec![ |
| 11 | "smol", |
| 12 | "This is another text", |
| 13 | "This is the third text", |
| 14 | ]) |
| 15 | .add_row(vec![ |
| 16 | "smol", |
| 17 | "Now\nadd some\nmulti line stuff", |
| 18 | "This is awesome", |
| 19 | ]); |
| 20 | |
| 21 | table |
| 22 | } |
| 23 | |
| 24 | /// Ensure max-, min- and fixed-width constraints are respected |
| 25 | #[test] |
| 26 | fn fixed_max_min_constraints() { |
| 27 | let mut table = get_constraint_table(); |
| 28 | |
| 29 | table.set_constraints(vec![ |
| 30 | LowerBoundary(Fixed(10)), |
| 31 | UpperBoundary(Fixed(8)), |
| 32 | Absolute(Fixed(10)), |
| 33 | ]); |
| 34 | |
| 35 | println!("{table}"); |
| 36 | let expected = " |
| 37 | +----------+--------+----------+ |
| 38 | | smol | Header | Header3 | |
| 39 | | | 2 | | |
| 40 | +==============================+ |
| 41 | | smol | This | This is | |
| 42 | | | is ano | the | |
| 43 | | | ther | third | |
| 44 | | | text | text | |
| 45 | |----------+--------+----------| |
| 46 | | smol | Now | This is | |
| 47 | | | add | awesome | |
| 48 | | | some | | |
| 49 | | | multi | | |
| 50 | | | line | | |
| 51 | | | stuff | | |
| 52 | +----------+--------+----------+"; |
| 53 | println!("{expected}"); |
| 54 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 55 | |
| 56 | // Now try this again when using dynamic content arrangement |
| 57 | // The table tries to arrange to 28 characters, |
| 58 | // but constraints enforce a width of at least 10+10+2+1+4 = 27 |
| 59 | // min_width + max_width + middle_padding + middle_min_width + borders |
| 60 | // Since the left and right column are fixed, the middle column should only get a width of 2 |
| 61 | table |
| 62 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 63 | .set_width(28); |
| 64 | |
| 65 | println!("{table}"); |
| 66 | let expected = " |
| 67 | +----------+----+----------+ |
| 68 | | smol | He | Header3 | |
| 69 | | | ad | | |
| 70 | | | er | | |
| 71 | | | 2 | | |
| 72 | +==========================+ |
| 73 | | smol | Th | This is | |
| 74 | | | is | the | |
| 75 | | | is | third | |
| 76 | | | an | text | |
| 77 | | | ot | | |
| 78 | | | he | | |
| 79 | | | r | | |
| 80 | | | te | | |
| 81 | | | xt | | |
| 82 | |----------+----+----------| |
| 83 | | smol | No | This is | |
| 84 | | | w | awesome | |
| 85 | | | ad | | |
| 86 | | | d | | |
| 87 | | | so | | |
| 88 | | | me | | |
| 89 | | | mu | | |
| 90 | | | lt | | |
| 91 | | | i | | |
| 92 | | | li | | |
| 93 | | | ne | | |
| 94 | | | st | | |
| 95 | | | uf | | |
| 96 | | | f | | |
| 97 | +----------+----+----------+"; |
| 98 | println!("{expected}"); |
| 99 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 100 | } |
| 101 | |
| 102 | /// Max and Min constraints won't be considered, if they are unnecessary. |
| 103 | /// This is true for normal and dynamic arrangement tables. |
| 104 | #[rstest::rstest] |
| 105 | #[case(ContentArrangement::Dynamic)] |
| 106 | #[case(ContentArrangement::Disabled)] |
| 107 | fn unnecessary_max_min_constraints(#[case] arrangement: ContentArrangement) { |
| 108 | let mut table = get_constraint_table(); |
| 109 | |
| 110 | table |
| 111 | .set_width(80) |
| 112 | .set_constraints(vec![LowerBoundary(Fixed(1)), UpperBoundary(Fixed(30))]) |
| 113 | .set_content_arrangement(arrangement); |
| 114 | println!("{table}"); |
| 115 | let expected = " |
| 116 | +------+----------------------+------------------------+ |
| 117 | | smol | Header2 | Header3 | |
| 118 | +======================================================+ |
| 119 | | smol | This is another text | This is the third text | |
| 120 | |------+----------------------+------------------------| |
| 121 | | smol | Now | This is awesome | |
| 122 | | | add some | | |
| 123 | | | multi line stuff | | |
| 124 | +------+----------------------+------------------------+"; |
| 125 | println!("{expected}"); |
| 126 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 127 | } |
| 128 | |
| 129 | /// The user can specify constraints that result in bigger width than actually provided |
| 130 | /// This is allowed, but results in a wider table than actually aimed for. |
| 131 | /// Anyway we still try to fit everything as good as possible, which of course breaks stuff. |
| 132 | #[test] |
| 133 | fn constraints_bigger_than_table_width() { |
| 134 | let mut table = get_constraint_table(); |
| 135 | |
| 136 | table |
| 137 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 138 | .set_width(28) |
| 139 | .set_constraints(vec![ |
| 140 | UpperBoundary(Fixed(50)), |
| 141 | LowerBoundary(Fixed(30)), |
| 142 | ContentWidth, |
| 143 | ]); |
| 144 | |
| 145 | println!("{table}"); |
| 146 | let expected = " |
| 147 | +---+------------------------------+------------------------+ |
| 148 | | s | Header2 | Header3 | |
| 149 | | m | | | |
| 150 | | o | | | |
| 151 | | l | | | |
| 152 | +===========================================================+ |
| 153 | | s | This is another text | This is the third text | |
| 154 | | m | | | |
| 155 | | o | | | |
| 156 | | l | | | |
| 157 | |---+------------------------------+------------------------| |
| 158 | | s | Now | This is awesome | |
| 159 | | m | add some | | |
| 160 | | o | multi line stuff | | |
| 161 | | l | | | |
| 162 | +---+------------------------------+------------------------+"; |
| 163 | println!("{expected}"); |
| 164 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 165 | } |
| 166 | |
| 167 | /// Test correct usage of the Percentage constraint. |
| 168 | /// Percentage allows to set a fixed width. |
| 169 | #[test] |
| 170 | fn percentage() { |
| 171 | let mut table = get_constraint_table(); |
| 172 | |
| 173 | // Set a percentage of 20% for the first column. |
| 174 | // The the rest should arrange accordingly. |
| 175 | table |
| 176 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 177 | .set_width(40) |
| 178 | .set_constraints(vec![Absolute(Percentage(20))]); |
| 179 | |
| 180 | println!("{table}"); |
| 181 | let expected = " |
| 182 | +-------+---------------+--------------+ |
| 183 | | smol | Header2 | Header3 | |
| 184 | +======================================+ |
| 185 | | smol | This is | This is the | |
| 186 | | | another text | third text | |
| 187 | |-------+---------------+--------------| |
| 188 | | smol | Now | This is | |
| 189 | | | add some | awesome | |
| 190 | | | multi line | | |
| 191 | | | stuff | | |
| 192 | +-------+---------------+--------------+"; |
| 193 | println!("{expected}"); |
| 194 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 195 | } |
| 196 | |
| 197 | /// A single percentage constraint should be 100% at most. |
| 198 | #[test] |
| 199 | fn max_100_percentage() { |
| 200 | let mut table = Table::new(); |
| 201 | table |
| 202 | .set_header(vec!["smol"]) |
| 203 | .add_row(vec!["smol"]) |
| 204 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 205 | .set_width(40) |
| 206 | .set_constraints(vec![Absolute(Percentage(200))]); |
| 207 | |
| 208 | println!("{table}"); |
| 209 | let expected = " |
| 210 | +--------------------------------------+ |
| 211 | | smol | |
| 212 | +======================================+ |
| 213 | | smol | |
| 214 | +--------------------------------------+"; |
| 215 | println!("{expected}"); |
| 216 | assert_table_line_width(&table, 40); |
| 217 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 218 | } |
| 219 | |
| 220 | #[test] |
| 221 | fn percentage_second() { |
| 222 | let mut table = get_constraint_table(); |
| 223 | |
| 224 | table |
| 225 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 226 | .set_width(40) |
| 227 | .set_constraints(vec![ |
| 228 | LowerBoundary(Percentage(40)), |
| 229 | UpperBoundary(Percentage(30)), |
| 230 | Absolute(Percentage(30)), |
| 231 | ]); |
| 232 | |
| 233 | println!("{table}"); |
| 234 | let expected = " |
| 235 | +--------------+----------+----------+ |
| 236 | | smol | Header2 | Header3 | |
| 237 | +====================================+ |
| 238 | | smol | This is | This is | |
| 239 | | | another | the | |
| 240 | | | text | third | |
| 241 | | | | text | |
| 242 | |--------------+----------+----------| |
| 243 | | smol | Now | This is | |
| 244 | | | add some | awesome | |
| 245 | | | multi | | |
| 246 | | | line | | |
| 247 | | | stuff | | |
| 248 | +--------------+----------+----------+"; |
| 249 | println!("{expected}"); |
| 250 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn max_percentage() { |
| 255 | let mut table = get_constraint_table(); |
| 256 | |
| 257 | table |
| 258 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 259 | .set_width(40) |
| 260 | .set_constraints(vec![ |
| 261 | ContentWidth, |
| 262 | UpperBoundary(Percentage(30)), |
| 263 | Absolute(Percentage(30)), |
| 264 | ]); |
| 265 | |
| 266 | println!("{table}"); |
| 267 | let expected = " |
| 268 | +------+----------+----------+ |
| 269 | | smol | Header2 | Header3 | |
| 270 | +============================+ |
| 271 | | smol | This is | This is | |
| 272 | | | another | the | |
| 273 | | | text | third | |
| 274 | | | | text | |
| 275 | |------+----------+----------| |
| 276 | | smol | Now | This is | |
| 277 | | | add some | awesome | |
| 278 | | | multi | | |
| 279 | | | line | | |
| 280 | | | stuff | | |
| 281 | +------+----------+----------+"; |
| 282 | println!("{expected}"); |
| 283 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 284 | } |
| 285 | |
| 286 | /// Ensure that both min and max in [Boundaries] is respected |
| 287 | #[test] |
| 288 | fn min_max_boundary() { |
| 289 | let mut table = get_constraint_table(); |
| 290 | |
| 291 | table |
| 292 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 293 | .set_width(40) |
| 294 | .set_constraints(vec![ |
| 295 | Boundaries { |
| 296 | lower: Percentage(50), |
| 297 | upper: Fixed(2), |
| 298 | }, |
| 299 | Boundaries { |
| 300 | lower: Fixed(15), |
| 301 | upper: Percentage(50), |
| 302 | }, |
| 303 | Absolute(Percentage(30)), |
| 304 | ]); |
| 305 | |
| 306 | println!("{table}"); |
| 307 | let expected = " |
| 308 | +------------------+---------------+----------+ |
| 309 | | smol | Header2 | Header3 | |
| 310 | +=============================================+ |
| 311 | | smol | This is | This is | |
| 312 | | | another text | the | |
| 313 | | | | third | |
| 314 | | | | text | |
| 315 | |------------------+---------------+----------| |
| 316 | | smol | Now | This is | |
| 317 | | | add some | awesome | |
| 318 | | | multi line | | |
| 319 | | | stuff | | |
| 320 | +------------------+---------------+----------+"; |
| 321 | println!("{expected}"); |
| 322 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 323 | } |
| 324 | |
| 325 | /// Empty table with zero width constraint. |
| 326 | #[rstest::rstest] |
| 327 | #[case(ContentArrangement::Dynamic)] |
| 328 | #[case(ContentArrangement::Disabled)] |
| 329 | fn empty_table(#[case] arrangement: ContentArrangement) { |
| 330 | let mut table = Table::new(); |
| 331 | table |
| 332 | .add_row(vec![""]) |
| 333 | .set_content_arrangement(arrangement) |
| 334 | .set_constraints(vec![Absolute(Fixed(0))]); |
| 335 | |
| 336 | println!("{table}"); |
| 337 | let expected = " |
| 338 | +---+ |
| 339 | | | |
| 340 | +---+"; |
| 341 | println!("{expected}"); |
| 342 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 343 | } |
| 344 | |
| 345 | /// Test that successive `Fixed` `LowerBoundary` constraints are respected. |
| 346 | /// |
| 347 | /// This covers the edge-case that a later column enforces a lower-boundary |
| 348 | /// constraint, resulting in the average space for the remaining columns to shrink significantly. |
| 349 | /// This should then result in the other columns to be fixed as well. |
| 350 | #[test] |
| 351 | fn lower_fixed_boundary() { |
| 352 | let mut table = Table::new(); |
| 353 | table |
| 354 | .add_row(vec!["123", "123", "1234", "1234567891234"]) |
| 355 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 356 | .set_width(33); |
| 357 | |
| 358 | table |
| 359 | .column_mut(2) |
| 360 | .unwrap() |
| 361 | .set_constraint(LowerBoundary(Fixed(5))); |
| 362 | |
| 363 | table |
| 364 | .column_mut(3) |
| 365 | .unwrap() |
| 366 | .set_constraint(LowerBoundary(Fixed(14))); |
| 367 | |
| 368 | let expected = " |
| 369 | +-----+----+-----+--------------+ |
| 370 | | 123 | 12 | 123 | 123456789123 | |
| 371 | | | 3 | 4 | 4 | |
| 372 | +-----+----+-----+--------------+"; |
| 373 | |
| 374 | println!("{expected}"); |
| 375 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 376 | } |
| 377 | |
| 378 | /// Test that `LowerBoundary` constraints are respected, even if a split would result in |
| 379 | /// a smaller size. |
| 380 | #[test] |
| 381 | fn lower_fixed_boundary_with_split() { |
| 382 | let mut table = Table::new(); |
| 383 | table |
| 384 | .add_row(vec!["123", "123", "123 343"]) |
| 385 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 386 | .set_width(5); |
| 387 | |
| 388 | table |
| 389 | .column_mut(2) |
| 390 | .unwrap() |
| 391 | .set_constraint(LowerBoundary(Fixed(6))); |
| 392 | |
| 393 | let expected = " |
| 394 | +---+---+------+ |
| 395 | | 1 | 1 | 123 | |
| 396 | | 2 | 2 | 343 | |
| 397 | | 3 | 3 | | |
| 398 | +---+---+------+"; |
| 399 | println!("{expected}"); |
| 400 | assert_eq!(expected, "\n".to_string() + &table.to_string()); |
| 401 | } |