From b4a7e7e6e9092a9e48ad29ce365c9f61207faf3c Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Tue, 19 Jul 2022 20:35:13 +0300 Subject: [PATCH] nu-table: Add a few tests (#6074) Signed-off-by: Maxim Zhiburt --- crates/nu-table/tests/constrains.rs | 285 +++++++++++++++++ crates/nu-table/tests/style.rs | 464 ++++++++++++++++++++++++++-- 2 files changed, 727 insertions(+), 22 deletions(-) create mode 100644 crates/nu-table/tests/constrains.rs diff --git a/crates/nu-table/tests/constrains.rs b/crates/nu-table/tests/constrains.rs new file mode 100644 index 0000000000..9f8af2ce96 --- /dev/null +++ b/crates/nu-table/tests/constrains.rs @@ -0,0 +1,285 @@ +use std::{collections::HashMap, usize}; + +use nu_protocol::{Config, TrimStrategy}; +use nu_table::{Alignments, StyledString, Table, TableTheme as theme, TextStyle}; + +#[test] +fn data_and_header_has_different_size() { + let table = Table::new(row(3), vec![row(5); 2], theme::heavy()); + + let expected = "┏━━━┳━━━┳━━━┳━━━┳━━━┓\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\ + ┣━━━╋━━━╋━━━╋━━━╋━━━┫\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\ + ┗━━━┻━━━┻━━━┻━━━┻━━━┛"; + + assert_eq!( + draw_table(&table, usize::MAX, &Config::default()).as_deref(), + Some(expected) + ); + + let table = Table::new(row(5), vec![row(3); 2], theme::heavy()); + + let expected = "┏━━━┳━━━┳━━━┳━━━┳━━━┓\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ 4 ┃\n\ + ┣━━━╋━━━╋━━━╋━━━╋━━━┫\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ ┃ ┃\n\ + ┗━━━┻━━━┻━━━┻━━━┻━━━┛"; + + assert_eq!( + draw_table(&table, usize::MAX, &Config::default()).as_deref(), + Some(expected) + ); +} + +#[test] +fn termwidth_too_small() { + let table = Table::new(row(3), vec![row(5); 2], theme::heavy()); + let cfg = Config::default(); + + assert!(draw_table(&table, 4, &cfg).is_some()); + + for i in 0..4 { + assert!(draw_table(&table, i, &cfg).is_none()); + } + + let cfg = Config { + trim_strategy: TrimStrategy::Truncate { suffix: None }, + ..Default::default() + }; + + assert!(draw_table(&table, 4, &cfg).is_some()); + for i in 0..4 { + assert!(draw_table(&table, i, &cfg).is_none()); + } +} + +#[test] +fn wrap_test() { + let table = Table::new( + vec![ + styled_str("123 45678"), + styled_str("qweqw eqwe"), + styled_str("xxx xx xx x xx x xx xx"), + styled_str("qqq qqq qqqq qqq qq"), + styled_str("qw"), + ], + vec![row(5); 2], + theme::heavy(), + ); + let cfg = Config { + trim_strategy: TrimStrategy::Wrap { + try_to_keep_words: false, + }, + ..Default::default() + }; + + assert_eq!( + draw_table(&table, 4, &cfg).unwrap(), + "┏━━┓\n┃ ┃\n┣━━┫\n┃ ┃\n┃ ┃\n┗━━┛" + ); + assert_eq!( + draw_table(&table, 5, &cfg).unwrap(), + "┏━━━┓\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┣━━━┫\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┗━━━┛" + ); + assert_eq!( + draw_table(&table, 6, &cfg).unwrap(), + "┏━━━━┓\n┃ .. ┃\n┃ . ┃\n┣━━━━┫\n┃ .. ┃\n┃ . ┃\n┃ .. ┃\n┃ . ┃\n┗━━━━┛" + ); + assert_eq!( + draw_table(&table, 9, &cfg).unwrap(), + "┏━━━━━┓\n┃ ... ┃\n┣━━━━━┫\n┃ ... ┃\n┃ ... ┃\n┗━━━━━┛" + ); + assert_eq!(draw_table(&table, 10, &cfg).unwrap(), "┏━━━━┳━━━┓\n┃ 12 ┃ . ┃\n┃ 3 ┃ . ┃\n┃ 45 ┃ . ┃\n┃ 67 ┃ ┃\n┃ 8 ┃ ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┗━━━━┻━━━┛"); + assert_eq!( + draw_table(&table, 21, &cfg).unwrap(), + "┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┃ 4567 ┃ w eq ┃ ┃\n┃ 8 ┃ we ┃ ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 29, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ ... ┃\n┃ 8 ┃ we ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 49, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ xxx xx ┃ qqq qqq ┃ ... ┃\n┃ 8 ┃ we ┃ xx x xx ┃ qqqq q ┃ ┃\n┃ ┃ ┃ x xx x ┃ qq qq ┃ ┃\n┃ ┃ ┃ x ┃ ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛" + ); +} + +#[test] +fn wrap_keep_words_test() { + let table = Table::new( + vec![ + styled_str("123 45678"), + styled_str("qweqw eqwe"), + styled_str("xxx xx xx x xx x xx xx"), + styled_str("qqq qqq qqqq qqq qq"), + styled_str("qw"), + ], + vec![row(5); 2], + theme::heavy(), + ); + let cfg = Config { + trim_strategy: TrimStrategy::Wrap { + try_to_keep_words: true, + }, + ..Default::default() + }; + + assert_eq!( + draw_table(&table, 4, &cfg).unwrap(), + "┏━━┓\n┃ ┃\n┣━━┫\n┃ ┃\n┃ ┃\n┗━━┛" + ); + assert_eq!( + draw_table(&table, 5, &cfg).unwrap(), + "┏━━━┓\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┣━━━┫\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┃ . ┃\n┗━━━┛" + ); + assert_eq!( + draw_table(&table, 6, &cfg).unwrap(), + "┏━━━━┓\n┃ .. ┃\n┃ . ┃\n┣━━━━┫\n┃ .. ┃\n┃ . ┃\n┃ .. ┃\n┃ . ┃\n┗━━━━┛" + ); + assert_eq!( + draw_table(&table, 9, &cfg).unwrap(), + "┏━━━━━┓\n┃ ... ┃\n┣━━━━━┫\n┃ ... ┃\n┃ ... ┃\n┗━━━━━┛" + ); + assert_eq!(draw_table(&table, 10, &cfg).unwrap(), "┏━━━━┳━━━┓\n┃ 12 ┃ . ┃\n┃ 34 ┃ . ┃\n┃ 56 ┃ . ┃\n┃ 78 ┃ ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┃ 0 ┃ . ┃\n┃ ┃ . ┃\n┃ ┃ . ┃\n┗━━━━┻━━━┛"); + assert_eq!( + draw_table(&table, 21, &cfg).unwrap(), + "┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┃ 4567 ┃ w ┃ ┃\n┃ 8 ┃ eqwe ┃ ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 29, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 ┃ qweqw ┃ ... ┃\n┃ 45678 ┃ eqwe ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 49, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 ┃ qweqw ┃ xxx xx ┃ qqq qqq ┃ ... ┃\n┃ 45678 ┃ eqwe ┃ xx x xx ┃ qqqq ┃ ┃\n┃ ┃ ┃ x xx xx ┃ qqq qq ┃ ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛" + ); +} + +#[test] +fn truncate_test() { + let table = Table::new( + vec![ + styled_str("123 45678"), + styled_str("qweqw eqwe"), + styled_str("xxx xx xx x xx x xx xx"), + styled_str("qqq qqq qqqq qqq qq"), + styled_str("qw"), + ], + vec![row(5); 2], + theme::heavy(), + ); + let cfg = Config { + trim_strategy: TrimStrategy::Truncate { suffix: None }, + ..Default::default() + }; + + assert_eq!( + draw_table(&table, 4, &cfg).unwrap(), + "┏━━┓\n┃ ┃\n┣━━┫\n┃ ┃\n┃ ┃\n┗━━┛" + ); + assert_eq!( + draw_table(&table, 5, &cfg).unwrap(), + "┏━━━┓\n┃ . ┃\n┣━━━┫\n┃ . ┃\n┃ . ┃\n┗━━━┛" + ); + assert_eq!( + draw_table(&table, 6, &cfg).unwrap(), + "┏━━━━┓\n┃ .. ┃\n┣━━━━┫\n┃ .. ┃\n┃ .. ┃\n┗━━━━┛" + ); + assert_eq!( + draw_table(&table, 9, &cfg).unwrap(), + "┏━━━━━┓\n┃ ... ┃\n┣━━━━━┫\n┃ ... ┃\n┃ ... ┃\n┗━━━━━┛" + ); + assert_eq!( + draw_table(&table, 10, &cfg).unwrap(), + "┏━━━━┳━━━┓\n┃ 12 ┃ . ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ 0 ┃ . ┃\n┗━━━━┻━━━┛" + ); + assert_eq!( + draw_table(&table, 21, &cfg).unwrap(), + "┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 123 ┃ qweq ┃ ... ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 29, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 49, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 4567 ┃ qweqw eq ┃ xxx xx ┃ qqq qqq ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛" + ); +} + +#[test] +fn truncate_with_suffix_test() { + let table = Table::new( + vec![ + styled_str("123 45678"), + styled_str("qweqw eqwe"), + styled_str("xxx xx xx x xx x xx xx"), + styled_str("qqq qqq qqqq qqq qq"), + styled_str("qw"), + ], + vec![row(5); 2], + theme::heavy(), + ); + let cfg = Config { + trim_strategy: TrimStrategy::Truncate { + suffix: Some(String::from("...")), + }, + ..Default::default() + }; + + assert_eq!( + draw_table(&table, 4, &cfg).unwrap(), + "┏━━┓\n┃ ┃\n┣━━┫\n┃ ┃\n┃ ┃\n┗━━┛" + ); + assert_eq!( + draw_table(&table, 5, &cfg).unwrap(), + "┏━━━┓\n┃ . ┃\n┣━━━┫\n┃ . ┃\n┃ . ┃\n┗━━━┛" + ); + assert_eq!( + draw_table(&table, 6, &cfg).unwrap(), + "┏━━━━┓\n┃ .. ┃\n┣━━━━┫\n┃ .. ┃\n┃ .. ┃\n┗━━━━┛" + ); + assert_eq!( + draw_table(&table, 9, &cfg).unwrap(), + "┏━━━━━┓\n┃ ... ┃\n┣━━━━━┫\n┃ ... ┃\n┃ ... ┃\n┗━━━━━┛" + ); + assert_eq!( + draw_table(&table, 10, &cfg).unwrap(), + "┏━━━━┳━━━┓\n┃ .. ┃ . ┃\n┣━━━━╋━━━┫\n┃ 0 ┃ . ┃\n┃ 0 ┃ . ┃\n┗━━━━┻━━━┛" + ); + assert_eq!( + draw_table(&table, 21, &cfg).unwrap(), + "┏━━━━━━┳━━━━━━┳━━━━━┓\n┃ 1... ┃ q... ┃ ... ┃\n┣━━━━━━╋━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━┻━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 29, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━┓\n┃ 123 4... ┃ qweqw... ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ ... ┃\n┃ 0 ┃ 1 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━┛" + ); + assert_eq!( + draw_table(&table, 49, &cfg).unwrap(), + "┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ 123 4... ┃ qweqw... ┃ xxx ... ┃ qqq ... ┃ ... ┃\n┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━╋━━━━━━━━━╋━━━━━┫\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃ ... ┃\n┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━┻━━━━━━━━━┻━━━━━┛" + ); +} + +fn draw_table(table: &Table, limit: usize, cfg: &Config) -> Option { + let styles = HashMap::default(); + let alignments = Alignments::default(); + table.draw_table(cfg, &styles, alignments, limit) +} + +fn row(count_columns: usize) -> Vec { + let mut row = Vec::with_capacity(count_columns); + + for i in 0..count_columns { + row.push(StyledString::new(i.to_string(), TextStyle::default())); + } + + row +} + +fn styled_str(s: &str) -> StyledString { + StyledString::new(s.to_string(), TextStyle::default()) +} diff --git a/crates/nu-table/tests/style.rs b/crates/nu-table/tests/style.rs index c92ddab001..323f35646e 100644 --- a/crates/nu-table/tests/style.rs +++ b/crates/nu-table/tests/style.rs @@ -1,34 +1,454 @@ use std::collections::HashMap; use nu_protocol::Config; -use nu_table::{Alignments, StyledString, Table, TableTheme, TextStyle}; +use nu_table::{Alignments, StyledString, Table, TableTheme as theme, TextStyle}; #[test] -fn test_rounded_style() { - let headers = vec![no_style_str("Hello"), no_style_str("World")]; - let data = vec![vec![no_style_str("1"), no_style_str("2")]]; - - let table = Table::new(headers, data.clone(), TableTheme::rounded()); - let table = table.draw_table( - &Config::default(), - &HashMap::default(), - Alignments::default(), - std::usize::MAX, +fn test_rounded() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::rounded())), + "╭───┬───┬───┬───╮\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ╰───┴───┴───┴───╯" ); - assert_eq!(table.as_deref(), Some("╭───────┬───────╮\n│ Hello │ World │\n├───────┼───────┤\n│ 1 │ 2 │\n╰───────┴───────╯")); - - let table = Table::new(Vec::new(), data, TableTheme::rounded()); - let table = table.draw_table( - &Config::default(), - &HashMap::default(), - Alignments::default(), - std::usize::MAX, + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::rounded())), + "╭───┬───┬───┬───╮\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ╰───┴───┴───┴───╯" ); - assert_eq!(table.as_deref(), Some("╭───┬───╮\n│ 1 │ 2 │\n╰───┴───╯")); + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::rounded())), + "╭───┬───┬───┬───╮\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ╰───┴───┴───┴───╯" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::rounded())), + "╭───┬───┬───┬───╮\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ╰───┴───┴───┴───╯" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::rounded())), + "" + ); } -fn no_style_str(text: &str) -> StyledString { - StyledString::new(text.to_owned(), TextStyle::default()) +#[test] +fn test_basic() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::basic())), + "+---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::basic())), + "+---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::basic())), + "+---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::basic())), + "+---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+\n\ + | 0 | 1 | 2 | 3 |\n\ + +---+---+---+---+" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::basic())), + "" + ); +} + +#[test] +fn test_reinforced() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::reinforced())), + "┏───┬───┬───┬───┓\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ┗───┴───┴───┴───┛" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::reinforced())), + "┏───┬───┬───┬───┓\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ┗───┴───┴───┴───┛" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::reinforced())), + "┏───┬───┬───┬───┓\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ┗───┴───┴───┴───┛" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::reinforced())), + "┏───┬───┬───┬───┓\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ┗───┴───┴───┴───┛" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::reinforced())), + "" + ); +} + +#[test] +fn test_compact() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::compact())), + concat!( + "───┬───┬───┬───\n", + " 0 │ 1 │ 2 │ 3 \n", + "───┼───┼───┼───\n", + " 0 │ 1 │ 2 │ 3 \n", + " 0 │ 1 │ 2 │ 3 \n", + "───┴───┴───┴───", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::compact())), + concat!( + "───┬───┬───┬───\n", + " 0 │ 1 │ 2 │ 3 \n", + "───┼───┼───┼───\n", + " 0 │ 1 │ 2 │ 3 \n", + "───┴───┴───┴───", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::compact())), + concat!("───┬───┬───┬───\n", " 0 │ 1 │ 2 │ 3 \n", "───┴───┴───┴───",) + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::compact())), + concat!( + "───┬───┬───┬───\n", + " 0 │ 1 │ 2 │ 3 \n", + " 0 │ 1 │ 2 │ 3 \n", + "───┴───┴───┴───", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::compact())), + "" + ); +} + +#[test] +fn test_compact_double() { + assert_eq!( + draw_table(&Table::new( + row(4), + vec![row(4); 2], + theme::compact_double() + )), + concat!( + "═══╦═══╦═══╦═══\n", + " 0 ║ 1 ║ 2 ║ 3 \n", + "═══╬═══╬═══╬═══\n", + " 0 ║ 1 ║ 2 ║ 3 \n", + " 0 ║ 1 ║ 2 ║ 3 \n", + "═══╩═══╩═══╩═══", + ) + ); + + assert_eq!( + draw_table(&Table::new( + row(4), + vec![row(4); 1], + theme::compact_double() + )), + concat!( + "═══╦═══╦═══╦═══\n", + " 0 ║ 1 ║ 2 ║ 3 \n", + "═══╬═══╬═══╬═══\n", + " 0 ║ 1 ║ 2 ║ 3 \n", + "═══╩═══╩═══╩═══", + ) + ); + + assert_eq!( + draw_table(&Table::new( + row(4), + vec![row(4); 0], + theme::compact_double() + )), + concat!("═══╦═══╦═══╦═══\n", " 0 ║ 1 ║ 2 ║ 3 \n", "═══╩═══╩═══╩═══",) + ); + + assert_eq!( + draw_table(&Table::new( + row(0), + vec![row(4); 2], + theme::compact_double() + )), + concat!( + "═══╦═══╦═══╦═══\n", + " 0 ║ 1 ║ 2 ║ 3 \n", + " 0 ║ 1 ║ 2 ║ 3 \n", + "═══╩═══╩═══╩═══", + ) + ); + + assert_eq!( + draw_table(&Table::new( + row(0), + vec![row(0); 0], + theme::compact_double() + )), + "" + ); +} + +#[test] +fn test_heavy() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::heavy())), + "┏━━━┳━━━┳━━━┳━━━┓\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┣━━━╋━━━╋━━━╋━━━┫\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┗━━━┻━━━┻━━━┻━━━┛" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::heavy())), + "┏━━━┳━━━┳━━━┳━━━┓\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┣━━━╋━━━╋━━━╋━━━┫\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┗━━━┻━━━┻━━━┻━━━┛" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::heavy())), + "┏━━━┳━━━┳━━━┳━━━┓\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┗━━━┻━━━┻━━━┻━━━┛" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::heavy())), + "┏━━━┳━━━┳━━━┳━━━┓\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃\n\ + ┗━━━┻━━━┻━━━┻━━━┛" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::heavy())), + "" + ); +} + +#[test] +fn test_light() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::light())), + concat!( + " 0 1 2 3 \n", + "───────────────\n", + " 0 1 2 3 \n", + " 0 1 2 3 ", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::light())), + concat!(" 0 1 2 3 \n", "───────────────\n", " 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::light())), + concat!(" 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::light())), + concat!(" 0 1 2 3 \n", " 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::light())), + "" + ); +} + +#[test] +fn test_none() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::none())), + concat!(" 0 1 2 3 \n", " 0 1 2 3 \n", " 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::none())), + concat!(" 0 1 2 3 \n", " 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::none())), + concat!(" 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::none())), + concat!(" 0 1 2 3 \n", " 0 1 2 3 ") + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::none())), + "" + ); +} + +#[test] +fn test_thin() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::thin())), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::thin())), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::thin())), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::thin())), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::thin())), + "" + ); +} + +#[test] +fn test_with_love() { + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 2], theme::with_love())), + concat!( + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 1], theme::with_love())), + concat!( + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(4), vec![row(4); 0], theme::with_love())), + concat!("❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", " 0 ❤ 1 ❤ 2 ❤ 3 \n", "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤",) + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(4); 2], theme::with_love())), + concat!( + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤\n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + " 0 ❤ 1 ❤ 2 ❤ 3 \n", + "❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤", + ) + ); + + assert_eq!( + draw_table(&Table::new(row(0), vec![row(0); 0], theme::with_love())), + "" + ); +} + +fn draw_table(table: &Table) -> String { + let cfg = Config::default(); + let styles = HashMap::default(); + let alignments = Alignments::default(); + table + .draw_table(&cfg, &styles, alignments, std::usize::MAX) + .expect("Unexpectdly got no table") +} + +fn row(count_columns: usize) -> Vec { + let mut row = Vec::with_capacity(count_columns); + + for i in 0..count_columns { + row.push(StyledString::new(i.to_string(), TextStyle::default())); + } + + row }