use pretty_assertions::assert_eq;
use comfy_table::*;
fn get_max_min_table() -> Table {
let mut table = Table::new();
table
.set_header(&vec!["smol", "Header2", "Header3"])
.add_row(&vec![
"smol",
"This is another text",
"This is the third text",
])
.add_row(&vec![
"smol",
"Now\nadd some\nmulti line stuff",
"This is awesome",
]);
table
}
#[test]
fn fixed_max_min_constraints() {
let mut table = get_max_min_table();
table.set_constraints(vec![
ColumnConstraint::MinWidth(10),
ColumnConstraint::MaxWidth(8),
ColumnConstraint::Width(10),
]);
println!("{}", table.to_string());
let expected = "
+----------+--------+----------+
| smol | Header | Header3 |
| | 2 | |
+==============================+
| smol | This | This is |
| | is ano | the |
| | ther | third |
| | text | text |
|----------+--------+----------|
| smol | Now | This is |
| | add | awesome |
| | some | |
| | multi | |
| | line | |
| | stuff | |
+----------+--------+----------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
table
.set_content_arrangement(ContentArrangement::Dynamic)
.set_table_width(28);
println!("{}", table.to_string());
let expected = "
+----------+----+----------+
| smol | He | Header3 |
| | ad | |
| | er | |
| | 2 | |
+==========================+
| smol | Th | This is |
| | is | the |
| | is | third |
| | an | text |
| | ot | |
| | he | |
| | r | |
| | te | |
| | xt | |
|----------+----+----------|
| smol | No | This is |
| | w | awesome |
| | ad | |
| | d | |
| | so | |
| | me | |
| | mu | |
| | lt | |
| | i | |
| | li | |
| | ne | |
| | st | |
| | uf | |
| | f | |
+----------+----+----------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
#[test]
fn unnecessary_max_min_constraints() {
let mut table = get_max_min_table();
table.set_constraints(vec![
ColumnConstraint::MinWidth(1),
ColumnConstraint::MaxWidth(30),
]);
println!("{}", table.to_string());
let expected = "
+------+----------------------+------------------------+
| smol | Header2 | Header3 |
+======================================================+
| smol | This is another text | This is the third text |
|------+----------------------+------------------------|
| smol | Now | This is awesome |
| | add some | |
| | multi line stuff | |
+------+----------------------+------------------------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
table.set_content_arrangement(ContentArrangement::Dynamic);
println!("{}", table.to_string());
let expected = "
+------+----------------------+------------------------+
| smol | Header2 | Header3 |
+======================================================+
| smol | This is another text | This is the third text |
|------+----------------------+------------------------|
| smol | Now | This is awesome |
| | add some | |
| | multi line stuff | |
+------+----------------------+------------------------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
#[test]
fn constraints_bigger_than_table_width() {
let mut table = get_max_min_table();
table
.set_content_arrangement(ContentArrangement::Dynamic)
.set_table_width(28)
.set_constraints(vec![
ColumnConstraint::MaxWidth(50),
ColumnConstraint::MinWidth(30),
ColumnConstraint::ContentWidth,
]);
println!("{}", table.to_string());
let expected = "
+---+------------------------------+------------------------+
| s | Header2 | Header3 |
| m | | |
| o | | |
| l | | |
+===========================================================+
| s | This is another text | This is the third text |
| m | | |
| o | | |
| l | | |
|---+------------------------------+------------------------|
| s | Now | This is awesome |
| m | add some | |
| o | multi line stuff | |
| l | | |
+---+------------------------------+------------------------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
#[test]
fn percentage() {
let mut table = get_max_min_table();
table
.set_content_arrangement(ContentArrangement::Dynamic)
.set_table_width(40)
.set_constraints(vec![
ColumnConstraint::Percentage(20),
]);
println!("{}", table.to_string());
let expected = "
+--------+--------------+--------------+
| smol | Header2 | Header3 |
+======================================+
| smol | This is | This is the |
| | another text | third text |
|--------+--------------+--------------|
| smol | Now | This is |
| | add some | awesome |
| | multi line | |
| | stuff | |
+--------+--------------+--------------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
table.set_table_width(40)
.set_constraints(vec![
ColumnConstraint::MinPercentage(40),
ColumnConstraint::MaxPercentage(30),
ColumnConstraint::Percentage(30),
]);
println!("{}", table.to_string());
let expected = "
+----------------+--------+------------+
| smol | Header | Header3 |
| | 2 | |
+======================================+
| smol | This | This is |
| | is ano | the third |
| | ther | text |
| | text | |
|----------------+--------+------------|
| smol | Now | This is |
| | add | awesome |
| | some | |
| | multi | |
| | line | |
| | stuff | |
+----------------+--------+------------+";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}