#[cfg(feature = "color_codes")]
use ::colorful::{Color, Colorful};
use crate::{Align::*, AsciiTable, Width::*};
#[cfg(not(feature = "auto_table_width"))]
#[test]
fn default_max_width() {
assert_eq!(Default, AsciiTable::new().max_width());
}
#[cfg(feature = "auto_table_width")]
#[test]
fn default_max_width() {
assert_eq!(Auto, AsciiTable::new().max_width());
}
#[test]
fn protect_public_api() {
let config = AsciiTable::new();
let mut buffer = Vec::new();
let input: Vec<Vec<String>> = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: Vec<Vec<&str>> = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: Vec<&[String]> = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: Vec<&[&str]> = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: &[Vec<String>] = ::std::default::Default::default();
config.writeln(&mut buffer, input).unwrap();
let input: &[Vec<&str>] = ::std::default::Default::default();
config.writeln(&mut buffer, input).unwrap();
let input: &[&[String]] = ::std::default::Default::default();
config.writeln(&mut buffer, input).unwrap();
let input: &[&[&str]] = ::std::default::Default::default();
config.writeln(&mut buffer, input).unwrap();
let input: Vec<&[String; 10]> = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: Vec<&[&str; 10]> = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: [Vec<String>; 10] = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: [Vec<&str>; 10] = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: [[String; 10]; 10] = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
let input: [[&str; 10]; 10] = ::std::default::Default::default();
config.writeln(&mut buffer, &input).unwrap();
config.writeln(&mut buffer, input).unwrap();
}
fn test(config: AsciiTable, input: &[&[&str]], expected: &str) {
let mut buffer = Vec::new();
config.writeln(&mut buffer, input).unwrap();
let actual = String::from_utf8(buffer).unwrap();
assert_eq!(expected, actual);
}
fn cube_config() -> AsciiTable {
let mut result = AsciiTable::new();
result.column(0).set_header("a");
result.column(1).set_header("b");
result.column(2).set_header("c");
result
}
#[test]
fn empty_rows() {
test(
AsciiTable::new(),
&[],
"┌──┐\n\
└──┘\n",
);
}
#[test]
fn empty_columns() {
test(
AsciiTable::new(),
&[&[]],
"┌──┐\n\
│ │\n\
└──┘\n",
);
}
#[test]
fn empty_cells() {
test(
AsciiTable::new(),
&[&["", "", ""], &["", "", ""], &["", "", ""]],
"┌──┬──┬──┐\n\
│ │ │ │\n\
│ │ │ │\n\
│ │ │ │\n\
└──┴──┴──┘\n",
);
}
#[test]
fn cube_with_header() {
test(
cube_config(),
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌───┬───┬───┐\n\
│ a │ b │ c │\n\
├───┼───┼───┤\n\
│ 1 │ 2 │ 3 │\n\
│ 4 │ 5 │ 6 │\n\
│ 7 │ 8 │ 9 │\n\
└───┴───┴───┘\n",
);
}
#[test]
fn cube_with_no_rows() {
test(
cube_config(),
&[],
"┌───┬───┬───┐\n\
│ a │ b │ c │\n\
├───┼───┼───┤\n\
└───┴───┴───┘\n",
);
}
#[test]
fn cube_with_no_header() {
test(
AsciiTable::new(),
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌───┬───┬───┐\n\
│ 1 │ 2 │ 3 │\n\
│ 4 │ 5 │ 6 │\n\
│ 7 │ 8 │ 9 │\n\
└───┴───┴───┘\n",
);
}
#[test]
fn cube_with_empty_column1() {
test(
AsciiTable::new(),
&[&["", "2", "3"], &["", "5", "6"], &["", "8", "9"]],
"┌──┬───┬───┐\n\
│ │ 2 │ 3 │\n\
│ │ 5 │ 6 │\n\
│ │ 8 │ 9 │\n\
└──┴───┴───┘\n",
);
}
#[test]
fn cube_with_empty_column2() {
test(
AsciiTable::new(),
&[&["1", "", "3"], &["4", "", "6"], &["7", "", "9"]],
"┌───┬──┬───┐\n\
│ 1 │ │ 3 │\n\
│ 4 │ │ 6 │\n\
│ 7 │ │ 9 │\n\
└───┴──┴───┘\n",
);
}
#[test]
fn cube_with_empty_column3() {
test(
AsciiTable::new(),
&[&["1", "2", ""], &["4", "5", ""], &["7", "8", ""]],
"┌───┬───┬──┐\n\
│ 1 │ 2 │ │\n\
│ 4 │ 5 │ │\n\
│ 7 │ 8 │ │\n\
└───┴───┴──┘\n",
);
}
#[test]
fn one_cell() {
test(
AsciiTable::new(),
&[&["1"]],
"┌───┐\n\
│ 1 │\n\
└───┘\n",
);
}
#[test]
fn smallest_cell() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(4));
config
},
&[&["123"]],
"┌──┐\n\
│ │\n\
└──┘\n",
);
}
#[test]
fn smallest_cell2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(0);
config
},
&[&["123"]],
"┌──┐\n\
│ │\n\
└──┘\n",
);
}
#[test]
fn smallest_cube() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(10));
config
},
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌──┬──┬──┐\n\
│ │ │ │\n\
│ │ │ │\n\
│ │ │ │\n\
└──┴──┴──┘\n",
);
}
#[test]
fn smallest_cube2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(0);
config.column(1).set_max_width(0);
config.column(2).set_max_width(0);
config
},
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌──┬──┬──┐\n\
│ │ │ │\n\
│ │ │ │\n\
│ │ │ │\n\
└──┴──┴──┘\n",
);
}
#[test]
fn show_no_content_for_cell() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(5));
config
},
&[&["123"]],
"┌───┐\n\
│ + │\n\
└───┘\n",
);
}
#[test]
fn show_no_content_for_cell2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(1);
config
},
&[&["123"]],
"┌───┐\n\
│ + │\n\
└───┘\n",
);
}
#[test]
fn show_one_character_for_cell() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(6));
config
},
&[&["123"]],
"┌────┐\n\
│ 1+ │\n\
└────┘\n",
);
}
#[test]
fn show_one_character_for_cell2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(2);
config
},
&[&["123"]],
"┌────┐\n\
│ 1+ │\n\
└────┘\n",
);
}
#[test]
fn smallest_cell_with_header() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(4));
config.column(0).set_header("foo");
config
},
&[&["123"]],
"┌──┐\n\
│ │\n\
├──┤\n\
│ │\n\
└──┘\n",
);
}
#[test]
fn smallest_cell_with_header2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(0).set_header("foo");
config
},
&[&["123"]],
"┌──┐\n\
│ │\n\
├──┤\n\
│ │\n\
└──┘\n",
);
}
#[test]
fn smallest_cube_with_header() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(10));
config.column(0).set_header("abc");
config.column(1).set_header("def");
config.column(2).set_header("ghi");
config
},
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌──┬──┬──┐\n\
│ │ │ │\n\
├──┼──┼──┤\n\
│ │ │ │\n\
│ │ │ │\n\
│ │ │ │\n\
└──┴──┴──┘\n",
);
}
#[test]
fn smallest_cube_with_header2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("abc").set_max_width(0);
config.column(1).set_header("def").set_max_width(0);
config.column(2).set_header("ghi").set_max_width(0);
config
},
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌──┬──┬──┐\n\
│ │ │ │\n\
├──┼──┼──┤\n\
│ │ │ │\n\
│ │ │ │\n\
│ │ │ │\n\
└──┴──┴──┘\n",
);
}
#[test]
fn show_no_content_for_header() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(5));
config.column(0).set_header("abc");
config
},
&[&[""]],
"┌───┐\n\
│ + │\n\
├───┤\n\
│ │\n\
└───┘\n",
);
}
#[test]
fn show_no_content_for_header2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("abc").set_max_width(1);
config
},
&[&[""]],
"┌───┐\n\
│ + │\n\
├───┤\n\
│ │\n\
└───┘\n",
);
}
#[test]
fn show_one_character_for_header() {
test(
{
let mut config = AsciiTable::new();
config.set_max_width(Fixed(6));
config.column(0).set_header("abc");
config
},
&[&[""]],
"┌────┐\n\
│ a+ │\n\
├────┤\n\
│ │\n\
└────┘\n",
);
}
#[test]
fn show_one_character_for_header2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("abc").set_max_width(2);
config
},
&[&[""]],
"┌────┐\n\
│ a+ │\n\
├────┤\n\
│ │\n\
└────┘\n",
);
}
#[test]
fn cube_with_partial_content() {
test(
cube_config(),
&[&["1", "2", "3"], &["4", "5"], &["7"]],
"┌───┬───┬───┐\n\
│ a │ b │ c │\n\
├───┼───┼───┤\n\
│ 1 │ 2 │ 3 │\n\
│ 4 │ 5 │ │\n\
│ 7 │ │ │\n\
└───┴───┴───┘\n",
);
}
#[test]
fn cube_with_partial_content_reversed() {
test(
cube_config(),
&[&["1"], &["4", "5"], &["7", "8", "9"]],
"┌───┬───┬───┐\n\
│ a │ b │ c │\n\
├───┼───┼───┤\n\
│ 1 │ │ │\n\
│ 4 │ 5 │ │\n\
│ 7 │ 8 │ 9 │\n\
└───┴───┴───┘\n",
);
}
#[test]
fn resize_column() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("a");
config
},
&[&["1"], &["23"], &["456"]],
"┌─────┐\n\
│ a │\n\
├─────┤\n\
│ 1 │\n\
│ 23 │\n\
│ 456 │\n\
└─────┘\n",
);
}
#[test]
fn resize_column_reversed() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("a");
config
},
&[&["123"], &["45"], &["6"]],
"┌─────┐\n\
│ a │\n\
├─────┤\n\
│ 123 │\n\
│ 45 │\n\
│ 6 │\n\
└─────┘\n",
);
}
#[test]
fn resize_column_via_header() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("foo");
config
},
&[&["1"], &["2"], &["3"]],
"┌─────┐\n\
│ foo │\n\
├─────┤\n\
│ 1 │\n\
│ 2 │\n\
│ 3 │\n\
└─────┘\n",
);
}
#[test]
fn partial_header_at_start() {
test(
cube_config(),
&[
&["1", "2", "3", "0"],
&["4", "5", "6", "0"],
&["7", "8", "9", "0"],
],
"┌───┬───┬───┬───┐\n\
│ a │ b │ c │ │\n\
├───┼───┼───┼───┤\n\
│ 1 │ 2 │ 3 │ 0 │\n\
│ 4 │ 5 │ 6 │ 0 │\n\
│ 7 │ 8 │ 9 │ 0 │\n\
└───┴───┴───┴───┘\n",
);
}
#[test]
fn partial_header_at_end() {
test(
{
let mut config = AsciiTable::new();
config.column(2).set_header("c");
config
},
&[&["1", "2", "3"], &["4", "5", "6"], &["7", "8", "9"]],
"┌───┬───┬───┐\n\
│ │ │ c │\n\
├───┼───┼───┤\n\
│ 1 │ 2 │ 3 │\n\
│ 4 │ 5 │ 6 │\n\
│ 7 │ 8 │ 9 │\n\
└───┴───┴───┘\n",
);
}
#[test]
fn dont_ignore_unused_header() {
test(
cube_config(),
&[&["1"], &["2"], &["3"]],
"┌───┬───┬───┐\n\
│ a │ b │ c │\n\
├───┼───┼───┤\n\
│ 1 │ │ │\n\
│ 2 │ │ │\n\
│ 3 │ │ │\n\
└───┴───┴───┘\n",
);
}
#[test]
fn align_right() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("a").set_align(Right);
config
},
&[&["1"], &["23"], &["456"]],
"┌─────┐\n\
│ a │\n\
├─────┤\n\
│ 1 │\n\
│ 23 │\n\
│ 456 │\n\
└─────┘\n",
);
}
#[test]
fn align_center() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_header("a").set_align(Center);
config
},
&[&["1"], &["23"], &["456"], &["7890"], &["12345"]],
"┌───────┐\n\
│ a │\n\
├───────┤\n\
│ 1 │\n\
│ 23 │\n\
│ 456 │\n\
│ 7890 │\n\
│ 12345 │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_zero() {
test(
AsciiTable::new(),
&[&["\u{1b}[0mHello\u{1b}[0m"]],
"┌───────┐\n\
│ \u{1b}[0mHello\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_zero_inbetween() {
test(
AsciiTable::new(),
&[&["He\u{1b}[0ml\u{1b}[0mlo"]],
"┌───────┐\n\
│ He\u{1b}[0ml\u{1b}[0mlo │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_m5() {
test(
AsciiTable::new(),
&[&[&"mmmmm"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()]],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mmmmmm\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_b5() {
test(
AsciiTable::new(),
&[&[&"[[[[["
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()]],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1m[[[[[\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_s5() {
test(
AsciiTable::new(),
&[&[&";;;;;"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()]],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1m;;;;;\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_n5() {
test(
AsciiTable::new(),
&[&[&"00000"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()]],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1m00000\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_missing_m() {
test(
AsciiTable::new(),
&[&["\u{1b}[0Hello\u{1b}[0"]],
"┌─────────────┐\n\
│ \u{1b}[0Hello\u{1b}[0 │\n\
└─────────────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes() {
test(
AsciiTable::new(),
&[
&[&"Hello"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
&[&"Hello".gradient(Color::Red).to_string()],
],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mHello\u{1b}[0m │\n\
│ \u{1b}[38;2;255;0;0mH\u{1b}[38;2;255;6;0me\u{1b}[38;2;255;13;0ml\u{1b}[38;2;255;19;0ml\u{1b}[38;2;255;26;0mo\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_in_header() {
test(
{
let mut config = AsciiTable::new();
let text = "Hello".color(Color::Blue).bg_color(Color::Yellow).bold();
config.column(0).set_header(text.to_string());
config
},
&[&[""]],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mHello\u{1b}[0m │\n\
├───────┤\n\
│ │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_pad_right() {
test(
AsciiTable::new(),
&[
&[&"Hello"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
&[&"H"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mHello\u{1b}[0m │\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mH\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_pad_left() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_align(Right);
config
},
&[
&[&"Hello"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
&[&"H"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
],
"┌───────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mHello\u{1b}[0m │\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mH\u{1b}[0m │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_truncate() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(2);
config
},
&[
&[&"Hello"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
&[&"H"
.color(Color::Blue)
.bg_color(Color::Yellow)
.bold()
.to_string()],
],
"┌────┐\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mH\u{1b}[0m+ │\n\
│ \u{1b}[38;5;4m\u{1b}[48;5;3;1mH\u{1b}[0m │\n\
└────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_truncate_visible_trailing_segment() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(5);
config
},
&[&[&"\u{1b}[0mabc\u{1b}[0mdef"]],
"┌───────┐\n\
│ \u{1b}[0mabc\u{1b}[0md+ │\n\
└───────┘\n",
);
}
#[cfg(feature = "color_codes")]
#[test]
fn color_codes_truncate_fill_to_end() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(5);
config
},
&[&[&"\u{1b}[0mabcdef\u{1b}[0m\u{1b}[0m"]],
"┌───────┐\n\
│ \u{1b}[0mabcd\u{1b}[0m\u{1b}[0m+ │\n\
└───────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters() {
test(
AsciiTable::new(),
&[&["👩"]],
"┌────┐\n\
│ 👩 │\n\
└────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters2() {
test(
AsciiTable::new(),
&[&["👩🔬"]],
"┌──────┐\n\
│ 👩🔬 │\n\
└──────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters_truncate1() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(1);
config
},
&[&["👩👩👩👩"]],
"┌───┐\n\
│ + │\n\
└───┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters_truncate2() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(2);
config
},
&[&["👩👩👩👩"]],
"┌────┐\n\
│ + │\n\
└────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters_truncate3() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(3);
config
},
&[&["👩👩👩👩"]],
"┌─────┐\n\
│ 👩+ │\n\
└─────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters_truncate4() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(4);
config
},
&[&["👩👩👩👩"]],
"┌──────┐\n\
│ 👩+ │\n\
└──────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters_truncate5() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(5);
config
},
&[&["👩👩👩👩"]],
"┌───────┐\n\
│ 👩👩+ │\n\
└───────┘\n",
);
}
#[cfg(feature = "wide_characters")]
#[test]
fn wide_characters_truncate6() {
test(
{
let mut config = AsciiTable::new();
config.column(0).set_max_width(6);
config
},
&[&["👩👩👩👩"]],
"┌────────┐\n\
│ 👩👩+ │\n\
└────────┘\n",
);
}