Skip to content

Commit

Permalink
some tests for inner ansi style
Browse files Browse the repository at this point in the history
  • Loading branch information
blueforesticarus committed Nov 12, 2022
1 parent dab6194 commit 96e44ef
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
98 changes: 98 additions & 0 deletions tests/all/inner_style_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use comfy_table::{presets::UTF8_FULL, *};
use pretty_assertions::assert_eq;

fn get_preset_table() -> Table {
let mut table = Table::new();
table.load_preset(UTF8_FULL);
table.set_content_arrangement(ContentArrangement::Dynamic);
table.set_width(85);

let mut row = Row::new();
row.add_cell(Cell::new(format!(
"hello{}cell1",
console::style("123\n456").dim().blue()
)));
row.add_cell(Cell::new("cell2"));

table.add_row(row);

let mut row = Row::new();
row.add_cell(Cell::new(
format!(r"cell sys-devices-pci00:00-0000:000:07:00.1-usb2-2\x2d1-2\x2d1.3-2\x2d1.3:1.0-host2-target2:0:0-2:0:0:1-block-sdb{}", console::style(".device").bold().red())
));
row.add_cell(Cell::new(
"cell4 asdfasfsad asdfasdf sad fas df asdf as df asdf asdfasdfasdfasdfasdfasdfa dsfa sdf asdf asd f asdf as df sadf asd fas df "
));
table.add_row(row);

let mut row = Row::new();
row.add_cell(Cell::new("cell5"));
row.add_cell(Cell::new("cell6"));
table.add_row(row);

table
}

#[test]
fn styled_table() {
console::set_colors_enabled(true);
let mut table = get_preset_table();
table.force_no_tty().enforce_styling();
println!("{table}");
let expected = "
┌─────────────────────────────────────────┬─────────────────────────────────────────┐
│ hello\u{1b}[34m\u{1b}[2m123\u{1b}[0m ┆ cell2 │
\u{1b}[34m\u{1b}[2m456\u{1b}[0mcell1 ┆ │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ cell sys-devices-pci00:00-0000:000:07:0 ┆ cell4 asdfasfsad asdfasdf sad fas df │
│ 0.1-usb2-2\\x2d1-2\\x2d1.3-2\\x2d1.3:1.0-h ┆ asdf as df asdf │
│ ost2-target2:0:0-2:0:0:1-block-sdb\u{1b}[31m\u{1b}[1m.devi\u{1b}[0m ┆ asdfasdfasdfasdfasdfasdfa dsfa sdf asdf │
\u{1b}[31m\u{1b}[1mce\u{1b}[0m ┆ asd f asdf as df sadf asd fas df │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ cell5 ┆ cell6 │
└─────────────────────────────────────────┴─────────────────────────────────────────┘";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}

#[test]
fn no_style_styled_table() {
console::set_colors_enabled(true);
let mut table = get_preset_table();
table.force_no_tty();

println!("{table}");
let expected = "
┌─────────────────────────────────────────┬─────────────────────────────────────────┐
│ hello\u{1b}[34m\u{1b}[2m123\u{1b}[0m ┆ cell2 │
\u{1b}[34m\u{1b}[2m456\u{1b}[0mcell1 ┆ │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ cell sys-devices-pci00:00-0000:000:07:0 ┆ cell4 asdfasfsad asdfasdf sad fas df │
│ 0.1-usb2-2\\x2d1-2\\x2d1.3-2\\x2d1.3:1.0-h ┆ asdf as df asdf │
│ ost2-target2:0:0-2:0:0:1-block-sdb\u{1b}[31m\u{1b}[1m.devi\u{1b}[0m ┆ asdfasdfasdfasdfasdfasdfa dsfa sdf asdf │
\u{1b}[31m\u{1b}[1mce\u{1b}[0m ┆ asd f asdf as df sadf asd fas df │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ cell5 ┆ cell6 │
└─────────────────────────────────────────┴─────────────────────────────────────────┘";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}

#[test]
fn styled_text_only_table() {
console::set_colors_enabled(true);
let mut table = get_preset_table();
table.force_no_tty().enforce_styling().style_text_only();
println!("{table}");
let expected = "
┌─────────────────────────────────────────┬─────────────────────────────────────────┐
│ hello\u{1b}[34m\u{1b}[2m123\u{1b}[0m ┆ cell2 │
\u{1b}[34m\u{1b}[2m456\u{1b}[0mcell1 ┆ │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ cell sys-devices-pci00:00-0000:000:07:0 ┆ cell4 asdfasfsad asdfasdf sad fas df │
│ 0.1-usb2-2\\x2d1-2\\x2d1.3-2\\x2d1.3:1.0-h ┆ asdf as df asdf │
│ ost2-target2:0:0-2:0:0:1-block-sdb\u{1b}[31m\u{1b}[1m.devi\u{1b}[0m ┆ asdfasdfasdfasdfasdfasdfa dsfa sdf asdf │
\u{1b}[31m\u{1b}[1mce\u{1b}[0m ┆ asd f asdf as df sadf asd fas df │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ cell5 ┆ cell6 │
└─────────────────────────────────────────┴─────────────────────────────────────────┘";
assert_eq!("\n".to_string() + &table.to_string(), expected);
}
2 changes: 2 additions & 0 deletions tests/all/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod constraints_test;
mod content_arrangement_test;
mod custom_delimiter_test;
mod hidden_test;
#[cfg(feature = "ansi")]
mod inner_style_test;
mod modifiers_test;
mod padding_test;
mod presets_test;
Expand Down

0 comments on commit 96e44ef

Please sign in to comment.