diff options
Diffstat (limited to 'vendor/tabled/tests')
40 files changed, 15455 insertions, 0 deletions
diff --git a/vendor/tabled/tests/core/builder_test.rs b/vendor/tabled/tests/core/builder_test.rs new file mode 100644 index 000000000..7751df12d --- /dev/null +++ b/vendor/tabled/tests/core/builder_test.rs @@ -0,0 +1,748 @@ +#![cfg(feature = "std")] + +use std::iter::FromIterator; + +use tabled::builder::Builder; + +use testing_table::test_table; + +test_table!( + push_record, + { + let mut b = Builder::default(); + b.push_record(["1", "2", "3"]); + b.push_record(["a", "b", "c"]); + b.push_record(["d", "e", "f"]); + b.build() + }, + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + set_header, + { + let mut b = Builder::default(); + b.set_header(["1", "2", "3"]); + b.push_record(["a", "b", "c"]); + b.push_record(["d", "e", "f"]); + b.build() + }, + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + header_remove_0, + { + let mut b = Builder::default(); + b.set_header(["1", "2", "3"]); + b.push_record(["a", "b", "c"]); + b.push_record(["d", "e", "f"]); + b.remove_header(); + b.build() + }, + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + header_remove_1, + { + let mut b = Builder::default(); + b.set_header(["1", "2", "3", "4", "5"]); + b.push_record(["a", "b", "c"]); + b.push_record(["d", "e", "f"]); + b.remove_header(); + b.build() + }, + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + from_iter, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]).build(), + "+---+-----------+" + "| n | name |" + "+---+-----------+" + "| 0 | Dmitriy |" + "+---+-----------+" + "| 1 | Vladislav |" + "+---+-----------+" +); + +test_table!( + used_with_different_number_of_columns_0, + { + let mut b = Builder::default(); + b.set_header(["1", "2"]); + b.push_record(["a", "b", "c"]); + b.push_record(["d"]); + b.build() + }, + "+---+---+---+" + "| 1 | 2 | |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | | |" + "+---+---+---+" +); + +test_table!( + used_with_different_number_of_columns_1, + { + let mut b = Builder::default(); + b.set_header(["1", "2", "3"]); + b.push_record(["a", "b"]); + b.push_record(["d"]); + b.build() + }, + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | |" + "+---+---+---+" + "| d | | |" + "+---+---+---+" +); + +test_table!( + used_with_different_number_of_columns_2, + { + let mut b = Builder::default(); + b.set_header(["1"]); + b.push_record(["a", "b"]); + b.push_record(["d", "e", "f"]); + b.build() + }, + "+---+---+---+" + "| 1 | | |" + "+---+---+---+" + "| a | b | |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + with_default_cell_0, + { + let mut b = Builder::default(); + b.set_header(["1", "2"]).set_default_text("NaN").push_record(["a", "b", "c"]).push_record(["d"]); + b.build() + }, + "+---+-----+-----+" + "| 1 | 2 | NaN |" + "+---+-----+-----+" + "| a | b | c |" + "+---+-----+-----+" + "| d | NaN | NaN |" + "+---+-----+-----+" +); + +test_table!( + with_default_cell_1, + { + let mut b = Builder::default(); + b.set_header(["1"]).set_default_text("NaN").push_record(["a", "b"]).push_record(["d", "e", "f"]); + b.build() + }, + "+---+-----+-----+" + "| 1 | NaN | NaN |" + "+---+-----+-----+" + "| a | b | NaN |" + "+---+-----+-----+" + "| d | e | f |" + "+---+-----+-----+" +); + +test_table!( + extend, + { + let mut b = Builder::default(); + b.extend(["1", "2", "3"]); + b.extend(["a", "b", "c"]); + b.extend(["d", "e", "f"]); + b.build() + }, + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + from_vector_0, + Builder::from_iter(vec![ + vec!["1".to_string(), "2".to_string(), "3".to_string()], + vec!["a".to_string(), "b".to_string(), "c".to_string()], + vec!["d".to_string(), "e".to_string(), "f".to_string()], + ]) + .build(), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + from_with_empty_lines_0, + Builder::from_iter(vec![ + vec!["1".to_string(), "2".to_string(), "3".to_string()], + vec![], + vec![], + vec!["d".to_string(), "e".to_string(), "f".to_string()], + ]) + .build(), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| | | |" + "+---+---+---+" + "| | | |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + from_with_empty_lines_1, + Builder::from_iter(vec![ + vec!["1".to_string(), "2".to_string(), "3".to_string()], + vec![], + vec!["d".to_string(), "e".to_string(), "f".to_string()], + ]) + .build(), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| | | |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + from_with_empty_lines_2, + Builder::from_iter(vec![ + vec![], + vec!["1".to_string(), "2".to_string(), "3".to_string()], + vec!["d".to_string(), "e".to_string(), "f".to_string()], + ]) + .build(), + "+---+---+---+" + "| | | |" + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + from_with_empty_lines_3, + Builder::from_iter(vec![ + vec!["1".to_string(), "2".to_string(), "3".to_string()], + vec!["d".to_string(), "e".to_string(), "f".to_string()], + vec![], + ]) + .build(), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" + "| | | |" + "+---+---+---+" +); + +test_table!( + clean_0, + clean(Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["d", "e", "f"]])), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + clean_1, + clean(Builder::from_iter([["", "2", "3"], ["", "b", "c"], ["", "e", "f"]])), + "+---+---+" + "| 2 | 3 |" + "+---+---+" + "| b | c |" + "+---+---+" + "| e | f |" + "+---+---+" +); + +test_table!( + clean_2, + clean(Builder::from_iter([["1", "", "3"], ["a", "", "c"], ["d", "", "f"]])), + "+---+---+" + "| 1 | 3 |" + "+---+---+" + "| a | c |" + "+---+---+" + "| d | f |" + "+---+---+" +); + +test_table!( + clean_3, + clean(Builder::from_iter([["1", "2", ""], ["a", "b", ""], ["d", "e", ""]])), + "+---+---+" + "| 1 | 2 |" + "+---+---+" + "| a | b |" + "+---+---+" + "| d | e |" + "+---+---+" +); + +test_table!( + clean_4, + clean(Builder::from_iter([["", "", "3"], ["", "", "c"], ["", "", "f"]])), + "+---+" + "| 3 |" + "+---+" + "| c |" + "+---+" + "| f |" + "+---+" +); + +test_table!( + clean_5, + clean(Builder::from_iter([["1", "", ""], ["a", "", ""], ["d", "", ""]])), + "+---+" + "| 1 |" + "+---+" + "| a |" + "+---+" + "| d |" + "+---+" +); + +test_table!( + clean_6, + clean(Builder::from_iter([["", "2", ""], ["", "b", ""], ["", "e", ""]])), + "+---+" + "| 2 |" + "+---+" + "| b |" + "+---+" + "| e |" + "+---+" +); + +test_table!( + clean_7, + clean(Builder::from_iter([["", "", ""], ["a", "b", "c"], ["d", "e", "f"]])), + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + clean_8, + clean(Builder::from_iter([["1", "2", "3"], ["", "", ""], ["d", "e", "f"]])), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + clean_9, + clean(Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["", "", ""]])), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" +); + +test_table!( + clean_10, + clean(Builder::from_iter([["", "", ""], ["", "", ""], ["d", "e", "f"]])), + "+---+---+---+" + "| d | e | f |" + "+---+---+---+" +); + +test_table!( + clean_11, + clean(Builder::from_iter([["1", "2", "3"], ["", "", ""], ["", "", ""]])), + "+---+---+---+" + "| 1 | 2 | 3 |" + "+---+---+---+" +); + +test_table!( + clean_12, + clean(Builder::from_iter([["", "", ""], ["a", "b", "c"], ["", "", ""]])), + "+---+---+---+" + "| a | b | c |" + "+---+---+---+" +); + +test_table!( + clean_13, + clean(Builder::from_iter([["1", "", "3"], ["", "", ""], ["d", "", "f"]])), + "+---+---+" + "| 1 | 3 |" + "+---+---+" + "| d | f |" + "+---+---+" +); + +test_table!( + clean_with_columns_0, + clean(Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["d", "e", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| 1 | 2 | 3 |" + "+------+------+------+" + "| a | b | c |" + "+------+------+------+" + "| d | e | f |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_1, + clean(Builder::from_iter([["", "2", "3"], ["", "b", "c"], ["", "e", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+" + "| col2 | col3 |" + "+------+------+" + "| 2 | 3 |" + "+------+------+" + "| b | c |" + "+------+------+" + "| e | f |" + "+------+------+" +); + +test_table!( + clean_with_columns_2, + clean(Builder::from_iter([["1", "", "3"], ["a", "", "c"], ["d", "", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+" + "| col1 | col3 |" + "+------+------+" + "| 1 | 3 |" + "+------+------+" + "| a | c |" + "+------+------+" + "| d | f |" + "+------+------+" +); + +test_table!( + clean_with_columns_3, + clean(Builder::from_iter([["1", "2", ""], ["a", "b", ""], ["d", "e", ""]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+" + "| col1 | col2 |" + "+------+------+" + "| 1 | 2 |" + "+------+------+" + "| a | b |" + "+------+------+" + "| d | e |" + "+------+------+" +); + +test_table!( + clean_with_columns_4, + clean(Builder::from_iter([["", "", "3"], ["", "", "c"], ["", "", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+" + "| col3 |" + "+------+" + "| 3 |" + "+------+" + "| c |" + "+------+" + "| f |" + "+------+" +); + +test_table!( + clean_with_columns_5, + clean(Builder::from_iter([["1", "", ""], ["a", "", ""], ["d", "", ""]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+" + "| col1 |" + "+------+" + "| 1 |" + "+------+" + "| a |" + "+------+" + "| d |" + "+------+" +); + +test_table!( + clean_with_columns_6, + clean(Builder::from_iter([["", "2", ""], ["", "b", ""], ["", "e", ""]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+" + "| col2 |" + "+------+" + "| 2 |" + "+------+" + "| b |" + "+------+" + "| e |" + "+------+" +); + +test_table!( + clean_with_columns_7, + clean( + Builder::from_iter([["", "", ""], ["", "", ""], ["", "", ""]]) + .set_header(["col1", "col2", "col3"]) + .clone() + ), + "" +); + +test_table!( + clean_with_columns_8, + clean(Builder::from_iter([["", "", ""], ["a", "b", "c"], ["d", "e", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| a | b | c |" + "+------+------+------+" + "| d | e | f |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_9, + clean(Builder::from_iter([["1", "2", "3"], ["", "", ""], ["d", "e", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| 1 | 2 | 3 |" + "+------+------+------+" + "| d | e | f |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_10, + clean(Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["", "", ""]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| 1 | 2 | 3 |" + "+------+------+------+" + "| a | b | c |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_11, + clean(Builder::from_iter([["", "", ""], ["", "", ""], ["d", "e", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| d | e | f |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_12, + clean(Builder::from_iter([["1", "2", "3"], ["", "", ""], ["", "", ""]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| 1 | 2 | 3 |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_13, + clean(Builder::from_iter([["", "", ""], ["a", "b", "c"], ["", "", ""]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+------+" + "| col1 | col2 | col3 |" + "+------+------+------+" + "| a | b | c |" + "+------+------+------+" +); + +test_table!( + clean_with_columns_14, + clean(Builder::from_iter([["1", "", "3"], ["", "", ""], ["d", "", "f"]]).set_header(["col1", "col2", "col3"]).clone()), + "+------+------+" + "| col1 | col3 |" + "+------+------+" + "| 1 | 3 |" + "+------+------+" + "| d | f |" + "+------+------+" +); + +test_table!(clean_empty_0, clean(Builder::from_iter([[""; 0]; 0])), ""); + +test_table!(clean_empty_1, clean(Builder::from_iter([[""; 0]; 10])), ""); + +test_table!( + index, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]).index().build(), + "+---+---+-----------+" + "| | 0 | 1 |" + "+---+---+-----------+" + "| 0 | n | name |" + "+---+---+-----------+" + "| 1 | 0 | Dmitriy |" + "+---+---+-----------+" + "| 2 | 1 | Vladislav |" + "+---+---+-----------+" +); + +test_table!( + index_set_name, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]) + .index() + .name(Some("A index name".into())) + .build(), + "+--------------+---+-----------+" + "| | 0 | 1 |" + "+--------------+---+-----------+" + "| A index name | | |" + "+--------------+---+-----------+" + "| 0 | n | name |" + "+--------------+---+-----------+" + "| 1 | 0 | Dmitriy |" + "+--------------+---+-----------+" + "| 2 | 1 | Vladislav |" + "+--------------+---+-----------+" +); + +test_table!( + index_enumeration, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]) + .index() + .hide() + .build(), + "+---+-----------+" + "| 0 | 1 |" + "+---+-----------+" + "| n | name |" + "+---+-----------+" + "| 0 | Dmitriy |" + "+---+-----------+" + "| 1 | Vladislav |" + "+---+-----------+" +); + +test_table!( + set_index, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]) + .index() + .column(1) + .build(), + "+-----------+---+" + "| | 0 |" + "+-----------+---+" + "| 1 | |" + "+-----------+---+" + "| name | n |" + "+-----------+---+" + "| Dmitriy | 0 |" + "+-----------+---+" + "| Vladislav | 1 |" + "+-----------+---+" +); + +test_table!( + set_index_and_set_index_name_0, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]) + .index() + .column(1) + .name(Some("Hello".into())) + .build(), + "+-----------+---+" + "| | 0 |" + "+-----------+---+" + "| Hello | |" + "+-----------+---+" + "| name | n |" + "+-----------+---+" + "| Dmitriy | 0 |" + "+-----------+---+" + "| Vladislav | 1 |" + "+-----------+---+" +); + +test_table!( + set_index_and_set_index_name_1, + Builder::from_iter([["n", "name"], ["0", "Dmitriy"], ["1", "Vladislav"]]) + .index() + .column(1) + .name(None) + .build(), + "+-----------+---+" + "| | 0 |" + "+-----------+---+" + "| name | n |" + "+-----------+---+" + "| Dmitriy | 0 |" + "+-----------+---+" + "| Vladislav | 1 |" + "+-----------+---+" +); + +test_table!( + index_transpose, + Builder::from_iter([["n", "name", "zz"], ["0", "Dmitriy", "123"], ["1", "Vladislav", "123"]]) + .index() + .transpose() + .build(), + "+---+------+---------+-----------+" + "| | 0 | 1 | 2 |" + "+---+------+---------+-----------+" + "| 0 | n | 0 | 1 |" + "+---+------+---------+-----------+" + "| 1 | name | Dmitriy | Vladislav |" + "+---+------+---------+-----------+" + "| 2 | zz | 123 | 123 |" + "+---+------+---------+-----------+" +); + +fn clean(mut b: Builder) -> String { + b.clean(); + b.build().to_string() +} diff --git a/vendor/tabled/tests/core/compact_table.rs b/vendor/tabled/tests/core/compact_table.rs new file mode 100644 index 000000000..1bdaad998 --- /dev/null +++ b/vendor/tabled/tests/core/compact_table.rs @@ -0,0 +1,119 @@ +#![cfg(feature = "std")] + +use tabled::{ + grid::{ + config::CompactConfig, dimension::CompactGridDimension, dimension::Estimate, + records::IterRecords, + }, + tables::CompactTable, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + compact_new, + CompactTable::new(Matrix::new(3, 3).to_vec()).to_string(), + "" +); + +test_table!( + compact_with_dimension, + { + let data = Matrix::with_no_frame(3, 3).to_vec(); + let mut dims = CompactGridDimension::default(); + dims.estimate(IterRecords::new(&data, 3, None), &CompactConfig::default()); + CompactTable::with_dimension(data, dims).columns(3).to_string() + }, + "+-----+-----+-----+" + "| 0-0 | 0-1 | 0-2 |" + "|-----+-----+-----|" + "| 1-0 | 1-1 | 1-2 |" + "|-----+-----+-----|" + "| 2-0 | 2-1 | 2-2 |" + "+-----+-----+-----+" +); + +test_table!( + compact_width, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec().to_vec()).columns(3).width(5).to_string(), + "+-----+-----+-----+" + "| 0-0 | 0-1 | 0-2 |" + "|-----+-----+-----|" + "| 1-0 | 1-1 | 1-2 |" + "|-----+-----+-----|" + "| 2-0 | 2-1 | 2-2 |" + "+-----+-----+-----+" +); + +test_table!( + compact_width_pad_not_included, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3).width(3).to_string(), + "+---+---+---+" + "| 0-0 | 0-1 | 0-2 |" + "|---+---+---|" + "| 1-0 | 1-1 | 1-2 |" + "|---+---+---|" + "| 2-0 | 2-1 | 2-2 |" + "+---+---+---+" +); + +test_table!( + compact_width_bigger, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3).width(10).to_string(), + "+----------+----------+----------+" + "| 0-0 | 0-1 | 0-2 |" + "|----------+----------+----------|" + "| 1-0 | 1-1 | 1-2 |" + "|----------+----------+----------|" + "| 2-0 | 2-1 | 2-2 |" + "+----------+----------+----------+" +); + +test_table!( + compact_columns, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3).to_string(), + "+--+--+--+" + "| 0-0 | 0-1 | 0-2 |" + "|--+--+--|" + "| 1-0 | 1-1 | 1-2 |" + "|--+--+--|" + "| 2-0 | 2-1 | 2-2 |" + "+--+--+--+" +); + +test_table!( + compact_cols_zero, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()) + .columns(0) + .to_string(), + "" +); + +test_table!( + compact_cols_less, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()) + .columns(1) + .to_string(), + "+--+" + "| 0-0 |" + "|--|" + "| 1-0 |" + "|--|" + "| 2-0 |" + "+--+" +); + +test_table!( + compact_cols_more, + CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()) + .columns(5) + .to_string(), + "+--+--+--+--+--+" + "| 0-0 | 0-1 | 0-2 |" + "|--+--+--+--+--|" + "| 1-0 | 1-1 | 1-2 |" + "|--+--+--+--+--|" + "| 2-0 | 2-1 | 2-2 |" + "+--+--+--+--+--+" +); diff --git a/vendor/tabled/tests/core/extended_table_test.rs b/vendor/tabled/tests/core/extended_table_test.rs new file mode 100644 index 000000000..8e511127d --- /dev/null +++ b/vendor/tabled/tests/core/extended_table_test.rs @@ -0,0 +1,472 @@ +#![cfg(feature = "std")] + +#[cfg(feature = "color")] +use owo_colors::{AnsiColors, OwoColorize}; + +use tabled::{tables::ExtendedTable, Tabled}; + +use crate::matrix::Matrix; +use testing_table::{static_table, test_table}; + +macro_rules! assert_expanded_display { + ( $data:expr, $expected:expr ) => { + let table = ExtendedTable::new($data).to_string(); + assert_eq!(table, $expected); + }; +} + +macro_rules! build_tabled_type { + ( $name:ident, $length:expr, $fields:expr, $headers:expr ) => { + #[derive(Debug, Clone, Copy)] + struct $name; + + impl Tabled for $name { + const LENGTH: usize = $length; + + fn fields(&self) -> Vec<std::borrow::Cow<'_, str>> { + $fields.iter().map(|s| s.to_string().into()).collect() + } + + fn headers() -> Vec<std::borrow::Cow<'static, str>> { + $headers.iter().map(|s| s.to_string().into()).collect() + } + } + }; +} + +test_table!( + display, + ExtendedTable::from(Matrix::vec(3, 3)), + "-[ RECORD 0 ]-" + "N | 0" + "column 0 | 0-0" + "column 1 | 0-1" + "column 2 | 0-2" + "-[ RECORD 1 ]-" + "N | 1" + "column 0 | 1-0" + "column 1 | 1-1" + "column 2 | 1-2" + "-[ RECORD 2 ]-" + "N | 2" + "column 0 | 2-0" + "column 1 | 2-1" + "column 2 | 2-2" +); + +#[test] +fn display_empty_records() { + build_tabled_type!(TestType, 3, ["He", "123", "asd"], ["1", "2", "3"]); + let data: Vec<TestType> = vec![]; + assert_expanded_display!(data, ""); +} + +#[test] +fn display_empty() { + build_tabled_type!( + TestType, + 3, + { + let d: Vec<String> = vec![]; + d + }, + { + let d: Vec<String> = vec![]; + d + } + ); + let data: Vec<TestType> = vec![]; + assert_expanded_display!(data, ""); +} + +#[test] +fn display_empty_2() { + build_tabled_type!(EmptyType, 0, [""; 0], [""; 0]); + assert_expanded_display!(&[EmptyType], "-[ RECORD 0 ]-"); +} + +#[test] +fn display_dynamic_header_template() { + { + build_tabled_type!(TestType, 3, ["He", "123", "asd"], ["1", "2", "3"]); + assert_expanded_display!( + &[TestType], + static_table!( + "-[ RECORD 0 ]-" + "1 | He" + "2 | 123" + "3 | asd" + ) + ); + } + { + build_tabled_type!(TestType, 3, ["He", "123", "asd"], ["11", "2222222", "3"]); + assert_expanded_display!( + &[TestType], + static_table!( + "-[ RECORD 0 ]-" + "11 | He" + "2222222 | 123" + "3 | asd" + ) + ); + } + { + build_tabled_type!( + TestType, + 3, + ["HeheHehe", "123", "asd"], + ["11", "2222222", "3"] + ); + assert_expanded_display!( + &[TestType], + static_table!( + "-[ RECORD 0 ]-----" + "11 | HeheHehe" + "2222222 | 123" + "3 | asd" + ) + ); + } + { + build_tabled_type!(TestType, 3, ["He", "123", "asd"], ["11111111111", "2", "3"]); + assert_expanded_display!( + &[TestType], + static_table!( + "-[ RECORD 0 ]----" + "11111111111 | He" + "2 | 123" + "3 | asd" + ) + ); + } + { + build_tabled_type!( + TestType, + 3, + ["He", "123", "asd"], + ["1111111111111", "2", "3"] + ); + assert_expanded_display!( + &[TestType], + static_table!( + "-[ RECORD 0 ]-+----" + "1111111111111 | He" + "2 | 123" + "3 | asd" + ) + ); + } + { + build_tabled_type!( + TestType, + 3, + ["He", "123", "asd"], + ["11111111111111111111111111111", "2", "3"] + ); + assert_expanded_display!( + &[TestType], + static_table!( + "-[ RECORD 0 ]-----------------+----" + "11111111111111111111111111111 | He" + "2 | 123" + "3 | asd" + ) + ); + } + { + build_tabled_type!(TestType, 3, ["22"], ["11111111111"]); + assert_expanded_display!( + std::iter::repeat(TestType).take(11), + static_table!( + "-[ RECORD 0 ]---" + "11111111111 | 22" + "-[ RECORD 1 ]---" + "11111111111 | 22" + "-[ RECORD 2 ]---" + "11111111111 | 22" + "-[ RECORD 3 ]---" + "11111111111 | 22" + "-[ RECORD 4 ]---" + "11111111111 | 22" + "-[ RECORD 5 ]---" + "11111111111 | 22" + "-[ RECORD 6 ]---" + "11111111111 | 22" + "-[ RECORD 7 ]---" + "11111111111 | 22" + "-[ RECORD 8 ]---" + "11111111111 | 22" + "-[ RECORD 9 ]---" + "11111111111 | 22" + "-[ RECORD 10 ]--" + "11111111111 | 22" + ) + ); + } +} + +#[test] +fn display_multiline_field() { + build_tabled_type!(TestType, 3, ["1", "2", "3"], ["Hello\nWorld", "123", "asd"]); + assert_expanded_display!( + [TestType], + static_table!( + "-[ RECORD 0 ]---" + "Hello\\nWorld | 1" + "123 | 2" + "asd | 3" + ) + ); +} + +#[test] +fn display_multiline_record_value() { + let mut data = Matrix::list::<2, 3>(); + data[0][0] = "Hello\nWorld".to_string(); + data[0][1] = "123".to_string(); + data[0][2] = "asd".to_string(); + + assert_expanded_display!( + data, + static_table!( + "-[ RECORD 0 ]----------" + "N | Hello\\nWorld" + "column 0 | 123" + "column 1 | asd" + "column 2 | 0-2" + "-[ RECORD 1 ]----------" + "N | 1" + "column 0 | 1-0" + "column 1 | 1-1" + "column 2 | 1-2" + ) + ); +} + +test_table!( + display_with_truncate, + { + let data = Matrix::new(3, 3).insert((1, 0), "a long string").to_vec(); + let mut table = ExtendedTable::from(data); + table.truncate(14, ""); + table.to_string() + }, + "-[ RECORD 0 ]-" + "N | a l" + "column 0 | 0-0" + "column 1 | 0-1" + "column 2 | 0-2" + "-[ RECORD 1 ]-" + "N | 1" + "column 0 | 1-0" + "column 1 | 1-1" + "column 2 | 1-2" + "-[ RECORD 2 ]-" + "N | 2" + "column 0 | 2-0" + "column 1 | 2-1" + "column 2 | 2-2" +); + +test_table!( + truncate_with_suffix, + { + let data = Matrix::new(3, 3).insert((1, 0), "a long string").to_vec(); + let mut table = ExtendedTable::from(data); + table.truncate(15, ".."); + table.to_string() + }, + "-[ RECORD 0 ]-" + "N | .." + "column 0 | .." + "column 1 | .." + "column 2 | .." + "-[ RECORD 1 ]-" + "N | .." + "column 0 | .." + "column 1 | .." + "column 2 | .." + "-[ RECORD 2 ]-" + "N | .." + "column 0 | .." + "column 1 | .." + "column 2 | .." +); + +#[test] +fn truncate_big_fields() { + build_tabled_type!( + TestType, + 3, + ["1", "2", "3"], + ["A quite big field", "123", "asd"] + ); + let data: Vec<TestType> = vec![TestType, TestType]; + + let mut table = ExtendedTable::new(&data); + table.truncate(14, ".."); + let table = table.to_string(); + + assert_eq!( + table, + static_table!( + "-[ RECORD 0 ]-" + "A quite.. | .." + "123 | .." + "asd | .." + "-[ RECORD 1 ]-" + "A quite.. | .." + "123 | .." + "asd | .." + ) + ); + + let mut table = ExtendedTable::new(&data); + table.truncate(15, ".."); + let table = table.to_string(); + + assert_eq!( + table, + static_table!( + "-[ RECORD 0 ]--" + "A quite .. | .." + "123 | .." + "asd | .." + "-[ RECORD 1 ]--" + "A quite .. | .." + "123 | .." + "asd | .." + ) + ); + + let mut table = ExtendedTable::new(&data); + table.truncate(0, ".."); + let table = table.to_string(); + + assert_eq!( + table, + static_table!( + "-[ RECORD 0 ]-----+--" + "A quite big field | 1" + "123 | 2" + "asd | 3" + "-[ RECORD 1 ]-----+--" + "A quite big field | 1" + "123 | 2" + "asd | 3" + ) + ); + + let mut table = ExtendedTable::new(&data); + table.truncate(20, "......"); + let table = table.to_string(); + + assert_eq!( + table, + static_table!( + "-[ RECORD 0 ]-------" + "A qui...... | ......" + "123 | ......" + "asd | ......" + "-[ RECORD 1 ]-------" + "A qui...... | ......" + "123 | ......" + "asd | ......" + ) + ); +} + +test_table!( + truncate_too_small, + { + let data = Matrix::new(3, 3).insert((1, 0), "a long string").to_vec(); + let mut table = ExtendedTable::from(data); + let success = table.truncate(2, ""); + assert!(!success); + table + }, + "-[ RECORD 0 ]-----------" + "N | a long string" + "column 0 | 0-0" + "column 1 | 0-1" + "column 2 | 0-2" + "-[ RECORD 1 ]-----------" + "N | 1" + "column 0 | 1-0" + "column 1 | 1-1" + "column 2 | 1-2" + "-[ RECORD 2 ]-----------" + "N | 2" + "column 0 | 2-0" + "column 1 | 2-1" + "column 2 | 2-2" +); + +#[cfg(feature = "color")] +#[test] +fn display_colored() { + let mut data = Matrix::list::<3, 3>(); + data[0][2] = "https://getfedora.org/" + .red() + .on_color(AnsiColors::Blue) + .to_string(); + data[1][2] = "https://www.opensuse.org/" + .green() + .on_color(AnsiColors::Black) + .to_string(); + data[2][2] = "https://endeavouros.com/".blue().underline().to_string(); + + assert_expanded_display!( + data, + static_table!( + "-[ RECORD 0 ]------------------------------------------------------------" + "N | 0" + "column 0 | 0-0" + "column 1 | \\u{1b}[31;44mhttps://getfedora.org/\\u{1b}[0m" + "column 2 | 0-2" + "-[ RECORD 1 ]------------------------------------------------------------" + "N | 1" + "column 0 | 1-0" + "column 1 | \\u{1b}[32;40mhttps://www.opensuse.org/\\u{1b}[0m" + "column 2 | 1-2" + "-[ RECORD 2 ]------------------------------------------------------------" + "N | 2" + "column 0 | 2-0" + "column 1 | \\u{1b}[4m\\u{1b}[34mhttps://endeavouros.com/\\u{1b}[39m\\u{1b}[0m" + "column 2 | 2-2" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn display_with_truncate_colored() { + let mut data = Matrix::list::<2, 3>(); + data[0][2] = "https://getfedora.org/".red().to_string(); + data[1][1] = "https://endeavouros.com/" + .white() + .on_color(AnsiColors::Black) + .to_string(); + data[1][2] = "https://www.opensuse.org/".to_string(); + + let mut table = ExtendedTable::new(&data); + table.truncate(20, ""); + let table = table.to_string(); + + assert_eq!( + table, + static_table!( + "-[ RECORD 0 ]-------" + "N | 0" + "column 0 | 0-0" + "column 1 | \\u{1b}[31" + "column 2 | 0-2" + "-[ RECORD 1 ]-------" + "N | 1" + "column 0 | \\u{1b}[37" + "column 1 | https://w" + "column 2 | 1-2" + ) + ); +} diff --git a/vendor/tabled/tests/core/index_test.rs b/vendor/tabled/tests/core/index_test.rs new file mode 100644 index 000000000..f6e107a5a --- /dev/null +++ b/vendor/tabled/tests/core/index_test.rs @@ -0,0 +1,194 @@ +#![cfg(feature = "std")] + +use std::iter::FromIterator; + +use crate::matrix::Matrix; +use tabled::{builder::Builder, Table}; +use testing_table::test_table; + +test_table!( + builder_index, + Table::builder(Matrix::list::<3, 2>()).index().build(), + "+---+---+----------+----------+" + "| | N | column 0 | column 1 |" + "+---+---+----------+----------+" + "| 0 | 0 | 0-0 | 0-1 |" + "+---+---+----------+----------+" + "| 1 | 1 | 1-0 | 1-1 |" + "+---+---+----------+----------+" + "| 2 | 2 | 2-0 | 2-1 |" + "+---+---+----------+----------+" +); + +test_table!( + builder_index_transpose, + Table::builder(Matrix::list::<4, 2>()).index().transpose().build(), + "+----------+-----+-----+-----+-----+" + "| | 0 | 1 | 2 | 3 |" + "+----------+-----+-----+-----+-----+" + "| N | 0 | 1 | 2 | 3 |" + "+----------+-----+-----+-----+-----+" + "| column 0 | 0-0 | 1-0 | 2-0 | 3-0 |" + "+----------+-----+-----+-----+-----+" + "| column 1 | 0-1 | 1-1 | 2-1 | 3-1 |" + "+----------+-----+-----+-----+-----+" +); + +test_table!( + builder_index_0, + Table::builder(Matrix::list::<4, 2>()).index().column(0).build(), + "+---+----------+----------+" + "| | column 0 | column 1 |" + "+---+----------+----------+" + "| N | | |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" + "| 2 | 2-0 | 2-1 |" + "+---+----------+----------+" + "| 3 | 3-0 | 3-1 |" + "+---+----------+----------+" +); + +test_table!( + builder_index_0_no_name, + Table::builder(Matrix::list::<4, 2>()).index().column(0).name(None).build(), + "+---+----------+----------+" + "| | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" + "| 2 | 2-0 | 2-1 |" + "+---+----------+----------+" + "| 3 | 3-0 | 3-1 |" + "+---+----------+----------+" +); + +test_table!( + builder_index_0_name, + Table::builder(Matrix::list::<4, 2>()).index().column(0).name(Some("Hello World".into())).build(), + "+-------------+----------+----------+" + "| | column 0 | column 1 |" + "+-------------+----------+----------+" + "| Hello World | | |" + "+-------------+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+-------------+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+-------------+----------+----------+" + "| 2 | 2-0 | 2-1 |" + "+-------------+----------+----------+" + "| 3 | 3-0 | 3-1 |" + "+-------------+----------+----------+" +); + +test_table!( + builder_index_0_name_transpose, + Table::builder(Matrix::list::<4, 2>()).index().column(0).name(Some("Hello World".into())).transpose().build(), + "+-------------+-----+-----+-----+-----+" + "| Hello World | 0 | 1 | 2 | 3 |" + "+-------------+-----+-----+-----+-----+" + "| column 0 | 0-0 | 1-0 | 2-0 | 3-0 |" + "+-------------+-----+-----+-----+-----+" + "| column 1 | 0-1 | 1-1 | 2-1 | 3-1 |" + "+-------------+-----+-----+-----+-----+" +); + +test_table!( + builder_index_with_no_columns, + Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["d", "e", "f"]]).index().build(), + "+---+---+---+---+" + "| | 0 | 1 | 2 |" + "+---+---+---+---+" + "| 0 | 1 | 2 | 3 |" + "+---+---+---+---+" + "| 1 | a | b | c |" + "+---+---+---+---+" + "| 2 | d | e | f |" + "+---+---+---+---+" +); + +test_table!( + builder_index_with_no_columns_and_name, + Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["d", "e", "f"]]) + .index() + .name(Some("Hello World".into())) + .build(), + "+-------------+---+---+---+" + "| | 0 | 1 | 2 |" + "+-------------+---+---+---+" + "| Hello World | | | |" + "+-------------+---+---+---+" + "| 0 | 1 | 2 | 3 |" + "+-------------+---+---+---+" + "| 1 | a | b | c |" + "+-------------+---+---+---+" + "| 2 | d | e | f |" + "+-------------+---+---+---+" +); + +test_table!( + builder_index_with_no_columns_transpose, + Builder::from_iter([["1", "2", "3"], ["a", "b", "c"], ["d", "e", "f"]]) + .index() + .transpose() + .build(), + "+---+---+---+---+" + "| | 0 | 1 | 2 |" + "+---+---+---+---+" + "| 0 | 1 | a | d |" + "+---+---+---+---+" + "| 1 | 2 | b | e |" + "+---+---+---+---+" + "| 2 | 3 | c | f |" + "+---+---+---+---+" +); + +test_table!(builder_index_empty, Builder::default().index().build(), ""); + +test_table!( + builder_index_transpose_empty, + Builder::default().index().transpose().build(), + "" +); + +test_table!( + builder_index_invalid_dosnt_panic, + Builder::default().index().column(100).build(), + "" +); + +test_table!( + builder_index_name_doesnt_shown_when_empty, + Builder::default() + .index() + .name(Some("Hello World".into())) + .build(), + "" +); + +#[test] +fn builder_index_transpose_transpose() { + let data = Matrix::list::<4, 2>(); + let builder = Table::builder(data).index(); + + let orig_table = builder.clone().build().to_string(); + let two_times_transposed_table = builder.transpose().transpose().build().to_string(); + + assert_eq!(orig_table, two_times_transposed_table,); +} + +#[test] +fn builder_index_no_name_transpose_transpose() { + let data = Matrix::list::<4, 2>(); + let builder = Table::builder(data).index().name(None); + + let orig_table = builder.clone().build().to_string(); + let two_times_transposed_table = builder.transpose().transpose().build().to_string(); + + assert_eq!(orig_table, two_times_transposed_table,); +} diff --git a/vendor/tabled/tests/core/iter_table.rs b/vendor/tabled/tests/core/iter_table.rs new file mode 100644 index 000000000..dd38ec71c --- /dev/null +++ b/vendor/tabled/tests/core/iter_table.rs @@ -0,0 +1,214 @@ +#![cfg(feature = "std")] + +use tabled::tables::IterTable; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + iter_table, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()), + "+-----+-----+-----+" + "| 0-0 | 0-1 | 0-2 |" + "+-----+-----+-----+" + "| 1-0 | 1-1 | 1-2 |" + "+-----+-----+-----+" + "| 2-0 | 2-1 | 2-2 |" + "+-----+-----+-----+" +); + +test_table!( + iter_table_cols, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3), + "+-----+-----+-----+" + "| 0-0 | 0-1 | 0-2 |" + "+-----+-----+-----+" + "| 1-0 | 1-1 | 1-2 |" + "+-----+-----+-----+" + "| 2-0 | 2-1 | 2-2 |" + "+-----+-----+-----+" +); + +test_table!( + iter_table_cols_less, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(2), + "+-----+-----+" + "| 0-0 | 0-1 |" + "+-----+-----+" + "| 1-0 | 1-1 |" + "+-----+-----+" + "| 2-0 | 2-1 |" + "+-----+-----+" +); + +test_table!( + iter_table_cols_zero, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(0), + "" +); + +test_table!( + iter_table_iterator, + { + let mut buf = String::new(); + IterTable::new((0..3).map(|i: usize| (0..5).map(move |j: usize| format!("{i},{j}")))).fmt(&mut buf).unwrap(); + buf + }, + "+-----+-----+-----+-----+-----+" + "| 0,0 | 0,1 | 0,2 | 0,3 | 0,4 |" + "+-----+-----+-----+-----+-----+" + "| 1,0 | 1,1 | 1,2 | 1,3 | 1,4 |" + "+-----+-----+-----+-----+-----+" + "| 2,0 | 2,1 | 2,2 | 2,3 | 2,4 |" + "+-----+-----+-----+-----+-----+" +); + +test_table!( + iter_table_width, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).width(2), + "+----+----+----+" + "| 0- | 0- | 0- |" + "+----+----+----+" + "| 1- | 1- | 1- |" + "+----+----+----+" + "| 2- | 2- | 2- |" + "+----+----+----+" +); + +test_table!( + iter_table_height_does_not_work, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).height(5), + "+-----+-----+-----+" + "| 0-0 | 0-1 | 0-2 |" + "| | | |" + "| | | |" + "| | | |" + "| | | |" + "+-----+-----+-----+" + "| 1-0 | 1-1 | 1-2 |" + "| | | |" + "| | | |" + "| | | |" + "| | | |" + "+-----+-----+-----+" + "| 2-0 | 2-1 | 2-2 |" + "| | | |" + "| | | |" + "| | | |" + "| | | |" + "+-----+-----+-----+" +); + +test_table!( + iter_table_sniff_0, + IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).sniff(0), + "" +); + +test_table!( + iter_table_multiline, + IterTable::new( + vec![ + vec!["0", "1", "2", "3"], + vec!["0\n1\n2\n3\n4", "0\n1\n2\n\n\n3\n4", "0\n1\n2\n3\n4\n\n\n", "0\n1\n2\n\n\n3\n4\n"] + ] + ), + "+---+---+---+---+" + "| 0 | 1 | 2 | 3 |" + "+---+---+---+---+" + "| 0 | 0 | 0 | 0 |" + "| 1 | 1 | 1 | 1 |" + "| 2 | 2 | 2 | 2 |" + "| 3 | | 3 | |" + "| 4 | | 4 | |" + "| | 3 | | 3 |" + "| | 4 | | 4 |" + "| | | | |" + "+---+---+---+---+" +); + +test_table!( + iter_table_multiline_sniff_1, + IterTable::new( + vec![ + vec!["0", "1", "2", "3"], + vec!["0\n1\n2\n3\n4", "0\n1\n2\n\n\n3\n4", "0\n1\n2\n3\n4\n\n\n", "0\n1\n2\n\n\n3\n4\n"] + ] + ) + .sniff(1), + "+---+---+---+---+\n| 0 | 1 | 2 | 3 |\n+---+---+---+---+\n| 0\n1\n2\n3\n4 | 0\n1\n2\n\n\n3\n4 | 0\n1\n2\n3\n4\n\n\n | 0\n1\n2\n\n\n3\n4\n |\n+---+---+---+---+" +); + +test_table!( + iter_table_multiline_sniff_2, + IterTable::new( + vec![ + vec!["0", "1", "2", "3"], + vec!["0\n1\n2\n3\n4", "0\n1\n2\n\n\n3\n4", "0\n1\n2\n3\n4\n\n\n", "0\n1\n2\n\n\n3\n4\n"], + vec!["0\n1\n2\n3\n4", "0\n1\n2\n\n\n3\n4", "0\n1\n2\n3\n4\n\n\n", "0\n1\n2\n\n\n3\n4\n"], + ] + ) + .sniff(2), + "+---+---+---+---+\n| 0 | 1 | 2 | 3 |\n+---+---+---+---+\n| 0 | 0 | 0 | 0 |\n| 1 | 1 | 1 | 1 |\n| 2 | 2 | 2 | 2 |\n| 3 | | 3 | |\n| 4 | | 4 | |\n| | 3 | | 3 |\n| | 4 | | 4 |\n| | | | |\n+---+---+---+---+\n| 0\n1\n2\n3\n4 | 0\n1\n2\n\n\n3\n4 | 0\n1\n2\n3\n4\n\n\n | 0\n1\n2\n\n\n3\n4\n |\n+---+---+---+---+" +); + +test_table!( + iter_table_multiline_height_work, + IterTable::new( + vec![ + vec!["0", "1", "2", "3"], + vec!["0\n1\n2\n3\n4", "0\n1\n2\n\n\n3\n4", "0\n1\n2\n3\n4\n\n\n", "0\n1\n2\n\n\n3\n4\n"] + ] + ) + .height(3) + , + "+---+---+---+---+" + "| 0 | 1 | 2 | 3 |" + "| | | | |" + "| | | | |" + "+---+---+---+---+" + "| 0 | 0 | 0 | 0 |" + "| 1 | 1 | 1 | 1 |" + "| 2 | 2 | 2 | 2 |" + "+---+---+---+---+" +); + +test_table!( + iter_table_sniff_cut, + IterTable::new( + vec![ + vec!["12", "12", "22", "32"], + vec!["0", "0", "0", "0"], + vec!["023", "123", "223", "323"], + ] + ) + .sniff(2) + , + "+----+----+----+----+" + "| 12 | 12 | 22 | 32 |" + "+----+----+----+----+" + "| 0 | 0 | 0 | 0 |" + "+----+----+----+----+" + "| 02 | 12 | 22 | 32 |" + "+----+----+----+----+" +); + +test_table!( + iter_table_sniff, + IterTable::new( + vec![ + vec!["023", "123", "223", "323"], + vec!["12", "12", "22", "32"], + vec!["0", "0", "0", "0"], + ] + ) + .sniff(2) + , + "+-----+-----+-----+-----+" + "| 023 | 123 | 223 | 323 |" + "+-----+-----+-----+-----+" + "| 12 | 12 | 22 | 32 |" + "+-----+-----+-----+-----+" + "| 0 | 0 | 0 | 0 |" + "+-----+-----+-----+-----+" +); diff --git a/vendor/tabled/tests/core/mod.rs b/vendor/tabled/tests/core/mod.rs new file mode 100644 index 000000000..14a5404b7 --- /dev/null +++ b/vendor/tabled/tests/core/mod.rs @@ -0,0 +1,7 @@ +mod builder_test; +mod compact_table; +mod extended_table_test; +mod index_test; +mod iter_table; +mod pool_table; +mod table_test; diff --git a/vendor/tabled/tests/core/pool_table.rs b/vendor/tabled/tests/core/pool_table.rs new file mode 100644 index 000000000..ee28dae78 --- /dev/null +++ b/vendor/tabled/tests/core/pool_table.rs @@ -0,0 +1,634 @@ +#![cfg(feature = "std")] + +use tabled::{ + grid::dimension::{DimensionPriority, PoolTableDimension}, + settings::{formatting::AlignmentStrategy, Alignment, Margin, Padding, Style}, + tables::{PoolTable, TableValue}, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +#[cfg(feature = "color")] +use tabled::grid::color::StaticColor; + +test_table!( + pool_table, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()), + "+-----+-----+-----+" + "| 0-0 | 0-1 | 0-2 |" + "+-----+-----+-----+" + "| 1-0 | 1-1 | 1-2 |" + "+-----+-----+-----+" + "| 2-0 | 2-1 | 2-2 |" + "+-----+-----+-----+" +); + +test_table!( + pool_table_1, + PoolTable::new([vec!["111111", "222"], vec!["111", "2233", "1", "2", "3"]]), + "+-------------+----------+" + "| 111111 | 222 |" + "+-----+------++--+---+---+" + "| 111 | 2233 | 1 | 2 | 3 |" + "+-----+------+---+---+---+" +); + +test_table!( + pool_table_2, + PoolTable::new([vec!["111", "2233", "1", "2", "3"], vec!["111111", "222"]]), + "+-----+------+---+---+---+" + "| 111 | 2233 | 1 | 2 | 3 |" + "+-----+------++--+---+---+" + "| 111111 | 222 |" + "+-------------+----------+" +); + +test_table!( + pool_table_3, + PoolTable::new([vec!["1\n11", "2\n2\n3\n3", "1", "\n2\n", "3"], vec!["11\n111\n1", "2\n2\n2"]]), + "+----+---+---+---+---+" + "| 1 | 2 | 1 | | 3 |" + "| 11 | 2 | | 2 | |" + "| | 3 | | | |" + "| | 3 | | | |" + "+----+---+--++---+---+" + "| 11 | 2 |" + "| 111 | 2 |" + "| 1 | 2 |" + "+-----------+--------+" +); + +test_table!( + pool_table_4, + PoolTable::new([vec!["11\n111\n1", "2\n2\n2"], vec!["1\n11", "2\n2\n3\n3", "1", "\n2\n", "3"]]), + "+-----------+--------+" + "| 11 | 2 |" + "| 111 | 2 |" + "| 1 | 2 |" + "+----+---+--++---+---+" + "| 1 | 2 | 1 | | 3 |" + "| 11 | 2 | | 2 | |" + "| | 3 | | | |" + "| | 3 | | | |" + "+----+---+---+---+---+" +); + +test_table!( + pool_table_multiline, + PoolTable::new([ + ["1", "2\n2", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3", "2\n2"] + ]), + "+---+---+---+" + "| 1 | 2 | 3 |" + "| | 2 | 3 |" + "| | | 3 |" + "+---+---+---+" + "| 3 | 2 | 1 |" + "| 3 | 2 | |" + "| 3 | | |" + "+---+---+---+" + "| 1 | 3 | 2 |" + "| | 3 | 2 |" + "| | 3 | |" + "+---+---+---+" +); + +test_table!( + pool_table_value, + PoolTable::from(TableValue::Row(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("0-0")), TableValue::Cell(String::from("0-1")), TableValue::Cell(String::from("0-2"))]), + TableValue::Column(vec![TableValue::Cell(String::from("1-0")), TableValue::Cell(String::from("1-1")), TableValue::Cell(String::from("1-2"))]), + TableValue::Column(vec![TableValue::Cell(String::from("2-0")), TableValue::Cell(String::from("2-1")), TableValue::Cell(String::from("2-2"))]), + ])) + .with(Style::modern()), + "┌─────┬─────┬─────┐" + "│ 0-0 │ 1-0 │ 2-0 │" + "├─────┼─────┼─────┤" + "│ 0-1 │ 1-1 │ 2-1 │" + "├─────┼─────┼─────┤" + "│ 0-2 │ 1-2 │ 2-2 │" + "└─────┴─────┴─────┘" +); + +test_table!( + pool_table_value_1, + PoolTable::from(TableValue::Row(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("0-0")), TableValue::Cell(String::from("0-1")), TableValue::Cell(String::from("0-2"))]), + TableValue::Column(vec![TableValue::Cell(String::from("1-0")), TableValue::Cell(String::from("1-1")), TableValue::Cell(String::from("1-2"))]), + TableValue::Column(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("2-1")), + TableValue::Cell(String::from("2-2")), + ]), + ])) + .with(Style::modern()), + "┌─────┬─────┬──────┐" + "│ 0-0 │ 1-0 │ 2-01 │" + "│ │ ├──────┤" + "│ │ │ 2-02 │" + "├─────┼─────┼──────┤" + "│ 0-1 │ 1-1 │ 2-03 │" + "│ │ ├──────┤" + "├─────┼─────┤ 2-1 │" + "│ 0-2 │ 1-2 ├──────┤" + "│ │ │ 2-2 │" + "└─────┴─────┴──────┘" +); + +test_table!( + pool_table_value_2, + PoolTable::from(TableValue::Row(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("0-0")), TableValue::Cell(String::from("0-1")), TableValue::Cell(String::from("0-2"))]), + TableValue::Column(vec![ + TableValue::Row(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Column(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("1-1")), + TableValue::Cell(String::from("1-2")) + ]), + TableValue::Column(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("2-1")), + TableValue::Cell(String::from("2-2")) + ]), + ])) + .with(Style::modern()), + "┌─────┬──────┬──────┬──────┬──────┐" + "│ 0-0 │ 2-01 │ 2-02 │ 2-03 │ 2-01 │" + "│ ├──────┴──────┴──────┤ │" + "│ │ 2-01 ├──────┤" + "├─────┼────────────────────┤ 2-02 │" + "│ 0-1 │ 2-02 ├──────┤" + "│ ├────────────────────┤ 2-03 │" + "│ │ 2-03 ├──────┤" + "├─────┼────────────────────┤ 2-1 │" + "│ 0-2 │ 1-1 │ │" + "│ ├────────────────────┼──────┤" + "│ │ 1-2 │ 2-2 │" + "└─────┴────────────────────┴──────┘" +); + +test_table!( + pool_table_value_3, + PoolTable::from(TableValue::Row(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("0-0")), TableValue::Cell(String::from("0-1")), TableValue::Cell(String::from("0-2"))]), + TableValue::Column(vec![ + TableValue::Row(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Column(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("1-1")), + TableValue::Row(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("1-2")) + ]), + TableValue::Column(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("2-\n0\n1")), TableValue::Cell(String::from("2\n-\n0\n2")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("2-1")), + TableValue::Column(vec![TableValue::Cell(String::from("2-0\n1")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("2-2")) + ]), + ])) + .with(Style::modern()), + "┌─────┬──────┬──────┬──────┬──────┐" + "│ 0-0 │ 2-01 │ 2-02 │ 2-03 │ 2- │" + "│ │ │ │ │ 0 │" + "│ │ │ │ │ 1 │" + "│ ├──────┴──────┴──────┼──────┤" + "│ │ 2-01 │ 2 │" + "│ │ │ - │" + "│ ├────────────────────┤ 0 │" + "├─────┤ 2-02 │ 2 │" + "│ 0-1 │ ├──────┤" + "│ ├────────────────────┤ 2-03 │" + "│ │ 2-03 ├──────┤" + "│ ├────────────────────┤ 2-1 │" + "│ │ 1-1 ├──────┤" + "│ │ │ 2-0 │" + "├─────┤ │ 1 │" + "│ 0-2 ├──────┬──────┬──────┼──────┤" + "│ │ 2-01 │ 2-02 │ 2-03 │ 2-02 │" + "│ │ │ │ ├──────┤" + "│ ├──────┴──────┴──────┤ 2-03 │" + "│ │ 1-2 ├──────┤" + "│ │ │ 2-2 │" + "└─────┴────────────────────┴──────┘" +); + +test_table!( + pool_table_example, + { + let data = vec![ + vec!["Hello World", "Hello World", "Hello World"], + vec!["Hello", "", "Hello"], + vec!["W", "o", "r", "l", "d"], + ]; + + let data = TableValue::Column( + data.into_iter() + .map(|row| { + TableValue::Row( + row.into_iter() + .map(|text| TableValue::Cell(text.to_owned())) + .collect(), + ) + }) + .collect(), + ); + + PoolTable::from(data) + .with(Style::modern()) + .with(Alignment::center()) + .to_string() + }, + "┌─────────────┬─────────────┬─────────────┐" + "│ Hello World │ Hello World │ Hello World │" + "├─────────────┴─┬──────────┬┴─────────────┤" + "│ Hello │ │ Hello │" + "├────────┬──────┴─┬───────┬┴──────┬───────┤" + "│ W │ o │ r │ l │ d │" + "└────────┴────────┴───────┴───────┴───────┘" +); + +test_table!( + pool_table_value_empty_row, + PoolTable::from(TableValue::Row(vec![])) + .with(Style::modern()), + "┌──┐" + "│ │" + "└──┘" +); + +test_table!( + pool_table_value_empty_column, + PoolTable::from(TableValue::Column(vec![])) + .with(Style::modern()), + "┌──┐" + "│ │" + "└──┘" +); + +test_table!( + pool_table_value_empty_cell, + PoolTable::from(TableValue::Cell(String::from(""))) + .with(Style::modern()), + "┌──┐" + "│ │" + "└──┘" +); + +test_table!( + pool_table_padding, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Padding::new(1, 2, 3, 4)), + "+------+------+------+" + "| | | |" + "| | | |" + "| | | |" + "| 0-0 | 0-1 | 0-2 |" + "| | | |" + "| | | |" + "| | | |" + "| | | |" + "+------+------+------+" + "| | | |" + "| | | |" + "| | | |" + "| 1-0 | 1-1 | 1-2 |" + "| | | |" + "| | | |" + "| | | |" + "| | | |" + "+------+------+------+" + "| | | |" + "| | | |" + "| | | |" + "| 2-0 | 2-1 | 2-2 |" + "| | | |" + "| | | |" + "| | | |" + "| | | |" + "+------+------+------+" +); + +#[cfg(feature = "color")] +test_table!( + pool_table_padding_2, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()) + .with(Padding::new(1, 2, 3, 4) + .fill('!', '@', '#', '$') + .colorize( + StaticColor::new("\u{1b}[34m", "\u{1b}[39m"), + StaticColor::new("\u{1b}[34m", "\u{1b}[39m"), + StaticColor::new("\u{1b}[34m", "\u{1b}[39m"), + StaticColor::new("\u{1b}[34m", "\u{1b}[39m"), + ) + ), + "+------+------+------+" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m!\u{1b}[39m0-0\u{1b}[34m@@\u{1b}[39m|\u{1b}[34m!\u{1b}[39m0-1\u{1b}[34m@@\u{1b}[39m|\u{1b}[34m!\u{1b}[39m0-2\u{1b}[34m@@\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "+------+------+------+" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m!\u{1b}[39m1-0\u{1b}[34m@@\u{1b}[39m|\u{1b}[34m!\u{1b}[39m1-1\u{1b}[34m@@\u{1b}[39m|\u{1b}[34m!\u{1b}[39m1-2\u{1b}[34m@@\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "+------+------+------+" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|\u{1b}[34m######\u{1b}[39m|" + "|\u{1b}[34m!\u{1b}[39m2-0\u{1b}[34m@@\u{1b}[39m|\u{1b}[34m!\u{1b}[39m2-1\u{1b}[34m@@\u{1b}[39m|\u{1b}[34m!\u{1b}[39m2-2\u{1b}[34m@@\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|\u{1b}[34m$$$$$$\u{1b}[39m|" + "+------+------+------+" +); + +test_table!( + pool_table_margin, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Margin::new(1, 2, 3, 4).fill('!', '@', '#', '$')), + "!###################@@" + "!###################@@" + "!###################@@" + "!+-----+-----+-----+@@" + "!| 0-0 | 0-1 | 0-2 |@@" + "!+-----+-----+-----+@@" + "!| 1-0 | 1-1 | 1-2 |@@" + "!+-----+-----+-----+@@" + "!| 2-0 | 2-1 | 2-2 |@@" + "!+-----+-----+-----+@@" + "!$$$$$$$$$$$$$$$$$$$@@" + "!$$$$$$$$$$$$$$$$$$$@@" + "!$$$$$$$$$$$$$$$$$$$@@" + "!$$$$$$$$$$$$$$$$$$$@@" +); + +test_table!( + pool_table_alignment_bottom, + PoolTable::new([ + ["1", "2\n2", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3", "2\n2"] + ]) + .with(Alignment::bottom()), + "+---+---+---+" + "| | | 3 |" + "| | 2 | 3 |" + "| 1 | 2 | 3 |" + "+---+---+---+" + "| 3 | | |" + "| 3 | 2 | |" + "| 3 | 2 | 1 |" + "+---+---+---+" + "| | 3 | |" + "| | 3 | 2 |" + "| 1 | 3 | 2 |" + "+---+---+---+" +); + +test_table!( + pool_table_alignment_center_vertical, + PoolTable::new([ + ["1", "2\n2", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3", "2\n2"] + ]) + .with(Alignment::center_vertical()), + "+---+---+---+" + "| | 2 | 3 |" + "| 1 | 2 | 3 |" + "| | | 3 |" + "+---+---+---+" + "| 3 | 2 | |" + "| 3 | 2 | 1 |" + "| 3 | | |" + "+---+---+---+" + "| | 3 | 2 |" + "| 1 | 3 | 2 |" + "| | 3 | |" + "+---+---+---+" +); + +test_table!( + pool_table_alignment_right, + PoolTable::new([ + ["1 ", "2\n2 ", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3 ", "2\n2"] + ]) + .with(Alignment::right()), + "+------------------------+----------+---+" + "| 1 | 2 | 3 |" + "| | 2 | 3 |" + "| | | 3 |" + "+-------------+----------+-+--------+---+" + "| 3 | 2 | 1 |" + "| 3 | 2 | |" + "| 3 | | |" + "+------+------+------------+-----+------+" + "| 1 | 3 | 2 |" + "| | 3 | 2 |" + "| | 3 | |" + "+------+-------------------------+------+" +); + +test_table!( + pool_table_alignment_right_per_line, + PoolTable::new([ + ["1 ", "2\n2 ", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3 ", "2\n2"] + ]) + .with(Alignment::right()) + .with(AlignmentStrategy::PerLine), + "+------------------------+----------+---+" + "| 1 | 2 | 3 |" + "| | 2 | 3 |" + "| | | 3 |" + "+-------------+----------+-+--------+---+" + "| 3 | 2 | 1 |" + "| 3 | 2 | |" + "| 3 | | |" + "+------+------+------------+-----+------+" + "| 1 | 3 | 2 |" + "| | 3 | 2 |" + "| | 3 | |" + "+------+-------------------------+------+" +); + +test_table!( + pool_table_alignment_center_horizontal, + PoolTable::new([ + ["1 ", "2\n2 ", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3 ", "2\n2"] + ]) + .with(Alignment::center()), + "+------------------------+----------+---+" + "| 1 | 2 | 3 |" + "| | 2 | 3 |" + "| | | 3 |" + "+-------------+----------+-+--------+---+" + "| 3 | 2 | 1 |" + "| 3 | 2 | |" + "| 3 | | |" + "+------+------+------------+-----+------+" + "| 1 | 3 | 2 |" + "| | 3 | 2 |" + "| | 3 | |" + "+------+-------------------------+------+" +); + +test_table!( + pool_table_alignment_center_horizontal_line_strategy, + PoolTable::new([ + ["1 ", "2\n22222", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3 ", "2\n2"] + ]) + .with(Alignment::center()) + .with(AlignmentStrategy::PerLine), + "+------------------------+-------+---+" + "| 1 | 2 | 3 |" + "| | 22222 | 3 |" + "| | | 3 |" + "+------------+-----------+-------+---+" + "| 3 | 2 | 1 |" + "| 3 | 2 | |" + "| 3 | | |" + "+-----+------+-----------+-----+-----+" + "| 1 | 3 | 2 |" + "| | 3 | 2 |" + "| | 3 | |" + "+-----+------------------------+-----+" +); + +test_table!( + pool_table_style_empty, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Style::empty()), + " 0-0 0-1 0-2 " + " 1-0 1-1 1-2 " + " 2-0 2-1 2-2 " +); + +test_table!( + pool_table_style_markdown, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Style::markdown()), + "| 0-0 | 0-1 | 0-2 |" + "| 1-0 | 1-1 | 1-2 |" + "| 2-0 | 2-1 | 2-2 |" +); + +test_table!( + pool_table_style_rounded, + PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Style::rounded()), + "╭─────┬─────┬─────╮" + "│ 0-0 │ 0-1 │ 0-2 │" + " ───── ───── ───── " + "│ 1-0 │ 1-1 │ 1-2 │" + " ───── ───── ───── " + "│ 2-0 │ 2-1 │ 2-2 │" + "╰─────┴─────┴─────╯" +); + +test_table!( + pool_table_dim_ctrl_0, + PoolTable::from(TableValue::Row(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("0-0")), TableValue::Cell(String::from("0-1")), TableValue::Cell(String::from("0-2"))]), + TableValue::Column(vec![TableValue::Cell(String::from("1-0")), TableValue::Cell(String::from("1-1")), TableValue::Cell(String::from("1-2"))]), + TableValue::Column(vec![ + TableValue::Column(vec![TableValue::Cell(String::from("2-01")), TableValue::Cell(String::from("2-02")), TableValue::Cell(String::from("2-03"))]), + TableValue::Cell(String::from("2-1")), + TableValue::Cell(String::from("2-2")), + ]), + ])) + .with(PoolTableDimension::new(DimensionPriority::Last, DimensionPriority::Last)), + "+-----+-----+------+" + "| 0-0 | 1-0 | 2-01 |" + "+-----+-----+------+" + "| 0-1 | 1-1 | 2-02 |" + "+-----+-----+------+" + "| 0-2 | 1-2 | 2-03 |" + "| | +------+" + "| | | 2-1 |" + "| | +------+" + "| | | 2-2 |" + "+-----+-----+------+" +); + +test_table!( + pool_table_dim_ctrl_1, + PoolTable::new([ + ["1 ", "2\n2 ", "3\n3\n3"], + ["3\n3\n3", "2\n2", "1"], + ["1", "3\n3\n3 ", "2\n2"] + ]) + .with(PoolTableDimension::new(DimensionPriority::List, DimensionPriority::List)), + "+------------------------+----------+---+" + "| 1 | 2 | 3 |" + "| | 2 | 3 |" + "| | | 3 |" + "+-------------+----------+-+--------+---+" + "| 3 | 2 | 1 |" + "| 3 | 2 | |" + "| 3 | | |" + "+------+------+------------+-----+------+" + "| 1 | 3 | 2 |" + "| | 3 | 2 |" + "| | 3 | |" + "+------+-------------------------+------+" +); + +test_table!( + pool_table_2_columns_1_cell, + PoolTable::from(TableValue::Row(vec![ + TableValue::Column(vec![ + TableValue::Cell(String::from("0-0")), + TableValue::Cell(String::from("0-1")), + TableValue::Cell(String::from("0-2")), + TableValue::Cell(String::from("0-3")), + TableValue::Cell(String::from("0-4")), + TableValue::Cell(String::from("0-5")), + ]), + TableValue::Column(vec![ + TableValue::Cell(String::from("1-0")), + TableValue::Cell(String::from("1-1")), + TableValue::Cell(String::from("1-2")), + TableValue::Cell(String::from("1-3")), + TableValue::Cell(String::from("1-4")), + TableValue::Cell(String::from("1-6")), + TableValue::Cell(String::from("1-7")), + TableValue::Cell(String::from("1-8")), + TableValue::Cell(String::from("1-9")), + ]), + TableValue::Cell(String::from("2-0")), + ])) + .with(PoolTableDimension::new(DimensionPriority::Last, DimensionPriority::Last)), + "+-----+-----+-----+" + "| 0-0 | 1-0 | 2-0 |" + "+-----+-----+ |" + "| 0-1 | 1-1 | |" + "+-----+-----+ |" + "| 0-2 | 1-2 | |" + "+-----+-----+ |" + "| 0-3 | 1-3 | |" + "+-----+-----+ |" + "| 0-4 | 1-4 | |" + "+-----+-----+ |" + "| 0-5 | 1-6 | |" + "| +-----+ |" + "| | 1-7 | |" + "| +-----+ |" + "| | 1-8 | |" + "| +-----+ |" + "| | 1-9 | |" + "+-----+-----+-----+" +); diff --git a/vendor/tabled/tests/core/table_test.rs b/vendor/tabled/tests/core/table_test.rs new file mode 100644 index 000000000..b949626bb --- /dev/null +++ b/vendor/tabled/tests/core/table_test.rs @@ -0,0 +1,847 @@ +#![cfg(feature = "std")] + +use std::iter::FromIterator; + +use tabled::{ + builder::Builder, + settings::{formatting::Charset, Height, Modify, Padding, Settings, Style, Width}, + Table, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +mod default_types { + use super::*; + + test_table!( + table_str_vec, + Table::new(vec!["hello", "world"]), + "+-------+" + "| &str |" + "+-------+" + "| hello |" + "+-------+" + "| world |" + "+-------+" + ); + + test_table!( + table_char_vec, + Table::new(vec!['a', 'b', 'c']), + "+------+" + "| char |" + "+------+" + "| a |" + "+------+" + "| b |" + "+------+" + "| c |" + "+------+" + ); + + test_table!( + table_bool_vec, + Table::new(vec![true, false, true]), + "+-------+" + "| bool |" + "+-------+" + "| true |" + "+-------+" + "| false |" + "+-------+" + "| true |" + "+-------+" + ); + + test_table!( + table_usize_vec, + Table::new(vec![0usize, 1usize, 2usize]), + "+-------+" + "| usize |" + "+-------+" + "| 0 |" + "+-------+" + "| 1 |" + "+-------+" + "| 2 |" + "+-------+" + ); + + test_table!( + table_isize_vec, + Table::new(vec![0isize, 1isize, 2isize]), + "+-------+" + "| isize |" + "+-------+" + "| 0 |" + "+-------+" + "| 1 |" + "+-------+" + "| 2 |" + "+-------+" + ); + + test_table!( + table_u8_vec, + Table::new(vec![0u8, 1u8, 2u8]), + "+----+" + "| u8 |" + "+----+" + "| 0 |" + "+----+" + "| 1 |" + "+----+" + "| 2 |" + "+----+" + ); + + test_table!( + table_u16_vec, + Table::new(vec![0u16, 1u16, 2u16]), + "+-----+" + "| u16 |" + "+-----+" + "| 0 |" + "+-----+" + "| 1 |" + "+-----+" + "| 2 |" + "+-----+" + ); + + test_table!( + table_u32_vec, + Table::new(vec![0u32, 1u32, 2u32]), + "+-----+" + "| u32 |" + "+-----+" + "| 0 |" + "+-----+" + "| 1 |" + "+-----+" + "| 2 |" + "+-----+" + ); + + test_table!( + table_u64_vec, + Table::new(vec![0u64, 1u64, 2u64]), + "+-----+" + "| u64 |" + "+-----+" + "| 0 |" + "+-----+" + "| 1 |" + "+-----+" + "| 2 |" + "+-----+" + ); + + test_table!( + table_u128_vec, + Table::new(vec![0u128, 1u128, 2u128]), + "+------+" + "| u128 |" + "+------+" + "| 0 |" + "+------+" + "| 1 |" + "+------+" + "| 2 |" + "+------+" + ); + + test_table!( + table_i8_vec, + Table::new(vec![0i8, 1i8, 2i8]), + "+----+" + "| i8 |" + "+----+" + "| 0 |" + "+----+" + "| 1 |" + "+----+" + "| 2 |" + "+----+" + ); + + test_table!( + table_i16_vec, + Table::new(vec![0i16, 1, 2]), + "+-----+" + "| i16 |" + "+-----+" + "| 0 |" + "+-----+" + "| 1 |" + "+-----+" + "| 2 |" + "+-----+" + ); + + test_table!( + table_i32_vec, + Table::new(vec![0i32, 1, 2]), + "+-----+" + "| i32 |" + "+-----+" + "| 0 |" + "+-----+" + "| 1 |" + "+-----+" + "| 2 |" + "+-----+" + ); + + test_table!( + table_i64_vec, + Table::new(vec![0i64, 1, 2]), + "+-----+" + "| i64 |" + "+-----+" + "| 0 |" + "+-----+" + "| 1 |" + "+-----+" + "| 2 |" + "+-----+" + ); + + test_table!( + table_i128_vec, + Table::new(vec![0i128, 1, 2]), + "+------+" + "| i128 |" + "+------+" + "| 0 |" + "+------+" + "| 1 |" + "+------+" + "| 2 |" + "+------+" + ); + + test_table!( + table_array, + Table::new(vec![[0, 1, 2], [3, 4, 5], [6, 7, 8]]), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 3 | 4 | 5 |" + "+---+---+---+" + "| 6 | 7 | 8 |" + "+---+---+---+" + ); +} + +test_table!( + table_tuple, + Table::new(vec![("we are in", 2020)]), + "+-----------+------+" + "| &str | i32 |" + "+-----------+------+" + "| we are in | 2020 |" + "+-----------+------+" +); + +test_table!( + table_single_tuple, + Table::new(vec![(2020,)]), + "+------+" + "| i32 |" + "+------+" + "| 2020 |" + "+------+" +); + +test_table!( + table_tuple_vec, + #[allow(clippy::needless_borrow)] + Table::new(&[(0, "Monday"), (1, "Thursday")]), + "+-----+----------+" + "| i32 | &str |" + "+-----+----------+" + "| 0 | Monday |" + "+-----+----------+" + "| 1 | Thursday |" + "+-----+----------+" +); + +test_table!( + build_table_from_iterator, + Matrix::new(3, 3).with(Style::psql()), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + multiline_table_test_0, + Table::new([["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]]) + .with(Charset::clean()) + .with(Style::modern()), + r#"┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐"# + r#"│ 0 │"# + r#"├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤"# + r#"│ This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html │"# + r#"│ │"# + r#"│ For convenience, we are providing full builds for Windows, Linux, and macOS. These are the "all extra features" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies │"# + r#"│ │"# + r#"└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘"# +); + +test_table!( + multiline_table_test_1, + Table::new([["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]]) + .with(Charset::clean()) + .with(Style::modern()) + .with(Width::wrap(100)), + "┌──────────────────────────────────────────────────────────────────────────────────────────────────┐" + "│ 0 │" + "├──────────────────────────────────────────────────────────────────────────────────────────────────┤" + "│ This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: http │" + "│ s://www.nushell.sh/blog/2020/09/01/nushell_0_19.html │" + "│ │" + "│ For convenience, we are providing full build │" + "│ s for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have │" + "│ the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/inst │" + "│ allation.md#dependencies │" + "│ │" + "└──────────────────────────────────────────────────────────────────────────────────────────────────┘" +); + +test_table!( + multiline_table_test_2, + Table::new([["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]]) + .with(Modify::new((1, 0)).with(Charset::clean())) + .with(Style::modern()) + .with(Width::wrap(100)), + "┌──────────────────────────────────────────────────────────────────────────────────────────────────┐" + "│ 0 │" + "├──────────────────────────────────────────────────────────────────────────────────────────────────┤" + "│ This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: http │" + "│ s://www.nushell.sh/blog/2020/09/01/nushell_0_19.html │" + "│ │" + "│ For convenience, we are providing full build │" + "│ s for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have │" + "│ the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/inst │" + "│ allation.md#dependencies │" + "│ │" + "└──────────────────────────────────────────────────────────────────────────────────────────────────┘" +); + +#[cfg(feature = "derive")] +mod derived { + use super::*; + + use std::collections::{BTreeMap, BTreeSet}; + + use tabled::Tabled; + + #[derive(Tabled)] + struct TestType { + f1: u8, + f2: &'static str, + } + + test_table!( + table_vector_structures, + Table::new([TestType { f1: 0, f2: "0" }, TestType { f1: 1, f2: "1" }]), + "+----+----+" + "| f1 | f2 |" + "+----+----+" + "| 0 | 0 |" + "+----+----+" + "| 1 | 1 |" + "+----+----+" + ); + + test_table!( + table_empty_vector_structures, + Table::new({let v: Vec<TestType> = Vec::new(); v}), + "+----+----+" + "| f1 | f2 |" + "+----+----+" + ); + + test_table!( + table_option, + Table::new(Some(TestType { f1: 0, f2: "0" })), + "+----+----+" + "| f1 | f2 |" + "+----+----+" + "| 0 | 0 |" + "+----+----+" + ); + + test_table!( + table_option_none, + Table::new(Option::<TestType>::None), + "+----+----+" + "| f1 | f2 |" + "+----+----+" + ); + + test_table!( + table_tuple_with_structure_vec, + Table::new([(0, TestType { f1: 0, f2: "0str" }), (1, TestType { f1: 1, f2: "1str" })]), + "+-----+----+------+" + "| i32 | f1 | f2 |" + "+-----+----+------+" + "| 0 | 0 | 0str |" + "+-----+----+------+" + "| 1 | 1 | 1str |" + "+-----+----+------+" + ); + + test_table!( + table_vector_structures_with_hidden_tabled, + Table::new({ + #[derive(Tabled)] + struct St { + #[allow(dead_code)] + #[tabled(skip)] + f1: u8, + f2: &'static str, + } + + vec![St { f1: 0, f2: "0" }, St { f1: 1, f2: "1" }] + }), + "+----+" + "| f2 |" + "+----+" + "| 0 |" + "+----+" + "| 1 |" + "+----+" + ); + + test_table!( + table_enum, + Table::new({ + #[derive(Tabled)] + enum Letters { + Vowels { character: char, lang: u8 }, + Consonant(char), + Digit, + } + + vec![ + Letters::Vowels { + character: 'a', + lang: 0, + }, + Letters::Consonant('w'), + Letters::Vowels { + character: 'b', + lang: 1, + }, + Letters::Vowels { + character: 'c', + lang: 2, + }, + Letters::Digit, + ] + }), + "+--------+-----------+-------+" + "| Vowels | Consonant | Digit |" + "+--------+-----------+-------+" + "| + | | |" + "+--------+-----------+-------+" + "| | + | |" + "+--------+-----------+-------+" + "| + | | |" + "+--------+-----------+-------+" + "| + | | |" + "+--------+-----------+-------+" + "| | | + |" + "+--------+-----------+-------+" + ); + + test_table!( + table_enum_with_hidden_variant, + Table::new({ + #[allow(dead_code)] + #[derive(Tabled)] + enum Letters { + Vowels { + character: char, + lang: u8, + }, + Consonant(char), + #[tabled(skip)] + Digit, + } + + vec![ + Letters::Vowels { + character: 'a', + lang: 0, + }, + Letters::Consonant('w'), + Letters::Vowels { + character: 'b', + lang: 1, + }, + Letters::Vowels { + character: 'c', + lang: 2, + }, + Letters::Digit, + ] + }), + "+--------+-----------+" + "| Vowels | Consonant |" + "+--------+-----------+" + "| + | |" + "+--------+-----------+" + "| | + |" + "+--------+-----------+" + "| + | |" + "+--------+-----------+" + "| + | |" + "+--------+-----------+" + "| | |" + "+--------+-----------+" + ); + + test_table!( + table_btreemap, + Table::new({ + #[derive(Tabled)] + struct A { + b: u8, + c: &'static str, + } + + let mut map = BTreeMap::new(); + map.insert(0, A { b: 1, c: "s1" }); + map.insert(1, A { b: 2, c: "s2" }); + map.insert(3, A { b: 3, c: "s3" }); + + map + }), + "+-----+---+----+" + "| i32 | b | c |" + "+-----+---+----+" + "| 0 | 1 | s1 |" + "+-----+---+----+" + "| 1 | 2 | s2 |" + "+-----+---+----+" + "| 3 | 3 | s3 |" + "+-----+---+----+" + ); + + test_table!( + table_emojie_utf8_style, + Table::new({ + #[derive(Tabled)] + struct Language { + name: &'static str, + designed_by: &'static str, + invented_year: usize, + } + + vec![ + Language { + name: "C 💕", + designed_by: "Dennis Ritchie", + invented_year: 1972, + }, + Language { + name: "Rust 👍", + designed_by: "Graydon Hoare", + invented_year: 2010, + }, + Language { + name: "Go 🧋", + designed_by: "Rob Pike", + invented_year: 2009, + }, + ] + }).with(Style::modern().remove_horizontal()), + // Note: It doesn't look good in VS Code + "┌─────────┬────────────────┬───────────────┐" + "│ name │ designed_by │ invented_year │" + "│ C 💕 │ Dennis Ritchie │ 1972 │" + "│ Rust 👍 │ Graydon Hoare │ 2010 │" + "│ Go 🧋 │ Rob Pike │ 2009 │" + "└─────────┴────────────────┴───────────────┘" + ); + + test_table!( + table_btreeset, + Table::new({ + #[derive(Tabled, PartialEq, Eq, PartialOrd, Ord)] + struct A { + b: u8, + c: &'static str, + } + + let mut map = BTreeSet::new(); + map.insert(A { b: 1, c: "s1" }); + map.insert(A { b: 2, c: "s2" }); + map.insert(A { b: 3, c: "s3" }); + + map + }), + "+---+----+" + "| b | c |" + "+---+----+" + "| 1 | s1 |" + "+---+----+" + "| 2 | s2 |" + "+---+----+" + "| 3 | s3 |" + "+---+----+" + ); + + test_table!( + table_emojie, + Table::new({ + #[derive(Tabled)] + struct Language { + name: &'static str, + designed_by: &'static str, + invented_year: usize, + } + + vec![ + Language { + name: "C 💕", + designed_by: "Dennis Ritchie", + invented_year: 1972, + }, + Language { + name: "Rust 👍", + designed_by: "Graydon Hoare", + invented_year: 2010, + }, + Language { + name: "Go 🧋", + designed_by: "Rob Pike", + invented_year: 2009, + }, + ] + }), + "+---------+----------------+---------------+" + "| name | designed_by | invented_year |" + "+---------+----------------+---------------+" + "| C 💕 | Dennis Ritchie | 1972 |" + "+---------+----------------+---------------+" + "| Rust 👍 | Graydon Hoare | 2010 |" + "+---------+----------------+---------------+" + "| Go 🧋 | Rob Pike | 2009 |" + "+---------+----------------+---------------+" + ); + + test_table!( + tuple_combination, + Table::new({ + #[derive(Tabled)] + enum Domain { + Security, + Embedded, + Frontend, + Unknown, + } + + #[derive(Tabled)] + struct Developer(#[tabled(rename = "name")] &'static str); + + vec![ + (Developer("Terri Kshlerin"), Domain::Embedded), + (Developer("Catalina Dicki"), Domain::Security), + (Developer("Jennie Schmeler"), Domain::Frontend), + (Developer("Maxim Zhiburt"), Domain::Unknown), + ] + }), + "+-----------------+----------+----------+----------+---------+" + "| name | Security | Embedded | Frontend | Unknown |" + "+-----------------+----------+----------+----------+---------+" + "| Terri Kshlerin | | + | | |" + "+-----------------+----------+----------+----------+---------+" + "| Catalina Dicki | + | | | |" + "+-----------------+----------+----------+----------+---------+" + "| Jennie Schmeler | | | + | |" + "+-----------------+----------+----------+----------+---------+" + "| Maxim Zhiburt | | | | + |" + "+-----------------+----------+----------+----------+---------+" + + ); + + test_table!( + table_trait, + Table::new({ + #[derive(Tabled)] + enum Domain { + Security, + Embedded, + Frontend, + Unknown, + } + + #[derive(Tabled)] + struct Developer(#[tabled(rename = "name")] &'static str); + + vec![ + (Developer("Terri Kshlerin"), Domain::Embedded), + (Developer("Catalina Dicki"), Domain::Security), + (Developer("Jennie Schmeler"), Domain::Frontend), + (Developer("Maxim Zhiburt"), Domain::Unknown), + ] + }), + "+-----------------+----------+----------+----------+---------+" + "| name | Security | Embedded | Frontend | Unknown |" + "+-----------------+----------+----------+----------+---------+" + "| Terri Kshlerin | | + | | |" + "+-----------------+----------+----------+----------+---------+" + "| Catalina Dicki | + | | | |" + "+-----------------+----------+----------+----------+---------+" + "| Jennie Schmeler | | | + | |" + "+-----------------+----------+----------+----------+---------+" + "| Maxim Zhiburt | | | | + |" + "+-----------------+----------+----------+----------+---------+" + ); + + test_table!( + table_emojie_multiline, + Table::new({ + #[derive(Tabled)] + struct Article { + name: &'static str, + author: &'static str, + text: &'static str, + rating: usize, + } + + vec![ + Article { + name: "Rebase vs Merge commit in depth 👋", + author: "Rose Kuphal DVM", + text: "A multiline\n text with 🤯 😳 🥵 🥶\n a bunch of emojies ☄️ 💥 🔥 🌪", + rating: 43, + }, + Article { + name: "Keep it simple", + author: "Unknown", + text: "🍳", + rating: 100, + }, + ] + }), + "+------------------------------------+-----------------+-------------------------------+--------+" + "| name | author | text | rating |" + "+------------------------------------+-----------------+-------------------------------+--------+" + "| Rebase vs Merge commit in depth 👋 | Rose Kuphal DVM | A multiline | 43 |" + "| | | text with 🤯 😳 🥵 🥶 | |" + "| | | a bunch of emojies ☄\u{fe0f} 💥 🔥 🌪 | |" + "+------------------------------------+-----------------+-------------------------------+--------+" + "| Keep it simple | Unknown | 🍳 | 100 |" + "+------------------------------------+-----------------+-------------------------------+--------+" + ); +} + +#[cfg(feature = "color")] +#[test] +fn multiline_table_test2() { + use testing_table::assert_table; + + let data = &[ + ["\u{1b}[37mThis is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\n\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\n\u{1b}[0m"], + ]; + + let mut table = Table::new(data); + table.with(Style::modern()); + + assert_table!( + ansi_str::AnsiStr::ansi_strip(&table.to_string()), + r#"┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐"# + r#"│ 0 │"# + r#"├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤"# + r#"│ This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html │"# + r#"│ │"# + r#"│ For convenience, we are providing full builds for Windows, Linux, and macOS. These are the "all extra features" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies │"# + r#"│ │"# + r#"└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘"# + ); + + table.with(Width::wrap(100)); + + assert_table!( + ansi_str::AnsiStr::ansi_strip(&table.to_string()), + "┌──────────────────────────────────────────────────────────────────────────────────────────────────┐" + "│ 0 │" + "├──────────────────────────────────────────────────────────────────────────────────────────────────┤" + "│ This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: http │" + "│ s://www.nushell.sh/blog/2020/09/01/nushell_0_19.html │" + "│ │" + "│ For convenience, we are providing full build │" + "│ s for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have │" + "│ the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/inst │" + "│ allation.md#dependencies │" + "│ │" + "└──────────────────────────────────────────────────────────────────────────────────────────────────┘" + ); +} + +test_table!( + table_1x1_empty, + { + Builder::from_iter(vec![vec![""]]).build() + .with(Style::modern()) + .with(Settings::new(Height::limit(0), Width::increase(10))) + }, + "┌────────┐" + "└────────┘" +); + +test_table!( + table_2x2_empty, + { + Builder::from_iter(vec![vec![" ", ""], vec![" ", ""]]).build() + .with(Style::modern()) + .with(Padding::zero()) + .with(Height::list([1, 0])) + }, + "┌─┬┐" + "│ ││" + "├─┼┤" + "└─┴┘" +); + +test_table!( + table_2x2_empty_height_list_together_with_width_list_dont_work_0, + { + Builder::from_iter(vec![vec!["", ""], vec!["", ""]]).build() + .with(Style::modern()) + .with(Padding::zero()) + .with(Height::list([1, 0])) + .with(Width::list([1, 0])) + }, + "┌─┬┐" + "│ ││" + "├─┼┤" + "│ ││" + "└─┴┘" +); + +test_table!( + table_2x2_empty_height_list_together_with_width_list_dont_work_1, + { + Builder::from_iter(vec![vec!["", ""], vec!["", ""]]).build() + .with(Style::modern()) + .with(Padding::zero()) + .with(Width::list([1, 0])) + .with(Height::list([1, 0])) + }, + "┌┬┐" + "│││" + "├┼┤" + "└┴┘" +); diff --git a/vendor/tabled/tests/derive/derive_test.rs b/vendor/tabled/tests/derive/derive_test.rs new file mode 100644 index 000000000..d27e43eee --- /dev/null +++ b/vendor/tabled/tests/derive/derive_test.rs @@ -0,0 +1,1090 @@ +#![cfg(feature = "derive")] +#![cfg(feature = "std")] + +use tabled::Tabled; + +// https://users.rust-lang.org/t/create-a-struct-from-macro-rules/19829 +macro_rules! test_tuple { + ( + $test_name:ident, + t: $(#[$struct_attr:meta])* { $( $(#[$attr:meta])* $ty:ty)* }, + init: { $($init:expr)* }, + expected: $headers:expr, $fields:expr, + $(pre: { $($init_block:stmt)* })? + ) => { + #[test] + fn $test_name() { + $($($init_block)*)? + + #[derive(Tabled)] + struct TestType( + $( $(#[$attr])* $ty, )* + ); + + let value = TestType($($init,)*); + + let fields: Vec<&'static str> = $fields.to_vec(); + let headers: Vec<&'static str> = $headers.to_vec(); + + assert_eq!(value.fields(), fields); + assert_eq!(TestType::headers(), headers); + assert_eq!(<TestType as Tabled>::LENGTH, headers.len()); + assert_eq!(<TestType as Tabled>::LENGTH, fields.len()); + } + }; +} + +macro_rules! test_enum { + ( + $test_name:ident, + t: $(#[$struct_attr:meta])* { $( $(#[$var_attr:meta])* $var:ident $({ $( $(#[$attr:meta])* $field:ident: $ty:ty),* $(,)? })? $(( $( $(#[$attr2:meta])* $ty2:ty),* $(,)? ))? )* }, + $(pre: { $($init_block:stmt)* })? + headers: $headers:expr, + tests: $($init:expr => $expected:expr,)* + ) => { + #[allow(dead_code, unused_imports)] + #[test] + fn $test_name() { + $($($init_block)*)? + + #[derive(Tabled)] + $(#[$struct_attr])* + enum TestType { + $( + $(#[$var_attr])* + $var $({ + $( $(#[$attr])* $field: $ty,)* + })? + + $(( + $( $(#[$attr2])* $ty2,)* + ))? + ),* + } + + let headers: Vec<&'static str> = $headers.to_vec(); + assert_eq!(TestType::headers(), headers); + assert_eq!(<TestType as Tabled>::LENGTH, headers.len()); + + { + use TestType::*; + $( + let variant = $init; + let fields: Vec<&'static str> = $expected.to_vec(); + assert_eq!(variant.fields(), fields); + )* + } + } + }; +} + +macro_rules! test_struct { + ( + $test_name:ident, + t: $(#[$struct_attr:meta])* { $( $(#[$attr:meta])* $field:ident: $ty:ty),* $(,)?} + $(pre: { $($init_block:stmt)* })? + init: { $( $val_field:ident: $val:expr),* $(,)?} + expected: $headers:expr, $fields:expr $(,)? + ) => { + + #[allow(dead_code, unused_imports)] + #[test] + fn $test_name() { + $($($init_block)*)? + + #[derive(Tabled)] + $(#[$struct_attr])* + struct TestType { + $( + $(#[$attr])* + $field: $ty, + )* + } + + let value = TestType { + $($val_field: $val,)* + }; + + let fields: Vec<&'static str> = $fields.to_vec(); + let headers: Vec<&'static str> = $headers.to_vec(); + assert_eq!(TestType::headers(), headers); + assert_eq!(value.fields(), fields); + assert_eq!(<TestType as Tabled>::LENGTH, headers.len()); + assert_eq!(<TestType as Tabled>::LENGTH, fields.len()); + } + }; +} + +#[allow(non_camel_case_types)] +type sstr = &'static str; + +mod tuple { + use super::*; + + test_tuple!(basic, t: { u8 sstr }, init: { 0 "v2" }, expected: ["0", "1"], ["0", "v2"],); + test_tuple!(empty, t: { }, init: { }, expected: [], [],); + + test_tuple!(rename, t: { u8 #[tabled(rename = "field 2")] sstr }, init: { 0 "123" }, expected: ["0", "field 2"], ["0", "123"],); + + test_tuple!(skip_0, t: { #[tabled(skip)] u8 #[tabled(rename = "field 2", skip)] sstr sstr }, init: { 0 "v2" "123" }, expected: ["2"], ["123"],); + test_tuple!(skip_1, t: { #[tabled(skip)] u8 #[tabled(skip)] #[tabled(rename = "field 2")] sstr sstr }, init: { 0 "v2" "123" }, expected: ["2"], ["123"],); + + test_tuple!(order_0, t: { #[tabled(order = 0)] u8 u8 u8}, init: { 0 1 2 }, expected: ["0", "1", "2"], ["0", "1", "2"],); + test_tuple!(order_1, t: { #[tabled(order = 1)] u8 u8 u8}, init: { 0 1 2 }, expected: ["1", "0", "2"], ["1", "0", "2"],); + test_tuple!(order_2, t: { #[tabled(order = 2)] u8 u8 u8}, init: { 0 1 2 }, expected: ["1", "2", "0"], ["1", "2", "0"],); + test_tuple!(order_3, t: { u8 #[tabled(order = 0)] u8 u8}, init: { 0 1 2 }, expected: ["1", "0", "2"], ["1", "0", "2"],); + test_tuple!(order_4, t: { u8 #[tabled(order = 1)] u8 u8}, init: { 0 1 2 }, expected: ["0", "1", "2"], ["0", "1", "2"],); + test_tuple!(order_5, t: { u8 #[tabled(order = 2)] u8 u8}, init: { 0 1 2 }, expected: ["0", "2", "1"], ["0", "2", "1"],); + test_tuple!(order_6, t: { u8 u8 #[tabled(order = 0)] u8}, init: { 0 1 2 }, expected: ["2", "0", "1"], ["2", "0", "1"],); + test_tuple!(order_7, t: { u8 u8 #[tabled(order = 1)] u8}, init: { 0 1 2 }, expected: ["0", "2", "1"], ["0", "2", "1"],); + test_tuple!(order_8, t: { u8 u8 #[tabled(order = 2)] u8}, init: { 0 1 2 }, expected: ["0", "1", "2"], ["0", "1", "2"],); + test_tuple!(order_9, t: { #[tabled(order = 2)] u8 u8 #[tabled(order = 0)] u8}, init: { 0 1 2 }, expected: ["2", "1", "0"], ["2", "1", "0"],); + test_tuple!(order_10, t: { #[tabled(order = 2)] u8 #[tabled(order = 1)] u8 u8}, init: { 0 1 2 }, expected: ["2", "1", "0"], ["2", "1", "0"],); + test_tuple!(order_11, t: { #[tabled(order = 2)] u8 #[tabled(order = 2)] u8 #[tabled(order = 1)] u8}, init: { 0 1 2 }, expected: ["0", "2", "1"], ["0", "2", "1"],); + test_tuple!(order_12, t: { #[tabled(order = 2)] u8 #[tabled(order = 2)] u8 #[tabled(order = 2)] u8}, init: { 0 1 2 }, expected: ["0", "1", "2"], ["0", "1", "2"],); + test_tuple!(order_13, t: { #[tabled(order = 1)] u8 #[tabled(order = 1)] u8 #[tabled(order = 1)] u8}, init: { 0 1 2 }, expected: ["0", "2", "1"], ["0", "2", "1"],); + test_tuple!(order_14, t: { #[tabled(order = 2)] u8 #[tabled(order = 1)] u8 #[tabled(order = 0)] u8}, init: { 0 1 2 }, expected: ["2", "1", "0"], ["2", "1", "0"],); + + test_tuple!(rename_all, t: #[tabled(rename_all = "UPPERCASE")] { u8 sstr}, init: { 0 "123" }, expected: ["0", "1"], ["0", "123"],); + test_tuple!(rename_all_field, t: { u8 #[tabled(rename_all = "UPPERCASE")] sstr}, init: { 0 "123" }, expected: ["0", "1"], ["0", "123"],); + test_tuple!(rename_all_field_with_rename_0, t: { u8 #[tabled(rename_all = "UPPERCASE", rename = "Something")] sstr}, init: { 0 "123" }, expected: ["0", "Something"], ["0", "123"],); + test_tuple!(rename_all_field_with_rename_1, t: { u8 #[tabled(rename = "Something")] #[tabled(rename_all = "UPPERCASE")] sstr}, init: { 0 "123" }, expected: ["0", "Something"], ["0", "123"],); + + test_tuple!( + display_option, + t: { u8 #[tabled(display_with = "display_option")] Option<sstr> }, + init: { 0 Some("v2") }, + expected: ["0", "1"], ["0", "some v2"], + pre: { + fn display_option(o: &Option<sstr>) -> String { + match o { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + ); + + test_tuple!( + display_option_args, + t: { u8 #[tabled(display_with("display_option", 1, "234"))] Option<sstr> }, + init: { 0 Some("v2") }, + expected: ["0", "1"], ["0", "some 1 234"], + pre: { + fn display_option(val: usize, text: &str) -> String { + format!("some {val} {text}") + } + } + ); + + test_tuple!( + display_option_self, + t: { u8 #[tabled(display_with = "Self::display_option")] Option<sstr> }, + init: { 0 Some("v2") }, + expected: ["0", "1"], ["0", "some v2"], + pre: { + impl TestType { + fn display_option(o: &Option<sstr>) -> String { + match o { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + } + ); + + test_tuple!( + display_option_self_2, + t: { u8 #[tabled(display_with("Self::display_option", self))] Option<sstr> }, + init: { 0 Some("v2") }, + expected: ["0", "1"], ["0", "some v2"], + pre: { + impl TestType { + fn display_option(o: &TestType) -> String { + match o.1 { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + } + ); + + test_tuple!( + display_option_self_3, + t: { u8 #[tabled(display_with("display_option", self))] Option<sstr> }, + init: { 0 Some("v2") }, + expected: ["0", "1"], ["0", "some v2"], + pre: { + fn display_option(o: &TestType) -> String { + match o.1 { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + ); + + // #[test] + // fn order_compile_fail_when_order_is_bigger_then_count_fields() { + // #[derive(Tabled)] + // struct St(#[tabled(order = 3)] u8, u8, u8); + // } +} + +mod enum_ { + use super::*; + + test_enum!( + basic, + t: { Security Embedded Frontend Unknown }, + headers: ["Security", "Embedded", "Frontend", "Unknown"], + tests: + Security => ["+", "", "", ""], + Embedded => ["", "+", "", ""], + Frontend => ["", "", "+", ""], + Unknown => ["", "", "", "+"], + ); + + test_enum!( + diverse, + t: { A { a: u8, b: i32 } B(sstr) K }, + headers: ["A", "B", "K"], + tests: + A { a: 1, b: 2 } => ["+", "", ""], + B("") => ["", "+", ""], + K => ["", "", "+"], + ); + + test_enum!( + rename_variant, + t: { #[tabled(rename = "Variant 1")] A { a: u8, b: i32 } #[tabled(rename = "Variant 2")] B(sstr) K }, + headers: ["Variant 1", "Variant 2", "K"], + tests: + A { a: 1, b: 2 } => ["+", "", ""], + B("") => ["", "+", ""], + K => ["", "", "+"], + ); + + test_enum!( + skip_variant, + t: { A { a: u8, b: i32 } #[tabled(skip)] B(sstr) K }, + headers: ["A", "K"], + tests: + A { a: 1, b: 2 } => ["+", ""], + B("") => ["", ""], + K => ["", "+"], + ); + + test_enum!( + inline_variant, + t: { + #[tabled(inline("Auto::"))] Auto { #[tabled(rename = "mod")] model: sstr, engine: sstr } + #[tabled(inline)] Bikecycle( #[tabled(rename = "name")] sstr, #[tabled(inline)] Bike ) + Skateboard + }, + pre: { + #[derive(Tabled)] + struct Bike { brand: sstr, price: f32 } + } + headers: ["Auto::mod", "Auto::engine", "name", "brand", "price", "Skateboard"], + tests: + Skateboard => ["", "", "", "", "", "+"], + Auto { model: "Mini", engine: "v8" } => ["Mini", "v8", "", "", "", ""], + Bikecycle("A bike", Bike { brand: "Canyon", price: 2000.0 })=> ["", "", "A bike", "Canyon", "2000", ""], + ); + + test_enum!( + inline_field_with_display_function, + t: { + #[tabled(inline("backend::"))] + Backend { #[tabled(display_with = "display", rename = "name")] value: sstr } + Frontend + }, + pre: { + fn display(_: sstr) -> String { + "asd".to_string() + } + } + headers: ["backend::name", "Frontend"], + tests: + Backend { value: "123" } => ["asd", ""], + Frontend => ["", "+"], + ); + + test_enum!( + inline_field_with_display_self_function, + t: { + #[tabled(inline("backend::"))] + Backend { #[tabled()] #[tabled(display_with("display", self), rename = "name")] value: sstr } + Frontend + }, + pre: { + fn display<T>(_: &T) -> String { + "asd".to_string() + } + } + headers: ["backend::name", "Frontend"], + tests: + Backend { value: "123" } => ["asd", ""], + Frontend => ["", "+"], + ); + + test_enum!( + with_display, + t: { + #[tabled(inline)] + A(#[tabled(display_with = "format::<4>")] sstr) + B + }, + pre: { + fn format<const ID: usize>(_: sstr) -> String { + ID.to_string() + } + } + headers: ["0", "B"], + tests: + A("") => ["4", ""], + B => ["", "+"], + ); + + test_enum!( + with_display_self, + t: { + #[tabled(inline)] + A(#[tabled(display_with("Self::format::<4>", self))] sstr) + B + }, + pre: { + impl TestType { + fn format<const ID: usize>(&self) -> String { + ID.to_string() + } + } + } + headers: ["0", "B"], + tests: + A("") => ["4", ""], + B => ["", "+"], + ); + + test_enum!( + rename_all_variant, + t: { + #[tabled(rename_all = "snake_case")] + VariantName1 { a: u8, b: i32 } + #[tabled(rename_all = "UPPERCASE")] + VariantName2(String) + K + }, + headers: ["variant_name1", "VARIANTNAME2", "K"], + tests: + ); + + test_enum!( + rename_all_enum, + t: #[tabled(rename_all = "snake_case")] { + VariantName1 { a: u8, b: i32 } + VariantName2(String) + K + }, + headers: ["variant_name1", "variant_name2", "k"], + tests: + ); + + test_enum!( + rename_all_enum_inherited_inside_struct_enum, + t: #[tabled(rename_all = "snake_case")] { + #[tabled(inline)] + VariantName1 { some_field_1: u8, some_field_2: i32 } + VariantName2(String) + K + }, + headers: ["some_field_1", "some_field_2", "variant_name2", "k"], + tests: + ); + + test_enum!( + rename_all_enum_inherited_inside_struct_override_by_rename_enum, + t: #[tabled(rename_all = "snake_case")] { + #[tabled(inline)] + VariantName1 { + #[tabled(rename = "f1")] + some_field_1: u8, + #[tabled(rename = "f2")] + some_field_2: i32, + } + VariantName2(String) + K + }, + headers: ["f1", "f2", "variant_name2", "k"], + tests: + ); + + test_enum!( + rename_all_enum_inherited_inside_struct_override_by_rename_all_enum, + t: #[tabled(rename_all = "snake_case")] { + #[tabled(inline)] + VariantName1 { + #[tabled(rename_all = "UPPERCASE")] + some_field_1: u8, + #[tabled(rename_all = "CamelCase")] + some_field_2: i32, + } + VariantName2(String) + K + }, + headers: ["SOMEFIELD1", "someField2", "variant_name2", "k"], + tests: + ); + + test_enum!( + rename_all_variant_inherited_inside_struct_enum, + t: #[tabled(rename_all = "snake_case")] { + #[tabled(inline)] + #[tabled(rename_all = "snake_case")] + VariantName1 { + some_field_1: u8, + some_field_2: i32, + } + VariantName2(String) + K + }, + headers: ["some_field_1", "some_field_2", "variant_name2", "k"], + tests: + ); + + test_enum!( + rename_all_variant_inherited_inside_struct_enum_overridden_by_rename, + t: #[tabled(rename_all = "snake_case")] { + #[tabled(inline, rename_all = "snake_case")] + VariantName1 { + #[tabled(rename = "f1")] + some_field_1: u8, + #[tabled(rename = "f2")] + some_field_2: i32, + } + VariantName2(String) + K + }, + headers: ["f1", "f2", "variant_name2", "k"], + tests: + ); + + test_enum!( + rename_all_variant_inherited_inside_struct_override_by_rename_all_enum, + t: #[tabled(rename_all = "snake_case")] { + #[tabled(rename_all = "snake_case", inline)] + VariantName1 { + #[tabled(rename_all = "UPPERCASE")] + some_field_1: u8, + #[tabled(rename_all = "CamelCase")] + some_field_2: i32, + } + VariantName2(String) + K + }, + headers: ["SOMEFIELD1", "someField2", "variant_name2", "k"], + tests: + ); + + test_enum!( + inline_enum_as_whole, + t: #[tabled(inline)] { + AbsdEgh { a: u8, b: i32 } + B(String) + K + }, + headers: ["TestType"], + tests: + AbsdEgh { a: 0, b: 0 } => ["AbsdEgh"], + B(String::new()) => ["B"], + K => ["K"], + ); + + test_enum!( + inline_enum_as_whole_and_rename, + t: + #[tabled(inline, rename_all = "snake_case")] + { + AbsdEgh { a: u8, b: i32 } + B(String) + K + }, + headers: ["TestType"], + tests: + AbsdEgh { a: 0, b: 0 } => ["absd_egh"], + B(String::new()) => ["b"], + K => ["k"], + ); + + test_enum!( + inline_enum_as_whole_and_rename_inner, + t: #[tabled(inline)] { + #[tabled(rename_all = "snake_case")] + AbsdEgh { a: u8, b: i32 } + #[tabled(rename_all = "lowercase")] + B(String) + K + }, + headers: ["TestType"], + tests: + AbsdEgh { a: 0, b: 0 } => ["absd_egh"], + B(String::new()) => ["b"], + K => ["K"], + ); + + test_enum!( + inline_enum_name, + t: #[tabled(inline("A struct name"))] { + AbsdEgh { a: u8, b: i32 } + B(String) + K + }, + headers: ["A struct name"], + tests: + AbsdEgh { a: 0, b: 0 } => ["AbsdEgh"], + B(String::new()) => ["B"], + K => ["K"], + ); + + test_enum!( + enum_display_with_variant, + t: { + #[tabled(display_with = "display_variant1")] + AbsdEgh { a: u8, b: i32 } + #[tabled(display_with = "display_variant2::<200>")] + B(String) + #[tabled(display_with = "some::bar::display_variant1")] + K + }, + pre: { + fn display_variant1() -> &'static str { + "Hello World" + } + + fn display_variant2<const VAL: usize>() -> String { + format!("asd {VAL}") + } + + pub mod some { + pub mod bar { + pub fn display_variant1() -> &'static str { + "Hello World 123" + } + } + } + } + headers: ["AbsdEgh", "B", "K"], + tests: + AbsdEgh { a: 0, b: 0 } => ["Hello World", "", ""], + B(String::new()) => ["", "asd 200", ""], + K => ["", "", "Hello World 123"], + ); + + test_enum!( + enum_display_with_self_variant, + t: { + #[tabled(display_with("display_variant1", self))] + AbsdEgh { a: u8, b: i32 } + #[tabled(display_with("display_variant2::<200, _>", self))] + B(String) + #[tabled(display_with("some::bar::display_variant1", self))] + K + }, + pre: { + fn display_variant1<D>(_: &D) -> &'static str { + "Hello World" + } + + fn display_variant2<const VAL: usize, D>(_: &D) -> String { + format!("asd {VAL}") + } + + pub mod some { + pub mod bar { + pub fn display_variant1<D>(_: &D) -> &'static str { + "Hello World 123" + } + } + } + } + headers: ["AbsdEgh", "B", "K"], + tests: + AbsdEgh { a: 0, b: 0 } => ["Hello World", "", ""], + B(String::new()) => ["", "asd 200", ""], + K => ["", "", "Hello World 123"], + ); + + test_enum!( + enum_display_with_arguments, + t: { + #[tabled(display_with("display1", 1, 2, self))] + AbsdEgh { a: u8, b: i32 } + #[tabled(display_with("display2::<200>", "Hello World"))] + B(String) + #[tabled(display_with("display1", 100, 200, self))] + K + }, + pre: { + fn display1<D>(val: usize, val2: usize, _: &D) -> String { + format!("{val} {val2}") + } + + fn display2<const VAL: usize>(val: &str) -> String { + format!("asd {VAL} {val}") + } + } + headers: ["AbsdEgh", "B", "K"], + tests: + AbsdEgh { a: 0, b: 0 } => ["1 2", "", ""], + B(String::new()) => ["", "asd 200 Hello World", ""], + K => ["", "", "100 200"], + ); + + test_enum!(order_0, t: { #[tabled(order = 0)] V1(u8) V2(u8) V3(u8) }, headers: ["V1", "V2", "V3"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "+", ""], V3(0) => ["", "", "+"],); + test_enum!(order_1, t: { #[tabled(order = 1)] V1(u8) V2(u8) V3(u8) }, headers: ["V2", "V1", "V3"], tests: V1(0) => ["", "+", ""], V2(0) => ["+", "", ""], V3(0) => ["", "", "+"],); + test_enum!(order_2, t: { #[tabled(order = 2)] V1(u8) V2(u8) V3(u8) }, headers: ["V2", "V3", "V1"], tests: V1(0) => ["", "", "+"], V2(0) => ["+", "", ""], V3(0) => ["", "+", ""],); + test_enum!(order_3, t: { V1(u8) #[tabled(order = 0)] V2(u8) V3(u8) }, headers: ["V2", "V1", "V3"], tests: V1(0) => ["", "+", ""], V2(0) => ["+", "", ""], V3(0) => ["", "", "+"],); + test_enum!(order_4, t: { V1(u8) #[tabled(order = 1)] V2(u8) V3(u8) }, headers: ["V1", "V2", "V3"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "+", ""], V3(0) => ["", "", "+"],); + test_enum!(order_5, t: { V1(u8) #[tabled(order = 2)] V2(u8) V3(u8) }, headers: ["V1", "V3", "V2"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "", "+"], V3(0) => ["", "+", ""],); + test_enum!(order_6, t: { V1(u8) V2(u8) #[tabled(order = 0)] V3(u8) }, headers: ["V3", "V1", "V2"], tests: V1(0) => ["", "+", ""], V2(0) => ["", "", "+"], V3(0) => ["+", "", ""],); + test_enum!(order_7, t: { V1(u8) V2(u8) #[tabled(order = 1)] V3(u8) }, headers: ["V1", "V3", "V2"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "", "+"], V3(0) => ["", "+", ""],); + test_enum!(order_8, t: { V1(u8) V2(u8) #[tabled(order = 2)] V3(u8) }, headers: ["V1", "V2", "V3"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "+", ""], V3(0) => ["", "", "+"],); + test_enum!(order_9, t: { #[tabled(order = 2)] V1(u8) V2(u8) #[tabled(order = 0)] V3(u8) }, headers: ["V3", "V2", "V1"], tests: V1(0) => ["", "", "+"], V2(0) => ["", "+", ""], V3(0) => ["+", "", ""],); + test_enum!(order_10, t: { #[tabled(order = 2)] V1(u8) V2(u8) #[tabled(order = 1)] V3(u8) }, headers: ["V2", "V3", "V1"], tests: V1(0) => ["", "", "+"], V2(0) => ["+", "", ""], V3(0) => ["", "+", ""],); + test_enum!(order_11, t: { #[tabled(order = 2)] V1(u8) #[tabled(order = 2)] V2(u8) #[tabled(order = 1)] V3(u8) }, headers: ["V1", "V3", "V2"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "", "+"], V3(0) => ["", "+", ""],); + test_enum!(order_12, t: { #[tabled(order = 2)] V1(u8) #[tabled(order = 1)] V2(u8) #[tabled(order = 0)] V3(u8) }, headers: ["V3", "V2", "V1"], tests: V1(0) => ["", "", "+"], V2(0) => ["", "+", ""], V3(0) => ["+", "", ""],); + test_enum!(order_13, t: { #[tabled(order = 0)] V1(u8) #[tabled(order = 0)] V2(u8) #[tabled(order = 0)] V3(u8) }, headers: ["V3", "V1", "V2"], tests: V1(0) => ["", "+", ""], V2(0) => ["", "", "+"], V3(0) => ["+", "", ""],); + test_enum!(order_14, t: { #[tabled(order = 1)] V1(u8) #[tabled(order = 1)] V2(u8) #[tabled(order = 1)] V3(u8) }, headers: ["V1", "V3", "V2"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "", "+"], V3(0) => ["", "+", ""],); + test_enum!(order_15, t: { #[tabled(order = 2)] V1(u8) #[tabled(order = 2)] V2(u8) #[tabled(order = 2)] V3(u8) }, headers: ["V1", "V2", "V3"], tests: V1(0) => ["+", "", ""], V2(0) => ["", "+", ""], V3(0) => ["", "", "+"],); + + test_enum!(order_0_inlined, t: #[tabled(inline)] { #[tabled(order = 1)] V1(u8) V2(u8) V3(u8) }, headers: ["TestType"], tests: V1(0) => ["V1"], V2(0) => ["V2"], V3(0) => ["V3"],); +} + +mod unit { + use super::*; + + #[test] + fn basic() { + #[derive(Tabled)] + struct St; + let st = St; + + assert!(st.fields().is_empty()); + assert!(St::headers().is_empty()); + assert_eq!(St::LENGTH, 0); + } +} + +mod structure { + use super::*; + + test_struct!(empty, t: { } init: { } expected: [], []); + test_struct!(general, t: { f1: u8, f2: sstr } init: { f1: 0, f2: "v2" } expected: ["f1", "f2"], ["0", "v2"]); + test_struct!(rename, t: { #[tabled(rename = "field 1")] f1: u8, #[tabled(rename = "field 2")] f2: sstr } init: { f1: 0, f2: "v2" } expected: ["field 1", "field 2"], ["0", "v2"]); + test_struct!(skip, t: { #[tabled(skip)] f1: u8, #[tabled(rename = "field 2", skip)] f2: sstr, f3: sstr } init: { f1: 0, f2: "v2", f3: "123" } expected: ["f3"], ["123"]); + test_struct!(skip_true, t: { #[tabled(skip = true)] f1: u8, #[tabled(rename = "field 2", skip = true)] f2: sstr, f3: sstr } init: { f1: 0, f2: "v2", f3: "123" } expected: ["f3"], ["123"]); + test_struct!( + inline, + t: { + #[tabled(inline = true)] + id: u8, + name: sstr, + #[tabled(inline)] + ed: Education + } + pre: { + #[derive(Tabled)] + struct Education { uni: sstr, graduated: bool } + } + init: { id: 0, name: "Maxim", ed: Education { uni: "BNTU", graduated: true }} + expected: ["u8", "name","uni","graduated"], ["0", "Maxim", "BNTU", "true"] + ); + test_struct!( + inline_with_prefix, + t: { + #[tabled(rename = "it's an ignored option", inline)] + id: u8, + name: sstr, + #[tabled(inline("education::"))] + ed: Education, + } + pre: { + #[derive(Tabled)] + struct Education { uni: sstr, graduated: bool } + } + init: { id: 0, name: "Maxim", ed: Education { uni: "BNTU", graduated: true }} + expected: ["u8", "name","education::uni","education::graduated"], ["0", "Maxim", "BNTU", "true"] + ); + test_struct!( + display_with, + t: { + f1: u8, + #[tabled(display_with = "display_option")] + f2: Option<sstr>, + } + pre: { + fn display_option(o: &Option<sstr>) -> String { + match o { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + init: { f1: 0, f2: Some("v2") } + expected: ["f1", "f2"], ["0", "some v2"] + ); + test_struct!( + display_with_args, + t: { + f1: u8, + #[tabled(display_with("display_option", 1, 2, 3))] + f2: Option<sstr>, + } + pre: { + fn display_option(v1: usize, v2: usize, v3: usize) -> String { + format!("{v1} {v2} {v3}") + } + } + init: { f1: 0, f2: Some("v2") } + expected: ["f1", "f2"], ["0", "1 2 3"] + ); + test_struct!( + display_with_self_static_method, + t: { + f1: u8, + #[tabled(display_with = "Self::display_option")] + f2: Option<sstr>, + } + pre: { + impl TestType { + fn display_option(o: &Option<sstr>) -> String { + match o { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + } + init: { f1: 0, f2: Some("v2") } + expected: ["f1", "f2"], ["0", "some v2"] + ); + test_struct!( + display_with_self_static_method_2, + t: { + f1: u8, + #[tabled(display_with("Self::display_option", self))] + f2: Option<sstr>, + } + pre: { + impl TestType { + fn display_option(o: &TestType) -> String { + match o.f2 { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + } + init: { f1: 0, f2: Some("v2") } + expected: ["f1", "f2"], ["0", "some v2"] + ); + test_struct!( + display_with_self_2_self_static_method_2, + t: { + f1: u8, + #[tabled(display_with("Self::display_option", self))] + f2: Option<sstr>, + } + pre: { + impl TestType { + fn display_option(&self) -> String { + match self.f2 { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + } + init: { f1: 0, f2: Some("v2") } + expected: ["f1", "f2"], ["0", "some v2"] + ); + test_struct!( + display_with_self_2_self_static_method, + t: { + f1: u8, + #[tabled(display_with("display_option", self))] + f2: Option<sstr>, + } + pre: { + fn display_option(o: &TestType) -> String { + match o.f2 { + Some(s) => format!("some {s}"), + None => "none".to_string(), + } + } + } + init: { f1: 0, f2: Some("v2") } + expected: ["f1", "f2"], ["0", "some v2"] + ); + test_struct!(order_0, t: { #[tabled(order = 0)] f0: u8, f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f0", "f1", "f2"], ["0", "1", "2"]); + test_struct!(order_1, t: { #[tabled(order = 1)] f0: u8, f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f1", "f0", "f2"], ["1", "0", "2"]); + test_struct!(order_2, t: { #[tabled(order = 2)] f0: u8, f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f1", "f2", "f0"], ["1", "2", "0"]); + test_struct!(order_3, t: { f0: u8, #[tabled(order = 0)] f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f1", "f0", "f2"], ["1", "0", "2"]); + test_struct!(order_4, t: { f0: u8, #[tabled(order = 1)] f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f0", "f1", "f2"], ["0", "1", "2"]); + test_struct!(order_5, t: { f0: u8, #[tabled(order = 2)] f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f0", "f2", "f1"], ["0", "2", "1"]); + test_struct!(order_6, t: { f0: u8, f1: u8, #[tabled(order = 0)] f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f2", "f0", "f1"], ["2", "0", "1"]); + test_struct!(order_7, t: { f0: u8, f1: u8, #[tabled(order = 1)] f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f0", "f2", "f1"], ["0", "2", "1"]); + test_struct!(order_8, t: { f0: u8, f1: u8, #[tabled(order = 2)] f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f0", "f1", "f2"], ["0", "1", "2"]); + test_struct!(order_9, t: { #[tabled(order = 2)] f0: u8, f1: u8, #[tabled(order = 0)] f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f2", "f1", "f0"], ["2", "1", "0"]); + test_struct!(order_10, t: { #[tabled(order = 2)] f0: u8, #[tabled(order = 1)] f1: u8, f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f2", "f1", "f0"], ["2", "1", "0"]); + test_struct!(order_11, t: { #[tabled(order = 2)] f0: u8, #[tabled(order = 2)] f1: u8, #[tabled(order = 1)] f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f0", "f2", "f1"], ["0", "2", "1"]); + test_struct!(order_12, t: { #[tabled(order = 2)] f0: u8, #[tabled(order = 1)] f1: u8, #[tabled(order = 0)] f2: u8 } init: { f0: 0, f1: 1, f2: 2 } expected: ["f2", "f1", "f0"], ["2", "1", "0"]); + + test_struct!( + rename_all, + t: #[tabled(rename_all = "UPPERCASE")] { f1: u8, f2: sstr } + init: { f1: 0, f2: "v2" } + expected: ["F1", "F2"], ["0", "v2"] + ); + test_struct!( + rename_all_override_in_field_by_rename, + t: #[tabled(rename_all = "UPPERCASE")] { #[tabled(rename = "213213")] f1: u8, f2: sstr } + init: { f1: 0, f2: "v2" } + expected: ["213213", "F2"], ["0", "v2"] + ); + test_struct!( + rename_all_override_in_field_by_rename_all, + t: #[tabled(rename_all = "UPPERCASE")] { #[tabled(rename_all = "lowercase")] f1: u8, f2: sstr } + init: { f1: 0, f2: "v2" } + expected: ["f1", "F2"], ["0", "v2"] + ); + test_struct!( + rename_all_field, + t: { #[tabled(rename_all = "lowercase")] f1: u8, #[tabled(rename_all = "UPPERCASE")] f2: sstr } + init: { f1: 0, f2: "v2" } + expected: ["f1", "F2"], ["0", "v2"] + ); + test_struct!( + rename_all_field_overridden_by_rename, + t: { #[tabled(rename_all = "lowercase", rename = "Hello")] f1: u8, #[tabled(rename_all = "UPPERCASE")] f2: sstr } + init: { f1: 0, f2: "v2" } + expected: ["Hello", "F2"], ["0", "v2"] + ); + + // #[test] + // fn order_compile_fail_when_order_is_bigger_then_count_fields() { + // #[derive(Tabled)] + // struct St { + // #[tabled(order = 3)] + // f0: u8, + // f1: u8, + // f2: u8, + // } + // } +} + +test_tuple!(skipped_fields_not_implement_display_tuple, t: { #[tabled(skip)] () sstr }, init: { () "123" }, expected: ["1"], ["123"],); +test_struct!(skipped_fields_not_implement_display_struct, t: { #[tabled(skip)] _unit: (), s: sstr } init: { _unit: (), s: "123" } expected: ["s"], ["123"],); +test_struct!( + skipped_fields_not_implement_display_struct_in_inline, + t: { s: sstr, #[tabled(inline)] f: S1 } + pre: { + #[derive(Tabled)] + struct S1 { + #[tabled(skip)] + _unit: (), + s: sstr, + } + } + init: { s: "123", f: S1 { _unit: (), s: "..." } } + expected: ["s", "s"], ["123", "..."], +); +test_enum!( + skipped_fields_not_implement_display_enum, + t: { + #[tabled(inline("A::"))] + A { + name: sstr + } + #[tabled(inline("B::"))] + B { + issue: usize, + name: sstr, + #[tabled(skip)] + _gem: (), + } + #[tabled(inline("C::"))] + C(usize, #[tabled(skip)] (), sstr) + D + }, + headers: ["A::name", "B::issue", "B::name", "C::0", "C::2", "D"], + tests: + A { name: "nrdxp" } => ["nrdxp", "", "", "", "", ""], + B { _gem: (), issue: 32, name: "nrdxp" } => ["", "32", "nrdxp", "", "", ""], + C(32, (), "nrdxp") => ["", "", "", "32", "nrdxp", ""], + D => ["", "", "", "", "", "+"], +); + +test_struct!( + ignore_display_with_when_used_with_inline, + t: { f1: sstr, f2: sstr, #[tabled(display_with = "print", inline)] f3: usize } + init: { f1: "123", f2: "456", f3: 789 } + expected: ["f1", "f2", "usize"], ["123", "456", "789"], +); +test_struct!( + ignore_display_with_when_used_with_inline_2, + t: { f1: sstr, f2: sstr, #[tabled(display_with = "print", )] #[tabled(inline)] f3: usize } + init: { f1: "123", f2: "456", f3: 789 } + expected: ["f1", "f2", "usize"], ["123", "456", "789"], +); +test_struct!( + display_with_and_rename, + t: { f1: sstr, f2: sstr, #[tabled(display_with = "print", rename = "asd")] f3: usize } + pre: { #[allow(dead_code)] fn print<T>(_: T) -> String { String::new() } } + init: { f1: "123", f2: "456", f3: 789 } + expected: ["f1", "f2", "asd"], ["123", "456", ""], +); +test_struct!( + display_with_and_rename_2, + t: { f1: sstr, f2: sstr, #[tabled(display_with = "print")] #[tabled(rename = "asd")] f3: usize } + pre: { #[allow(dead_code)] fn print<T>(_: T) -> String { String::new() } } + init: { f1: "123", f2: "456", f3: 789 } + expected: ["f1", "f2", "asd"], ["123", "456", ""], +); +test_struct!( + display_with_and_rename_all, + t: { f1: sstr, f2: sstr, #[tabled(display_with = "print", rename_all = "UPPERCASE")] f3: usize } + pre: { #[allow(dead_code)] fn print<T>(_: T) -> String { String::new() } } + init: { f1: "123", f2: "456", f3: 789 } + expected: ["f1", "f2", "F3"], ["123", "456", ""], +); + +#[test] +fn rename_all_variants() { + macro_rules! test_case { + ( $name:ident, $case:expr ) => { + #[derive(Tabled)] + #[tabled(rename_all = $case)] + struct $name { + field: usize, + } + }; + } + + test_case!(S1, "UPPERCASE"); + test_case!(S2, "lowercase"); + test_case!(S3, "camelCase"); + test_case!(S4, "PascalCase"); + test_case!(S5, "snake_case"); + test_case!(S6, "SCREAMING_SNAKE_CASE"); + test_case!(S7, "kebab-case"); + test_case!(S8, "verbatimcase"); +} + +// #[test] +// fn wrong_rename_all_panic_when_used_as_not_first() { +// #[derive(Tabled)] +// #[tabled(rename_all = "UPPERCASE")] +// #[tabled(rename_all = "some wrong case")] +// struct Struct1 { +// field: usize, +// } + +// let st = Struct1 { field: 789 }; + +// assert_eq!(Struct1::headers(), vec!["FIELD"],); +// assert_eq!(st.fields(), vec!["789"]); + +// #[derive(Tabled)] +// #[tabled(rename_all = "UPPERCASE", rename_all = "some wrong case")] +// struct Struct2 { +// field: usize, +// } + +// let st = Struct2 { field: 789 }; + +// assert_eq!(Struct1::headers(), vec!["FIELD"],); +// assert_eq!(st.fields(), vec!["789"]); +// } + +#[test] +fn rename_all_gets_last_value() { + #[derive(Tabled)] + #[tabled(rename_all = "UPPERCASE")] + #[tabled(rename_all = "PascalCase")] + struct Struct1 { + field: usize, + } + + let st = Struct1 { field: 789 }; + + assert_eq!(Struct1::headers(), vec!["Field"],); + assert_eq!(st.fields(), vec!["789"]); + + #[derive(Tabled)] + #[tabled(rename_all = "UPPERCASE", rename_all = "PascalCase")] + struct Struct2 { + field: usize, + } + + let st = Struct2 { field: 789 }; + + assert_eq!(Struct1::headers(), vec!["Field"],); + assert_eq!(st.fields(), vec!["789"]); +} + +#[test] +fn test_order_skip_usage() { + #[derive(Tabled, Default)] + pub struct Example { + #[tabled(skip)] + #[allow(dead_code)] + id: usize, + name: String, + #[tabled(order = 0)] + details: String, + } + + #[derive(Tabled, Default)] + pub struct Example2 { + #[tabled(skip)] + #[allow(dead_code)] + id: usize, + name: String, + #[tabled(order = 1)] + details: String, + } + + assert_eq!(Example::headers(), vec!["details", "name"],); + assert_eq!(Example::default().fields(), vec!["", ""]); +} + +#[test] +fn test_skip_enum_0() { + #[allow(dead_code)] + #[derive(Tabled)] + enum Letters { + Vowels { + character: char, + lang: u8, + }, + Consonant(char), + #[tabled(skip)] + Digit, + } + + assert_eq!(Letters::headers(), vec!["Vowels", "Consonant"]); + assert_eq!(Letters::Consonant('c').fields(), vec!["", "+"]); + assert_eq!(Letters::Digit.fields(), vec!["", ""]); +} + +mod __ { + #[test] + fn dont_import_the_trait() { + #[derive(tabled::Tabled)] + struct __; + } +} diff --git a/vendor/tabled/tests/derive/mod.rs b/vendor/tabled/tests/derive/mod.rs new file mode 100644 index 000000000..d0d4b359f --- /dev/null +++ b/vendor/tabled/tests/derive/mod.rs @@ -0,0 +1 @@ +mod derive_test; diff --git a/vendor/tabled/tests/macros/col_row_test.rs b/vendor/tabled/tests/macros/col_row_test.rs new file mode 100644 index 000000000..e6d70edf0 --- /dev/null +++ b/vendor/tabled/tests/macros/col_row_test.rs @@ -0,0 +1,264 @@ +#![cfg(feature = "macros")] +#![cfg(feature = "std")] + +use tabled::{ + col, row, + settings::{format::Format, object::Segment, Alignment, Modify, Padding}, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + row_pair_test, + row!( + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + Matrix::new(4, 4) + .with(Modify::new(Segment::all()).with(Alignment::right())) + .with(Modify::new(Segment::all()).with(Padding::new(1, 1, 1, 1))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("({s})")))), + ), + "+--------------------------------------------------------+-------------------------------------------------------------+" + "| +-------+--------------+--------------+--------------+ | +-----+------------+------------+------------+------------+ |" + "| | [N] | [column 0] | [column 1] | [column 2] | | | | | | | | |" + "| +-------+--------------+--------------+--------------+ | | (N) | (column 0) | (column 1) | (column 2) | (column 3) | |" + "| | [0] | [0-0] | [0-1] | [0-2] | | | | | | | | |" + "| +-------+--------------+--------------+--------------+ | +-----+------------+------------+------------+------------+ |" + "| | [1] | [1-0] | [1-1] | [1-2] | | | | | | | | |" + "| +-------+--------------+--------------+--------------+ | | (0) | (0-0) | (0-1) | (0-2) | (0-3) | |" + "| | [2] | [2-0] | [2-1] | [2-2] | | | | | | | | |" + "| +-------+--------------+--------------+--------------+ | +-----+------------+------------+------------+------------+ |" + "| | | | | | | | |" + "| | | (1) | (1-0) | (1-1) | (1-2) | (1-3) | |" + "| | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ |" + "| | | | | | | | |" + "| | | (2) | (2-0) | (2-1) | (2-2) | (2-3) | |" + "| | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ |" + "| | | | | | | | |" + "| | | (3) | (3-0) | (3-1) | (3-2) | (3-3) | |" + "| | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ |" + "+--------------------------------------------------------+-------------------------------------------------------------+" +); + +test_table!( + col_pair_test, + col!( + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + Matrix::new(4, 4) + .with(Modify::new(Segment::all()).with(Alignment::right())) + .with(Modify::new(Segment::all()).with(Padding::new(1, 1, 1, 1))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("({s})")))), + ), + "+-------------------------------------------------------------+" + "| +-------+--------------+--------------+--------------+ |" + "| | [N] | [column 0] | [column 1] | [column 2] | |" + "| +-------+--------------+--------------+--------------+ |" + "| | [0] | [0-0] | [0-1] | [0-2] | |" + "| +-------+--------------+--------------+--------------+ |" + "| | [1] | [1-0] | [1-1] | [1-2] | |" + "| +-------+--------------+--------------+--------------+ |" + "| | [2] | [2-0] | [2-1] | [2-2] | |" + "| +-------+--------------+--------------+--------------+ |" + "+-------------------------------------------------------------+" + "| +-----+------------+------------+------------+------------+ |" + "| | | | | | | |" + "| | (N) | (column 0) | (column 1) | (column 2) | (column 3) | |" + "| | | | | | | |" + "| +-----+------------+------------+------------+------------+ |" + "| | | | | | | |" + "| | (0) | (0-0) | (0-1) | (0-2) | (0-3) | |" + "| | | | | | | |" + "| +-----+------------+------------+------------+------------+ |" + "| | | | | | | |" + "| | (1) | (1-0) | (1-1) | (1-2) | (1-3) | |" + "| | | | | | | |" + "| +-----+------------+------------+------------+------------+ |" + "| | | | | | | |" + "| | (2) | (2-0) | (2-1) | (2-2) | (2-3) | |" + "| | | | | | | |" + "| +-----+------------+------------+------------+------------+ |" + "| | | | | | | |" + "| | (3) | (3-0) | (3-1) | (3-2) | (3-3) | |" + "| | | | | | | |" + "| +-----+------------+------------+------------+------------+ |" + "+-------------------------------------------------------------+" +); + +test_table!( + row_duplication_test, + row!( + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))); + 3 + ), + "+--------------------------------------------------------+--------------------------------------------------------+--------------------------------------------------------+" + "| +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ |" + "| | [N] | [column 0] | [column 1] | [column 2] | | | [N] | [column 0] | [column 1] | [column 2] | | | [N] | [column 0] | [column 1] | [column 2] | |" + "| +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ |" + "| | [0] | [0-0] | [0-1] | [0-2] | | | [0] | [0-0] | [0-1] | [0-2] | | | [0] | [0-0] | [0-1] | [0-2] | |" + "| +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ |" + "| | [1] | [1-0] | [1-1] | [1-2] | | | [1] | [1-0] | [1-1] | [1-2] | | | [1] | [1-0] | [1-1] | [1-2] | |" + "| +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ |" + "| | [2] | [2-0] | [2-1] | [2-2] | | | [2] | [2-0] | [2-1] | [2-2] | | | [2] | [2-0] | [2-1] | [2-2] | |" + "| +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ | +-------+--------------+--------------+--------------+ |" + "+--------------------------------------------------------+--------------------------------------------------------+--------------------------------------------------------+" +); + +test_table!( + col_and_rows_test, + col!( + row!( + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + Matrix::new(4, 4) + .with(Modify::new(Segment::all()).with(Alignment::right())) + .with(Modify::new(Segment::all()).with(Padding::new(1, 1, 1, 1))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("({s})")))), + ), + Matrix::new(3, 5) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(2, 2, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + ), + "+--------------------------------------------------------------------------------------------------------------------------+" + "| +--------------------------------------------------------+-------------------------------------------------------------+ |" + "| | +-------+--------------+--------------+--------------+ | +-----+------------+------------+------------+------------+ | |" + "| | | [N] | [column 0] | [column 1] | [column 2] | | | | | | | | | |" + "| | +-------+--------------+--------------+--------------+ | | (N) | (column 0) | (column 1) | (column 2) | (column 3) | | |" + "| | | [0] | [0-0] | [0-1] | [0-2] | | | | | | | | | |" + "| | +-------+--------------+--------------+--------------+ | +-----+------------+------------+------------+------------+ | |" + "| | | [1] | [1-0] | [1-1] | [1-2] | | | | | | | | | |" + "| | +-------+--------------+--------------+--------------+ | | (0) | (0-0) | (0-1) | (0-2) | (0-3) | | |" + "| | | [2] | [2-0] | [2-1] | [2-2] | | | | | | | | | |" + "| | +-------+--------------+--------------+--------------+ | +-----+------------+------------+------------+------------+ | |" + "| | | | | | | | | | |" + "| | | | (1) | (1-0) | (1-1) | (1-2) | (1-3) | | |" + "| | | | | | | | | | |" + "| | | +-----+------------+------------+------------+------------+ | |" + "| | | | | | | | | | |" + "| | | | (2) | (2-0) | (2-1) | (2-2) | (2-3) | | |" + "| | | | | | | | | | |" + "| | | +-----+------------+------------+------------+------------+ | |" + "| | | | | | | | | | |" + "| | | | (3) | (3-0) | (3-1) | (3-2) | (3-3) | | |" + "| | | | | | | | | | |" + "| | | +-----+------------+------------+------------+------------+ | |" + "| +--------------------------------------------------------+-------------------------------------------------------------+ |" + "+--------------------------------------------------------------------------------------------------------------------------+" + "| +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | [N] | [column 0] | [column 1] | [column 2] | [column 3] | [column 4] | |" + "| +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | [0] | [0-0] | [0-1] | [0-2] | [0-3] | [0-4] | |" + "| +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | [1] | [1-0] | [1-1] | [1-2] | [1-3] | [1-4] | |" + "| +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | [2] | [2-0] | [2-1] | [2-2] | [2-3] | [2-4] | |" + "| +-------+--------------+--------------+--------------+--------------+--------------+ |" + "+--------------------------------------------------------------------------------------------------------------------------+" +); + +test_table!( + row_and_col_test, + row!( + col!( + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + Matrix::new(4, 4) + .with(Modify::new(Segment::all()).with(Alignment::right())) + .with(Modify::new(Segment::all()).with(Padding::new(1, 1, 1, 1))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("({s})")))), + ), + Matrix::new(3, 5) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(2, 2, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + ), + "+-----------------------------------------------------------------+--------------------------------------------------------------------------------------+" + "| +-------------------------------------------------------------+ | +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | +-------+--------------+--------------+--------------+ | | | [N] | [column 0] | [column 1] | [column 2] | [column 3] | [column 4] | |" + "| | | [N] | [column 0] | [column 1] | [column 2] | | | +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | +-------+--------------+--------------+--------------+ | | | [0] | [0-0] | [0-1] | [0-2] | [0-3] | [0-4] | |" + "| | | [0] | [0-0] | [0-1] | [0-2] | | | +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | +-------+--------------+--------------+--------------+ | | | [1] | [1-0] | [1-1] | [1-2] | [1-3] | [1-4] | |" + "| | | [1] | [1-0] | [1-1] | [1-2] | | | +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | +-------+--------------+--------------+--------------+ | | | [2] | [2-0] | [2-1] | [2-2] | [2-3] | [2-4] | |" + "| | | [2] | [2-0] | [2-1] | [2-2] | | | +-------+--------------+--------------+--------------+--------------+--------------+ |" + "| | +-------+--------------+--------------+--------------+ | | |" + "| +-------------------------------------------------------------+ | |" + "| | +-----+------------+------------+------------+------------+ | | |" + "| | | | | | | | | | |" + "| | | (N) | (column 0) | (column 1) | (column 2) | (column 3) | | | |" + "| | | | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ | | |" + "| | | | | | | | | | |" + "| | | (0) | (0-0) | (0-1) | (0-2) | (0-3) | | | |" + "| | | | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ | | |" + "| | | | | | | | | | |" + "| | | (1) | (1-0) | (1-1) | (1-2) | (1-3) | | | |" + "| | | | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ | | |" + "| | | | | | | | | | |" + "| | | (2) | (2-0) | (2-1) | (2-2) | (2-3) | | | |" + "| | | | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ | | |" + "| | | | | | | | | | |" + "| | | (3) | (3-0) | (3-1) | (3-2) | (3-3) | | | |" + "| | | | | | | | | | |" + "| | +-----+------------+------------+------------+------------+ | | |" + "| +-------------------------------------------------------------+ | |" + "+-----------------------------------------------------------------+--------------------------------------------------------------------------------------+" +); + +test_table!( + row_str_test, + row!("hello", "world"), + "+-------+-------+" + "| hello | world |" + "+-------+-------+" +); + +test_table!( + row_str_duplication_test, + row!("duplicate me"; 5), + "+--------------+--------------+--------------+--------------+--------------+" + "| duplicate me | duplicate me | duplicate me | duplicate me | duplicate me |" + "+--------------+--------------+--------------+--------------+--------------+" +); + +test_table!( + row_display_mixed_test, + row!("str", "string".to_string(), 55, false), + "+-----+--------+----+-------+" + "| str | string | 55 | false |" + "+-----+--------+----+-------+" +); + +test_table!( + col_display_mixed_test, + col!("str", "string".to_string(), 55, false), + "+--------+" + "| str |" + "+--------+" + "| string |" + "+--------+" + "| 55 |" + "+--------+" + "| false |" + "+--------+" +); diff --git a/vendor/tabled/tests/macros/mod.rs b/vendor/tabled/tests/macros/mod.rs new file mode 100644 index 000000000..be52241e9 --- /dev/null +++ b/vendor/tabled/tests/macros/mod.rs @@ -0,0 +1 @@ +mod col_row_test; diff --git a/vendor/tabled/tests/main.rs b/vendor/tabled/tests/main.rs new file mode 100644 index 000000000..35004b906 --- /dev/null +++ b/vendor/tabled/tests/main.rs @@ -0,0 +1,7 @@ +mod core; +mod derive; +mod macros; +mod settings; + +#[cfg(feature = "std")] +mod matrix; diff --git a/vendor/tabled/tests/matrix/matrix.rs b/vendor/tabled/tests/matrix/matrix.rs new file mode 100644 index 000000000..48ba55f9b --- /dev/null +++ b/vendor/tabled/tests/matrix/matrix.rs @@ -0,0 +1,161 @@ +use std::{ + fmt::{self, Display}, + iter::FromIterator, + string::ToString, +}; + +use tabled::{ + grid::config::ColoredConfig, + grid::dimension::CompleteDimensionVecRecords, + grid::records::vec_records::{CellInfo, VecRecords}, + settings::{object::Segment, Alignment, Modify, TableOption}, + Table, Tabled, +}; + +use super::matrix_list::MatrixList; + +/// A helper table factory. +/// +/// It uses center alignment by default, because it's more complex and may spot more issues. +#[derive(Debug, Clone)] +pub struct Matrix { + data: Vec<Vec<String>>, + size: (usize, usize), +} + +impl Matrix { + pub fn empty() -> Self { + Self { + data: vec![], + size: (0, 0), + } + } + + pub fn with_no_frame(rows: usize, columns: usize) -> Self { + Self { + data: create_matrix(rows, columns), + size: (rows, columns), + } + } + + pub fn new(rows: usize, columns: usize) -> Self { + Self::with_no_frame(rows, columns) + .with_header() + .with_index() + } + + pub fn vec(rows: usize, columns: usize) -> Vec<Vec<String>> { + Self::new(rows, columns).to_vec() + } + + pub fn table(rows: usize, columns: usize) -> Table { + Self::new(rows, columns).to_table() + } + + pub fn list<const ROWS: usize, const COLUMNS: usize>() -> Vec<MatrixList<COLUMNS, true>> { + create_list::<ROWS, COLUMNS>() + } + + pub fn iter<I, T>(iter: I) -> Table + where + I: IntoIterator<Item = T>, + T: Tabled, + { + let mut table = tabled::Table::new(iter); + table.with(Modify::new(Segment::all()).with(Alignment::center())); + table + } + + pub fn with_index(mut self) -> Self { + set_index(&mut self.data); + self + } + + pub fn with_header(mut self) -> Self { + set_header(&mut self.data, self.size.1); + self + } + + pub fn insert<V: ToString>(mut self, pos: tabled::grid::config::Position, value: V) -> Self { + self.data[pos.0][pos.1] = value.to_string(); + self + } + + pub fn to_table(&self) -> Table { + let mut table = tabled::Table::from_iter(self.data.clone()); + table.with(Modify::new(Segment::all()).with(Alignment::center())); + table + } + + pub fn to_vec(&self) -> Vec<Vec<String>> { + self.data.clone() + } + + pub fn with<O>(self, opt: O) -> Table + where + O: TableOption< + VecRecords<CellInfo<String>>, + CompleteDimensionVecRecords<'static>, + ColoredConfig, + >, + { + let mut table = self.to_table(); + table.with(opt); + table + } +} + +impl Display for Matrix { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.clone().to_table().fmt(f) + } +} + +fn create_matrix(rows: usize, columns: usize) -> Vec<Vec<String>> { + let mut arr = Vec::with_capacity(rows); + for row in 0..rows { + let mut data = Vec::with_capacity(columns); + for column in 0..columns { + let text = format!("{row}-{column}"); + data.push(text); + } + + arr.push(data); + } + + arr +} + +fn set_header(data: &mut Vec<Vec<String>>, columns: usize) { + data.insert( + 0, + (0..columns) + .map(|n| format!("column {n}")) + .collect::<Vec<_>>(), + ); +} + +fn set_index(data: &mut [Vec<String>]) { + if data.is_empty() { + return; + } + + data[0].insert(0, "N".to_owned()); + + for (n, row) in data.iter_mut().skip(1).enumerate() { + row.insert(0, n.to_string()); + } +} + +fn create_list<const ROWS: usize, const COLUMNS: usize>() -> Vec<MatrixList<COLUMNS, true>> { + let mut arr = Vec::with_capacity(ROWS); + for row in 0..ROWS { + let data = (0..COLUMNS) + .map(|column| format!("{row}-{column}")) + .collect::<Vec<_>>(); + let list = MatrixList::with_index(row, data); + arr.push(list); + } + + arr +} diff --git a/vendor/tabled/tests/matrix/matrix_list.rs b/vendor/tabled/tests/matrix/matrix_list.rs new file mode 100644 index 000000000..9f58400ca --- /dev/null +++ b/vendor/tabled/tests/matrix/matrix_list.rs @@ -0,0 +1,58 @@ +use std::{ + borrow::Cow, + iter::once, + ops::{Index, IndexMut}, +}; + +use tabled::Tabled; + +#[derive(Debug)] +pub struct MatrixList<const N: usize, const INDEX: bool> { + data: Vec<String>, +} + +impl<const N: usize> MatrixList<N, false> { + #[allow(dead_code)] + pub fn new(data: Vec<String>) -> Self { + Self { data } + } +} + +impl<const N: usize> MatrixList<N, true> { + pub fn with_index(index: usize, mut data: Vec<String>) -> Self { + assert_eq!(data.len(), N); + data.insert(0, index.to_string()); + Self { data } + } +} + +impl<const N: usize, const INDEX: bool> Index<usize> for MatrixList<N, INDEX> { + type Output = String; + + fn index(&self, index: usize) -> &Self::Output { + &self.data[index] + } +} + +impl<const N: usize, const INDEX: bool> IndexMut<usize> for MatrixList<N, INDEX> { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.data[index] + } +} + +impl<const N: usize, const INDEX: bool> Tabled for MatrixList<N, INDEX> { + const LENGTH: usize = N + 1; + + fn fields(&self) -> Vec<Cow<'_, str>> { + self.data.iter().cloned().map(Cow::Owned).collect() + } + + fn headers() -> Vec<Cow<'static, str>> { + let header = (0..N).map(|n| format!("column {n}")); + + match INDEX { + true => once("N".to_owned()).chain(header).map(Cow::Owned).collect(), + false => header.map(Cow::Owned).collect(), + } + } +} diff --git a/vendor/tabled/tests/matrix/mod.rs b/vendor/tabled/tests/matrix/mod.rs new file mode 100644 index 000000000..d2d42cfc5 --- /dev/null +++ b/vendor/tabled/tests/matrix/mod.rs @@ -0,0 +1,6 @@ +#[allow(clippy::module_inception)] +mod matrix; +mod matrix_list; + +pub use matrix::Matrix; +pub use matrix_list::MatrixList; diff --git a/vendor/tabled/tests/settings/alignment_test.rs b/vendor/tabled/tests/settings/alignment_test.rs new file mode 100644 index 000000000..0a8145ae2 --- /dev/null +++ b/vendor/tabled/tests/settings/alignment_test.rs @@ -0,0 +1,138 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + locator::ByColumnName, + object::{Columns, Rows, Segment}, + Alignment, Modify, Padding, Style, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + full_alignment, + Matrix::new(3, 3).with(Style::psql()).with(Modify::new(Segment::all()).with(Alignment::left())), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + head_and_data_alignment, + Matrix::new(3, 3) + .with(Modify::new(Rows::first()).with(Alignment::left())) + .with(Modify::new(Rows::new(1..)).with(Alignment::right())), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + full_alignment_multiline, + Matrix::new(3, 3).insert((3, 2), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | https:// | 2-2 " + " | | www | " + " | | . | " + " | | redhat | " + " | | .com | " + " | | /en | " +); + +test_table!( + vertical_alignment_test, + Matrix::new(3, 3) + .insert((2, 2), "E\nnde\navou\nros") + .insert((3, 2), "Red\nHat") + .insert((3, 3), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new(Columns::new(1..)).with(Alignment::bottom())), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | | E | " + " | | nde | " + " | | avou | " + " | 1-0 | ros | 1-2 " + " 2 | | | https:// " + " | | | www " + " | | | . " + " | | | redhat " + " | | Red | .com " + " | 2-0 | Hat | /en " +); + +test_table!( + alignment_doesnt_change_padding, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Padding::new(3, 0, 0, 0))) + .with(Modify::new(Segment::all()).with(Alignment::left())), + " N| column 0| column 1| column 2" + "----+-----------+-----------+-----------" + " 0| 0-0 | 0-1 | 0-2 " + " 1| 1-0 | 1-1 | 1-2 " + " 2| 2-0 | 2-1 | 2-2 " +); + +test_table!( + alignment_global, + Matrix::new(3, 3).with(Style::psql()).with(Alignment::right()), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + padding_by_column_name, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(ByColumnName::new("column 0")).with(Padding::new(3, 3, 0, 0))) + .with(Modify::new(Segment::all()).with(Alignment::center())), + " N | column 0 | column 1 | column 2 " + "---+--------------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + padding_by_column_name_not_first_row, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(ByColumnName::new("0-2")).with(Padding::new(3, 3, 0, 0))) + .with(Modify::new(Segment::all()).with(Alignment::center())), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + padding_by_column_name_not_existing, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(ByColumnName::new("column 01123123")).with(Padding::new(3, 3, 0, 0))) + .with(Modify::new(Segment::all()).with(Alignment::center())), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); diff --git a/vendor/tabled/tests/settings/color_test.rs b/vendor/tabled/tests/settings/color_test.rs new file mode 100644 index 000000000..b81c63ae3 --- /dev/null +++ b/vendor/tabled/tests/settings/color_test.rs @@ -0,0 +1,34 @@ +#![cfg(feature = "std")] + +use tabled::settings::{Color, Modify}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + color_global, + Matrix::new(3, 3).with(Color::FG_MAGENTA), + "+---+----------+----------+----------+" + "| \u{1b}[35mN\u{1b}[39m | \u{1b}[35mcolumn 0\u{1b}[39m | \u{1b}[35mcolumn 1\u{1b}[39m | \u{1b}[35mcolumn 2\u{1b}[39m |" + "+---+----------+----------+----------+" + "| \u{1b}[35m0\u{1b}[39m | \u{1b}[35m0-0\u{1b}[39m | \u{1b}[35m0-1\u{1b}[39m | \u{1b}[35m0-2\u{1b}[39m |" + "+---+----------+----------+----------+" + "| \u{1b}[35m1\u{1b}[39m | \u{1b}[35m1-0\u{1b}[39m | \u{1b}[35m1-1\u{1b}[39m | \u{1b}[35m1-2\u{1b}[39m |" + "+---+----------+----------+----------+" + "| \u{1b}[35m2\u{1b}[39m | \u{1b}[35m2-0\u{1b}[39m | \u{1b}[35m2-1\u{1b}[39m | \u{1b}[35m2-2\u{1b}[39m |" + "+---+----------+----------+----------+" +); + +test_table!( + color_cell, + Matrix::new(3, 3).with(Modify::new((0, 0)).with(Color::BG_BRIGHT_BLACK)), + "+---+----------+----------+----------+" + "| \u{1b}[100mN\u{1b}[49m | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); diff --git a/vendor/tabled/tests/settings/colorization.rs b/vendor/tabled/tests/settings/colorization.rs new file mode 100644 index 000000000..49911c494 --- /dev/null +++ b/vendor/tabled/tests/settings/colorization.rs @@ -0,0 +1,64 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + object::{Cell, Object}, + themes::Colorization, + Color, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + chess_2x3, + Matrix::new(2, 3).with(Colorization::chess(color1(), color2())), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 0\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106mcolumn 1\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 2\u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[106m \u{1b}[49m\u{1b}[106m0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m0-0\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m0-1\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-2\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41m1\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m1-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m1-1\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m1-2\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+" +); + +test_table!( + chess_3x3, + Matrix::new(3, 3).with(Colorization::chess(color1(), color2())), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106mcolumn 0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 1\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106mcolumn 2\u{1b}[49m\u{1b}[106m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[106m \u{1b}[49m\u{1b}[106m0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m0-0\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-1\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m0-2\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41m1\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m1-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m1-1\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m1-2\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[106m \u{1b}[49m\u{1b}[106m2\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m2-0\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m2-1\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m2-2\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+" +); + +test_table!( + rows, + Matrix::new(2, 3).with(Colorization::rows([color1(), color2(), color3()])), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 0\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 1\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 2\u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[106m \u{1b}[49m\u{1b}[106m0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-1\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-2\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[1m \u{1b}[22m\u{1b}[1m1\u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-0\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-1\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-2\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\n+---+----------+----------+----------+" +); + +test_table!( + columns, + Matrix::new(2, 3).with(Colorization::columns([color1(), color2(), color3()])), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106mcolumn 0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[1m \u{1b}[22m\u{1b}[1mcolumn 1\u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 2\u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41m0\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m0-1\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m0-2\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41m1\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m1-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-1\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m1-2\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+" +); + +test_table!( + by_row, + Matrix::new(2, 3).with(Colorization::by_row([color1(), color2(), color3()])), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106mcolumn 0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[1m \u{1b}[22m\u{1b}[1mcolumn 1\u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 2\u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[106m \u{1b}[49m\u{1b}[106m0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m0-0\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m0-1\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-2\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[1m \u{1b}[22m\u{1b}[1m1\u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m1-0\u{1b}[49m\u{1b}[41m \u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m1-1\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-2\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\n+---+----------+----------+----------+" +); + +test_table!( + by_column, + Matrix::new(2, 3).with(Colorization::by_column([color1(), color2(), color3()])), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 0\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 1\u{1b}[49m\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\u{1b}[41mcolumn 2\u{1b}[49m\u{1b}[41m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[106m \u{1b}[49m\u{1b}[106m0\u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-1\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-2\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m|\n+---+----------+----------+----------+\n|\u{1b}[1m \u{1b}[22m\u{1b}[1m1\u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-0\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-1\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-2\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m|\n+---+----------+----------+----------+" +); + +test_table!( + exact, + Matrix::new(2, 3).with(Colorization::exact([color1(), color2(), color3()], Cell::new(0, 0).and(Cell::new(1, 1)).and(Cell::new(2, 2)))), + "+---+----------+----------+----------+\n|\u{1b}[41m \u{1b}[49m\u{1b}[41mN\u{1b}[49m\u{1b}[41m \u{1b}[49m| column 0 | column 1 | column 2 |\n+---+----------+----------+----------+\n| 0 |\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m0-0\u{1b}[49m\u{1b}[106m \u{1b}[49m\u{1b}[106m \u{1b}[49m| 0-1 | 0-2 |\n+---+----------+----------+----------+\n| 1 | 1-0 |\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m1-1\u{1b}[22m\u{1b}[1m \u{1b}[22m\u{1b}[1m \u{1b}[22m| 1-2 |\n+---+----------+----------+----------+" +); + +fn color1() -> Color { + Color::BG_RED +} + +fn color2() -> Color { + Color::BG_BRIGHT_CYAN +} + +fn color3() -> Color { + Color::BOLD +} diff --git a/vendor/tabled/tests/settings/column_names_test.rs b/vendor/tabled/tests/settings/column_names_test.rs new file mode 100644 index 000000000..c2b23f1e3 --- /dev/null +++ b/vendor/tabled/tests/settings/column_names_test.rs @@ -0,0 +1,270 @@ +#![cfg(feature = "std")] + +use tabled::{ + grid::config::AlignmentHorizontal, + settings::{themes::ColumnNames, Color}, + Table, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + new, + Matrix::new(3, 3).with(ColumnNames::new(["1", "2", "3", "4"])), + "+1--+2---------+3---------+4---------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + new_more_names_then_columns, + Matrix::new(3, 3).with(ColumnNames::new(["1", "2", "3", "4", "5", "6", "7"])), + "+1--+2---------+3---------+4---------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + new_less_names_then_columns, + Matrix::new(3, 3).with(ColumnNames::new(["1", "2"])), + "+1--+2---------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + new_empty, + Matrix::new(3, 3).with(ColumnNames::new([""; 0])), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + default, + Matrix::new(3, 3).with(ColumnNames::default()), + "+N--+column 0+column 1+column 2+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+--------+--------+--------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+--------+--------+--------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+--------+--------+--------+" +); + +test_table!( + alignment_left, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Left)), + "+&str---+&str------+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + alignment_right, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Right)), + "+---&str+------&str+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + alignment_center, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Center)), + "+-&str--+---&str---+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + alignment_left_offset, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Left).set_offset(1)), + "+-&str--+-&str-----+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + alignment_left_offset_big, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Left).set_offset(30)), + "+------------------------------&str+------------------------------&str+" + "| Hello | World |" + "+----------------------------------+----------------------------------+" + "| and | looooong |" + "| | word |" + "+----------------------------------+----------------------------------+" +); + +test_table!( + alignment_left_offset_0, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Left).set_offset(0)), + "+&str---+&str------+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + alignment_right_offset, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Right).set_offset(1)), + "+--&str-+-----&str-+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + alignment_right_offset_big, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Right).set_offset(30)), + "+&str------------------------------+&str------------------------------+" + "| Hello | World |" + "+----------------------------------+----------------------------------+" + "| and | looooong |" + "| | word |" + "+----------------------------------+----------------------------------+" +); + +test_table!( + alignment_right_offset_0, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_alignment(AlignmentHorizontal::Right).set_offset(0)), + "+---&str+------&str+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + set_line, + Matrix::new(3, 3).with(ColumnNames::default().set_line(1)), + "+---+--------+--------+--------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+N--+column 0+column 1+column 2+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+--------+--------+--------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+--------+--------+--------+" +); + +test_table!( + set_line_max_out, + Matrix::new(3, 3).with(ColumnNames::default().set_line(100)), + "+---+--------+--------+--------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+--------+--------+--------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+--------+--------+--------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+--------+--------+--------+" +); + +test_table!( + set_line_0, + Matrix::new(3, 3).with(ColumnNames::default().set_line(0)), + "+N--+column 0+column 1+column 2+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+--------+--------+--------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+--------+--------+--------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+--------+--------+--------+" +); + +test_table!( + set_colors_some_some, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_colors([Some(Color::BG_BLACK), Some(Color::BG_BLUE)])), + "+\u{1b}[40m&\u{1b}[49m\u{1b}[40ms\u{1b}[49m\u{1b}[40mt\u{1b}[49m\u{1b}[40mr\u{1b}[49m---+\u{1b}[44m&\u{1b}[49m\u{1b}[44ms\u{1b}[49m\u{1b}[44mt\u{1b}[49m\u{1b}[44mr\u{1b}[49m------+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + set_colors_none_some, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_colors([None, Some(Color::BG_BLUE)])), + "+&str---+\u{1b}[44m&\u{1b}[49m\u{1b}[44ms\u{1b}[49m\u{1b}[44mt\u{1b}[49m\u{1b}[44mr\u{1b}[49m------+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + set_colors_none_none, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_colors([None, None])), + "+&str---+&str------+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); + +test_table!( + set_colors_empty, + Table::new([("Hello", "World"), ("and", "looooong\nword")]) + .with(ColumnNames::default().set_colors([None; 0])), + "+&str---+&str------+" + "| Hello | World |" + "+-------+----------+" + "| and | looooong |" + "| | word |" + "+-------+----------+" +); diff --git a/vendor/tabled/tests/settings/concat_test.rs b/vendor/tabled/tests/settings/concat_test.rs new file mode 100644 index 000000000..b988dcad9 --- /dev/null +++ b/vendor/tabled/tests/settings/concat_test.rs @@ -0,0 +1,140 @@ +#![cfg(feature = "std")] + +use tabled::settings::{Concat, Style}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + join_vertical_0, + Matrix::new(2, 3).insert((1, 0), "123").with(Style::psql()) + .with(Concat::vertical(Matrix::new(2, 3).to_table())) + .to_string(), + " N | column 0 | column 1 | column 2 " + "-----+----------+----------+----------" + " 123 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " +); + +test_table!( + join_vertical_1, + Matrix::new(2, 3) + .with(Concat::vertical(Matrix::new(2, 3).insert((1, 0), "123").with(Style::psql()))), + "+-----+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+-----+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+-----+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+-----+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+-----+----------+----------+----------+" + "| 123 | 0-0 | 0-1 | 0-2 |" + "+-----+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+-----+----------+----------+----------+" +); + +test_table!( + join_horizontal_0, + { + let mut table1 = Matrix::table(2, 3); + table1.with(Style::ascii()); + let mut table2 = Matrix::table(2, 3); + table2.with(Style::psql()); + table2.with(Concat::horizontal(table1)).to_string() + }, + " N | column 0 | column 1 | column 2 | N | column 0 | column 1 | column 2 " + "---+----------+----------+----------+---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 | 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 | 1 | 1-0 | 1-1 | 1-2 " +); + +test_table!( + join_horizontal_1, + { + let mut table1 = Matrix::table(2, 3); + table1.with(Style::ascii()); + let mut table2 = Matrix::table(2, 3); + table2.with(Style::psql()); + table1.with(Concat::horizontal(table2)).to_string() + }, + "+---+----------+----------+----------+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 | N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 | 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 | 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+---+----------+----------+----------+" +); + +test_table!( + join_vertical_different_size, + { + let mut table1 = Matrix::table(2, 2); + table1.with(Style::psql()); + let mut table2 = Matrix::table(2, 3); + table2.with(Style::psql()); + table1.with(Concat::vertical(table2)).to_string() + }, + " N | column 0 | column 1 | " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | " + " 1 | 1-0 | 1-1 | " + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " +); + +test_table!( + join_horizontal_different_size, + { + let mut table1 = Matrix::table(2, 3); + table1.with(Style::psql()); + let mut table2 = Matrix::table(3, 3); + table2.with(Style::psql()); + table1.with(Concat::horizontal(table2)).to_string() + }, + " N | column 0 | column 1 | column 2 | N | column 0 | column 1 | column 2 " + "---+----------+----------+----------+---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 | 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 | 1 | 1-0 | 1-1 | 1-2 " + " | | | | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + join_horizontal_with_not_default_empty_string, + { + let mut table1 = Matrix::table(2, 3); + table1.with(Style::psql()); + let mut table2 = Matrix::table(3, 3); + table2.with(Style::psql()); + table1.with(Concat::horizontal(table2).default_cell("NaN")).to_string() + }, + " N | column 0 | column 1 | column 2 | N | column 0 | column 1 | column 2 " + "-----+----------+----------+----------+---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 | 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 | 1 | 1-0 | 1-1 | 1-2 " + " NaN | NaN | NaN | NaN | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + join_vertical_with_not_default_empty_string, + { + let mut table1 = Matrix::table(2, 2); + table1.with(Style::psql()); + let mut table2 = Matrix::table(2, 3); + table2.with(Style::psql()); + table1.with(Concat::vertical(table2).default_cell("NaN")).to_string() + }, + " N | column 0 | column 1 | NaN " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | NaN " + " 1 | 1-0 | 1-1 | NaN " + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " +); diff --git a/vendor/tabled/tests/settings/disable_test.rs b/vendor/tabled/tests/settings/disable_test.rs new file mode 100644 index 000000000..cf50acf84 --- /dev/null +++ b/vendor/tabled/tests/settings/disable_test.rs @@ -0,0 +1,83 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + locator::ByColumnName, + object::{Columns, Rows, Segment}, + style::{HorizontalLine, Style}, + Alignment, Disable, Modify, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + disable_rows, + Matrix::new(3, 3).with(Disable::row(Rows::new(1..=2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + disable_header, + Matrix::new(3, 3).with(Style::psql()).with(Disable::row(Rows::first())), + " 0 | 0-0 | 0-1 | 0-2 " + "---+-----+-----+-----" + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + disable_all_table_via_rows, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Disable::row(Columns::new(..))), + "" +); + +test_table!( + disable_header_with_new_styling, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Disable::row(Rows::new(..1))) + .with(Style::modern().remove_horizontal().horizontals([HorizontalLine::new(1, Style::modern().get_horizontal())])), + "┌───┬─────┬─────┬─────┐" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "├───┼─────┼─────┼─────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴─────┴─────┴─────┘" +); + +test_table!( + disable_columns, + Matrix::new(3, 3).with(Style::psql()).with(Disable::column(Columns::first())), + " column 0 | column 1 | column 2 " + "----------+----------+----------" + " 0-0 | 0-1 | 0-2 " + " 1-0 | 1-1 | 1-2 " + " 2-0 | 2-1 | 2-2 " +); + +test_table!( + disable_column_by_name, + Matrix::new(3, 3).with(Style::psql()) + .with(Disable::column(ByColumnName::new("column 1"))) + .with(Disable::column(ByColumnName::new("column 3"))), + " N | column 0 | column 2 " + "---+----------+----------" + " 0 | 0-0 | 0-2 " + " 1 | 1-0 | 1-2 " + " 2 | 2-0 | 2-2 " +); + +test_table!( + disable_all_table_via_columns, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Disable::column(Columns::new(..))), + "" +); diff --git a/vendor/tabled/tests/settings/duplicate_test.rs b/vendor/tabled/tests/settings/duplicate_test.rs new file mode 100644 index 000000000..6d15fbfac --- /dev/null +++ b/vendor/tabled/tests/settings/duplicate_test.rs @@ -0,0 +1,189 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + object::{Cell, Columns, Rows, Segment}, + Dup, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + dup_cell_to_cell, + Matrix::new(3, 3).with(Dup::new(Cell::new(0, 0), Cell::new(0, 1))), + "+----------+----------+----------+----------+" + "| column 0 | column 0 | column 1 | column 2 |" + "+----------+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+----------+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+----------+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+----------+----------+----------+----------+" +); + +test_table!( + dup_cell_to_column, + Matrix::new(3, 3).with(Dup::new(Columns::single(1), Cell::new(0, 0))), + "+---+---+----------+----------+" + "| N | N | column 1 | column 2 |" + "+---+---+----------+----------+" + "| 0 | N | 0-1 | 0-2 |" + "+---+---+----------+----------+" + "| 1 | N | 1-1 | 1-2 |" + "+---+---+----------+----------+" + "| 2 | N | 2-1 | 2-2 |" + "+---+---+----------+----------+" +); + +test_table!( + dup_row_to_row_single, + Matrix::new(3, 3).with(Dup::new(Rows::single(1), Rows::single(0))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_row_to_row_single_to_many, + Matrix::new(3, 3).with(Dup::new(Rows::new(1..3), Rows::single(0))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_row_to_row_single_to_all, + Matrix::new(3, 3).with(Dup::new(Rows::new(1..), Rows::single(0))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_row_to_column_single, + Matrix::new(3, 3).with(Dup::new(Columns::single(1), Rows::single(0))), + "+---+----------+----------+----------+" + "| N | N | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | column 0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | column 1 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | column 2 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_column_to_row_single, + Matrix::new(3, 3).with(Dup::new(Columns::single(1), Columns::single(0))), + "+---+---+----------+----------+" + "| N | N | column 1 | column 2 |" + "+---+---+----------+----------+" + "| 0 | 0 | 0-1 | 0-2 |" + "+---+---+----------+----------+" + "| 1 | 1 | 1-1 | 1-2 |" + "+---+---+----------+----------+" + "| 2 | 2 | 2-1 | 2-2 |" + "+---+---+----------+----------+" +); + +test_table!( + dup_row_to_column_single_repeat, + Matrix::new(4, 3).with(Dup::new(Columns::single(1), Rows::single(0))), + "+---+----------+----------+----------+" + "| N | N | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | column 0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | column 1 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | column 2 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" + "| 3 | N | 3-1 | 3-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_column_to_row_single_stop, + Matrix::new(4, 3).with(Dup::new(Rows::single(1), Columns::single(0))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | 0 | 1 | 2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" + "| 3 | 3-0 | 3-1 | 3-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_row_to_global, + Matrix::new(4, 3).with(Dup::new(Segment::all(), Rows::single(0))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" +); + +test_table!( + dup_column_to_global, + Matrix::new(4, 3).with(Dup::new(Segment::all(), Columns::single(0))), + "+---+---+---+---+" + "| N | 0 | 1 | 2 |" + "+---+---+---+---+" + "| 3 | N | 0 | 1 |" + "+---+---+---+---+" + "| 2 | 3 | N | 0 |" + "+---+---+---+---+" + "| 1 | 2 | 3 | N |" + "+---+---+---+---+" + "| 0 | 1 | 2 | 3 |" + "+---+---+---+---+" +); + +test_table!( + dup_empty_table, + Matrix::empty().with(Dup::new(Segment::all(), Columns::single(0))), + "" +); + +test_table!( + dup_invalid_target, + Matrix::new(4, 3).with(Dup::new(Segment::all(), Columns::single(99))), + Matrix::new(4, 3), +); + +test_table!( + dup_invalid_source, + Matrix::new(4, 3).with(Dup::new(Rows::single(99), Columns::first())), + Matrix::new(4, 3), +); diff --git a/vendor/tabled/tests/settings/extract_test.rs b/vendor/tabled/tests/settings/extract_test.rs new file mode 100644 index 000000000..ce4697d7a --- /dev/null +++ b/vendor/tabled/tests/settings/extract_test.rs @@ -0,0 +1,242 @@ +#![cfg(feature = "std")] + +use tabled::{ + builder::Builder, + settings::{ + object::{Rows, Segment}, + Alignment, Disable, Extract, Format, Modify, Padding, + }, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + extract_segment_full_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::segment(.., ..)), + "+-------+--------------+--------------+--------------+" + "| [N] | [column 0] | [column 1] | [column 2] |" + "+-------+--------------+--------------+--------------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-------+--------------+--------------+--------------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-------+--------------+--------------+--------------+" + "| [2] | [2-0] | [2-1] | [2-2] |" + "+-------+--------------+--------------+--------------+" +); + +test_table!( + extract_segment_skip_top_row_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::segment(1.., ..)), + "+-------+---------+---------+---------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-------+---------+---------+---------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-------+---------+---------+---------+" + "| [2] | [2-0] | [2-1] | [2-2] |" + "+-------+---------+---------+---------+" +); + +test_table!( + extract_segment_skip_column_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::segment(.., 1..)), + "+--------------+--------------+--------------+" + "| [column 0] | [column 1] | [column 2] |" + "+--------------+--------------+--------------+" + "| [0-0] | [0-1] | [0-2] |" + "+--------------+--------------+--------------+" + "| [1-0] | [1-1] | [1-2] |" + "+--------------+--------------+--------------+" + "| [2-0] | [2-1] | [2-2] |" + "+--------------+--------------+--------------+" +); + +test_table!( + extract_segment_bottom_right_square_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::segment(2.., 2..)), + "+---------+---------+" + "| [1-1] | [1-2] |" + "+---------+---------+" + "| [2-1] | [2-2] |" + "+---------+---------+" +); + +test_table!( + extract_segment_middle_section_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::segment(1..3, 1..)), + "+---------+---------+---------+" + "| [0-0] | [0-1] | [0-2] |" + "+---------+---------+---------+" + "| [1-0] | [1-1] | [1-2] |" + "+---------+---------+---------+" +); + +test_table!( + extract_segment_empty_test, + Matrix::new(3, 3).with(Extract::segment(1..1, 1..1)), + "" +); + +test_table!( + extract_rows_full_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::rows(..)), + "+-------+--------------+--------------+--------------+" + "| [N] | [column 0] | [column 1] | [column 2] |" + "+-------+--------------+--------------+--------------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-------+--------------+--------------+--------------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-------+--------------+--------------+--------------+" + "| [2] | [2-0] | [2-1] | [2-2] |" + "+-------+--------------+--------------+--------------+" +); + +test_table!( + extract_rows_empty_test, + Matrix::new(3, 3).with(Extract::rows(0..0)), + "" +); + +test_table!( + extract_rows_partial_view_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::rows(0..=2)), + "+-------+--------------+--------------+--------------+" + "| [N] | [column 0] | [column 1] | [column 2] |" + "+-------+--------------+--------------+--------------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-------+--------------+--------------+--------------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-------+--------------+--------------+--------------+" +); + +test_table!( + extract_columns_full_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::columns(..)), + "+-------+--------------+--------------+--------------+" + "| [N] | [column 0] | [column 1] | [column 2] |" + "+-------+--------------+--------------+--------------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-------+--------------+--------------+--------------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-------+--------------+--------------+--------------+" + "| [2] | [2-0] | [2-1] | [2-2] |" + "+-------+--------------+--------------+--------------+" +); + +test_table!( + extract_columns_empty_test, + Matrix::new(3, 3).with(Extract::columns(0..0)), + "" +); + +test_table!( + extract_columns_partial_view_test, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))) + .with(Extract::columns(0..2)), + "+-------+--------------+" + "| [N] | [column 0] |" + "+-------+--------------+" + "| [0] | [0-0] |" + "+-------+--------------+" + "| [1] | [1-0] |" + "+-------+--------------+" + "| [2] | [2-0] |" + "+-------+--------------+" +); + +test_table!( + extract_inside_test, + Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(1..2, 1..2)), + "+-----+" + "| 1-0 |" + "+-----+" +); + +test_table!( + extract_left_test, + Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(.., ..1)), + "+---+" + "| 0 |" + "+---+" + "| 1 |" + "+---+" + "| 2 |" + "+---+" +); + +test_table!( + extract_right_test, + Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(.., 2..)), + "+-----+-----+" + "| 0-1 | 0-2 |" + "+-----+-----+" + "| 1-1 | 1-2 |" + "+-----+-----+" + "| 2-1 | 2-2 |" + "+-----+-----+" +); + +test_table!( + extract_top_test, + Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(..1, ..)), + "+---+-----+-----+-----+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+-----+-----+-----+" +); + +test_table!( + extract_bottom_test, + Matrix::new(3, 3).with(Disable::row(Rows::first())).with(Extract::segment(2.., ..)), + "+---+-----+-----+-----+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+-----+-----+-----+" +); + +test_table!( + extract_all_test, + Matrix::new(3, 3) + .with(Disable::row(Rows::first())) + .with(Extract::segment(3.., 3..)), + "" +); + +test_table!( + extract_empty_test, + Builder::default().build().with(Extract::segment(.., ..)), + "" +); diff --git a/vendor/tabled/tests/settings/format_test.rs b/vendor/tabled/tests/settings/format_test.rs new file mode 100644 index 000000000..82585ca62 --- /dev/null +++ b/vendor/tabled/tests/settings/format_test.rs @@ -0,0 +1,269 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + object::{Cell, Columns, Object, Rows, Segment}, + Alignment, Format, Modify, Padding, Style, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +#[cfg(feature = "color")] +use owo_colors::OwoColorize; + +test_table!( + formatting_full_test, + Matrix::new(3, 3).with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + "+-----+------------+------------+------------+" + "| [N] | [column 0] | [column 1] | [column 2] |" + "+-----+------------+------------+------------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-----+------------+------------+------------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-----+------------+------------+------------+" + "| [2] | [2-0] | [2-1] | [2-2] |" + "+-----+------------+------------+------------+" +); + +test_table!( + formatting_head_test, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::first()).with(Format::content(|s| format!(":{s}")))), + "| :N | :column 0 | :column 1 | :column 2 |" + "|----|-----------|-----------|-----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + formatting_row_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Rows::new(1..)).with(Format::content(|s| format!("<{s}>")))), + " N | column 0 | column 1 | column 2 " + "-----+----------+----------+----------" + " <0> | <0-0> | <0-1> | <0-2> " + " <1> | <1-0> | <1-1> | <1-2> " + " <2> | <2-0> | <2-1> | <2-2> " +); + +test_table!( + formatting_column_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Columns::single(0)).with(Format::content(|s| format!("(x) {s}")))), + " (x) N | column 0 | column 1 | column 2 " + "-------+----------+----------+----------" + " (x) 0 | 0-0 | 0-1 | 0-2 " + " (x) 1 | 1-0 | 1-1 | 1-2 " + " (x) 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + formatting_multiline_test, + Matrix::new(3, 3) + .insert((2, 2), "E\nnde\navou\nros") + .insert((3, 2), "Red\nHat") + .insert((3, 3), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("(x) {s}")).multiline())), + " (x) N | (x) column 0 | (x) column 1 | (x) column 2 " + "-------+--------------+--------------+--------------" + " (x) 0 | (x) 0-0 | (x) 0-1 | (x) 0-2 " + " (x) 1 | (x) 1-0 | (x) E | (x) 1-2 " + " | | (x) nde | " + " | | (x) avou | " + " | | (x) ros | " + " (x) 2 | (x) 2-0 | (x) Red | (x) https:// " + " | | (x) Hat | (x) www " + " | | | (x) . " + " | | | (x) redhat " + " | | | (x) .com " + " | | | (x) /en " +); + +test_table!( + formatting_cell_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Cell::new(0, 0)).with(Format::content(|s| format!("(x) {s}")))) + .with(Modify::new(Cell::new(0, 1)).with(Format::content(|s| format!("(x) {s}")))) + .with(Modify::new(Cell::new(0, 2)).with(Format::content(|s| format!("(x) {s}")))), + " (x) N | (x) column 0 | (x) column 1 | column 2 " + "-------+--------------+--------------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + formatting_combination_and_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with( + Modify::new(Columns::single(0).and(Rows::single(0))) + .with(Format::content(|s| format!("(x) {s}"))), + ), + " (x) N | (x) column 0 | (x) column 1 | (x) column 2 " + "-------+--------------+--------------+--------------" + " (x) 0 | 0-0 | 0-1 | 0-2 " + " (x) 1 | 1-0 | 1-1 | 1-2 " + " (x) 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + formatting_combination_not_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with( + Modify::new(Columns::single(0).and(Rows::single(0)).not(Cell::new(0, 0))) + .with(Format::content(|s| format!("(x) {s}"))), + ), + " N | (x) column 0 | (x) column 1 | (x) column 2 " + "-------+--------------+--------------+--------------" + " (x) 0 | 0-0 | 0-1 | 0-2 " + " (x) 1 | 1-0 | 1-1 | 1-2 " + " (x) 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + formatting_combination_inverse_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Columns::single(0).inverse()).with(Format::content(|s| format!("(x) {s}")))), + " N | (x) column 0 | (x) column 1 | (x) column 2 " + "---+--------------+--------------+--------------" + " 0 | (x) 0-0 | (x) 0-1 | (x) 0-2 " + " 1 | (x) 1-0 | (x) 1-1 | (x) 1-2 " + " 2 | (x) 2-0 | (x) 2-1 | (x) 2-2 " +); + +test_table!( + formatting_combination_intersect_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with( + Modify::new(Columns::new(1..3).intersect(Rows::new(1..3))) + .with(Format::content(|s| format!("(x) {s}"))), + ), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | (x) 0-0 | (x) 0-1 | 0-2 " + " 1 | (x) 1-0 | (x) 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + formatting_using_lambda_test, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::first()).with(Format::content(|s| format!(":{s}")))), + "| :N | :column 0 | :column 1 | :column 2 |" + "|----|-----------|-----------|-----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + formatting_using_function_test, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::first()).with(Format::content(str::to_uppercase))), + "| N | COLUMN 0 | COLUMN 1 | COLUMN 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + format_with_index, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::first()).with(Format::positioned(|a, (b, c)| match (b, c) { + (0, 0) => "(0, 0)".to_string(), + (0, 1) => "(0, 1)".to_string(), + (0, 2) => "(0, 2)".to_string(), + _ => a.to_string(), + }))), + "| (0, 0) | (0, 1) | (0, 2) | column 2 |" + "|--------|--------|--------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + format_doesnt_change_padding, + Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Padding::new(3, 1, 0, 0))) + .with(Modify::new(Segment::all()).with(Format::content(|s| format!("[{s}]")))), + "+-------+--------------+--------------+--------------+" + "| [N] | [column 0] | [column 1] | [column 2] |" + "+-------+--------------+--------------+--------------+" + "| [0] | [0-0] | [0-1] | [0-2] |" + "+-------+--------------+--------------+--------------+" + "| [1] | [1-0] | [1-1] | [1-2] |" + "+-------+--------------+--------------+--------------+" + "| [2] | [2-0] | [2-1] | [2-2] |" + "+-------+--------------+--------------+--------------+" +); + +test_table!( + formatting_content_str_test, + Matrix::new(3, 3).with(Modify::new(Segment::all()).with(Format::content(|_| String::from("Hello World")))), + "+-------------+-------------+-------------+-------------+" + "| Hello World | Hello World | Hello World | Hello World |" + "+-------------+-------------+-------------+-------------+" + "| Hello World | Hello World | Hello World | Hello World |" + "+-------------+-------------+-------------+-------------+" + "| Hello World | Hello World | Hello World | Hello World |" + "+-------------+-------------+-------------+-------------+" + "| Hello World | Hello World | Hello World | Hello World |" + "+-------------+-------------+-------------+-------------+" +); + +#[cfg(feature = "color")] +test_table!( + color_test, + Matrix::new(3, 3) + .with(Style::psql()) + .with( + Modify::new(Columns::new(..1).and(Columns::new(2..))) + .with(Format::content(|s| s.red().to_string())), + ) + .with(Modify::new(Columns::new(1..2)).with(Format::content(|s| s.blue().to_string()))), + " \u{1b}[31mN\u{1b}[39m | \u{1b}[34mcolumn 0\u{1b}[39m | \u{1b}[31mcolumn 1\u{1b}[39m | \u{1b}[31mcolumn 2\u{1b}[39m " + "---+----------+----------+----------" + " \u{1b}[31m0\u{1b}[39m | \u{1b}[34m0-0\u{1b}[39m | \u{1b}[31m0-1\u{1b}[39m | \u{1b}[31m0-2\u{1b}[39m " + " \u{1b}[31m1\u{1b}[39m | \u{1b}[34m1-0\u{1b}[39m | \u{1b}[31m1-1\u{1b}[39m | \u{1b}[31m1-2\u{1b}[39m " + " \u{1b}[31m2\u{1b}[39m | \u{1b}[34m2-0\u{1b}[39m | \u{1b}[31m2-1\u{1b}[39m | \u{1b}[31m2-2\u{1b}[39m " +); + +#[cfg(feature = "color")] +test_table!( + color_multiline_test, + Matrix::new(3, 3) + .insert((2, 2), "E\nnde\navou\nros") + .insert((3, 2), "Red\nHat") + .insert((3, 3), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new(Columns::new(..1)).with(Format::content(|s| s.red().to_string()).multiline())) + .with(Modify::new(Columns::new(1..2)).with(Format::content(|s| s.blue().to_string()).multiline())) + .with(Modify::new(Columns::new(2..)).with(Format::content(|s| s.green().to_string()).multiline())), + " \u{1b}[31mN\u{1b}[39m | \u{1b}[34mcolumn 0\u{1b}[39m | \u{1b}[32mcolumn 1\u{1b}[39m | \u{1b}[32mcolumn 2\u{1b}[39m " + "---+----------+----------+----------\n \u{1b}[31m0\u{1b}[39m | \u{1b}[34m0-0\u{1b}[39m | \u{1b}[32m0-1\u{1b}[39m | \u{1b}[32m0-2\u{1b}[39m " + " \u{1b}[31m1\u{1b}[39m | \u{1b}[34m1-0\u{1b}[39m | \u{1b}[32mE\u{1b}[39m | \u{1b}[32m1-2\u{1b}[39m " + " | | \u{1b}[32mnde\u{1b}[39m | " + " | | \u{1b}[32mavou\u{1b}[39m | " + " | | \u{1b}[32mros\u{1b}[39m | " + " \u{1b}[31m2\u{1b}[39m | \u{1b}[34m2-0\u{1b}[39m | \u{1b}[32mRed\u{1b}[39m | \u{1b}[32mhttps://\u{1b}[39m " + " | | \u{1b}[32mHat\u{1b}[39m | \u{1b}[32mwww\u{1b}[39m \n | | | \u{1b}[32m.\u{1b}[39m " + " | | | \u{1b}[32mredhat\u{1b}[39m " + " | | | \u{1b}[32m.com\u{1b}[39m " + " | | | \u{1b}[32m/en\u{1b}[39m " +); diff --git a/vendor/tabled/tests/settings/formatting_test.rs b/vendor/tabled/tests/settings/formatting_test.rs new file mode 100644 index 000000000..70f925a8b --- /dev/null +++ b/vendor/tabled/tests/settings/formatting_test.rs @@ -0,0 +1,68 @@ +#![cfg(feature = "std")] + +use tabled::settings::{formatting::Justification, object::Columns, Color, Modify}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + justification, + Matrix::new(3, 3).with(Justification::new('#')), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | ##0-0### | ##0-1### | ##0-2### |" + "+---+----------+----------+----------+" + "| 1 | ##1-0### | ##1-1### | ##1-2### |" + "+---+----------+----------+----------+" + "| 2 | ##2-0### | ##2-1### | ##2-2### |" + "+---+----------+----------+----------+" +); + +test_table!( + justification_color, + Matrix::new(3, 3).with(Justification::new('#').color(Color::BG_RED)), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | \u{1b}[41m##\u{1b}[49m0-0\u{1b}[41m###\u{1b}[49m | \u{1b}[41m##\u{1b}[49m0-1\u{1b}[41m###\u{1b}[49m | \u{1b}[41m##\u{1b}[49m0-2\u{1b}[41m###\u{1b}[49m |" + "+---+----------+----------+----------+" + "| 1 | \u{1b}[41m##\u{1b}[49m1-0\u{1b}[41m###\u{1b}[49m | \u{1b}[41m##\u{1b}[49m1-1\u{1b}[41m###\u{1b}[49m | \u{1b}[41m##\u{1b}[49m1-2\u{1b}[41m###\u{1b}[49m |" + "+---+----------+----------+----------+" + "| 2 | \u{1b}[41m##\u{1b}[49m2-0\u{1b}[41m###\u{1b}[49m | \u{1b}[41m##\u{1b}[49m2-1\u{1b}[41m###\u{1b}[49m | \u{1b}[41m##\u{1b}[49m2-2\u{1b}[41m###\u{1b}[49m |" + "+---+----------+----------+----------+" +); + +test_table!( + justification_columns, + Matrix::new(3, 3) + .with(Modify::new(Columns::single(1)).with(Justification::new('#'))) + .with(Modify::new(Columns::single(2)).with(Justification::new('@'))) + .with(Modify::new(Columns::single(3)).with(Justification::new('$'))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | ##0-0### | @@0-1@@@ | $$0-2$$$ |" + "+---+----------+----------+----------+" + "| 1 | ##1-0### | @@1-1@@@ | $$1-2$$$ |" + "+---+----------+----------+----------+" + "| 2 | ##2-0### | @@2-1@@@ | $$2-2$$$ |" + "+---+----------+----------+----------+" +); + +test_table!( + justification_color_columns, + Matrix::new(3, 3) + .with(Modify::new(Columns::single(1)).with(Justification::new('#').color(Color::BG_BLUE))) + .with(Modify::new(Columns::single(2)).with(Justification::new('@').color(Color::BG_RED))) + .with(Modify::new(Columns::single(3)).with(Justification::new('$').color(Color::BG_WHITE))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | \u{1b}[44m##\u{1b}[49m0-0\u{1b}[44m###\u{1b}[49m | \u{1b}[41m@@\u{1b}[49m0-1\u{1b}[41m@@@\u{1b}[49m | \u{1b}[47m$$\u{1b}[49m0-2\u{1b}[47m$$$\u{1b}[49m |" + "+---+----------+----------+----------+" + "| 1 | \u{1b}[44m##\u{1b}[49m1-0\u{1b}[44m###\u{1b}[49m | \u{1b}[41m@@\u{1b}[49m1-1\u{1b}[41m@@@\u{1b}[49m | \u{1b}[47m$$\u{1b}[49m1-2\u{1b}[47m$$$\u{1b}[49m |" + "+---+----------+----------+----------+" + "| 2 | \u{1b}[44m##\u{1b}[49m2-0\u{1b}[44m###\u{1b}[49m | \u{1b}[41m@@\u{1b}[49m2-1\u{1b}[41m@@@\u{1b}[49m | \u{1b}[47m$$\u{1b}[49m2-2\u{1b}[47m$$$\u{1b}[49m |" + "+---+----------+----------+----------+" +); diff --git a/vendor/tabled/tests/settings/height_test.rs b/vendor/tabled/tests/settings/height_test.rs new file mode 100644 index 000000000..48d6a6364 --- /dev/null +++ b/vendor/tabled/tests/settings/height_test.rs @@ -0,0 +1,263 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + object::{Columns, Segment}, + Alignment, Format, Height, Modify, Style, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +#[cfg(feature = "color")] +use owo_colors::OwoColorize; + +test_table!( + cell_height_increase, + Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Columns::first()) + .with(Height::increase(3)) + ) + .with(Modify::new(Segment::all()).with( + Alignment::center_vertical() + )), + "| N | | | |" + "| | column 0 | column 1 | column 2 |" + "| | | | |" + "|---|----------|----------|----------|" + "| 0 | | | |" + "| | 0-0 | 0-1 | 0-2 |" + "| | | | |" + "| 1 | | | |" + "| | 1-0 | 1-1 | 1-2 |" + "| | | | |" + "| 2 | | | |" + "| | 2-0 | 2-1 | 2-2 |" + "| | | | |" +); + +test_table!( + table_height_increase, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Alignment::center_vertical())) + .with(Height::increase(10)), + "| | column 0 | column 1 | column 2 |" + "| N | | | |" + "| | | | |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| | | | |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| | | | |" + "| 2 | 2-0 | 2-1 | 2-2 |" + "| | | | |" +); + +test_table!( + cell_height_increase_zero, + Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Columns::first()) + .with(Height::increase(0)) + ) + .with(Modify::new(Segment::all()).with( + Alignment::center_vertical() + )), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + table_height_increase_zero, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Alignment::center_vertical())) + .with(Height::increase(0)), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + cell_height_limit, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n")))) + .with( + Modify::new(Columns::first()) + .with(Height::limit(1)) + ) + .with(Modify::new(Segment::all()).with( + Alignment::center_vertical() + )), + "| xxxx | column 0 | column 1 | column 2 |" + "|------|----------|----------|----------|" + "| xxxx | 0-0 | 0-1 | 0-2 |" + "| xxxx | 1-0 | 1-1 | 1-2 |" + "| xxxx | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + table_height_limit, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n")))) + .with(Modify::new(Columns::first()).with(Alignment::center_vertical())) + .with(Height::limit(10)), + "| xxxx | column 0 | column 1 | column 2 |" + "| Nxxxx | | | |" + "|-------|----------|----------|----------|" + "| xxxx | 0-0 | 0-1 | 0-2 |" + "| 0xxxx | | | |" + "| xxxx | 1-0 | 1-1 | 1-2 |" + "| 1xxxx | | | |" + "| xxxx | 2-0 | 2-1 | 2-2 |" + "| 2xxxx | | | |" + "| xxxx | | | |" +); + +test_table!( + table_height_limit_style_change_after, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n")))) + .with(Modify::new(Columns::first()).with(Alignment::center_vertical())) + .with(Height::limit(7)), + "| xxxx | column 0 | column 1 | column 2 |" + "|-------|----------|----------|----------|" + "| xxxx | 0-0 | 0-1 | 0-2 |" + "| xxxx | 1-0 | 1-1 | 1-2 |" + "| 1xxxx | | | |" + "| xxxx | 2-0 | 2-1 | 2-2 |" + "| 2xxxx | | | |" +); + +test_table!( + cell_height_limit_zero, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n")))) + .with( + Modify::new(Columns::first()) + .with(Height::limit(0)) + ) + .with(Modify::new(Segment::all()).with( + Alignment::center_vertical() + )), + "| | column 0 | column 1 | column 2 |" + "|--|----------|----------|----------|" + "| | 0-0 | 0-1 | 0-2 |" + "| | 1-0 | 1-1 | 1-2 |" + "| | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + table_height_limit_zero, + Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Columns::new(..)) + .with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n"))) + ) + .with(Height::limit(0)), + "|--|--|--|--|" +); + +test_table!( + table_height_limit_zero_1, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Height::limit(0)) + .with( + Modify::new(Columns::new(..)).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n"))) + ), + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| | | | |" + "|------|------|------|------|" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| | | | |" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| | | | |" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| xxxx | xxxx | xxxx | xxxx |" + "| | | | |" +); + +#[cfg(feature = "color")] +test_table!( + cell_height_limit_colored, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n").red().to_string()))) + .with( + Modify::new(Columns::first()) + .with(Height::limit(1)) + ) + .with(Modify::new(Segment::all()).with( + Alignment::center_vertical() + )), + "| \u{1b}[31mxxxx\u{1b}[39m | column 0 | column 1 | column 2 |" + "|------|----------|----------|----------|" + "| \u{1b}[31mxxxx\u{1b}[39m | 0-0 | 0-1 | 0-2 |" + "| \u{1b}[31mxxxx\u{1b}[39m | 1-0 | 1-1 | 1-2 |" + "| \u{1b}[31mxxxx\u{1b}[39m | 2-0 | 2-1 | 2-2 |" +); + +#[cfg(feature = "color")] +test_table!( + table_height_limit_colored, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::first()).with(Format::content(|s| format!("xxxx\n{s}xxxx\nxxxx\n").blue().on_green().to_string()))) + .with(Modify::new(Columns::first()).with(Alignment::center_vertical())) + .with(Height::limit(10)), + "| \u{1b}[34;42mxxxx\u{1b}[39m\u{1b}[49m | column 0 | column 1 | column 2 |" + "| \u{1b}[34m\u{1b}[42mNxxxx\u{1b}[39m\u{1b}[49m | | | |" + "|-------|----------|----------|----------|" + "| \u{1b}[34;42mxxxx\u{1b}[39m\u{1b}[49m | 0-0 | 0-1 | 0-2 |" + "| \u{1b}[34m\u{1b}[42m0xxxx\u{1b}[39m\u{1b}[49m | | | |" + "| \u{1b}[34;42mxxxx\u{1b}[39m\u{1b}[49m | 1-0 | 1-1 | 1-2 |" + "| \u{1b}[34m\u{1b}[42m1xxxx\u{1b}[39m\u{1b}[49m | | | |" + "| \u{1b}[34;42mxxxx\u{1b}[39m\u{1b}[49m | 2-0 | 2-1 | 2-2 |" + "| \u{1b}[34m\u{1b}[42m2xxxx\u{1b}[39m\u{1b}[49m | | | |" + "| \u{1b}[34m\u{1b}[42mxxxx\u{1b}[39m\u{1b}[49m | | | |" +); + +#[cfg(feature = "macros")] +test_table!( + cell_height_1x1, + tabled::row![tabled::col!["SGML"].with(Height::increase(4))], + "+----------+" + "| +------+ |" + "| | SGML | |" + "| | | |" + "| +------+ |" + "+----------+" +); + +#[cfg(feature = "macros")] +test_table!( + cell_height_1x1_no_top_border, + tabled::row![tabled::col!["SGML"].with(Style::ascii().remove_top()).with(Height::increase(4))], + "+----------+" + "| | SGML | |" + "| | | |" + "| | | |" + "| +------+ |" + "+----------+" +); diff --git a/vendor/tabled/tests/settings/highlingt_test.rs b/vendor/tabled/tests/settings/highlingt_test.rs new file mode 100644 index 000000000..cb5ce75f3 --- /dev/null +++ b/vendor/tabled/tests/settings/highlingt_test.rs @@ -0,0 +1,419 @@ +#![cfg(feature = "std")] + +use tabled::{ + builder::Builder, + settings::{ + highlight::Highlight, + object::{Cell, Columns, Frame, Object, Rows, Segment}, + style::{Border, Style}, + }, +}; + +use crate::matrix::Matrix; +use testing_table::{static_table, test_table}; + +test_table!( + highlingt_object_exceeds_boundaries, + Matrix::new(3, 3).with(Style::modern()).with(Highlight::new(Cell::new(1000, 0), Border::filled('+'))), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + highlingt_empty_table, + Builder::default() + .build() + .with(Highlight::new(Segment::all(), Border::filled('+'))), + "" +); + +test_table!( + highlingt_cell, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new(Cell::new(0, 0), Border::filled('+'))) + .with(Highlight::new(Cell::new(1, 1), Border::filled('*'))), + "+++++──────────┬──────────┬──────────┐" + "+ N + column 0 │ column 1 │ column 2 │" + "++++************──────────┼──────────┤" + "│ 0 * 0-0 * 0-1 │ 0-2 │" + "├───************──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + highlingt_row, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new(Rows::single(0), Border::filled('+'))) + .with(Highlight::new(Rows::single(3), Border::filled('*'))), + "++++++++++++++++++++++++++++++++++++++" + "+ N │ column 0 │ column 1 │ column 2 +" + "++++++++++++++++++++++++++++++++++++++" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "**************************************" + "* 2 │ 2-0 │ 2-1 │ 2-2 *" + "**************************************" +); + +test_table!( + highlingt_column, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new(Columns::single(0), Border::filled('+'))) + .with(Highlight::new(Columns::single(2), Border::filled('*'))), + "+++++──────────************──────────┐" + "+ N + column 0 * column 1 * column 2 │" + "+───+──────────*──────────*──────────┤" + "+ 0 + 0-0 * 0-1 * 0-2 │" + "+───+──────────*──────────*──────────┤" + "+ 1 + 1-0 * 1-1 * 1-2 │" + "+───+──────────*──────────*──────────┤" + "+ 2 + 2-0 * 2-1 * 2-2 │" + "+++++──────────************──────────┘" +); + +test_table!( + highlingt_row_range, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new(Rows::new(1..3), Border::filled('+'))), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "++++++++++++++++++++++++++++++++++++++" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "++++++++++++++++++++++++++++++++++++++" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + highlingt_column_range, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new(Columns::new(..2), Border::filled('+'))), + "++++++++++++++++──────────┬──────────┐" + "+ N │ column 0 + column 1 │ column 2 │" + "+───┼──────────+──────────┼──────────┤" + "+ 0 │ 0-0 + 0-1 │ 0-2 │" + "+───┼──────────+──────────┼──────────┤" + "+ 1 │ 1-0 + 1-1 │ 1-2 │" + "+───┼──────────+──────────┼──────────┤" + "+ 2 │ 2-0 + 2-1 │ 2-2 │" + "++++++++++++++++──────────┴──────────┘" +); + +test_table!( + highlingt_frame, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new( + Frame, + Border::filled('+') + .corner_top_left('*') + .corner_top_right('#') + .corner_bottom_left('@') + .corner_bottom_right('.'), + )), + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "@++++++++++++++++++++++++++++++++++++." +); + +test_table!( + highlingt_full, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new( + Segment::all(), + Border::filled('+') + .corner_top_left('*') + .corner_top_right('#') + .corner_bottom_left('@') + .corner_bottom_right('.'), + )), + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "@++++++++++++++++++++++++++++++++++++." +); + +test_table!( + highlingt_single_column, + Matrix::table(3, 0) + .with(Style::modern()) + .with(Highlight::new(Cell::new(0, 0), Border::default().left('*').top('x'))) + .with(Highlight::new(Rows::new(1..3), Border::default().left('n'))), + "┌xxx┐" + "* N │" + "├───┤" + "n 0 │" + "n───┤" + "n 1 │" + "├───┤" + "│ 2 │" + "└───┘" +); + +test_table!( + highlingt_several_times, + Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new(Frame, Border::filled('*'))) + .with(Highlight::new(Cell::new(1, 1), Border::filled('#'))) + .with(Highlight::new(Columns::single(3), Border::filled('x'))), + "**************************xxxxxxxxxxxx" + "* N │ column 0 │ column 1 x column 2 x" + "*───############──────────x──────────x" + "* 0 # 0-0 # 0-1 x 0-2 x" + "*───############──────────x──────────x" + "* 1 │ 1-0 │ 1-1 x 1-2 x" + "*───┼──────────┼──────────x──────────x" + "* 2 │ 2-0 │ 2-1 x 2-2 x" + "**************************xxxxxxxxxxxx" +); + +// @todo +// +// #[test] +// fn highlingt_empty_border() { +// let data = create_vector::<3, 3>(); +// let table = Table::new(&data) +// .with(Style::modern()) +// .with(Highlight::new(Frame, Border::empty())) +// .to_string(); + +// let expected = static_table!( +// " N │ column 0 │ column 1 │ column 2 " +// "─── ──────────" +// " 0 0-0 │ 0-1 0-2 " +// "─── ──────────┼────────── ──────────" +// " 1 1-0 │ 1-1 1-2 " +// "─── ──────────" +// " 2 │ 2-0 │ 2-1 │ 2-2 " +// ); + +// assert_eq!(table, expected); +// } + +#[test] +fn highlingt_complex_figures() { + macro_rules! test_highlight { + ($object:expr, $expected:expr,) => { + let border = Border::filled('+') + .corner_top_left('*') + .corner_top_right('#') + .corner_bottom_left('@') + .corner_bottom_right('.'); + + let table = Matrix::new(3, 3) + .with(Style::modern()) + .with(Highlight::new($object, border)) + .to_string(); + + assert_eq!(table, $expected); + }; + } + + test_highlight!( + Segment::all().not(Segment::new(2.., 1..3)), + static_table!( + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+───*+++++++++++++++++++++#──────────+" + "+ 1 + 1-0 │ 1-1 + 1-2 +" + "+───+──────────┼──────────+──────────+" + "+ 2 + 2-0 │ 2-1 + 2-2 +" + "@+++.──────────┴──────────@++++++++++." + ), + ); + + test_highlight!( + Segment::all() + .not(Segment::new(0..1, 1..3)) + .not(Columns::single(0)), + static_table!( + "┌───┬──────────┬──────────*++++++++++#" + "│ N │ column 0 │ column 1 + column 2 +" + "├───*+++++++++++++++++++++.──────────+" + "│ 0 + 0-0 │ 0-1 │ 0-2 +" + "├───+──────────┼──────────┼──────────+" + "│ 1 + 1-0 │ 1-1 │ 1-2 +" + "├───+──────────┼──────────┼──────────+" + "│ 2 + 2-0 │ 2-1 │ 2-2 +" + "└───@++++++++++++++++++++++++++++++++." + ), + ); + + test_highlight!( + Segment::all().not(Segment::new(0..1, 1..3)), + static_table!( + "*+++#──────────┬──────────*++++++++++#" + "+ N + column 0 │ column 1 + column 2 +" + "+───@+++++++++++++++++++++.──────────+" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "@++++++++++++++++++++++++++++++++++++." + ), + ); + + test_highlight!( + Segment::all().not(Segment::new(1..2, 1..3)), + static_table!( + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "+───*+++++++++++++++++++++#──────────+" + "+ 0 + 0-0 │ 0-1 + 0-2 +" + "+───@+++++++++++++++++++++.──────────+" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "@++++++++++++++++++++++++++++++++++++." + ), + ); + + test_highlight!( + Cell::new(0, 0) + .and(Cell::new(3, 3)) + .and(Cell::new(0, 3)) + .and(Cell::new(3, 0)), + static_table!( + "*+++#──────────┬──────────*++++++++++#" + "+ N + column 0 │ column 1 + column 2 +" + "@+++.──────────┼──────────@++++++++++." + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "*+++#──────────┼──────────*++++++++++#" + "+ 2 + 2-0 │ 2-1 + 2-2 +" + "@+++.──────────┴──────────@++++++++++." + ), + ); + + test_highlight!( + Rows::single(0).and(Rows::single(3)), + static_table!( + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "@++++++++++++++++++++++++++++++++++++." + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "*++++++++++++++++++++++++++++++++++++#" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "@++++++++++++++++++++++++++++++++++++." + ), + ); + + test_highlight!( + Columns::single(0).and(Columns::single(3)), + static_table!( + "*+++#──────────┬──────────*++++++++++#" + "+ N + column 0 │ column 1 + column 2 +" + "+───+──────────┼──────────+──────────+" + "+ 0 + 0-0 │ 0-1 + 0-2 +" + "+───+──────────┼──────────+──────────+" + "+ 1 + 1-0 │ 1-1 + 1-2 +" + "+───+──────────┼──────────+──────────+" + "+ 2 + 2-0 │ 2-1 + 2-2 +" + "@+++.──────────┴──────────@++++++++++." + ), + ); + + test_highlight!( + Segment::all().not(Cell::new(3, 1).and(Cell::new(3, 2))), + static_table!( + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+───┼──────────┼──────────┼──────────+" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "+───*+++++++++++++++++++++#──────────+" + "+ 2 + 2-0 │ 2-1 + 2-2 +" + "@+++.──────────┴──────────@++++++++++." + ), + ); + + test_highlight!( + Rows::single(0) + .and(Cell::new(1, 1).and(Cell::new(1, 2))) + .and(Cell::new(2, 3)), + static_table!( + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "@+++#──────────┼──────────*++++++++++." + "│ 0 + 0-0 │ 0-1 + 0-2 │" + "├───@+++++++++++++++++++++*++++++++++#" + "│ 1 │ 1-0 │ 1-1 + 1-2 +" + "├───┼──────────┼──────────@++++++++++." + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" + ), + ); + + test_highlight!( + Segment::all() + .not(Segment::new(2.., 0..3)) + .not(Cell::new(1, 0)), + static_table!( + "*++++++++++++++++++++++++++++++++++++#" + "+ N │ column 0 │ column 1 │ column 2 +" + "@+++#──────────┼──────────┼──────────+" + "│ 0 + 0-0 │ 0-1 │ 0-2 +" + "├───@+++++++++++++++++++++#──────────+" + "│ 1 │ 1-0 │ 1-1 + 1-2 +" + "├───┼──────────┼──────────+──────────+" + "│ 2 │ 2-0 │ 2-1 + 2-2 +" + "└───┴──────────┴──────────@++++++++++." + ), + ); + + test_highlight!( + Segment::all() + .not(Segment::new(..1, 1..)) + .not(Segment::new(1..2, 2..)) + .not(Cell::new(2, 3)), + static_table!( + "*+++#──────────┬──────────┬──────────┐" + "+ N + column 0 │ column 1 │ column 2 │" + "+───@++++++++++#──────────┼──────────┤" + "+ 0 │ 0-0 + 0-1 │ 0-2 │" + "+───┼──────────@++++++++++#──────────┤" + "+ 1 │ 1-0 │ 1-1 + 1-2 │" + "+───┼──────────┼──────────@++++++++++#" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "@++++++++++++++++++++++++++++++++++++." + ), + ); +} diff --git a/vendor/tabled/tests/settings/margin_test.rs b/vendor/tabled/tests/settings/margin_test.rs new file mode 100644 index 000000000..513287350 --- /dev/null +++ b/vendor/tabled/tests/settings/margin_test.rs @@ -0,0 +1,195 @@ +#![cfg(feature = "std")] + +use tabled::settings::{object::Cell, Border, Highlight, Margin, Modify, Span, Style, Width}; + +use crate::matrix::Matrix; +use testing_table::{is_lines_equal, static_table, test_table}; + +#[cfg(feature = "color")] +use ::{owo_colors::OwoColorize, std::convert::TryFrom, tabled::settings::Color}; + +test_table!( + margin_with_table_based_on_grid_borders, + Matrix::new(3, 3) + .with(Style::extended()) + .with(Highlight::new(Cell::new(0, 0), Border::filled('+'))) + .with(Highlight::new(Cell::new(1, 1), Border::filled('*'))) + .with(Margin::new(1, 2, 1, 2).fill('>', '<', 'V', '^')), + "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" + ">+++++══════════╦══════════╦══════════╗<<" + ">+ N + column 0 ║ column 1 ║ column 2 ║<<" + ">++++************══════════╬══════════╣<<" + ">║ 0 * 0-0 * 0-1 ║ 0-2 ║<<" + ">╠═══************══════════╬══════════╣<<" + ">║ 1 ║ 1-0 ║ 1-1 ║ 1-2 ║<<" + ">╠═══╬══════════╬══════════╬══════════╣<<" + ">║ 2 ║ 2-0 ║ 2-1 ║ 2-2 ║<<" + ">╚═══╩══════════╩══════════╩══════════╝<<" + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" +); + +test_table!( + margin_without_table_based_on_grid_borders, + Matrix::new(3, 3) + .insert((3, 2), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new(Cell::new(3, 2)).with(Span::column(2))) + .with(Margin::new(1, 1, 1, 1).fill('>', '<', 'V', '^')), + "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" + "> N | column 0 | column 1 | column 2 <" + ">---+----------+----------+----------<" + "> 0 | 0-0 | 0-1 | 0-2 <" + "> 1 | 1-0 | 1-1 | 1-2 <" + "> 2 | 2-0 | https:// <" + "> | | www <" + "> | | . <" + "> | | redhat <" + "> | | .com <" + "> | | /en <" + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" +); + +test_table!( + table_with_empty_margin, + Matrix::new(3, 3) + .insert((3, 2), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new(Cell::new(3, 2)).with(Span::column(2))) + .with(Margin::new(0, 0, 0, 0).fill('>', '<', 'V', '^')), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | https:// " + " | | www " + " | | . " + " | | redhat " + " | | .com " + " | | /en " +); + +#[test] +fn table_with_margin_and_min_width() { + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Cell::new(1, 1)).with(Span::column(2))) + .with(Margin::new(1, 1, 1, 1).fill('>', '<', 'V', '^')) + .with(Width::truncate(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "VVVVVVVVVVVVVVVVVVVV" + "> | co | co | col <" + ">--+----+----+-----<" + "> | 0-0 | 0-2 <" + "> | 1- | 1- | 1-2 <" + "> | 2- | 2- | 2-2 <" + "^^^^^^^^^^^^^^^^^^^^" + ) + ); + assert!(is_lines_equal(&table, 20)); +} + +#[test] +fn table_with_margin_and_max_width() { + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Cell::new(1, 1)).with(Span::column(2))) + .with(Margin::new(1, 1, 1, 1).fill('>', '<', 'V', '^')) + .with(Width::increase(50)) + .to_string(); + + assert_eq!(papergrid::util::string::string_width_multiline(&table), 50); + assert_eq!( + table, + static_table!( + "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV" + "> N | column 0 | column 1 | column 2 <" + ">------+-------------+-------------+-------------<" + "> 0 | 0-0 | 0-2 <" + "> 1 | 1-0 | 1-1 | 1-2 <" + "> 2 | 2-0 | 2-1 | 2-2 <" + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" + ) + ); +} + +#[test] +#[ignore = "It's not yet clear what to do with such spans"] +fn table_0_spanned_with_width() { + let table = Matrix::table(0, 0) + .with(Modify::new(Cell::new(0, 0)).with(Span::column(0))) + .with(Width::increase(50)) + .to_string(); + + assert_eq!(table, "++\n|\n++\n"); + + let table = Matrix::table(0, 0) + .with(Modify::new(Cell::new(0, 0)).with(Span::column(0))) + .with(Width::truncate(50)) + .to_string(); + + assert_eq!(table, "++\n|\n++\n"); +} + +#[test] +fn margin_color_test_not_colored_feature() { + use tabled::settings::Color; + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Margin::new(2, 2, 2, 2).fill('>', '<', 'V', '^').colorize( + Color::BG_GREEN, + Color::BG_YELLOW, + Color::BG_RED, + Color::BG_BLUE, + )) + .to_string(); + + assert_eq!( + table, + static_table!( + "\u{1b}[41mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[49m" + "\u{1b}[41mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[49m" + "\u{1b}[42m>>\u{1b}[49m N | column 0 | column 1 | column 2 \u{1b}[43m<<\u{1b}[49m" + "\u{1b}[42m>>\u{1b}[49m---+----------+----------+----------\u{1b}[43m<<\u{1b}[49m" + "\u{1b}[42m>>\u{1b}[49m 0 | 0-0 | 0-1 | 0-2 \u{1b}[43m<<\u{1b}[49m" + "\u{1b}[42m>>\u{1b}[49m 1 | 1-0 | 1-1 | 1-2 \u{1b}[43m<<\u{1b}[49m" + "\u{1b}[42m>>\u{1b}[49m 2 | 2-0 | 2-1 | 2-2 \u{1b}[43m<<\u{1b}[49m" + "\u{1b}[44m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[49m" + "\u{1b}[44m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[49m" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn margin_color_test() { + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Margin::new(2, 2, 2, 2).fill('>', '<', 'V', '^').colorize( + Color::try_from(" ".red().bold().to_string()).unwrap(), + Color::try_from(" ".green().to_string()).unwrap(), + Color::try_from(" ".on_blue().red().bold().to_string()).unwrap(), + Color::try_from(" ".on_yellow().blue().to_string()).unwrap(), + )) + .to_string(); + + assert_eq!( + table, + static_table!( + "\u{1b}[1m\u{1b}[31m\u{1b}[44mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[22m\u{1b}[39m\u{1b}[49m" + "\u{1b}[1m\u{1b}[31m\u{1b}[44mVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\u{1b}[22m\u{1b}[39m\u{1b}[49m" + "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m N | column 0 | column 1 | column 2 \u{1b}[32m<<\u{1b}[39m" + "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m---+----------+----------+----------\u{1b}[32m<<\u{1b}[39m" + "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m 0 | 0-0 | 0-1 | 0-2 \u{1b}[32m<<\u{1b}[39m" + "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m 1 | 1-0 | 1-1 | 1-2 \u{1b}[32m<<\u{1b}[39m" + "\u{1b}[1m\u{1b}[31m>>\u{1b}[22m\u{1b}[39m 2 | 2-0 | 2-1 | 2-2 \u{1b}[32m<<\u{1b}[39m" + "\u{1b}[34m\u{1b}[43m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[39m\u{1b}[49m" + "\u{1b}[34m\u{1b}[43m^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\u{1b}[39m\u{1b}[49m" + ) + ); +} diff --git a/vendor/tabled/tests/settings/merge_test.rs b/vendor/tabled/tests/settings/merge_test.rs new file mode 100644 index 000000000..cbc119aa9 --- /dev/null +++ b/vendor/tabled/tests/settings/merge_test.rs @@ -0,0 +1,196 @@ +#![cfg(feature = "std")] + +use tabled::{settings::merge::Merge, Table}; + +use testing_table::test_table; + +test_table!( + merge_horizontal, + Table::new([[0, 1, 1], [1, 1, 2], [1, 1, 1]]).with(Merge::horizontal()), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 0 | 1 |" + "+---+---+---+" + "| 1 | 2 |" + "+---+---+---+" + "| 1 |" + "+---+---+---+" +); + +test_table!( + merge_horizontal_with_no_duplicates, + Table::new([[0, 1, 2], [0, 1, 2], [0, 1, 2]]).with(Merge::horizontal()), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" +); + +test_table!( + merge_horizontal_empty, + Table::new([[0usize; 0]]).with(Merge::horizontal()), + "" +); + +test_table!( + merge_vertical_0, + Table::new([[0, 3, 5], [0, 3, 3], [0, 2, 3]]).with(Merge::vertical()), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+ +---+---+" + "| | 3 | 5 |" + "+ + +---+" + "+ +---+ 3 +" + "| | 2 | |" + "+---+---+---+" +); + +test_table!( + merge_vertical_1, + Table::new([[0, 3, 2], [0, 3, 3], [0, 2, 3]]).with(Merge::vertical()), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+ +---+ +" + "+ + 3 +---+" + "+ +---+ 3 +" + "| | 2 | |" + "+---+---+---+" +); + +test_table!( + merge_vertical_with_no_duplicates, + Table::new([[5; 3], [15; 3], [115; 3]]).with(Merge::vertical()), + "+-----+-----+-----+" + "| 0 | 1 | 2 |" + "+-----+-----+-----+" + "| 5 | 5 | 5 |" + "+-----+-----+-----+" + "| 15 | 15 | 15 |" + "+-----+-----+-----+" + "| 115 | 115 | 115 |" + "+-----+-----+-----+" +); + +test_table!( + merge_vertical_empty, + Table::new([[0usize; 0]]).with(Merge::vertical()), + "" +); + +test_table!( + merge_horizontal_and_vertical_0, + Table::new([[3, 3, 5], [3, 7, 8], [9, 10, 11]]).with(Merge::horizontal()).with(Merge::vertical()), + "+---+----+----+" + "| 0 | 1 | 2 |" + "+---+----+----+" + "| 3 | 5 |" + "+---+----+----+" + "| 3 | 7 | 8 |" + "+---+----+----+" + "| 9 | 10 | 11 |" + "+---+----+----+" +); + +test_table!( + merge_horizontal_and_vertical_1, + Table::new([[0, 1, 1], [1, 1, 2], [1, 1, 1]]).with(Merge::horizontal()).with(Merge::vertical()), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+ +---+---+" + "| | 1 |" + "+---+---+---+" + "| 1 | 2 |" + "+---+---+---+" + "| 1 |" + "+---+---+---+" +); + +test_table!( + merge_horizontal_and_vertical_2, + Table::new([[3, 4, 5], [3, 3, 8], [3, 10, 11]]).with(Merge::horizontal()).with(Merge::vertical()), + "+---+----+----+" + "| 0 | 1 | 2 |" + "+---+----+----+" + "| 3 | 4 | 5 |" + "+---+----+----+" + "| 3 | 8 |" + "+---+----+----+" + "| 3 | 10 | 11 |" + "+---+----+----+" +); + +test_table!( + merge_horizontal_and_vertical_3, + Table::new([[3, 4, 5], [3, 3, 8], [3, 10, 11]]).with(Merge::vertical()).with(Merge::horizontal()), + "+---+----+----+" + "| 0 | 1 | 2 |" + "+---+----+----+" + "| 3 | 4 | 5 |" + "+ +----+----+" + "| | 3 | 8 |" + "+ +----+----+" + "| | 10 | 11 |" + "+---+----+----+" +); + +test_table!( + merge_horizontal_and_vertical_4, + Table::new([[3, 4, 5], [4, 4, 8], [3, 4, 11]]).with(Merge::vertical()).with(Merge::horizontal()), + "+---+---+----+" + "| 0 | 1 | 2 |" + "+---+---+----+" + "| 3 | 4 | 5 |" + "+---+ +----+" + "| 4 | | 8 |" + "+---+ +----+" + "| 3 | | 11 |" + "+---+---+----+" +); + +test_table!( + merge_horizontal_and_vertical_5, + Table::new([[3, 4, 4], [4, 4, 8], [3, 4, 11]]).with(Merge::vertical()).with(Merge::horizontal()), + "+---+---+----+" + "| 0 | 1 | 2 |" + "+---+---+----+" + "| 3 | 4 | 4 |" + "+---+ +----+" + "| 4 | | 8 |" + "+---+ +----+" + "| 3 | | 11 |" + "+---+---+----+" +); + +test_table!( + merge_horizontal_and_vertical_6, + Table::new([[4, 4, 4], [5, 4, 8], [3, 4, 11]]).with(Merge::vertical()).with(Merge::horizontal()), + "+---+---+----+" + "| 0 | 1 | 2 |" + "+---+---+----+" + "| 4 | 4 | 4 |" + "+---+ +----+" + "| 5 | | 8 |" + "+---+ +----+" + "| 3 | | 11 |" + "+---+---+----+" +); + +test_table!( + merge_horizontal_and_vertical_7, + Table::new([[0, 0, 0], [0, 0, 1], [2, 0, 0]]).with(Merge::horizontal()).with(Merge::vertical()), + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+---+" + "| 0 |" + "+---+---+---+" + "| 0 | 1 |" + "+---+---+---+" + "| 2 | 0 |" + "+---+---+---+" +); diff --git a/vendor/tabled/tests/settings/mod.rs b/vendor/tabled/tests/settings/mod.rs new file mode 100644 index 000000000..da76a5e14 --- /dev/null +++ b/vendor/tabled/tests/settings/mod.rs @@ -0,0 +1,23 @@ +mod alignment_test; +mod color_test; +mod colorization; +mod column_names_test; +mod concat_test; +mod disable_test; +mod duplicate_test; +mod extract_test; +mod format_test; +mod formatting_test; +mod height_test; +mod highlingt_test; +mod margin_test; +mod merge_test; +mod padding_test; +mod panel_test; +mod render_settings; +mod rotate_test; +mod shadow_test; +mod span_test; +mod split_test; +mod style_test; +mod width_test; diff --git a/vendor/tabled/tests/settings/padding_test.rs b/vendor/tabled/tests/settings/padding_test.rs new file mode 100644 index 000000000..9a3edb4aa --- /dev/null +++ b/vendor/tabled/tests/settings/padding_test.rs @@ -0,0 +1,138 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + object::{Rows, Segment}, + Alignment, Modify, Padding, Style, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +#[cfg(feature = "color")] +use ::{owo_colors::OwoColorize, std::convert::TryFrom, tabled::settings::Color}; + +test_table!( + padding, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Rows::new(1..)).with(Padding::new(1, 1, 0, 2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " | | | " + " | | | " + " 1 | 1-0 | 1-1 | 1-2 " + " | | | " + " | | | " + " 2 | 2-0 | 2-1 | 2-2 " + " | | | " + " | | | " +); + +test_table!( + padding_with_set_characters, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Padding::new(1, 2, 1, 1).fill('>', '<', 'V', '^'))), + "VVVV|VVVVVVVVVVV|VVVVVVVVVVV|VVVVVVVVVVV" + ">N<<|>column 0<<|>column 1<<|>column 2<<" + "^^^^|^^^^^^^^^^^|^^^^^^^^^^^|^^^^^^^^^^^" + "----+-----------+-----------+-----------" + "VVVV|VVVVVVVVVVV|VVVVVVVVVVV|VVVVVVVVVVV" + ">0<<|> 0-0 <<|> 0-1 <<|> 0-2 <<" + "^^^^|^^^^^^^^^^^|^^^^^^^^^^^|^^^^^^^^^^^" + "VVVV|VVVVVVVVVVV|VVVVVVVVVVV|VVVVVVVVVVV" + ">1<<|> 1-0 <<|> 1-1 <<|> 1-2 <<" + "^^^^|^^^^^^^^^^^|^^^^^^^^^^^|^^^^^^^^^^^" + "VVVV|VVVVVVVVVVV|VVVVVVVVVVV|VVVVVVVVVVV" + ">2<<|> 2-0 <<|> 2-1 <<|> 2-2 <<" + "^^^^|^^^^^^^^^^^|^^^^^^^^^^^|^^^^^^^^^^^" +); + +test_table!( + padding_with_set_characters_and_zero_ident, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Padding::zero().fill('>', '<', '^', 'V'))), + "N|column 0|column 1|column 2" + "-+--------+--------+--------" + "0| 0-0 | 0-1 | 0-2 " + "1| 1-0 | 1-1 | 1-2 " + "2| 2-0 | 2-1 | 2-2 " +); + +test_table!( + padding_multiline, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Rows::new(1..)).with(Padding::new(1, 1, 1, 1))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " | | | " + " 0 | 0-0 | 0-1 | 0-2 " + " | | | " + " | | | " + " 1 | 1-0 | 1-1 | 1-2 " + " | | | " + " | | | " + " 2 | 2-0 | 2-1 | 2-2 " + " | | | " +); + +test_table!( + padding_multiline_with_vertical_alignment, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::center()).with(Alignment::center_vertical())) + .with(Modify::new(Rows::new(1..)).with(Padding::new(1, 1, 1, 1))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " | | | " + " 0 | 0-0 | 0-1 | 0-2 " + " | | | " + " | | | " + " 1 | 1-0 | 1-1 | 1-2 " + " | | | " + " | | | " + " 2 | 2-0 | 2-1 | 2-2 " + " | | | " +); + +#[cfg(feature = "color")] +test_table!( + padding_color, + { + let padding = Padding::new(2, 2, 2, 2).colorize( + Color::try_from(' '.on_yellow().to_string()).unwrap(), + Color::try_from(' '.on_blue().to_string()).unwrap(), + Color::try_from(' '.on_red().to_string()).unwrap(), + Color::try_from(' '.on_green().to_string()).unwrap(), + ); + + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Rows::new(1..)).with(padding)) + }, + " N | column 0 | column 1 | column 2 \n-----+----------+----------+----------\n\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\n\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\n\u{1b}[43m \u{1b}[49m0\u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 0-0 \u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 0-1 \u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 0-2 \u{1b}[44m \u{1b}[49m\n\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m\n\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m\n\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\n\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\n\u{1b}[43m \u{1b}[49m1\u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 1-0 \u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 1-1 \u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 1-2 \u{1b}[44m \u{1b}[49m\n\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m\n\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m\n\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\n\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m|\u{1b}[41m \u{1b}[49m\n\u{1b}[43m \u{1b}[49m2\u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 2-0 \u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 2-1 \u{1b}[44m \u{1b}[49m|\u{1b}[43m \u{1b}[49m 2-2 \u{1b}[44m \u{1b}[49m\n\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m\n\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m|\u{1b}[42m \u{1b}[49m" +); + +test_table!( + padding_table, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Padding::new(1, 1, 0, 2)), + " N | column 0 | column 1 | column 2 " + " | | | " + " | | | " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " | | | " + " | | | " + " 1 | 1-0 | 1-1 | 1-2 " + " | | | " + " | | | " + " 2 | 2-0 | 2-1 | 2-2 " + " | | | " + " | | | " +); diff --git a/vendor/tabled/tests/settings/panel_test.rs b/vendor/tabled/tests/settings/panel_test.rs new file mode 100644 index 000000000..613fe1d0e --- /dev/null +++ b/vendor/tabled/tests/settings/panel_test.rs @@ -0,0 +1,337 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + object::{Cell, Object, Rows, Segment}, + style::{BorderSpanCorrection, HorizontalLine}, + Alignment, Border, Highlight, Modify, Panel, Span, Style, Width, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +test_table!( + panel_has_no_style_by_default, + Matrix::new(3, 3).with(Style::psql()).with(Panel::horizontal(0,"Linux Distributions")), + " Linux Distributions " + "---+----------+----------+----------" + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + highlight_panel_0, + Matrix::new(3, 3) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Style::psql()) + .with(Highlight::new(Cell::new(0, 0), Border::filled('#'))), + "##### " + "# Linux Distributions " + "#####----------+----------+----------" + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + highlight_panel_1, + Matrix::new(3, 3) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Style::psql()) + .with(Highlight::new(Cell::new(0, 0), Border::filled('#'))) + .with(Highlight::new(Cell::new(0, 1), Border::filled('#'))) + .with(Highlight::new(Cell::new(0, 2), Border::filled('#'))) + .with(Highlight::new(Cell::new(0, 3), Border::filled('#'))), + "######################################" + "# Linux Distributions #" + "######################################" + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + top_panel, + Matrix::new(3, 3) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::psql()), + " Linux Distributions " + "---+----------+----------+----------" + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + bottom_panel, + Matrix::new(3, 3) + .with(Panel::horizontal(4,"Linux Distributions")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::psql()), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + " Linux Distributions " +); + +test_table!( + inner_panel, + Matrix::new(3, 3) + .with(Panel::horizontal(2,"Linux Distributions")) + .with(Modify::new(Rows::new(2..)).with(Alignment::center())) + .with(Style::psql()), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " Linux Distributions " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + header, + Matrix::new(3, 3) + .with(Panel::header("Linux Distributions")) + .with(Style::psql()) + .with(Modify::new(Rows::new(0..1)).with(Alignment::center())), + " Linux Distributions " + "---+----------+----------+----------" + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + footer, + Matrix::new(3, 3) + .with(Panel::header("Linux Distributions")) + .with(Panel::footer("The end")) + .with(Style::psql()) + .with(Modify::new(Rows::first().and(Rows::last())).with(Alignment::center())), + " Linux Distributions " + "---+----------+----------+----------" + " N | column 0 | column 1 | column 2 " + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + " The end " +); + +test_table!( + panel_style_uses_most_left_and_right_cell_styles, + Matrix::iter([(0, 1)]).with(Panel::horizontal(0,"Numbers")).with(Style::modern()), + "┌─────┬─────┐" + "│ Numbers │" + "├─────┼─────┤" + "│ i32 │ i32 │" + "├─────┼─────┤" + "│ 0 │ 1 │" + "└─────┴─────┘" +); + +test_table!( + panel_style_change, + Matrix::iter([(0, 1)]) + .with(Panel::horizontal(0,"Numbers")) + .with(Style::modern().intersection_top('─').horizontals([HorizontalLine::new(1, Style::modern().get_horizontal()).intersection(Some('┬'))])) + .with(Modify::new(Cell::new(0, 0)).with(Alignment::center())), + "┌───────────┐" + "│ Numbers │" + "├─────┬─────┤" + "│ i32 │ i32 │" + "├─────┼─────┤" + "│ 0 │ 1 │" + "└─────┴─────┘" +); + +test_table!( + panel_style_uses_most_left_and_right_cell_styles_correct, + Matrix::iter([(0, 1)]) + .with(Panel::horizontal(0,"Numbers")) + .with(Style::modern()) + .with(BorderSpanCorrection), + "┌───────────┐" + "│ Numbers │" + "├─────┬─────┤" + "│ i32 │ i32 │" + "├─────┼─────┤" + "│ 0 │ 1 │" + "└─────┴─────┘" +); + +test_table!( + panel_style_change_correct, + Matrix::iter([(0, 1)]) + .with(Panel::horizontal(0,"Numbers")) + .with(Style::modern().intersection_top('─').horizontals([HorizontalLine::new(1, Style::modern().get_horizontal()).intersection(Some('┬'))])) + .with(BorderSpanCorrection) + .with(Modify::new(Cell::new(0, 0)).with(Alignment::center())), + "┌───────────┐" + "│ Numbers │" + "├───────────┤" // it's different because we use a top_intersection char by default when making style for `Panel`s. + "│ i32 │ i32 │" + "├─────┼─────┤" + "│ 0 │ 1 │" + "└─────┴─────┘" +); + +test_table!( + panel_in_single_column, + #[allow(clippy::needless_borrow)] + Matrix::iter(&[(0)]).with(Panel::horizontal(0,"Numbers")).with(Style::modern()), + "┌─────────┐" + "│ Numbers │" + "├─────────┤" + "│ i32 │" + "├─────────┤" + "│ 0 │" + "└─────────┘" +); + +test_table!( + panel_vertical_0, + Matrix::new(3, 3).with(Style::psql()).with(Panel::vertical(0,"Linux Distributions")), + " Linux Distributions | N | column 0 | column 1 | column 2 " + " +---+----------+----------+----------" + " | 0 | 0-0 | 0-1 | 0-2 " + " | 1 | 1-0 | 1-1 | 1-2 " + " | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + panel_vertical_1, + Matrix::new(3, 3).with(Style::psql()).with(Panel::vertical(1,"Linux Distributions")), + " N | Linux Distributions | column 0 | column 1 | column 2 " + "---+ +----------+----------+----------" + " 0 | | 0-0 | 0-1 | 0-2 " + " 1 | | 1-0 | 1-1 | 1-2 " + " 2 | | 2-0 | 2-1 | 2-2 " +); + +test_table!( + panel_vertical_2, + Matrix::new(3, 3).with(Style::psql()).with(Panel::vertical(4,"Linux Distributions")), + " N | column 0 | column 1 | column 2 | Linux Distributions " + "---+----------+----------+----------+ " + " 0 | 0-0 | 0-1 | 0-2 | " + " 1 | 1-0 | 1-1 | 1-2 | " + " 2 | 2-0 | 2-1 | 2-2 | " +); + +test_table!( + panel_vertical_0_wrap, + Matrix::new(3, 3).with(Style::psql()).with(Panel::vertical(0,"Linux Distributions")).with(Modify::new(Cell::new(0, 0)).with(Width::wrap(3))), + " Lin | N | column 0 | column 1 | column 2 " + " ux | | | | " + " Dis | | | | " + " tri +---+----------+----------+----------" + " but | 0 | 0-0 | 0-1 | 0-2 " + " ion | 1 | 1-0 | 1-1 | 1-2 " + " s | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + panel_vertical_0_wrap_0, + Matrix::new(3, 3).with(Style::psql()).with(Panel::vertical(0,"Linux Distributions")).with(Modify::new(Cell::new(0, 0)).with(Width::wrap(0))), + " | N | column 0 | column 1 | column 2 " + " +---+----------+----------+----------" + " | 0 | 0-0 | 0-1 | 0-2 " + " | 1 | 1-0 | 1-1 | 1-2 " + " | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + panel_vertical_0_wrap_100, + Matrix::new(3, 3).with(Style::psql()).with(Panel::vertical(0,"Linux Distributions")).with(Modify::new(Cell::new(0, 0)).with(Width::wrap(100))), + " Linux Distributions | N | column 0 | column 1 | column 2 " + " +---+----------+----------+----------" + " | 0 | 0-0 | 0-1 | 0-2 " + " | 1 | 1-0 | 1-1 | 1-2 " + " | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + panel_horizontal_set_0, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Panel::vertical(0,"asd")), + " asd | Linux Distributions " + " +---+----------+----------+----------" + " | N | column 0 | column 1 | column 2 " + " | 0 | 0-0 | 0-1 | 0-2 " + " | 1 | 1-0 | 1-1 | 1-2 " + " | 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + panel_horizontal_set_1, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Panel::vertical(0,"asd")) + .with(Panel::vertical(5,"asd")) + , + " asd | Linux Distributions | asd " + " +---+----------+----------+----------+ " + " | N | column 0 | column 1 | column 2 | " + " | 0 | 0-0 | 0-1 | 0-2 | " + " | 1 | 1-0 | 1-1 | 1-2 | " + " | 2 | 2-0 | 2-1 | 2-2 | " +); + +test_table!( + ignore_col_span_intersect_with_other_span, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Panel::vertical(0,"asd")) + .with(Panel::vertical(5,"zxc")) + .with(Modify::new((1, 3)).with(Span::column(3)).with("wwwww")), + " asd | Linux Distributions | zxc " + " +---+----------+-------+----------+ " + " | N | column 0 | wwwww | column 2 | " + " | 0 | 0-0 | 0-1 | 0-2 | " + " | 1 | 1-0 | 1-1 | 1-2 | " + " | 2 | 2-0 | 2-1 | 2-2 | " +); + +test_table!( + panel_horizontal_x_2, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Panel::horizontal(0,"Linux Distributions")) + .with(Panel::vertical(0,"asd")) + .with(Panel::vertical(5,"zxc")) + .with(Modify::new((1, 3)).with(Span::column(2)).with("wwwww")), + " asd | Linux Distributions | zxc " + " +---+----------+-----+-----+ " + " | N | column 0 | wwwww | " + " | 0 | 0-0 | 0-1 | 0-2 | " + " | 1 | 1-0 | 1-1 | 1-2 | " + " | 2 | 2-0 | 2-1 | 2-2 | " +); + +test_table!( + ignore_row_span_intersect_with_other_span, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Panel::horizontal(2,"Linux Distributions")) + .with(Panel::vertical(0,"asd")) + .with(Panel::vertical(5,"zxc")) + .with(Modify::new((0, 3)).with(Span::row(4)).with("xxxxx")), + " asd | N | column 0 | xxxxx | column 2 | zxc " + " +---+----------+-------+----------+ " + " | 0 | 0-0 | 0-1 | 0-2 | " + " | Linux Distributions | " + " | 1 | 1-0 | 1-1 | 1-2 | " + " | 2 | 2-0 | 2-1 | 2-2 | " +); diff --git a/vendor/tabled/tests/settings/render_settings.rs b/vendor/tabled/tests/settings/render_settings.rs new file mode 100644 index 000000000..f25a59a67 --- /dev/null +++ b/vendor/tabled/tests/settings/render_settings.rs @@ -0,0 +1,292 @@ +#![cfg(feature = "std")] + +use tabled::settings::{ + formatting::{AlignmentStrategy, TabSize, TrimStrategy}, + object::Segment, + Alignment, Modify, Span, Style, +}; + +use crate::matrix::{Matrix, MatrixList}; +use testing_table::test_table; + +#[cfg(feature = "color")] +use owo_colors::OwoColorize; + +test_table!( + alignment_per_line, + Matrix::iter(multiline_data1()) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::right()).with(AlignmentStrategy::PerLine)), + " N | column 0 | column 1 | column 2 " + "-----------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " asd | 1-0 | 1-1 | 1-2 " + " 21213123 | | | " + " | | | " + " asdasd | | | " + " | | | " + " | | | " + " 2 | 2-0 | https:// | 2-2 " + " | | www | " + " | | . | " + " | | redhat | " + " | | .com | " + " | | /en | " +); + +test_table!( + alignment_per_line_with_trim_0, + Matrix::iter(multiline_data1()) + .with(Style::psql()) + .with(Alignment::right()) + .with(AlignmentStrategy::PerLine) + .with(TrimStrategy::Horizontal), + " N | column 0 | column 1 | column 2 " + "-----------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " asd | 1-0 | 1-1 | 1-2 " + " 21213123 | | | " + " | | | " + " asdasd | | | " + " | | | " + " | | | " + " 2 | 2-0 | https:// | 2-2 " + " | | www | " + " | | . | " + " | | redhat | " + " | | .com | " + " | | /en | " +); + +test_table!( + alignment_per_line_with_trim_1, + Matrix::iter(multiline_data2()) + .with(Style::psql()) + .with(Modify::new(Segment::all()) + .with(Alignment::center_vertical()) + .with(Alignment::left()) + .with(AlignmentStrategy::PerLine) + .with(TrimStrategy::Both)), + " N | column 0 | column 1 | column 2 " + "-------------------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " | | | " + " | | | " + " | | | " + " asd | 1-0 | 1-1 | 1-2 " + " 21213123 asdasd | | | " + " | | | " + " | | | " + " | | | " + " | | https:// | " + " | | www | " + " 2 | 2-0 | . | 2-2 " + " | | redhat | " + " | | .com | " + " | | /en | " +); + +test_table!( + tab_isnot_handled_by_default_test, + Matrix::iter(tab_data1()).with(Style::psql()), + " N | column 0 | column 1 | column 2 " + "--------------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 123\t123\tasdasd | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | htt\tps:// | 2-2 " + " | | www | " + " | | . | " + " | | red\that | " + " | | .c\tom | " + " | | /en | " +); + +test_table!( + tab_size_test_0, + Matrix::iter(tab_data1()).with(Style::psql()).with(TabSize::new(4)), + " N | column 0 | column 1 | column 2 " + "----------------------+----------+--------------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 123 123 asdasd | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | htt ps:// | 2-2 " + " | | www | " + " | | . | " + " | | red hat | " + " | | .c om | " + " | | /en | " +); + +test_table!( + tab_size_test_1, + Matrix::iter(tab_data1()).with(Style::psql()).with(Modify::new(Segment::all()).with(Alignment::right())).with(TabSize::new(2)), + " N | column 0 | column 1 | column 2 " + "------------------+----------+------------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 123 123 asdasd | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | htt ps:// | 2-2 " + " | | www | " + " | | . | " + " | | red hat | " + " | | .c om | " + " | | /en | " +); + +test_table!( + tab_size_test_2, + Matrix::iter(tab_data1()).with(Style::psql()).with(Modify::new(Segment::all()).with(Alignment::right())).with(TabSize::new(0)), + " N | column 0 | column 1 | column 2 " + "--------------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 123123asdasd | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | https:// | 2-2 " + " | | www | " + " | | . | " + " | | redhat | " + " | | .com | " + " | | /en | " +); + +test_table!( + tab_size_span_test, + Matrix::iter(tab_data2()) + .with(TabSize::new(4)) + .with(Style::psql()) + .with(Modify::new((0, 0)).with(Span::column(3))) + .with(Modify::new((1, 0)).with(Span::column(2))) + .with(Modify::new((2, 1)).with(Span::column(2))), + " N | column 2 " + "----------------------+-----+--------------+----------" + " H ello World | 0-1 | 0-2 " + " 123 123 asdasd | 1-0 | 1-2 " + " 2 | 2-0 | htt ps:// | 2-2 " + " | | www | " + " | | . | " + " | | red hat | " + " | | .c om | " + " | | /en | " +); + +test_table!( + test_top_alignment_and_vertical_trim_1, + Matrix::iter(vec![" \n\n\n Hello World"]) + .with(Style::modern()) + .with(Modify::new(Segment::all()).with(Alignment::top()).with(TrimStrategy::Vertical)), + "┌─────────────────┐" + "│ &str │" + "├─────────────────┤" + "│ Hello World │" + "│ │" + "│ │" + "│ │" + "└─────────────────┘" +); + +#[cfg(feature = "color")] +test_table!( + trim_colored_string_test_2, + Matrix::iter(colored_data()) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::right()).with(TrimStrategy::None)), + " N | column 0 | column 1 | column 2 " + "-----------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " \u{1b}[31masd\u{1b}[39m | 1-0 | 1-1 | 1-2 " + " \u{1b}[31m21213123\u{1b}[39m | | | " + " | | | " + " \u{1b}[31m asdasd\u{1b}[39m | | | " + " | | | " + " \u{1b}[31m\u{1b}[39m | | | " + " 2 | 2-0 | \u{1b}[44mhttps://\u{1b}[49m | 2-2 " + " | | \u{1b}[44mwww\u{1b}[49m | " + " | | \u{1b}[44m.\u{1b}[49m | " + " | | \u{1b}[44mredhat\u{1b}[49m | " + " | | \u{1b}[44m.com\u{1b}[49m | " + " | | \u{1b}[44m/en\u{1b}[49m | " +); + +#[cfg(feature = "color")] +test_table!( + trim_colored_string_test_1, + Matrix::iter(colored_data()) + .with(Style::psql()) + .with( + Modify::new(Segment::all()) + .with(Alignment::right()) + .with(TrimStrategy::Horizontal) + .with(AlignmentStrategy::PerLine), + ), + " N | column 0 | column 1 | column 2 " + "-----------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " \u{1b}[31masd\u{1b}[39m | 1-0 | 1-1 | 1-2 " + " \u{1b}[31m21213123\u{1b}[39m | | | " + " | | | " + " \u{1b}[31masdasd\u{1b}[39m | | | " + " | | | " + " \u{1b}[31m\u{1b}[39m | | | " + " 2 | 2-0 | \u{1b}[44mhttps://\u{1b}[49m | 2-2 " + " | | \u{1b}[44mwww\u{1b}[49m | " + " | | \u{1b}[44m.\u{1b}[49m | " + " | | \u{1b}[44mredhat\u{1b}[49m | " + " | | \u{1b}[44m.com\u{1b}[49m | " + " | | \u{1b}[44m/en\u{1b}[49m | " +); +#[cfg(feature = "color")] +test_table!( + trim_colored_string_test_0, + Matrix::iter(colored_data()) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::right()).with(TrimStrategy::Horizontal)), + " N | column 0 | column 1 | column 2 " + "-----------+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " \u{1b}[31masd\u{1b}[39m | 1-0 | 1-1 | 1-2 " + " \u{1b}[31m21213123\u{1b}[39m | | | " + " | | | " + " \u{1b}[31masdasd\u{1b}[39m | | | " + " | | | " + " \u{1b}[31m\u{1b}[39m | | | " + " 2 | 2-0 | \u{1b}[44mhttps://\u{1b}[49m | 2-2 " + " | | \u{1b}[44mwww\u{1b}[49m | " + " | | \u{1b}[44m.\u{1b}[49m | " + " | | \u{1b}[44mredhat\u{1b}[49m | " + " | | \u{1b}[44m.com\u{1b}[49m | " + " | | \u{1b}[44m/en\u{1b}[49m | " +); + +fn multiline_data1() -> Vec<MatrixList<3, true>> { + let mut data = Matrix::list::<3, 3>(); + data[1][0] = String::from("asd\n21213123\n\n asdasd\n\n"); + data[2][2] = String::from("https://\nwww\n.\nredhat\n.com\n/en"); + data +} + +fn multiline_data2() -> Vec<MatrixList<3, true>> { + let mut data = Matrix::list::<3, 3>(); + data[1][0] = String::from("\n\n\nasd\n21213123 asdasd\n\n\n"); + data[2][2] = String::from("https://\nwww\n.\nredhat\n.com\n/en"); + data +} + +fn tab_data1() -> Vec<MatrixList<3, true>> { + let mut data = Matrix::list::<3, 3>(); + data[1][0] = String::from("123\t123\tasdasd"); + data[2][2] = String::from("htt\tps://\nwww\n.\nred\that\n.c\tom\n/en"); + data +} + +fn tab_data2() -> Vec<MatrixList<3, true>> { + let mut data = Matrix::list::<3, 3>(); + data[0][0] = String::from("\tH\t\tello\tWorld"); + data[1][0] = String::from("123\t123\tasdasd"); + data[2][2] = String::from("htt\tps://\nwww\n.\nred\that\n.c\tom\n/en"); + data +} + +#[cfg(feature = "color")] +fn colored_data() -> Vec<MatrixList<3, true>> { + let mut data = Matrix::list::<3, 3>(); + data[1][0] = "asd\n21213123\n\n asdasd\n\n".red().to_string(); + data[2][2] = "https://\nwww\n.\nredhat\n.com\n/en".on_blue().to_string(); + data +} diff --git a/vendor/tabled/tests/settings/rotate_test.rs b/vendor/tabled/tests/settings/rotate_test.rs new file mode 100644 index 000000000..42a781818 --- /dev/null +++ b/vendor/tabled/tests/settings/rotate_test.rs @@ -0,0 +1,214 @@ +#![cfg(feature = "std")] + +// todo: add method for SPACING between cells. + +use tabled::settings::{ + object::{Cell, Rows}, + Border, Highlight, Rotate, +}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +#[test] +fn test_rotate() { + let table = || Matrix::iter([(123, 456, 789), (234, 567, 891)]); + + assert_eq!( + table() + .with(Rotate::Left) + .with(Rotate::Left) + .with(Rotate::Left) + .with(Rotate::Left) + .to_string(), + table().to_string() + ); + assert_eq!( + table() + .with(Rotate::Right) + .with(Rotate::Right) + .with(Rotate::Right) + .with(Rotate::Right) + .to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Right).with(Rotate::Left).to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Left).with(Rotate::Right).to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Bottom).with(Rotate::Top).to_string(), + table().to_string() + ); + assert_eq!( + table() + .with(Rotate::Bottom) + .with(Rotate::Bottom) + .to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Top).with(Rotate::Top).to_string(), + table().to_string() + ); +} + +test_table!( + test_3x3_box_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Rotate::Left), + "+-----+-----+-----+" + "| i32 | 789 | 891 |" + "+-----+-----+-----+" + "| i32 | 456 | 567 |" + "+-----+-----+-----+" + "| i32 | 123 | 234 |" + "+-----+-----+-----+" +); + +test_table!( + test_3x3_box_1, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Rotate::Left).with(Rotate::Right).with(Rotate::Right), + "+-----+-----+-----+" + "| 234 | 123 | i32 |" + "+-----+-----+-----+" + "| 567 | 456 | i32 |" + "+-----+-----+-----+" + "| 891 | 789 | i32 |" + "+-----+-----+-----+" +); + +test_table!( + test_left_rotate, + Matrix::iter([(123, 456, 789), (234, 567, 891), (111, 222, 333)]).with(Rotate::Left), + "+-----+-----+-----+-----+" + "| i32 | 789 | 891 | 333 |" + "+-----+-----+-----+-----+" + "| i32 | 456 | 567 | 222 |" + "+-----+-----+-----+-----+" + "| i32 | 123 | 234 | 111 |" + "+-----+-----+-----+-----+" +); + +test_table!( + test_right_rotate, + Matrix::iter([(123, 456, 789), (234, 567, 891), (111, 222, 333)]).with(Rotate::Right), + "+-----+-----+-----+-----+" + "| 111 | 234 | 123 | i32 |" + "+-----+-----+-----+-----+" + "| 222 | 567 | 456 | i32 |" + "+-----+-----+-----+-----+" + "| 333 | 891 | 789 | i32 |" + "+-----+-----+-----+-----+" +); + +test_table!( + test_bottom_rotate, + Matrix::iter([(123, 456, 789), (234, 567, 891), (111, 222, 333)]).with(Rotate::Bottom), + "+-----+-----+-----+" + "| 111 | 222 | 333 |" + "+-----+-----+-----+" + "| 234 | 567 | 891 |" + "+-----+-----+-----+" + "| 123 | 456 | 789 |" + "+-----+-----+-----+" + "| i32 | i32 | i32 |" + "+-----+-----+-----+" +); + +test_table!( + test_top_rotate, + Matrix::iter([(123, 456, 789), (234, 567, 891), (111, 222, 333)]).with(Rotate::Top), + "+-----+-----+-----+" + "| 111 | 222 | 333 |" + "+-----+-----+-----+" + "| 234 | 567 | 891 |" + "+-----+-----+-----+" + "| 123 | 456 | 789 |" + "+-----+-----+-----+" + "| i32 | i32 | i32 |" + "+-----+-----+-----+" +); + +test_table!( + rotate_preserve_border_styles_test_0, + Matrix::iter([(123, 456, 789), (234, 567, 891), (111, 222, 333)]) + .with(Highlight::new(Rows::single(0), Border::default().top('*'))) + .with(Rotate::Left), + "+*****************+-----+" + "| i32 | 789 | 891 | 333 |" + "+-----+-----+-----+-----+" + "| i32 | 456 | 567 | 222 |" + "+-----+-----+-----+-----+" + "| i32 | 123 | 234 | 111 |" + "+-----+-----+-----+-----+" +); + +// it's a correct behaviour because +// when we sen bottom border of cell(0, 2) we also set top border of cell(1, 2) +// +// todo: determine if it's correct +test_table!( + rotate_preserve_border_styles_test_1, + Matrix::iter([(123, 456, 789), (234, 567, 891), (111, 222, 333)]) + .with(Highlight::new(Cell::new(0, 2), Border::default().bottom('*'))) + .with(Rotate::Left), + "+-----+-----+-----+-----+" + "| i32 | 789 | 891 | 333 |" + "+-----+-----+*****+-----+" + "| i32 | 456 | 567 | 222 |" + "+-----+-----+-----+-----+" + "| i32 | 123 | 234 | 111 |" + "+-----+-----+-----+-----+" +); + +test_table!( + test_left_rotate_1, + Matrix::iter([(0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5)]).with(Rotate::Left), + "+-----+---+---+" + "| i32 | 5 | 5 |" + "+-----+---+---+" + "| i32 | 4 | 4 |" + "+-----+---+---+" + "| i32 | 3 | 3 |" + "+-----+---+---+" + "| i32 | 2 | 2 |" + "+-----+---+---+" + "| i32 | 1 | 1 |" + "+-----+---+---+" + "| i32 | 0 | 0 |" + "+-----+---+---+" +); + +test_table!( + test_right_rotate_1, + Matrix::iter([(0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5)]).with(Rotate::Right), + "+---+---+-----+" + "| 0 | 0 | i32 |" + "+---+---+-----+" + "| 1 | 1 | i32 |" + "+---+---+-----+" + "| 2 | 2 | i32 |" + "+---+---+-----+" + "| 3 | 3 | i32 |" + "+---+---+-----+" + "| 4 | 4 | i32 |" + "+---+---+-----+" + "| 5 | 5 | i32 |" + "+---+---+-----+" +); + +test_table!( + test_bottom_rotate_1, + Matrix::iter([(0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5)]).with(Rotate::Bottom), + "+-----+-----+-----+-----+-----+-----+" + "| 0 | 1 | 2 | 3 | 4 | 5 |" + "+-----+-----+-----+-----+-----+-----+" + "| 0 | 1 | 2 | 3 | 4 | 5 |" + "+-----+-----+-----+-----+-----+-----+" + "| i32 | i32 | i32 | i32 | i32 | i32 |" + "+-----+-----+-----+-----+-----+-----+" +); diff --git a/vendor/tabled/tests/settings/shadow_test.rs b/vendor/tabled/tests/settings/shadow_test.rs new file mode 100644 index 000000000..2d169698c --- /dev/null +++ b/vendor/tabled/tests/settings/shadow_test.rs @@ -0,0 +1,105 @@ +#![cfg(feature = "std")] + +use tabled::settings::{Shadow, Style}; + +use crate::matrix::Matrix; +use testing_table::test_table; + +#[cfg(feature = "color")] +use ::{owo_colors::OwoColorize, std::convert::TryFrom, tabled::settings::Color}; + +test_table!( + test_shadow_bottom_right_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Style::psql()).with(Shadow::new(1)), + " i32 | i32 | i32 " + "-----+-----+-----▒" + " 123 | 456 | 789 ▒" + " 234 | 567 | 891 ▒" + " ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒" +); + +test_table!( + test_shadow_bottom_left_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Style::psql()).with(Shadow::new(1).set_left()), + " i32 | i32 | i32 " + "▒-----+-----+-----" + "▒ 123 | 456 | 789 " + "▒ 234 | 567 | 891 " + "▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ " +); + +test_table!( + test_shadow_top_right_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Style::psql()).with(Shadow::new(1).set_top()), + " ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒" + " i32 | i32 | i32 ▒" + "-----+-----+-----▒" + " 123 | 456 | 789 ▒" + " 234 | 567 | 891 " +); + +test_table!( + test_shadow_top_left_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Style::psql()).with(Shadow::new(1).set_top().set_left()), + "▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ " + "▒ i32 | i32 | i32 " + "▒-----+-----+-----" + "▒ 123 | 456 | 789 " + " 234 | 567 | 891 " +); + +test_table!( + test_shadow_set_fill, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Shadow::new(1).set_fill('▓')), + "+-----+-----+-----+ " + "| i32 | i32 | i32 |▓" + "+-----+-----+-----+▓" + "| 123 | 456 | 789 |▓" + "+-----+-----+-----+▓" + "| 234 | 567 | 891 |▓" + "+-----+-----+-----+▓" + " ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓" +); + +test_table!( + test_shadow_size_1, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Shadow::new(2).set_fill('▓')), + "+-----+-----+-----+ " + "| i32 | i32 | i32 |▓▓" + "+-----+-----+-----+▓▓" + "| 123 | 456 | 789 |▓▓" + "+-----+-----+-----+▓▓" + "| 234 | 567 | 891 |▓▓" + "+-----+-----+-----+▓▓" + " ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓" + " ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓" +); + +test_table!( + test_shadow_set_offset_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Shadow::new(2).set_offset(3)), + "+-----+-----+-----+ " + "| i32 | i32 | i32 | " + "+-----+-----+-----+ " + "| 123 | 456 | 789 |▒▒" + "+-----+-----+-----+▒▒" + "| 234 | 567 | 891 |▒▒" + "+-----+-----+-----+▒▒" + " ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒" + " ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒" +); + +#[cfg(feature = "color")] +test_table!( + test_shadow_set_color_0, + Matrix::iter([(123, 456, 789), (234, 567, 891)]).with(Shadow::new(2).set_offset(3).set_color(Color::try_from(' '.red().to_string()).unwrap())), + "+-----+-----+-----+ " + "| i32 | i32 | i32 | " + "+-----+-----+-----+ " + "| 123 | 456 | 789 |\u{1b}[31m▒▒\u{1b}[39m" + "+-----+-----+-----+\u{1b}[31m▒▒\u{1b}[39m" + "| 234 | 567 | 891 |\u{1b}[31m▒▒\u{1b}[39m" + "+-----+-----+-----+\u{1b}[31m▒▒\u{1b}[39m" + " \u{1b}[31m▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒\u{1b}[39m" + " \u{1b}[31m▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒\u{1b}[39m" +); diff --git a/vendor/tabled/tests/settings/span_test.rs b/vendor/tabled/tests/settings/span_test.rs new file mode 100644 index 000000000..ed61d8619 --- /dev/null +++ b/vendor/tabled/tests/settings/span_test.rs @@ -0,0 +1,1234 @@ +#![cfg(feature = "std")] +#![allow(clippy::redundant_clone)] + +use std::iter::FromIterator; + +use tabled::{ + builder::Builder, + grid::config::Position, + settings::{ + object::{Columns, Segment}, + style::{Border, BorderSpanCorrection, Style}, + Alignment, Highlight, Modify, Padding, Panel, Span, + }, + Table, +}; + +use crate::matrix::Matrix; +use testing_table::{static_table, test_table}; + +test_table!( + span_column_test_0, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Columns::single(0)).with(Span::column(2))), + " N | column 1 | column 2 " + "-+-+----------+----------" + " 0 | 0-1 | 0-2 " + " 1 | 1-1 | 1-2 " + " 2 | 2-1 | 2-2 " +); + +test_table!( + span_column_test_1, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Columns::new(1..2)).with(Span::column(2))), + " N | column 0 | column 2 " + "---+-----+----+----------" + " 0 | 0-0 | 0-2 " + " 1 | 1-0 | 1-2 " + " 2 | 2-0 | 2-2 " +); + +test_table!( + span_column_test_2, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Columns::single(0)).with(Span::column(4))), + " N " + "+++" + " 0 " + " 1 " + " 2 " +); + +test_table!( + cell_span_test_0, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((0, 0)).with(Span::column(2))), + " N | column 1 | column 2 " + "---+-----+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_1, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 0)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_2, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((2, 0)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_3, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((3, 0)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_4, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((0, 1)).with(Span::column(2))), + " N | column 0 | column 2 " + "---+-----+-----+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_5, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 1)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_6, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((2, 1)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_7, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((3, 1)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-2 " +); + +test_table!( + cell_span_test_8, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 2)).with(Span::column(2))), + " N | column 0 | column 1 " + "---+----------+-----+-----" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_9, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((1, 2)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_10, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((2, 2)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + cell_span_test_11, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((3, 2)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 " +); + +test_table!( + span_multiline, + Matrix::new(3, 3) + .insert((3, 2), "https://\nwww\n.\nredhat\n.com\n/en") + .with(Style::psql()) + .with(Modify::new((3, 2)).with(Span::column(2))), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | https:// " + " | | www " + " | | . " + " | | redhat " + " | | .com " + " | | /en " +); + +test_table!( + indent_works_in_spaned_columns, + Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Padding::new(3, 0, 0, 0))) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 1)).with(Span::column(3))) + .with(Modify::new((3, 1)).with(Span::column(3))), + " N| column 0| column 1| column 2" + "----+-----------+-----------+-----------" + " 0| 0-0 " + " 1| 1-0 | 1-1 | 1-2 " + " 2| 2-0 " +); + +test_table!( + spaned_columns_with_collision, + Matrix::iter([["just 1 column"; 5]; 5]) + .with(Style::modern()) + .with( + Modify::new((0, 0)) + .with(Span::column(5)) + .with("span all 5 columns"), + ) + .with( + Modify::new((1, 0)) + .with(Span::column(4)) + .with("span 4 columns"), + ) + .with( + Modify::new((2, 0)) + .with(Span::column(3)) + .with("span 3 columns"), + ) + .with( + Modify::new((2, 3)) + .with(Span::column(2)) + .with("span 2 columns"), + ) + .with( + Modify::new((3, 0)) + .with(Span::column(2)) + .with("span 3 columns"), + ) + .with( + Modify::new((3, 2)) + .with(Span::column(3)) + .with("span 3 columns"), + ) + .with( + Modify::new((4, 1)) + .with(Span::column(4)) + .with("span 4 columns"), + ), + "┌───────────────┬───────────────┬───────────────┬───────────────┬───────────────┐" + "│ span all 5 columns │" + "├───────────────┼───────────────┼───────────────┼───────────────┼───────────────┤" + "│ span 4 columns │ just 1 column │" + "├───────────────┼───────────────┼───────────────┼───────────────┼───────────────┤" + "│ span 3 columns │ span 2 columns │" + "├───────────────┼───────────────┼───────────────┼───────────────┼───────────────┤" + "│ span 3 columns │ span 3 columns │" + "├───────────────┼───────────────┼───────────────┼───────────────┼───────────────┤" + "│ just 1 column │ span 4 columns │" + "├───────────────┼───────────────┼───────────────┼───────────────┼───────────────┤" + "│ just 1 column │ just 1 column │ just 1 column │ just 1 column │ just 1 column │" + "└───────────────┴───────────────┴───────────────┴───────────────┴───────────────┘" +); + +test_table!( + span_with_panel_test_0, + Matrix::iter([[1, 2, 3]]) + .with(Panel::horizontal(0,"Tabled Releases")) + .with(Modify::new((1, 0)).with(Span::column(2))) + .with(Style::ascii()), + "+-----+-----+-----+" + "| Tabled Releases |" + "+-----+-----+-----+" + "| 0 | 2 |" + "+-----+-----+-----+" + "| 1 | 2 | 3 |" + "+-----+-----+-----+" +); + +test_table!( + span_with_panel_test_1, + Matrix::iter([[1, 2, 3], [4, 5, 6]]) + .with(Panel::horizontal(0,"Tabled Releases")) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(Style::ascii()), + "+-----+-----+-----+" + "| Tabled Releases |" + "+-----+-----+-----+" + "| 0 | 1 | 2 |" + "+-----+-----+-----+" + "| 1 | 3 |" + "+-----+-----+-----+" + "| 4 | 5 | 6 |" + "+-----+-----+-----+" +); + +test_table!( + span_with_panel_test_2, + Matrix::iter([[1, 2, 3], [4, 5, 6]]) + .with(Panel::horizontal(0,"Tabled Releases")) + .with(Modify::new((1, 0)).with(Span::column(2))) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(Style::ascii()), + "+-----+-----+-----+" + "| Tabled Releases |" + "+-----+-----+-----+" + "| 0 | 2 |" + "+-----+-----+-----+" + "| 1 | 3 |" + "+-----+-----+-----+" + "| 4 | 5 | 6 |" + "+-----+-----+-----+" +); + +test_table!( + span_with_panel_with_correction_test_0, + Matrix::iter([[1, 2, 3]]) + .with(Panel::horizontal(0,"Tabled Releases")) + .with(Modify::new((1, 0)).with(Span::column(2))) + .with(Style::ascii()) + .with(BorderSpanCorrection), + "+-----------------+" + "| Tabled Releases |" + "+-----------+-----+" + "| 0 | 2 |" + "+-----+-----+-----+" + "| 1 | 2 | 3 |" + "+-----+-----+-----+" +); + +test_table!( + span_with_panel_with_correction_test_1, + Matrix::iter([[1, 2, 3], [4, 5, 6]]) + .with(Panel::horizontal(0,"Tabled Releases")) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(Style::ascii()) + .with(BorderSpanCorrection), + "+-----------------+" + "| Tabled Releases |" + "+-----+-----+-----+" + "| 0 | 1 | 2 |" + "+-----+-----+-----+" + "| 1 | 3 |" + "+-----+-----+-----+" + "| 4 | 5 | 6 |" + "+-----+-----+-----+" +); + +test_table!( + span_with_panel_with_correction_test_2, + Matrix::iter([[1, 2, 3], [4, 5, 6]]) + .with(Panel::horizontal(0,"Tabled Releases")) + .with(Modify::new((1, 0)).with(Span::column(2))) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(Style::ascii()) + .with(BorderSpanCorrection), + "+-----------------+" + "| Tabled Releases |" + "+-----------+-----+" + "| 0 | 2 |" + "+-----------+-----+" + "| 1 | 3 |" + "+-----+-----+-----+" + "| 4 | 5 | 6 |" + "+-----+-----+-----+" +); + +#[test] +#[should_panic] +#[ignore = "span zero not yet decided"] +fn span_column_exceeds_boundaries_test() { + // todo: determine if it's the right behaiviour + + Matrix::new(3, 3) + .with(Modify::new(Columns::single(0)).with(Span::column(100))) + .to_string(); +} + +#[test] +#[ignore = "span zero not yet decided"] +fn span_cell_exceeds_boundaries_test() { + // these tests shows that exiding boundaries causes invalid behaiviour + // + // todo: determine if it's the right behaiviour + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((0, 0)).with(Span::column(20))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N " + "---+-----+-----+-----" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 1)).with(Span::column(20))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 0)).with(Span::column(20))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); +} + +#[test] +#[ignore = "span zero not yet decided"] +fn span_zero_test() { + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 0)).with(Span::column(0))) + .to_string(); + + assert_eq!( + table, + static_table!( + " column 0 | column 1 | column 2 " + "----+-----+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 1)).with(Span::column(0))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 1 | column 2 " + "---+-----+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 2)).with(Span::column(0))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 2 " + "---+-----+-----+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 3)).with(Span::column(0))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 " + "---+----------+-----+-----" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 4)).with(Span::column(0))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::psql()) + .with(Modify::new((0, 0)).with(Span::column(0))) + .with(Modify::new((1, 1)).with(Span::column(0))) + .with(Modify::new((2, 2)).with(Span::column(0))) + .with(Modify::new((3, 2)).with(Span::column(0))) + .with(Modify::new((3, 1)).with(Span::column(0))) + .to_string(); + + assert_eq!( + table, + static_table!( + " column 0 | column 1 | column 2 " + "------+-------+------+----------" + " 0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-2 " + " 2 | 2-2 " + ) + ); +} + +#[test] +#[ignore = "span zero not yet decided"] +fn span_all_table_to_zero_test() { + let table = Matrix::table(2, 2) + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Span::column(0))) + .to_string(); + + // todo: determine whether it's correct + assert_eq!(table, static_table!("\n++\n\n\n")); +} + +mod row { + use tabled::settings::object::Rows; + + use super::*; + + #[test] + fn span_row_test() { + let table = Matrix::new(3, 3); + { + let table_str = table + .clone() + .with(Style::ascii()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Rows::single(0)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table_str, + static_table!( + "+---+----------+----------+----------+" + "+ N + column 0 + column 1 + column 2 +" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" + ) + ); + + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Rows::single(0)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N + column 0 + column 1 + column 2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Rows::new(1..2)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Rows::single(0)).with(Span::row(4))) + .to_string(); + + assert_eq!(table, " N + column 0 + column 1 + column 2 "); + } + } + + #[test] + fn cell_span_test() { + let table = Matrix::new(3, 3); + { + // first column cells row span = 2 + + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((0, 0)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + " +----------+----------+----------" + " | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 0)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new((2, 0)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " | 2-0 | 2-1 | 2-2 " + ) + ); + } + } + + { + // first row cells row span = 2 + + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((0, 1)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+ +----------+----------" + " 0 | | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((0, 2)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+ +----------" + " 0 | 0-0 | | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new((0, 3)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+ " + " 0 | 0-0 | 0-1 | " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + } + + { + // second column span=2 + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 1)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((2, 1)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | | 2-1 | 2-2 " + ) + ); + } + } + { + // 3rd column span=2 + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 2)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((2, 2)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | | 2-2 " + ) + ); + } + } + { + // 4th column span=2 + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((1, 3)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + } + { + let table = table + .clone() + .with(Style::psql()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new((2, 3)).with(Span::row(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | " + ) + ); + } + } + } + + #[test] + fn span_with_panel_with_correction_test() { + let data = [[1, 2, 3]]; + let table = Table::new(data) + .with(Modify::new((0, 0)).with(Span::row(2))) + .with(Style::ascii()) + .with(BorderSpanCorrection) + .to_string(); + + assert_eq!( + table, + static_table!( + "+---+---+---+" + "| 0 | 1 | 2 |" + "| +---+---+" + "| | 2 | 3 |" + "+---+---+---+" + ) + ); + + let data = [[1, 2, 3], [4, 5, 6]]; + let table = Table::new(data) + .with(Modify::new((1, 0)).with(Span::row(2))) + .with(Modify::new((0, 2)).with(Span::row(3))) + .with(Style::ascii()) + .with(BorderSpanCorrection) + .to_string(); + + assert_eq!( + table, + static_table!( + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+---+ |" + "| 1 | 2 | |" + "| +---+ |" + "| | 5 | |" + "+---+---+---+" + ) + ); + + let data = [[1, 2, 3], [4, 5, 6]]; + let table = Table::new(data) + .with(Modify::new((1, 0)).with(Span::row(2))) + .with(Modify::new((0, 2)).with(Span::row(3))) + .with(Modify::new((0, 1)).with(Span::row(2))) + .with(Style::ascii()) + .with(BorderSpanCorrection) + .to_string(); + + assert_eq!( + table, + static_table!( + "+---+---+---+" + "| 0 | 1 | 2 |" + "+---+ | |" + "| 1 +---+ |" + "| | 5 | |" + "+---+---+---+" + ) + ); + + let data = [[1, 2, 3], [4, 5, 6]]; + let table = Table::new(data) + .with(Modify::new((1, 0)).with(Span::row(2))) + .with(Modify::new((0, 1)).with(Span::row(2)).with(Span::column(2))) + .with(Style::ascii()) + .with(BorderSpanCorrection) + .to_string(); + + assert_eq!( + table, + static_table!( + "+---+-------+" + "| 0 | 1 |" + "+---+ +" + "| 1 +---+---+" + "| | 5 | 6 |" + "+---+---+---+" + ) + ); + } + + #[test] + fn span_example_test() { + let data = [["just 1 column"; 5]; 5]; + + let h_span = |r, c, span| Modify::new((r, c)).with(Span::column(span)); + let v_span = |r, c, span| Modify::new((r, c)).with(Span::row(span)); + + let table = Table::new(data) + .with(h_span(0, 0, 5).with(String::from("span all 5 columns"))) + .with(h_span(1, 0, 4).with(String::from("span 4 columns"))) + .with(h_span(2, 0, 2).with(String::from("span 2 columns"))) + .with(v_span(2, 4, 4).with(String::from("just 1 column\nspan\n4\ncolumns"))) + .with(v_span(3, 1, 2).with(String::from("span 2 columns\nspan\n2\ncolumns"))) + .with(v_span(2, 3, 3).with(String::from("just 1 column\nspan\n3\ncolumns"))) + .with(h_span(3, 1, 2)) + .with(Style::modern()) + .with(BorderSpanCorrection) + .with(Modify::new(Segment::all()).with(Alignment::center_vertical())) + .to_string(); + + assert_eq!( + table, + static_table!( + "┌───────────────────────────────────────────────────────────────────────────────┐" + "│ span all 5 columns │" + "├───────────────────────────────────────────────────────────────┬───────────────┤" + "│ span 4 columns │ just 1 column │" + "├───────────────────────────────┬───────────────┬───────────────┼───────────────┤" + "│ span 2 columns │ just 1 column │ │ │" + "├───────────────┬───────────────┴───────────────┤ just 1 column │ │" + "│ just 1 column │ span 2 columns │ span │ just 1 column │" + "│ │ span │ 3 │ span │" + "├───────────────┤ 2 │ columns │ 4 │" + "│ just 1 column │ columns │ │ columns │" + "├───────────────┼───────────────┬───────────────┼───────────────┤ │" + "│ just 1 column │ just 1 column │ just 1 column │ just 1 column │ │" + "└───────────────┴───────────────┴───────────────┴───────────────┴───────────────┘" + ) + ) + } + + #[test] + fn highlight_row_span_test() { + let data = [ + ["1", "2\n2\n2\n2\n2\n2\n2\n2", "3"], + ["4", "5", "6"], + ["7", "8", "9"], + ]; + let table = Table::new(data) + .with(Modify::new((1, 1)).with(Span::row(3))) + .with(Style::modern()) + .with(Highlight::new(Columns::single(1), Border::filled('*'))) + .to_string(); + + assert_eq!( + table, + static_table!( + "┌───*****───┐" + "│ 0 * 1 * 2 │" + "├───*───*───┤" + "│ 1 * 2 * 3 │" + "│ * 2 * │" + "├───* 2 *───┤" + "│ 4 * 2 * 6 │" + "│ * 2 * │" + "├───* 2 *───┤" + "│ 7 * 2 * 9 │" + "│ * 2 * │" + "└───*****───┘" + ) + ); + } +} + +#[test] +fn highlight_row_col_span_test() { + let data = [ + ["1", "2\n2\n2\n2\n2\n2\n2\n2", "3", "0"], + ["4", "5", "6", "0"], + ["7", "8", "9", "0"], + ]; + let table = Table::new(data) + .with(Modify::new((1, 1)).with(Span::row(3)).with(Span::column(2))) + .with(Style::modern()) + .with(Highlight::new(Columns::new(1..3), Border::filled('*'))) + .to_string(); + + assert_eq!( + table, + static_table!( + "┌───*********───┐" + "│ 0 * 1 │ 2 * 3 │" + "├───*───┼───*───┤" + "│ 1 * 2 * 0 │" + "│ * 2 * │" + "├───* 2 *───┤" + "│ 4 * 2 * 0 │" + "│ * 2 * │" + "├───* 2 *───┤" + "│ 7 * 2 * 0 │" + "│ * 2 * │" + "└───*********───┘" + ) + ); +} + +test_table!( + column_span_bigger_then_max, + Matrix::new(3, 3).with(Modify::new((0, 0)).with(Span::column(100))), + "+---+-----+-----+-----+" + "| N |" + "+---+-----+-----+-----+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+-----+-----+-----+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+-----+-----+-----+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+-----+-----+-----+" +); + +test_table!( + row_span_bigger_then_max, + Matrix::new(3, 3).with(Modify::new((0, 0)).with(Span::row(100))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+ +----------+----------+----------+" + "| | 0-0 | 0-1 | 0-2 |" + "+ +----------+----------+----------+" + "| | 1-0 | 1-1 | 1-2 |" + "+ +----------+----------+----------+" + "| | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + column_span_invalid_position_row, + Matrix::new(3, 3).with(Modify::new((1000, 0)).with(Span::column(2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + column_span_invalid_position_column, + Matrix::new(3, 3).with(Modify::new((0, 1000)).with(Span::column(2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + column_span_invalid_position_row_and_column, + Matrix::new(3, 3).with(Modify::new((1000, 1000)).with(Span::column(2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + row_span_invalid_position_row, + Matrix::new(3, 3).with(Modify::new((1000, 0)).with(Span::row(2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + row_span_invalid_position_column, + Matrix::new(3, 3).with(Modify::new((0, 1000)).with(Span::row(2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + row_span_invalid_position_row_and_column, + Matrix::new(3, 3).with(Modify::new((1000, 1000)).with(Span::row(2))), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + fix_qc_0, + { + let data: [[i64; 39]; 2] = [[2542785870, 2382388818, 2879895075, 2885436543, 2331131758, 219892320, 2503640226, 3754929678, 2206481860, 686909682, 3456499235, 931699300, 1556722454, 958179233, 3896072307, 2042612749, 3354379549, 3272539286, 3926297167, 4294967295, 1650407458, 3322068437, 4294967295, 446762625, 829020202, 4150192304, 3430619243, 3460609391, 2992017103, 513091574, 1514148367, 2166549688, 1401371431, 2854075038, 1286733939, 2959901405, 4152658371, 0, 4224074215], [360331598, 3736108702, 2948800064, 2121584548, 1609988995, 469935087, 3974876615, 2193609088, 3568111892, 732365859, 0, 4294967295, 2994498036, 198522721, 1784359340, 1, 2732726754, 592359359, 3016729802, 878533877, 2997437699, 3573361662, 1111570515, 4294967295, 2245782848, 1383106893, 0, 0, 2869976103, 1611436878, 1682224972, 3249055253, 1562255501, 1370527728, 240481955, 334260406, 2247343342, 3000635978, 395723768]]; + let row_spans = [2, 1, 27, 111, 226, 221, 121, 22, 252, 30, 115, 85, 255, 126, 26, 245, 36, 50, 255, 211, 47, 114, 174, 173, 145, 138, 78, 198, 253, 229, 151, 243, 242, 30, 52, 116, 177, 25, 1, 32, 28, 48, 225, 103, 17, 243, 0, 128, 69, 206, 221, 105, 239, 74, 184, 48, 178, 237, 120, 228, 184, 1, 132, 118, 14, 187]; + let col_spans = [7, 91, 56, 246, 73]; + + let data = data.iter().map(|row| row.iter().map(ToString::to_string)); + let rspans = create_span_list(2, 39).zip(row_spans.iter()).map(|(pos, span)| Modify::new(pos).with(Span::column(*span))).collect::<Vec<_>>(); + let cspans = create_span_list(2, 39).zip(col_spans.iter()).map(|(pos, span)| Modify::new(pos).with(Span::row(*span))).collect::<Vec<_>>(); + + Builder::from_iter(data).build().with(Style::ascii()).with(rspans).with(cspans).to_string() + }, + "+------+-----++++++++++++++++++++++++++++------------+------------+------------+------------+------------+-----------+-----------+------------+------------+-----------+" + "| 2542785870 | 2879895075 | 513091574 |" + "+ + +------------+------------+------------+------------+------------+-----------+-----------+------------+------------+-----------+" + "| | | 1611436878 | 1682224972 | 3249055253 | 1562255501 | 1370527728 | 240481955 | 334260406 | 2247343342 | 3000635978 | 395723768 |" + "+------+-----++++++++++++++++++++++++++++------------+------------+------------+------------+------------+-----------+-----------+------------+------------+-----------+" +); + +fn create_span_list(count_rows: usize, count_cols: usize) -> impl Iterator<Item = Position> { + (0..count_rows).flat_map(move |r| (0..count_cols).map(move |c| (r, c))) +} diff --git a/vendor/tabled/tests/settings/split_test.rs b/vendor/tabled/tests/settings/split_test.rs new file mode 100644 index 000000000..ac13d8071 --- /dev/null +++ b/vendor/tabled/tests/settings/split_test.rs @@ -0,0 +1,277 @@ +#![cfg(feature = "std")] + +use std::iter::FromIterator; + +use tabled::{builder::Builder, settings::split::Split, Table}; + +use testing_table::test_table; + +test_table!( + split_column_test, + Table::from_iter(['a'..='z']).with(Split::column(12)), + "+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | g | h | i | j | k | l |" + "+---+---+---+---+---+---+---+---+---+---+---+---+" + "| m | n | o | p | q | r | s | t | u | v | w | x |" + "+---+---+---+---+---+---+---+---+---+---+---+---+" + "| y | z | | | | | | | | | | |" + "+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_column_2_test, + Table::from_iter(['a'..='z']) + .with(Split::column(12)) + .with(Split::column(4)), + "+---+---+---+---+" + "| a | b | c | d |" + "+---+---+---+---+" + "| e | f | g | h |" + "+---+---+---+---+" + "| i | j | k | l |" + "+---+---+---+---+" + "| m | n | o | p |" + "+---+---+---+---+" + "| q | r | s | t |" + "+---+---+---+---+" + "| u | v | w | x |" + "+---+---+---+---+" + "| y | z | | |" + "+---+---+---+---+" +); + +test_table!( + split_column_retain_test, + Table::from_iter(['a'..='z']) + .with(Split::column(12)) + .with(Split::column(4).retain()), + "+---+---+---+---+" + "| a | b | c | d |" + "+---+---+---+---+" + "| e | f | g | h |" + "+---+---+---+---+" + "| i | j | k | l |" + "+---+---+---+---+" + "| m | n | o | p |" + "+---+---+---+---+" + "| q | r | s | t |" + "+---+---+---+---+" + "| u | v | w | x |" + "+---+---+---+---+" + "| y | z | | |" + "+---+---+---+---+" + "| | | | |" + "+---+---+---+---+" + "| | | | |" + "+---+---+---+---+" +); + +test_table!( + split_row_test, + Table::from_iter(['a'..='z']) + .with(Split::column(12)) + .with(Split::column(4)) + .with(Split::row(1).concat()), // take it back to the original shape + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_row_2_test, + Table::from_iter(['a'..='z']) + .with(Split::column(12)) + .with(Split::column(4)) + .with(Split::row(2).concat()), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | i | j | k | l | q | r | s | t | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| e | f | g | h | m | n | o | p | u | v | w | x | | |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_colum_index_beyond_size_test, + Table::from_iter(['a'..='z']) + .with(Split::column(10000)), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_row_index_beyond_size_test, + Table::from_iter(['a'..='z']) + .with(Split::row(10000)), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_empty_table_test, + Builder::default().build().with(Split::column(10000)), + "" +); + +test_table!( + split_column_zero_argument_test, + Table::from_iter(['a'..='z']).with(Split::column(0)), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_row_zero_argument_test, + Table::from_iter(['a'..='z']).with(Split::row(0)), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_blank_table_test, + Table::from_iter([vec![String::new(); 26]]).with(Split::column(12)), + "+--+--+--+--+--+--+--+--+--+--+--+--+" + "| | | | | | | | | | | | |" // first section is protected + "+--+--+--+--+--+--+--+--+--+--+--+--+" +); + +test_table!( + split_blank_table_2_test, + Table::from_iter([vec![String::new(); 26]]).with(Split::column(12).retain()), + "+--+--+--+--+--+--+--+--+--+--+--+--+" + "| | | | | | | | | | | | |" + "+--+--+--+--+--+--+--+--+--+--+--+--+" + "| | | | | | | | | | | | |" + "+--+--+--+--+--+--+--+--+--+--+--+--+" + "| | | | | | | | | | | | |" + "+--+--+--+--+--+--+--+--+--+--+--+--+" +); + +test_table!( + split_zip_test, + Table::from_iter(['a'..='z']) + .with(Split::column(6)) + .with(Split::row(2)), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | m | y | b | n | z | c | o | d | p | e | q | f | r |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| g | s | | h | t | | i | u | j | v | k | w | l | x |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_concat_test, + Table::from_iter(['a'..='z']) + .with(Split::column(6)) + .with(Split::row(2).concat()), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | b | c | d | e | f | m | n | o | p | q | r | y | z |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| g | h | i | j | k | l | s | t | u | v | w | x | | |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_clean_test, + Table::from_iter(['a'..='z']) + .with(Split::column(12)) + .with(Split::row(2)), + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| a | y | b | z | c | d | e | f | g | h | i | j | k | l |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" + "| m | | n | | o | p | q | r | s | t | u | v | w | x |" + "+---+---+---+---+---+---+---+---+---+---+---+---+---+---+" +); + +test_table!( + split_retain_test, + Table::from_iter(['a'..='z']) + .with(Split::column(12)) + .with(Split::row(2).retain()), + "+---+---+---+---+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+" + "| a | y | b | z | c | | d | | e | | f | | g | | h | | i | | j | | k | | l | |" + "+---+---+---+---+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+" + "| m | | n | | o | | p | | q | | r | | s | | t | | u | | v | | w | | x | |" + "+---+---+---+---+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+" +); + +test_table!( + split_mostly_blank_test, + Table::from_iter([vec![ + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "A", + ]]).with(Split::column(5)) + .with(Split::row(2)), + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | A | |" + "+--+--+--+---+--+" +); + +test_table!( + split_mostly_blank_retain_test, + Table::from_iter([vec![ + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "A", + ]]).with(Split::column(5).retain()), + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | | |" + "+--+--+--+---+--+" + "| | | | A | |" + "+--+--+--+---+--+" +); + +test_table!( + split_scattered_values_test, + Table::from_iter([vec![ + "", "", "", "", "", "g", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "A", + ]]).with(Split::column(5)), + "+---+--+--+---+--+" + "| | | | | |" + "+---+--+--+---+--+" + "| g | | | | |" + "+---+--+--+---+--+" + "| | | | A | |" + "+---+--+--+---+--+" +); + +test_table!( + split_scattered_values_column_and_row_test, + Table::from_iter([vec![ + "", "", "", "", "", "g", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "A", + ]]).with(Split::column(5)).with(Split::row(2)), + "+---+--+--+--+---+--+" + "| | | | | A | |" + "+---+--+--+--+---+--+" + "| g | | | | | |" + "+---+--+--+--+---+--+" +); + +test_table!( + split_scattered_values_column_and_row_retain_test, + Table::from_iter([vec![ + "", "", "", "", "", "g", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "A", + ]]).with(Split::column(5).retain()).with(Split::row(2).retain()), + "+---+--+--+--+--+--+--+--+--+--+--+--+--+--+--+---+--+--+--+--+" + "| | | | | | | | | | | | | | | | A | | | | |" + "+---+--+--+--+--+--+--+--+--+--+--+--+--+--+--+---+--+--+--+--+" + "| g | | | | | | | | | | | | | | | | | | | |" + "+---+--+--+--+--+--+--+--+--+--+--+--+--+--+--+---+--+--+--+--+" +); diff --git a/vendor/tabled/tests/settings/style_test.rs b/vendor/tabled/tests/settings/style_test.rs new file mode 100644 index 000000000..884192471 --- /dev/null +++ b/vendor/tabled/tests/settings/style_test.rs @@ -0,0 +1,2606 @@ +#![cfg(feature = "std")] + +use std::iter::FromIterator; + +use tabled::{ + builder::Builder, + settings::{ + object::{Columns, Rows, Segment}, + style::{ + Border, BorderChar, BorderColor, BorderSpanCorrection, BorderText, HorizontalLine, + Line, Offset, RawStyle, Style, VerticalLine, + }, + Color, Format, Highlight, Modify, Padding, Span, + }, + Table, +}; + +use crate::matrix::Matrix; +use testing_table::{static_table, test_table}; + +#[cfg(feature = "color")] +use ::{owo_colors::OwoColorize, std::convert::TryFrom}; + +test_table!( + default_style, + Matrix::new(3, 3).with(Style::ascii()), + "+---+----------+----------+----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+----------+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+---+----------+----------+----------+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+---+----------+----------+----------+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+---+----------+----------+----------+" +); + +test_table!( + psql_style, + Matrix::new(3, 3).with(Style::psql()), + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | 0-0 | 0-1 | 0-2 " + " 1 | 1-0 | 1-1 | 1-2 " + " 2 | 2-0 | 2-1 | 2-2 " +); + +test_table!( + markdown_style, + Matrix::new(3, 3).with(Style::markdown()), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + modern_style, + Matrix::new(3, 3).with(Style::modern()), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + rounded_style, + Matrix::new(3, 3).with(Style::rounded()), + "╭───┬──────────┬──────────┬──────────╮" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "╰───┴──────────┴──────────┴──────────╯" +); + +test_table!( + sharp_style, + Matrix::new(3, 3).with(Style::sharp()), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + modern_clean_style, + Matrix::new(3, 3).with(Style::modern().remove_horizontal().horizontals(vec![HorizontalLine::new(1, Style::modern().get_horizontal())])), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + blank_style, + Matrix::new(3, 3).with(Style::blank()), + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " +); + +test_table!( + extended_style, + Matrix::new(3, 3).with(Style::extended()), + "╔═══╦══════════╦══════════╦══════════╗" + "║ N ║ column 0 ║ column 1 ║ column 2 ║" + "╠═══╬══════════╬══════════╬══════════╣" + "║ 0 ║ 0-0 ║ 0-1 ║ 0-2 ║" + "╠═══╬══════════╬══════════╬══════════╣" + "║ 1 ║ 1-0 ║ 1-1 ║ 1-2 ║" + "╠═══╬══════════╬══════════╬══════════╣" + "║ 2 ║ 2-0 ║ 2-1 ║ 2-2 ║" + "╚═══╩══════════╩══════════╩══════════╝" +); + +test_table!( + ascii_dots_style, + Matrix::new(3, 3).with(Style::dots()), + "......................................" + ": N : column 0 : column 1 : column 2 :" + ":...:..........:..........:..........:" + ": 0 : 0-0 : 0-1 : 0-2 :" + ":...:..........:..........:..........:" + ": 1 : 1-0 : 1-1 : 1-2 :" + ":...:..........:..........:..........:" + ": 2 : 2-0 : 2-1 : 2-2 :" + ":...:..........:..........:..........:" +); + +test_table!( + re_structured_text_style, + Matrix::new(3, 3).with(Style::re_structured_text()), + "=== ========== ========== ==========" + " N column 0 column 1 column 2 " + "=== ========== ========== ==========" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + "=== ========== ========== ==========" +); + +test_table!( + ascii_rounded_style, + Matrix::new(3, 3).with(Style::ascii_rounded()), + ".------------------------------------." + "| N | column 0 | column 1 | column 2 |" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + "'------------------------------------'" +); + +test_table!( + style_head_changes, + Matrix::new(3, 3).with(Style::modern().remove_horizontal()), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + style_frame_changes, + Matrix::new(3, 3).with(Style::modern().remove_top().remove_bottom().remove_horizontal()), + "│ N │ column 0 │ column 1 │ column 2 │" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" +); + +test_table!( + custom_style, + Matrix::new(3, 3) + .with(Style::blank() + .bottom('*') + .vertical('\'') + .horizontal('`') + .intersection('\'') + .intersection_bottom('\'') + .horizontals(vec![HorizontalLine::new(1, Line::full('x', '*', 'q', 'w'))])), + " N ' column 0 ' column 1 ' column 2 " + "qxxx*xxxxxxxxxx*xxxxxxxxxx*xxxxxxxxxxw" + " 0 ' 0-0 ' 0-1 ' 0-2 " + " ```'``````````'``````````'`````````` " + " 1 ' 1-0 ' 1-1 ' 1-2 " + " ```'``````````'``````````'`````````` " + " 2 ' 2-0 ' 2-1 ' 2-2 " + " ***'**********'**********'********** " +); + +test_table!( + style_single_cell_0, + Matrix::table(0, 0), + "+---+" + "| N |" + "+---+" +); + +test_table!( + style_single_cell_1, + Matrix::table(0, 0).with(Style::blank()), + " N " +); + +test_table!( + top_border_override_first_test, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(Rows::first())), + "-Table---------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + top_border_override_last_test, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(Rows::last())), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "-Table---------+----------+" +); + +test_table!( + top_border_override_new_test, + Matrix::table(2, 2) + .with(BorderText::new("-Table").horizontal(1)) + .with(BorderText::new("-Table").horizontal(2)), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "-Table---------+----------+" + "| 0 | 0-0 | 0-1 |" + "-Table---------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + top_border_override_new_doesnt_panic_when_index_is_invalid, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(100)), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + top_override_doesnt_work_with_style_with_no_top_border_test, + Matrix::table(2, 2) + .with(Style::psql()) + .with(BorderText::new("-Table").horizontal(Rows::first())), + " N | column 0 | column 1 " + "---+----------+----------" + " 0 | 0-0 | 0-1 " + " 1 | 1-0 | 1-1 " +); + +test_table!( + top_border_override_cleared_after_restyling_test, + Matrix::table(2, 2) + .with(BorderText::new("-Table").horizontal(Rows::first())) + .with(Style::ascii()), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + top_border_override_with_big_string_test, + Matrix::table(2, 2) + .with(BorderText::new("-Tableeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee1231").horizontal(Rows::first())), + "-Tableeeeeeeeeeeeeeeeeeeeee" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_0, + Matrix::table(2, 2) + .with(Style::empty()) + .with(Modify::new(Rows::first()).with(Border::default().bottom('-'))) + .with(BorderText::new("-Table").horizontal(1)), + " N column 0 column 1 " + "-Table-----------------" + " 0 0-0 0-1 " + " 1 1-0 1-1 " +); + +test_table!( + border_color_global, + { Matrix::table(2, 2).with(BorderColor::default().bottom(Color::FG_RED)) }, + "+---+----------+----------+\n\ + | N | column 0 | column 1 |\n\ + +\u{1b}[31m---\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+\n\ + | 0 | 0-0 | 0-1 |\n\ + +\u{1b}[31m---\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+\n\ + | 1 | 1-0 | 1-1 |\n\ + +\u{1b}[31m---\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+" +); + +#[cfg(feature = "color")] +test_table!( + border_text_colored, + Matrix::table(2, 2) + .with(BorderText::new("-Table").horizontal(1)) + .with(BorderText::new("-Table213123").horizontal(2)) + .with(Modify::new(Rows::single(1)).with(BorderColor::default().bottom(Color::FG_RED))) + .with(Modify::new(Rows::single(2)).with(BorderColor::default().bottom(Color::try_from(" ".blue().on_green().to_string()).unwrap()))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "-Table---------+----------+" + "| 0 | 0-0 | 0-1 |" + "-\u{1b}[31mTab\u{1b}[39ml\u{1b}[31me213123---\u{1b}[39m+\u{1b}[31m----------\u{1b}[39m+" + "| 1 | 1-0 | 1-1 |" + "+\u{1b}[34m\u{1b}[42m---\u{1b}[39m\u{1b}[49m+\u{1b}[34m\u{1b}[42m----------\u{1b}[39m\u{1b}[49m+\u{1b}[34m\u{1b}[42m----------\u{1b}[39m\u{1b}[49m+" +); + +test_table!( + border_text_offset_test_0, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(1).offset(Offset::Begin(5))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+-Table----+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_offset_test_1, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(1).offset(Offset::Begin(15))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+-----------Table-----+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_offset_test_2, + Matrix::table(2, 2).with(BorderText::new("Table").horizontal(1).offset(Offset::End(5))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+------Table" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_offset_test_3, + Matrix::table(2, 2).with(BorderText::new("Table").horizontal(1).offset(Offset::End(15))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+-------Table---------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_offset_test_4, + Matrix::table(2, 2).with(BorderText::new("Table").horizontal(1).offset(Offset::End(21))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+-Table----+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_offset_test_5, + Matrix::table(2, 2).with(BorderText::new("Table").horizontal(1).offset(Offset::End(25))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+-Table--------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_text_offset_test_6, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(1).offset(Offset::Begin(21))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+------Table" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_override_color, + Matrix::table(2, 2).with(BorderText::new("-Table").horizontal(Rows::first()).color(Color::FG_BLUE)), + "\u{1b}[34m-\u{1b}[39m\u{1b}[34mT\u{1b}[39m\u{1b}[34ma\u{1b}[39m\u{1b}[34mb\u{1b}[39m\u{1b}[34ml\u{1b}[39m\u{1b}[34me\u{1b}[39m---------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + empty_style, + Matrix::new(3, 3) + .with(Style::empty()) + .with(Modify::new(Segment::all()).with(Padding::zero())), + "Ncolumn 0column 1column 2" + "0 0-0 0-1 0-2 " + "1 1-0 1-1 1-2 " + "2 2-0 2-1 2-2 " +); + +test_table!( + single_column_style_0, + Matrix::table(2, 0).with(Style::modern()), + "┌───┐" + "│ N │" + "├───┤" + "│ 0 │" + "├───┤" + "│ 1 │" + "└───┘" +); + +test_table!( + single_column_style_1, + Matrix::table(2, 0).with(Style::blank()), + " N " + " 0 " + " 1 " +); + +test_table!( + single_column_last_row_style, + Matrix::table(3, 0).with(Style::re_structured_text()), + "===" + " N " + "===" + " 0 " + " 1 " + " 2 " + "===" +); + +test_table!( + single_cell_style, + Builder::from_iter([[""]]).build().with(Style::modern()), + "┌──┐" + "│ │" + "└──┘" +); + +test_table!( + border_test_0, + Matrix::table(2, 2).with(Modify::new(Rows::single(1)).with(Border::filled('*').top('#'))), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "*###*##########*##########*" + "* 0 * 0-0 * 0-1 *" + "***************************" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_test_1, + Matrix::table(2, 2) + .with(Style::empty()) + .with(Modify::new(Rows::single(1)).with(Border::filled('*').top('#'))), + " N column 0 column 1 " + "*###*##########*##########*" + "* 0 * 0-0 * 0-1 *" + "***************************" + " 1 1-0 1-1 " +); + +test_table!( + style_frame_test_0, + Matrix::table(2, 2).with(Highlight::new(Rows::single(1), Style::modern().get_frame())), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "┌─────────────────────────┐" + "│ 0 | 0-0 | 0-1 │" + "└─────────────────────────┘" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + style_frame_test_1, + Matrix::table(2, 2) + .with(Style::blank()) + .with(Highlight::new(Rows::single(0), Style::extended().get_frame())) + .with(Highlight::new(Rows::single(2), Style::extended().get_frame())), + "╔═════════════════════════╗" + "║ N column 0 column 1 ║" + "╚═════════════════════════╝" + " 0 0-0 0-1 " + "╔═════════════════════════╗" + "║ 1 1-0 1-1 ║" + "╚═════════════════════════╝" +); + +test_table!( + single_column_off_horizontal_test, + Matrix::table(3, 0).with(Style::ascii().remove_horizontal().remove_vertical()), + "+---+" + "| N |" + "| 0 |" + "| 1 |" + "| 2 |" + "+---+" +); + +test_table!( + single_row_test, + Matrix::table(0, 3).with(Style::modern()), + "┌───┬──────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "└───┴──────────┴──────────┴──────────┘" +); + +test_table!( + empty_border_text_doesnt_panic_test, + Matrix::table(2, 2).with(BorderText::new("").horizontal(0)), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + span_correct_test_0, + Matrix::table(6, 4) + .with(Modify::new((0, 3)).with(Span::column(2))) + .with(Modify::new((1, 0)).with(Span::column(3))) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(Modify::new((2, 3)).with(Span::column(2))) + .with(Modify::new((3, 0)).with(Span::column(5))) + .with(Modify::new((4, 1)).with(Span::column(4))) + .with(Modify::new((5, 0)).with(Span::column(5))) + .with(Modify::new((6, 0)).with(Span::column(5))) + .with(BorderSpanCorrection), + "+---+----------+----------+-----------+" + "| N | column 0 | column 1 | column 2 |" + "+---+----------+----------+-----+-----+" + "| 0 | 0-2 | 0-3 |" + "+--------------+----------+-----+-----+" + "| 1 | 1-1 | 1-2 |" + "+--------------+----------+-----------+" + "| 2 |" + "+---+---------------------------------+" + "| 3 | 3-0 |" + "+---+---------------------------------+" + "| 4 |" + "+-------------------------------------+" + "| 5 |" + "+-------------------------------------+" +); + +test_table!( + span_correct_test_1, + Matrix::table(6, 4) + .with(Modify::new((0, 0)).with(Span::column(5))) + .with(Modify::new((1, 0)).with(Span::column(3))) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(Modify::new((2, 3)).with(Span::column(2))) + .with(Modify::new((3, 0)).with(Span::column(5))) + .with(Modify::new((4, 1)).with(Span::column(4))) + .with(Modify::new((5, 0)).with(Span::column(5))) + .with(Modify::new((6, 0)).with(Span::column(5))) + .with(BorderSpanCorrection), + "+----------------------+" + "| N |" + "+----------+-----+-----+" + "| 0 | 0-2 | 0-3 |" + "+----+-----+-----+-----+" + "| 1 | 1-1 | 1-2 |" + "+----+-----+-----------+" + "| 2 |" + "+---+------------------+" + "| 3 | 3-0 |" + "+---+------------------+" + "| 4 |" + "+----------------------+" + "| 5 |" + "+----------------------+" +); + +test_table!( + style_settings_usage_test_0, + Matrix::new(3, 3) + .insert((1, 1), "a longer string") + .with({ + let mut style: RawStyle = Style::modern().into(); + style + .set_bottom(Some('a')) + .set_left(Some('b')) + .set_right(None) + .set_top(None) + .set_intersection(Some('x')) + .set_intersection_top(None) + .set_corner_top_left(None) + .set_corner_top_right(None); + style + }), + "b N │ column 0 │ column 1 │ column 2 " + "├───x─────────────────x──────────x──────────┤" + "b 0 │ a longer string │ 0-1 │ 0-2 " + "├───x─────────────────x──────────x──────────┤" + "b 1 │ 1-0 │ 1-1 │ 1-2 " + "├───x─────────────────x──────────x──────────┤" + "b 2 │ 2-0 │ 2-1 │ 2-2 " + "└aaa┴aaaaaaaaaaaaaaaaa┴aaaaaaaaaa┴aaaaaaaaaa┘" +); + +test_table!( + style_settings_usage_test_1, + Matrix::new(3, 3) + .insert((1, 1), "a longer string") + .with({ + let mut style: RawStyle = Style::modern().into(); + style.set_bottom(None); + style + }), + "┌───┬─────────────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼─────────────────┼──────────┼──────────┤" + "│ 0 │ a longer string │ 0-1 │ 0-2 │" + "├───┼─────────────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "├───┼─────────────────┼──────────┼──────────┤" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "└ ┴ ┴ ┴ ┘" +); + +test_table!( + style_settings_usage_test_2, + Matrix::new(3, 3) + .insert((1, 1), "a longer string") + .with({ + let mut style: RawStyle = Style::modern().into(); + style.set_bottom(None); + style + }) + .with(Modify::new(Rows::last()).with(Border::default().corner_bottom_left('*'))), + "┌───┬─────────────────┬──────────┬──────────┐" + "│ N │ column 0 │ column 1 │ column 2 │" + "├───┼─────────────────┼──────────┼──────────┤" + "│ 0 │ a longer string │ 0-1 │ 0-2 │" + "├───┼─────────────────┼──────────┼──────────┤" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "├───┼─────────────────┼──────────┼──────────┤" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "* * * * ┘" +); + +test_table!( + border_none_test_0, + Matrix::table(2, 2) + .with(Style::ascii()) + .with(Modify::new(Rows::single(1)).with(Border::filled('*').top('#'))) + .with(Modify::new(Rows::single(1)).with(Border::empty())), + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "+---+----------+----------+" + "| 0 | 0-0 | 0-1 |" + "+---+----------+----------+" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" +); + +test_table!( + border_none_test_1, + Matrix::table(2, 2) + .with(Style::empty()) + .with(Modify::new(Rows::single(1)).with(Border::filled('*').top('#'))) + .with(Modify::new(Columns::single(1)).with(Border::empty())), + " N column 0 column 1 " + "*### ##########*" + "* 0 0-0 0-1 *" + "**** ***********" + " 1 1-0 1-1 " +); + +#[test] +fn custom_style_test() { + macro_rules! test_style { + ($style:expr, $expected:expr $(,)*) => { + let table = Matrix::new(3, 3).with($style).to_string(); + assert_eq!(table, $expected); + }; + } + + // Single + + test_style!( + Style::empty().top('-'), + static_table!( + "---------------------------------" + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ), + ); + test_style!( + Style::empty().bottom('-'), + static_table!( + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + "---------------------------------" + ), + ); + test_style!( + Style::empty().left('-'), + static_table!( + "- N column 0 column 1 column 2 " + "- 0 0-0 0-1 0-2 " + "- 1 1-0 1-1 1-2 " + "- 2 2-0 2-1 2-2 " + ), + ); + test_style!( + Style::empty().right('-'), + static_table!( + " N column 0 column 1 column 2 -" + " 0 0-0 0-1 0-2 -" + " 1 1-0 1-1 1-2 -" + " 2 2-0 2-1 2-2 -" + ), + ); + test_style!( + Style::empty().horizontal('-'), + static_table!( + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + "---------------------------------" + " 1 1-0 1-1 1-2 " + "---------------------------------" + " 2 2-0 2-1 2-2 " + ), + ); + test_style!( + Style::empty().horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]), + static_table!( + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ), + ); + test_style!( + Style::empty().vertical('-'), + static_table!( + " N - column 0 - column 1 - column 2 " + " 0 - 0-0 - 0-1 - 0-2 " + " 1 - 1-0 - 1-1 - 1-2 " + " 2 - 2-0 - 2-1 - 2-2 " + ), + ); + + // Combinations + + test_style!( + Style::empty().top('-').bottom('+'), + static_table!( + "---------------------------------" + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + "+++++++++++++++++++++++++++++++++" + ) + ); + test_style!( + Style::empty().top('-').left('+'), + static_table!( + "+---------------------------------" + "+ N column 0 column 1 column 2 " + "+ 0 0-0 0-1 0-2 " + "+ 1 1-0 1-1 1-2 " + "+ 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty().top('-').right('+'), + static_table!( + "---------------------------------+" + " N column 0 column 1 column 2 +" + " 0 0-0 0-1 0-2 +" + " 1 1-0 1-1 1-2 +" + " 2 2-0 2-1 2-2 +" + ) + ); + test_style!( + Style::empty().top('-').horizontal('+'), + static_table!( + "---------------------------------" + " N column 0 column 1 column 2 " + "+++++++++++++++++++++++++++++++++" + " 0 0-0 0-1 0-2 " + "+++++++++++++++++++++++++++++++++" + " 1 1-0 1-1 1-2 " + "+++++++++++++++++++++++++++++++++" + " 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty().top('-').vertical('+'), + static_table!( + "---+----------+----------+----------" + " N + column 0 + column 1 + column 2 " + " 0 + 0-0 + 0-1 + 0-2 " + " 1 + 1-0 + 1-1 + 1-2 " + " 2 + 2-0 + 2-1 + 2-2 " + ) + ); + test_style!( + Style::empty() + .top('-') + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('+'))]), + static_table!( + "---------------------------------" + " N column 0 column 1 column 2 " + "+++++++++++++++++++++++++++++++++" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + ); + + test_style!( + Style::empty().bottom('-').top('+'), + static_table!( + "+++++++++++++++++++++++++++++++++" + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + "---------------------------------" + ) + ); + test_style!( + Style::empty().bottom('-').left('+'), + static_table!( + "+ N column 0 column 1 column 2 " + "+ 0 0-0 0-1 0-2 " + "+ 1 1-0 1-1 1-2 " + "+ 2 2-0 2-1 2-2 " + "+---------------------------------" + ) + ); + test_style!( + Style::empty().bottom('-').right('+'), + static_table!( + " N column 0 column 1 column 2 +" + " 0 0-0 0-1 0-2 +" + " 1 1-0 1-1 1-2 +" + " 2 2-0 2-1 2-2 +" + "---------------------------------+" + ) + ); + test_style!( + Style::empty().bottom('-').vertical('+'), + static_table!( + " N + column 0 + column 1 + column 2 " + " 0 + 0-0 + 0-1 + 0-2 " + " 1 + 1-0 + 1-1 + 1-2 " + " 2 + 2-0 + 2-1 + 2-2 " + "---+----------+----------+----------" + ) + ); + test_style!( + Style::empty().bottom('-').horizontal('+'), + static_table!( + " N column 0 column 1 column 2 " + "+++++++++++++++++++++++++++++++++" + " 0 0-0 0-1 0-2 " + "+++++++++++++++++++++++++++++++++" + " 1 1-0 1-1 1-2 " + "+++++++++++++++++++++++++++++++++" + " 2 2-0 2-1 2-2 " + "---------------------------------" + ) + ); + test_style!( + Style::empty() + .bottom('-') + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('+'))]), + static_table!( + " N column 0 column 1 column 2 " + "+++++++++++++++++++++++++++++++++" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + "---------------------------------" + ) + ); + + test_style!( + Style::empty().left('-').top('+'), + static_table!( + "++++++++++++++++++++++++++++++++++" + "- N column 0 column 1 column 2 " + "- 0 0-0 0-1 0-2 " + "- 1 1-0 1-1 1-2 " + "- 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty().left('-').bottom('+'), + static_table!( + "- N column 0 column 1 column 2 " + "- 0 0-0 0-1 0-2 " + "- 1 1-0 1-1 1-2 " + "- 2 2-0 2-1 2-2 " + "++++++++++++++++++++++++++++++++++" + ) + ); + test_style!( + Style::empty().left('-').right('+'), + static_table!( + "- N column 0 column 1 column 2 +" + "- 0 0-0 0-1 0-2 +" + "- 1 1-0 1-1 1-2 +" + "- 2 2-0 2-1 2-2 +" + ) + ); + test_style!( + Style::empty().left('-').vertical('+'), + static_table!( + "- N + column 0 + column 1 + column 2 " + "- 0 + 0-0 + 0-1 + 0-2 " + "- 1 + 1-0 + 1-1 + 1-2 " + "- 2 + 2-0 + 2-1 + 2-2 " + ) + ); + test_style!( + Style::empty().left('-').horizontal('+'), + static_table!( + "- N column 0 column 1 column 2 " + "++++++++++++++++++++++++++++++++++" + "- 0 0-0 0-1 0-2 " + "++++++++++++++++++++++++++++++++++" + "- 1 1-0 1-1 1-2 " + "++++++++++++++++++++++++++++++++++" + "- 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty() + .left('-') + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('+'))]), + static_table!( + "- N column 0 column 1 column 2 " + " +++++++++++++++++++++++++++++++++" + "- 0 0-0 0-1 0-2 " + "- 1 1-0 1-1 1-2 " + "- 2 2-0 2-1 2-2 " + ) + ); + + test_style!( + Style::empty().right('-').top('+'), + static_table!( + "++++++++++++++++++++++++++++++++++" + " N column 0 column 1 column 2 -" + " 0 0-0 0-1 0-2 -" + " 1 1-0 1-1 1-2 -" + " 2 2-0 2-1 2-2 -" + ) + ); + test_style!( + Style::empty().right('-').bottom('+'), + static_table!( + " N column 0 column 1 column 2 -" + " 0 0-0 0-1 0-2 -" + " 1 1-0 1-1 1-2 -" + " 2 2-0 2-1 2-2 -" + "++++++++++++++++++++++++++++++++++" + ) + ); + test_style!( + Style::empty().right('-').left('+'), + static_table!( + "+ N column 0 column 1 column 2 -" + "+ 0 0-0 0-1 0-2 -" + "+ 1 1-0 1-1 1-2 -" + "+ 2 2-0 2-1 2-2 -" + ) + ); + test_style!( + Style::empty().right('-').vertical('+'), + static_table!( + " N + column 0 + column 1 + column 2 -" + " 0 + 0-0 + 0-1 + 0-2 -" + " 1 + 1-0 + 1-1 + 1-2 -" + " 2 + 2-0 + 2-1 + 2-2 -" + ) + ); + test_style!( + Style::empty().right('-').horizontal('+'), + static_table!( + " N column 0 column 1 column 2 -" + "++++++++++++++++++++++++++++++++++" + " 0 0-0 0-1 0-2 -" + "++++++++++++++++++++++++++++++++++" + " 1 1-0 1-1 1-2 -" + "++++++++++++++++++++++++++++++++++" + " 2 2-0 2-1 2-2 -" + ) + ); + test_style!( + Style::empty() + .right('-') + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('+'))]), + static_table!( + " N column 0 column 1 column 2 -" + "+++++++++++++++++++++++++++++++++ " + " 0 0-0 0-1 0-2 -" + " 1 1-0 1-1 1-2 -" + " 2 2-0 2-1 2-2 -" + ) + ); + + test_style!( + Style::empty().vertical('-').top('+'), + static_table!( + "++++++++++++++++++++++++++++++++++++" + " N - column 0 - column 1 - column 2 " + " 0 - 0-0 - 0-1 - 0-2 " + " 1 - 1-0 - 1-1 - 1-2 " + " 2 - 2-0 - 2-1 - 2-2 " + ) + ); + test_style!( + Style::empty().vertical('-').bottom('+'), + static_table!( + " N - column 0 - column 1 - column 2 " + " 0 - 0-0 - 0-1 - 0-2 " + " 1 - 1-0 - 1-1 - 1-2 " + " 2 - 2-0 - 2-1 - 2-2 " + "++++++++++++++++++++++++++++++++++++" + ) + ); + test_style!( + Style::empty().vertical('-').left('+'), + static_table!( + "+ N - column 0 - column 1 - column 2 " + "+ 0 - 0-0 - 0-1 - 0-2 " + "+ 1 - 1-0 - 1-1 - 1-2 " + "+ 2 - 2-0 - 2-1 - 2-2 " + ) + ); + test_style!( + Style::empty().vertical('-').right('+'), + static_table!( + " N - column 0 - column 1 - column 2 +" + " 0 - 0-0 - 0-1 - 0-2 +" + " 1 - 1-0 - 1-1 - 1-2 +" + " 2 - 2-0 - 2-1 - 2-2 +" + ) + ); + test_style!( + Style::empty().vertical('-').horizontal('+'), + static_table!( + " N - column 0 - column 1 - column 2 " + "++++++++++++++++++++++++++++++++++++" + " 0 - 0-0 - 0-1 - 0-2 " + "++++++++++++++++++++++++++++++++++++" + " 1 - 1-0 - 1-1 - 1-2 " + "++++++++++++++++++++++++++++++++++++" + " 2 - 2-0 - 2-1 - 2-2 " + ) + ); + test_style!( + Style::empty() + .vertical('-') + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('+'))]), + static_table!( + " N - column 0 - column 1 - column 2 " + "+++ ++++++++++ ++++++++++ ++++++++++" + " 0 - 0-0 - 0-1 - 0-2 " + " 1 - 1-0 - 1-1 - 1-2 " + " 2 - 2-0 - 2-1 - 2-2 " + ) + ); + + test_style!( + Style::empty().horizontal('-').top('+'), + static_table!( + "+++++++++++++++++++++++++++++++++" + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + "---------------------------------" + " 1 1-0 1-1 1-2 " + "---------------------------------" + " 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty().horizontal('-').bottom('+'), + static_table!( + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + "---------------------------------" + " 1 1-0 1-1 1-2 " + "---------------------------------" + " 2 2-0 2-1 2-2 " + "+++++++++++++++++++++++++++++++++" + ) + ); + test_style!( + Style::empty().horizontal('-').left('+'), + static_table!( + "+ N column 0 column 1 column 2 " + "+---------------------------------" + "+ 0 0-0 0-1 0-2 " + "+---------------------------------" + "+ 1 1-0 1-1 1-2 " + "+---------------------------------" + "+ 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty().horizontal('-').right('+'), + static_table!( + " N column 0 column 1 column 2 +" + "---------------------------------+" + " 0 0-0 0-1 0-2 +" + "---------------------------------+" + " 1 1-0 1-1 1-2 +" + "---------------------------------+" + " 2 2-0 2-1 2-2 +" + ) + ); + test_style!( + Style::empty().horizontal('-').vertical('+'), + static_table!( + " N + column 0 + column 1 + column 2 " + "---+----------+----------+----------" + " 0 + 0-0 + 0-1 + 0-2 " + "---+----------+----------+----------" + " 1 + 1-0 + 1-1 + 1-2 " + "---+----------+----------+----------" + " 2 + 2-0 + 2-1 + 2-2 " + ) + ); + test_style!( + Style::empty() + .horizontal('-') + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('+'))]), + static_table!( + " N column 0 column 1 column 2 " + "+++++++++++++++++++++++++++++++++" + " 0 0-0 0-1 0-2 " + "---------------------------------" + " 1 1-0 1-1 1-2 " + "---------------------------------" + " 2 2-0 2-1 2-2 " + ) + ); + + test_style!( + Style::empty() + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]) + .top('+'), + static_table!( + "+++++++++++++++++++++++++++++++++" + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty() + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]) + .bottom('+'), + static_table!( + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + "+++++++++++++++++++++++++++++++++" + ) + ); + test_style!( + Style::empty() + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]) + .left('+'), + static_table!( + "+ N column 0 column 1 column 2 " + "+---------------------------------" + "+ 0 0-0 0-1 0-2 " + "+ 1 1-0 1-1 1-2 " + "+ 2 2-0 2-1 2-2 " + ) + ); + test_style!( + Style::empty() + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]) + .right('+'), + static_table!( + " N column 0 column 1 column 2 +" + "---------------------------------+" + " 0 0-0 0-1 0-2 +" + " 1 1-0 1-1 1-2 +" + " 2 2-0 2-1 2-2 +" + ) + ); + test_style!( + Style::empty() + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]) + .vertical('+'), + static_table!( + " N + column 0 + column 1 + column 2 " + "---+----------+----------+----------" + " 0 + 0-0 + 0-1 + 0-2 " + " 1 + 1-0 + 1-1 + 1-2 " + " 2 + 2-0 + 2-1 + 2-2 " + ) + ); + test_style!( + Style::empty() + .horizontals(vec![HorizontalLine::new(1, Line::default()).main(Some('-'))]) + .horizontal('+'), + static_table!( + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + "+++++++++++++++++++++++++++++++++" + " 1 1-0 1-1 1-2 " + "+++++++++++++++++++++++++++++++++" + " 2 2-0 2-1 2-2 " + ) + ); + + // Full + + test_style!( + Style::empty() + .top('-') + .bottom('+') + .left('|') + .right('*') + .horizontal('x') + .horizontals(vec![HorizontalLine::new(1, Line::filled('z'))]) + .vertical('#'), + static_table!( + "|---#----------#----------#----------*" + "| N # column 0 # column 1 # column 2 *" + "zzzz#zzzzzzzzzz#zzzzzzzzzz#zzzzzzzzzzz" + "| 0 # 0-0 # 0-1 # 0-2 *" + "xxxx#xxxxxxxxxx#xxxxxxxxxx#xxxxxxxxxxx" + "| 1 # 1-0 # 1-1 # 1-2 *" + "xxxx#xxxxxxxxxx#xxxxxxxxxx#xxxxxxxxxxx" + "| 2 # 2-0 # 2-1 # 2-2 *" + "|+++#++++++++++#++++++++++#++++++++++*" + ), + ); + + let full_style = Style::empty() + .top('-') + .bottom('+') + .left('|') + .right('*') + .horizontal('x') + .horizontals(vec![HorizontalLine::new(1, Line::filled(','))]) + .vertical('#') + .intersection_bottom('@') + .intersection_top('!') + .intersection_left('=') + .intersection_right('$') + .intersection('+') + .corner_top_left(';') + .corner_bottom_left('?') + .corner_top_right('.') + .corner_bottom_right('%'); + test_style!( + full_style.clone(), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); + + // Overwrite intersections and corners + + test_style!( + full_style.clone().top('q'), + static_table!( + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); + test_style!( + full_style.clone().bottom('q'), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + ) + ); + test_style!( + full_style.clone().left('w'), + static_table!( + "w---!----------!----------!----------." + "w N # column 0 # column 1 # column 2 *" + "w,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "w 0 # 0-0 # 0-1 # 0-2 *" + "wxxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "w 1 # 1-0 # 1-1 # 1-2 *" + "wxxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "w 2 # 2-0 # 2-1 # 2-2 *" + "w+++@++++++++++@++++++++++@++++++++++%" + ) + ); + test_style!( + full_style.clone().right('i'), + static_table!( + ";---!----------!----------!----------i" + "| N # column 0 # column 1 # column 2 i" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,i" + "| 0 # 0-0 # 0-1 # 0-2 i" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxxi" + "| 1 # 1-0 # 1-1 # 1-2 i" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxxi" + "| 2 # 2-0 # 2-1 # 2-2 i" + "?+++@++++++++++@++++++++++@++++++++++i" + ) + ); + test_style!( + full_style.clone().horizontal('q'), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + "| 1 # 1-0 # 1-1 # 1-2 *" + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); + test_style!( + full_style.clone().vertical('q'), + static_table!( + ";---q----------q----------q----------." + "| N q column 0 q column 1 q column 2 *" + ",,,,q,,,,,,,,,,q,,,,,,,,,,q,,,,,,,,,,," + "| 0 q 0-0 q 0-1 q 0-2 *" + "=xxxqxxxxxxxxxxqxxxxxxxxxxqxxxxxxxxxx$" + "| 1 q 1-0 q 1-1 q 1-2 *" + "=xxxqxxxxxxxxxxqxxxxxxxxxxqxxxxxxxxxx$" + "| 2 q 2-0 q 2-1 q 2-2 *" + "?+++q++++++++++q++++++++++q++++++++++%" + ) + ); + test_style!( + full_style + .clone() + .horizontals(vec![HorizontalLine::new(1, Line::filled('q'))]), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); + + // Turn off borders + + let empty_table = static_table!( + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ); + test_style!(Style::empty().top('-').remove_top(), empty_table); + test_style!(Style::empty().bottom('-').remove_bottom(), empty_table); + test_style!(Style::empty().right('-').remove_right(), empty_table); + test_style!(Style::empty().left('-').remove_left(), empty_table); + test_style!( + Style::empty().horizontal('-').remove_horizontal(), + empty_table + ); + test_style!(Style::empty().vertical('-').remove_vertical(), empty_table); + test_style!( + Style::empty().horizontals(vec![HorizontalLine::new( + 1, + Line::new(Some('-'), None, None, None) + )]), + static_table!( + " N column 0 column 1 column 2 " + "---------------------------------" + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + ); + + test_style!( + full_style.clone().remove_top(), + static_table!( + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); + test_style!( + full_style.clone().remove_bottom(), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + ) + ); + test_style!( + full_style.clone().remove_right(), + static_table!( + ";---!----------!----------!----------" + "| N # column 0 # column 1 # column 2 " + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 " + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx" + "| 1 # 1-0 # 1-1 # 1-2 " + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx" + "| 2 # 2-0 # 2-1 # 2-2 " + "?+++@++++++++++@++++++++++@++++++++++" + ) + ); + test_style!( + full_style.clone().remove_left(), + static_table!( + "---!----------!----------!----------." + " N # column 0 # column 1 # column 2 *" + ",,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + " 0 # 0-0 # 0-1 # 0-2 *" + "xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + " 1 # 1-0 # 1-1 # 1-2 *" + "xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + " 2 # 2-0 # 2-1 # 2-2 *" + "+++@++++++++++@++++++++++@++++++++++%" + ) + ); + test_style!( + full_style.clone().remove_horizontal(), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + ",,,,#,,,,,,,,,,#,,,,,,,,,,#,,,,,,,,,,," + "| 0 # 0-0 # 0-1 # 0-2 *" + "| 1 # 1-0 # 1-1 # 1-2 *" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); + test_style!( + full_style.clone().remove_vertical(), + static_table!( + ";---------------------------------." + "| N column 0 column 1 column 2 *" + ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,," + "| 0 0-0 0-1 0-2 *" + "=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$" + "| 1 1-0 1-1 1-2 *" + "=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$" + "| 2 2-0 2-1 2-2 *" + "?+++++++++++++++++++++++++++++++++%" + ) + ); + test_style!( + full_style.remove_horizontals(), + static_table!( + ";---!----------!----------!----------." + "| N # column 0 # column 1 # column 2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 0 # 0-0 # 0-1 # 0-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 1 # 1-0 # 1-1 # 1-2 *" + "=xxx+xxxxxxxxxx+xxxxxxxxxx+xxxxxxxxxx$" + "| 2 # 2-0 # 2-1 # 2-2 *" + "?+++@++++++++++@++++++++++@++++++++++%" + ) + ); +} + +#[test] +fn test_default_border_usage() { + macro_rules! test_border { + ($modify:expr, $expected:expr) => { + let table = Matrix::new(3, 3) + .insert((1, 1), "a longer string") + .with(Style::empty()) + .with($modify) + .to_string(); + + assert_eq!(table, $expected); + }; + } + + test_border! { + Modify::new((3, 2)).with(Border::default().corner_bottom_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + " * " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().corner_bottom_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + " * " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().bottom('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + " ********** " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().bottom('*').corner_bottom_left('#')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + " #********** " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().bottom('*').corner_bottom_right('#')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + " **********# " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 * 2-1 2-2 " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().corner_top_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().left('#').corner_top_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 # 2-1 2-2 " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().left('#').corner_bottom_left('@').corner_top_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 # 2-1 2-2 " + " @ " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().right('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 * 2-2 " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().corner_top_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().right('#').corner_top_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 2-1 # 2-2 " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().right('#').corner_top_right('*').corner_bottom_right('@')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 2-1 # 2-2 " + " @ " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::default().right('#').corner_top_right('*').corner_bottom_left('@')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " * " + " 2 2-0 2-1 # 2-2 " + " @ " + ) + } + test_border! { + Modify::new((3, 2)).with(Border::filled('@')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " @@@@@@@@@@@@ " + " 2 2-0 @ 2-1 @ 2-2 " + " @@@@@@@@@@@@ " + ) + } + + test_border! { + Modify::new((1, 2)).with(Border::default().corner_bottom_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " * " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().corner_bottom_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " * " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().bottom('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " ********** " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().bottom('*').corner_bottom_left('#')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " #********** " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().bottom('*').corner_bottom_right('#')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " **********# " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().left('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string * 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().corner_top_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().left('#').corner_top_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string # 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().left('#').corner_bottom_left('@').corner_top_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string # 0-1 0-2 " + " @ " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().right('*')), + static_table!( + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 * 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().corner_top_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().right('#').corner_top_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string 0-1 # 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().right('#').corner_top_right('*').corner_bottom_right('@')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string 0-1 # 0-2 " + " @ " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::default().right('#').corner_top_right('*').corner_bottom_left('@')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string 0-1 # 0-2 " + " @ " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((1, 2)).with(Border::filled('@')), + static_table!( + " N column 0 column 1 column 2 " + " @@@@@@@@@@@@ " + " 0 a longer string @ 0-1 @ 0-2 " + " @@@@@@@@@@@@ " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + + test_border! { + Modify::new((0, 3)).with(Border::default().corner_bottom_left('*')), + static_table!( + " N column 0 column 1 column 2 " + " * " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().corner_bottom_right('*')), + static_table!( + " N column 0 column 1 column 2 " + " *" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().bottom('*')), + static_table!( + " N column 0 column 1 column 2 " + " **********" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().bottom('*').corner_bottom_left('#')), + static_table!( + " N column 0 column 1 column 2 " + " #**********" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().bottom('*').corner_bottom_right('#')), + static_table!( + " N column 0 column 1 column 2 " + " **********#" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().left('*')), + static_table!( + " N column 0 column 1 * column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().corner_top_left('*')), + static_table!( + " * " + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().left('#').corner_top_left('*')), + static_table!( + " * " + " N column 0 column 1 # column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().left('#').corner_bottom_left('@').corner_top_left('*')), + static_table!( + " * " + " N column 0 column 1 # column 2 " + " @ " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().right('*')), + static_table!( + " N column 0 column 1 column 2 *" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().corner_top_right('*')), + static_table!( + " *" + " N column 0 column 1 column 2 " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().right('#').corner_top_right('*')), + static_table!( + " *" + " N column 0 column 1 column 2 #" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().right('#').corner_top_right('*').corner_bottom_right('@')), + static_table!( + " *" + " N column 0 column 1 column 2 #" + " @" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::default().right('#').corner_top_right('*').corner_bottom_left('@')), + static_table!( + " *" + " N column 0 column 1 column 2 #" + " @ " + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } + test_border! { + Modify::new((0, 3)).with(Border::filled('@')), + static_table!( + " @@@@@@@@@@@@" + " N column 0 column 1 @ column 2 @" + " @@@@@@@@@@@@" + " 0 a longer string 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + ) + } +} + +#[cfg(feature = "color")] +#[test] +fn border_colored_test() { + let table = Matrix::table(2, 2) + .with(Style::ascii()) + .with( + Modify::new(Rows::single(1)) + .with( + BorderColor::filled(Color::try_from('*'.blue().to_string()).unwrap()) + .top(Color::try_from('#'.truecolor(12, 220, 100).to_string()).unwrap()), + ) + .with(Border::filled('*').top('#')), + ) + .to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "*###*##########*##########*" + "* 0 * 0-0 * 0-1 *" + "***************************" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" + ) + ); + + assert_eq!( + table, + static_table!( + "+---+----------+----------+" + "| N | column 0 | column 1 |" + "\u{1b}[34m*\u{1b}[39m\u{1b}[38;2;12;220;100m###\u{1b}[39m\u{1b}[34m*\u{1b}[39m\u{1b}[38;2;12;220;100m##########\u{1b}[39m\u{1b}[34m*\u{1b}[39m\u{1b}[38;2;12;220;100m##########\u{1b}[39m\u{1b}[34m*\u{1b}[39m" + "\u{1b}[34m*\u{1b}[39m 0 \u{1b}[34m*\u{1b}[39m 0-0 \u{1b}[34m*\u{1b}[39m 0-1 \u{1b}[34m*\u{1b}[39m" + "\u{1b}[34m***************************\u{1b}[39m" + "| 1 | 1-0 | 1-1 |" + "+---+----------+----------+" + ) + ); + + let table = Matrix::table(2, 2) + .with(Style::empty()) + .with( + Modify::new(Rows::single(1)) + .with( + BorderColor::filled(Color::try_from('*'.blue().to_string()).unwrap()) + .top(Color::try_from('#'.truecolor(12, 220, 100).to_string()).unwrap()), + ) + .with(Border::filled('*').top('#')), + ) + .to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + " N column 0 column 1 " + "*###*##########*##########*" + "* 0 * 0-0 * 0-1 *" + "***************************" + " 1 1-0 1-1 " + ) + ); + + assert_eq!( + table, + " N column 0 column 1 \n\u{1b}[34m*\u{1b}[39m\u{1b}[38;2;12;220;100m###\u{1b}[39m\u{1b}[34m*\u{1b}[39m\u{1b}[38;2;12;220;100m##########\u{1b}[39m\u{1b}[34m*\u{1b}[39m\u{1b}[38;2;12;220;100m##########\u{1b}[39m\u{1b}[34m*\u{1b}[39m\n\u{1b}[34m*\u{1b}[39m 0 \u{1b}[34m*\u{1b}[39m 0-0 \u{1b}[34m*\u{1b}[39m 0-1 \u{1b}[34m*\u{1b}[39m\n\u{1b}[34m***************************\u{1b}[39m\n 1 1-0 1-1 ", + ); +} + +#[cfg(feature = "color")] +#[test] +fn style_with_color_test() { + let mut style: RawStyle = Style::ascii().into(); + style + .set_left(Some('[')) + .set_right(Some(']')) + .set_top(Some('-')) + .set_bottom(Some('-')) + .set_vertical(Some('|')) + .set_intersection(Some('+')); + style + .set_color_left(Color::FG_RED) + .set_color_right(Color::FG_RED) + .set_color_top(Color::FG_BLUE) + .set_color_bottom(Color::FG_BLUE) + .set_color_vertical(Color::FG_YELLOW) + .set_color_intersection(Color::try_from(' '.purple().to_string()).unwrap()); + + let table = Matrix::new(3, 3).with(style).to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + "+---+----------+----------+----------+" + "[ N | column 0 | column 1 | column 2 ]" + "+---+----------+----------+----------+" + "[ 0 | 0-0 | 0-1 | 0-2 ]" + "+---+----------+----------+----------+" + "[ 1 | 1-0 | 1-1 | 1-2 ]" + "+---+----------+----------+----------+" + "[ 2 | 2-0 | 2-1 | 2-2 ]" + "+---+----------+----------+----------+" + ) + ); + + assert_eq!(table, "+\u{1b}[34m---\u{1b}[39m+\u{1b}[34m----------\u{1b}[39m+\u{1b}[34m----------\u{1b}[39m+\u{1b}[34m----------\u{1b}[39m+\n\u{1b}[31m[\u{1b}[39m N \u{1b}[33m|\u{1b}[39m column 0 \u{1b}[33m|\u{1b}[39m column 1 \u{1b}[33m|\u{1b}[39m column 2 \u{1b}[31m]\u{1b}[39m\n+---\u{1b}[35m+\u{1b}[39m----------\u{1b}[35m+\u{1b}[39m----------\u{1b}[35m+\u{1b}[39m----------+\n\u{1b}[31m[\u{1b}[39m 0 \u{1b}[33m|\u{1b}[39m 0-0 \u{1b}[33m|\u{1b}[39m 0-1 \u{1b}[33m|\u{1b}[39m 0-2 \u{1b}[31m]\u{1b}[39m\n+---\u{1b}[35m+\u{1b}[39m----------\u{1b}[35m+\u{1b}[39m----------\u{1b}[35m+\u{1b}[39m----------+\n\u{1b}[31m[\u{1b}[39m 1 \u{1b}[33m|\u{1b}[39m 1-0 \u{1b}[33m|\u{1b}[39m 1-1 \u{1b}[33m|\u{1b}[39m 1-2 \u{1b}[31m]\u{1b}[39m\n+---\u{1b}[35m+\u{1b}[39m----------\u{1b}[35m+\u{1b}[39m----------\u{1b}[35m+\u{1b}[39m----------+\n\u{1b}[31m[\u{1b}[39m 2 \u{1b}[33m|\u{1b}[39m 2-0 \u{1b}[33m|\u{1b}[39m 2-1 \u{1b}[33m|\u{1b}[39m 2-2 \u{1b}[31m]\u{1b}[39m\n+\u{1b}[34m---\u{1b}[39m+\u{1b}[34m----------\u{1b}[39m+\u{1b}[34m----------\u{1b}[39m+\u{1b}[34m----------\u{1b}[39m+"); +} + +test_table!( + empty_line_clears_lines, + Matrix::new(3, 3).with(Style::rounded().remove_horizontals()), + "╭───┬──────────┬──────────┬──────────╮" + "│ N │ column 0 │ column 1 │ column 2 │" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "╰───┴──────────┴──────────┴──────────╯" +); + +test_table!( + empty_line_clears_lines_1, + Matrix::new(3, 3).with(Style::rounded().remove_horizontals()), + "╭───┬──────────┬──────────┬──────────╮" + "│ N │ column 0 │ column 1 │ column 2 │" + "│ 0 │ 0-0 │ 0-1 │ 0-2 │" + "│ 1 │ 1-0 │ 1-1 │ 1-2 │" + "│ 2 │ 2-0 │ 2-1 │ 2-2 │" + "╰───┴──────────┴──────────┴──────────╯" +); + +test_table!( + border_color, + { + use tabled::settings::Color; + Matrix::new(3, 3).with(Style::psql()).with(Color::BG_GREEN) + }, + " \u{1b}[42mN\u{1b}[49m | \u{1b}[42mcolumn 0\u{1b}[49m | \u{1b}[42mcolumn 1\u{1b}[49m | \u{1b}[42mcolumn 2\u{1b}[49m \n---+----------+----------+----------\n \u{1b}[42m0\u{1b}[49m | \u{1b}[42m0-0\u{1b}[49m | \u{1b}[42m0-1\u{1b}[49m | \u{1b}[42m0-2\u{1b}[49m \n \u{1b}[42m1\u{1b}[49m | \u{1b}[42m1-0\u{1b}[49m | \u{1b}[42m1-1\u{1b}[49m | \u{1b}[42m1-2\u{1b}[49m \n \u{1b}[42m2\u{1b}[49m | \u{1b}[42m2-0\u{1b}[49m | \u{1b}[42m2-1\u{1b}[49m | \u{1b}[42m2-2\u{1b}[49m " +); + +test_table!( + text_color, + { + use tabled::settings::Color; + Matrix::new(3, 3).with(Style::psql()).with(Modify::new(Segment::all()).with(Color::BG_BLACK)) + }, + " \u{1b}[40mN\u{1b}[49m | \u{1b}[40mcolumn 0\u{1b}[49m | \u{1b}[40mcolumn 1\u{1b}[49m | \u{1b}[40mcolumn 2\u{1b}[49m \n---+----------+----------+----------\n \u{1b}[40m0\u{1b}[49m | \u{1b}[40m0-0\u{1b}[49m | \u{1b}[40m0-1\u{1b}[49m | \u{1b}[40m0-2\u{1b}[49m \n \u{1b}[40m1\u{1b}[49m | \u{1b}[40m1-0\u{1b}[49m | \u{1b}[40m1-1\u{1b}[49m | \u{1b}[40m1-2\u{1b}[49m \n \u{1b}[40m2\u{1b}[49m | \u{1b}[40m2-0\u{1b}[49m | \u{1b}[40m2-1\u{1b}[49m | \u{1b}[40m2-2\u{1b}[49m " +); + +test_table!( + verticals_0, + Matrix::new(3, 3) + .with(Style::rounded().verticals(vec![VerticalLine::new(0, Line::filled('+')), VerticalLine::new(4, Line::filled('+'))])), + "+───┬──────────┬──────────┬──────────+" + "+ N │ column 0 │ column 1 │ column 2 +" + "├───┼──────────┼──────────┼──────────┤" + "+ 0 │ 0-0 │ 0-1 │ 0-2 +" + "+ 1 │ 1-0 │ 1-1 │ 1-2 +" + "+ 2 │ 2-0 │ 2-1 │ 2-2 +" + "+───┴──────────┴──────────┴──────────+" +); + +test_table!( + verticals_1, + Matrix::new(3, 3) + .with(Style::rounded().verticals((1..4).map(|i| VerticalLine::new(i, Line::filled('+'))))), + "╭───+──────────+──────────+──────────╮" + "│ N + column 0 + column 1 + column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 + 0-0 + 0-1 + 0-2 │" + "│ 1 + 1-0 + 1-1 + 1-2 │" + "│ 2 + 2-0 + 2-1 + 2-2 │" + "╰───+──────────+──────────+──────────╯" +); + +test_table!( + verticals_2, + Matrix::new(3, 3).with(Style::rounded().verticals(vec![VerticalLine::new(1, Line::filled('+'))])), + "╭───+──────────┬──────────┬──────────╮" + "│ N + column 0 │ column 1 │ column 2 │" + "├───┼──────────┼──────────┼──────────┤" + "│ 0 + 0-0 │ 0-1 │ 0-2 │" + "│ 1 + 1-0 │ 1-1 │ 1-2 │" + "│ 2 + 2-0 │ 2-1 │ 2-2 │" + "╰───+──────────┴──────────┴──────────╯" +); + +test_table!( + verticals_3, + Matrix::new(3, 3).with(Style::ascii().verticals([VerticalLine::new(1, Line::filled('*'))])), + "+---*----------+----------+----------+" + "| N * column 0 | column 1 | column 2 |" + "+---*----------+----------+----------+" + "| 0 * 0-0 | 0-1 | 0-2 |" + "+---*----------+----------+----------+" + "| 1 * 1-0 | 1-1 | 1-2 |" + "+---*----------+----------+----------+" + "| 2 * 2-0 | 2-1 | 2-2 |" + "+---*----------+----------+----------+" +); + +test_table!( + verticals_4, + Matrix::new(3, 3).with(Style::ascii().verticals((0..10).map(|i| VerticalLine::new(i, Line::new(Some('*'), Some('x'), Some('c'), Some('2')))))), + "c---c----------c----------c----------c" + "* N * column 0 * column 1 * column 2 *" + "x---x----------x----------x----------x" + "* 0 * 0-0 * 0-1 * 0-2 *" + "x---x----------x----------x----------x" + "* 1 * 1-0 * 1-1 * 1-2 *" + "x---x----------x----------x----------x" + "* 2 * 2-0 * 2-1 * 2-2 *" + "2---2----------2----------2----------2" +); + +test_table!( + vertical_line_0, + Matrix::new(3, 3) + .with(HorizontalLine::new(1, Line::new(Some('8'), Some('8'), Some('8'), Some('8')))) + .with(VerticalLine::new(1, Line::new(Some('*'), Some('x'), Some('c'), Some('2')))), + "+---c----------+----------+----------+" + "| N * column 0 | column 1 | column 2 |" + "88888888888888888888888888888888888888" + "| 0 * 0-0 | 0-1 | 0-2 |" + "+---x----------+----------+----------+" + "| 1 * 1-0 | 1-1 | 1-2 |" + "+---x----------+----------+----------+" + "| 2 * 2-0 | 2-1 | 2-2 |" + "+---2----------+----------+----------+" +); + +test_table!( + vertical_line_1, + Matrix::new(3, 3) + .with(Style::empty()) + .with(VerticalLine::new(1, Line::new(Some('*'), Some('x'), Some('c'), Some('2')))), + " c " + " N * column 0 column 1 column 2 " + " 0 * 0-0 0-1 0-2 " + " 1 * 1-0 1-1 1-2 " + " 2 * 2-0 2-1 2-2 " + " 2 " +); + +test_table!( + vertical_line_2, + Matrix::new(3, 3) + .with(Style::empty()) + .with(VerticalLine::new(1, Line::new(None, Some('x'), Some('c'), Some('2')))), + " c " + " N column 0 column 1 column 2 " + " 0 0-0 0-1 0-2 " + " 1 1-0 1-1 1-2 " + " 2 2-0 2-1 2-2 " + " 2 " +); + +test_table!( + vertical_line_3, + Matrix::new(3, 3) + .with(Style::empty()) + .with(VerticalLine::new(1, Line::new(Some('*'), Some('x'), None, None))), + " N * column 0 column 1 column 2 " + " 0 * 0-0 0-1 0-2 " + " 1 * 1-0 1-1 1-2 " + " 2 * 2-0 2-1 2-2 " +); + +test_table!( + override_horizontal_border_on_line, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::single(1)) + .with(BorderChar::horizontal(':', Offset::Begin(0))) + .with(BorderChar::horizontal(':', Offset::End(0))) + ), + "| N | column 0 | column 1 | column 2 |" + "|:-:|:--------:|:--------:|:--------:|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + override_horizontal_border_on_borders, + Matrix::new(3, 3) + .with(Modify::new(Rows::new(..5)) + .with(BorderChar::horizontal(':', Offset::Begin(0))) + .with(BorderChar::horizontal('y', Offset::Begin(3))) + .with(BorderChar::horizontal(':', Offset::End(0))) + .with(BorderChar::horizontal('x', Offset::End(3))) + ), + "+:-:+:--y--x--:+:--y--x--:+:--y--x--:+" + "| N | column 0 | column 1 | column 2 |" + "+:-:+:--y--x--:+:--y--x--:+:--y--x--:+" + "| 0 | 0-0 | 0-1 | 0-2 |" + "+:-:+:--y--x--:+:--y--x--:+:--y--x--:+" + "| 1 | 1-0 | 1-1 | 1-2 |" + "+:-:+:--y--x--:+:--y--x--:+:--y--x--:+" + "| 2 | 2-0 | 2-1 | 2-2 |" + "+:-:+:--y--x--:+:--y--x--:+:--y--x--:+" +); + +test_table!( + override_horizontal_border_on_border, + Matrix::new(3, 3) + .with(Modify::new(Rows::new(..5)) + .with(Border::filled('[')) + .with(BorderChar::horizontal(':', Offset::Begin(0))) + .with(BorderChar::horizontal('y', Offset::Begin(3))) + .with(BorderChar::horizontal(':', Offset::End(0))) + .with(BorderChar::horizontal('x', Offset::End(3))) + ), + "[:[:[:[[y[[x[[:[:[[y[[x[[:[:[[y[[x[[:[" + "[ N [ column 0 [ column 1 [ column 2 [" + "[:[:[:[[y[[x[[:[:[[y[[x[[:[:[[y[[x[[:[" + "[ 0 [ 0-0 [ 0-1 [ 0-2 [" + "[:[:[:[[y[[x[[:[:[[y[[x[[:[:[[y[[x[[:[" + "[ 1 [ 1-0 [ 1-1 [ 1-2 [" + "[:[:[:[[y[[x[[:[:[[y[[x[[:[:[[y[[x[[:[" + "[ 2 [ 2-0 [ 2-1 [ 2-2 [" + "[:[:[:[[y[[x[[:[:[[y[[x[[:[:[[y[[x[[:[" +); + +test_table!( + override_vertical_border_on_line, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::single(1)) + .with(BorderChar::vertical(':', Offset::Begin(0))) + ), + "| N : column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 : 0-0 | 0-1 | 0-2 |" + "| 1 : 1-0 | 1-1 | 1-2 |" + "| 2 : 2-0 | 2-1 | 2-2 |" +); + +test_table!( + override_vertical_border_on_line_1, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::single(1)) + .with(BorderChar::vertical(':', Offset::End(0))) + ), + "| N : column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 : 0-0 | 0-1 | 0-2 |" + "| 1 : 1-0 | 1-1 | 1-2 |" + "| 2 : 2-0 | 2-1 | 2-2 |" +); + +test_table!( + override_vertical_border_on_line_multiline, + Matrix::new(3, 3) + .with(Modify::new(Rows::single(1)).with(Format::content(|s| format!("\nsome text\ntext\n{s}\ntext\ntext\n")))) + .with(Style::markdown()) + .with(Modify::new(Columns::single(1)) + .with(BorderChar::vertical(':', Offset::Begin(4))) + ), + "| N | column 0 | column 1 | column 2 |" + "|-----------|-----------|-----------|-----------|" + "| | | | |" + "| some text | some text | some text | some text |" + "| text | text | text | text |" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| text : text | text | text |" + "| text | text | text | text |" + "| | | | |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + override_vertical_border_on_line_multiline_2, + Matrix::new(3, 3) + .with(Modify::new(Rows::single(1)).with(Format::content(|s| format!("\nsome text\ntext\n{s}\ntext\ntext\n")))) + .with(Style::markdown()) + .with(Modify::new(Columns::single(1)) + .with(BorderChar::vertical(':', Offset::End(4))) + ), + "| N | column 0 | column 1 | column 2 |" + "|-----------|-----------|-----------|-----------|" + "| | | | |" + "| some text | some text | some text | some text |" + "| text : text | text | text |" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| text | text | text | text |" + "| text | text | text | text |" + "| | | | |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + override_vertical_and_horizontal_border_on_line, + Matrix::new(3, 3) + .with(Modify::new(Rows::single(1)).with(Format::content(|s| format!("\nsome text\ntext\n{s}\ntext\ntext\n")))) + .with(Style::markdown()) + .with(Modify::new(Columns::new(..5)) + .with(BorderChar::vertical('y', Offset::Begin(0))) + .with(BorderChar::vertical('^', Offset::End(0))) + ) + .with(Modify::new(Rows::single(1)) + .with(BorderChar::horizontal('x', Offset::Begin(0))) + .with(BorderChar::horizontal('@', Offset::End(0))) + ), + "y N y column 0 y column 1 y column 2 y" + "|x---------@|x---------@|x---------@|x---------@|" + "y y y y y" + "| some text | some text | some text | some text |" + "| text | text | text | text |" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| text | text | text | text |" + "| text | text | text | text |" + "^ ^ ^ ^ ^" + "y 1 y 1-0 y 1-1 y 1-2 y" + "y 2 y 2-0 y 2-1 y 2-2 y" +); + +test_table!( + table_format_alignment_left_test, + format!("{:<}", Table::new(vec!["hello", "world", "!"])), + "+-------+" + "| &str |" + "+-------+" + "| hello |" + "+-------+" + "| world |" + "+-------+" + "| ! |" + "+-------+" +); + +test_table!( + table_format_alignment_right_test, + format!("{:>}", Table::new(vec!["hello", "world", "!"])), + "+-------+" + "| &str |" + "+-------+" + "| hello |" + "+-------+" + "| world |" + "+-------+" + "| ! |" + "+-------+" +); + +test_table!( + table_format_alignment_center_test, + format!("{:^}", Table::new(vec!["hello", "world", "!"])), + "+-------+" + "| &str |" + "+-------+" + "| hello |" + "+-------+" + "| world |" + "+-------+" + "| ! |" + "+-------+" +); + +test_table!( + table_format_width_0_test, + format!("{:<13}", Table::new(vec!["hello", "world", "!"])), + " +-------+" + " | &str |" + " +-------+" + " | hello |" + " +-------+" + " | world |" + " +-------+" + " | ! |" + " +-------+" +); + +test_table!( + table_format_width_1_test, + format!("{:>13}", Table::new(vec!["hello", "world", "!"])), + "+-------+ " + "| &str | " + "+-------+ " + "| hello | " + "+-------+ " + "| world | " + "+-------+ " + "| ! | " + "+-------+ " +); + +test_table!( + table_format_width_2_test, + format!("{:^13}", Table::new(vec!["hello", "world", "!"])), + " +-------+ " + " | &str | " + " +-------+ " + " | hello | " + " +-------+ " + " | world | " + " +-------+ " + " | ! | " + " +-------+ " +); + +test_table!( + table_format_width_3_test, + format!("{:x^13}", Table::new(vec!["hello", "world", "!"])), + "xx+-------+xx" + "xx| &str |xx" + "xx+-------+xx" + "xx| hello |xx" + "xx+-------+xx" + "xx| world |xx" + "xx+-------+xx" + "xx| ! |xx" + "xx+-------+xx" +); + +test_table!( + table_format_width_4_test, + format!("{:x<13}", Table::new(vec!["hello", "world", "!"])), + "xxxx+-------+" + "xxxx| &str |" + "xxxx+-------+" + "xxxx| hello |" + "xxxx+-------+" + "xxxx| world |" + "xxxx+-------+" + "xxxx| ! |" + "xxxx+-------+" +); + +test_table!( + table_format_width_5_test, + format!("{:x>13}", Table::new(vec!["hello", "world", "!"])), + "+-------+xxxx" + "| &str |xxxx" + "+-------+xxxx" + "| hello |xxxx" + "+-------+xxxx" + "| world |xxxx" + "+-------+xxxx" + "| ! |xxxx" + "+-------+xxxx" +); + +test_table!( + table_style_no_bottom_no_new_line, + Matrix::table(0, 0).with(Style::markdown().remove_horizontals()), + "| N |" +); diff --git a/vendor/tabled/tests/settings/width_test.rs b/vendor/tabled/tests/settings/width_test.rs new file mode 100644 index 000000000..d2bc84a46 --- /dev/null +++ b/vendor/tabled/tests/settings/width_test.rs @@ -0,0 +1,2836 @@ +#![cfg(feature = "std")] + +use tabled::{ + grid::util::string::string_width_multiline, + settings::{ + formatting::{TabSize, TrimStrategy}, + object::{Columns, Object, Rows, Segment}, + peaker::{PriorityMax, PriorityMin}, + width::{Justify, MinWidth, SuffixLimit, Width}, + Alignment, Margin, Modify, Padding, Panel, Settings, Span, Style, + }, +}; + +use crate::matrix::Matrix; +use testing_table::{is_lines_equal, static_table, test_table}; + +#[cfg(feature = "color")] +use ::{ansi_str::AnsiStr, owo_colors::OwoColorize}; + +#[cfg(all(feature = "derive", feature = "color"))] +use ::owo_colors::AnsiColors; + +test_table!( + max_width, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::new(1..).not(Rows::single(0))).with(Width::truncate(1))), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0 | 0 | 0 |" + "| 1 | 1 | 1 | 1 |" + "| 2 | 2 | 2 | 2 |" +); + +test_table!( + max_width_with_suffix, + Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Columns::new(1..).not(Rows::single(0))) + .with(Width::truncate(2).suffix("...")), + ), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | .. | .. | .. |" + "| 1 | .. | .. | .. |" + "| 2 | .. | .. | .. |" +); + +test_table!( + max_width_doesnt_icrease_width_if_it_is_smaller, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::new(1..).not(Rows::single(0))).with(Width::truncate(50))), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + max_width_wrapped, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::new(1..).not(Rows::single(0))).with(Width::wrap(2))), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0- | 0- | 0- |" + "| | 0 | 1 | 2 |" + "| 1 | 1- | 1- | 1- |" + "| | 0 | 1 | 2 |" + "| 2 | 2- | 2- | 2- |" + "| | 0 | 1 | 2 |" +); + +test_table!( + max_width_wrapped_does_nothing_if_str_is_smaller, + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Columns::new(1..).not(Rows::single(0))).with(Width::wrap(100))), + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" +); + +test_table!( + max_width_wrapped_keep_words_0, + { + let table = Matrix::iter(vec!["this is a long sentence"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + assert!(is_lines_equal(&table, 17 + 2 + 2)); + + table + }, + "| &str |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +test_table!( + max_width_wrapped_keep_words_1, + { + let table = Matrix::iter(vec!["this is a long sentence"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + assert!(is_lines_equal(&table, 17 + 2 + 2)); + + table + }, + "| &str |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +test_table!( + max_width_wrapped_keep_words_2, + { + let table = Matrix::iter(vec!["this is a long sentence"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + assert!(is_lines_equal(&table, 17 + 2 + 2)); + + table + }, + "| &str |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_3, + { + let table = Matrix::iter(vec!["this is a long sentence"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + assert!(is_lines_equal(&table, 17 + 2 + 2)); + + table + }, + // 'sentence' doesn't have a space ' sentence' because we use left alignment + "| &str |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +#[cfg(not(feature = "color"))] +test_table!( + max_width_wrapped_keep_words_3, + { + let table = Matrix::iter(vec!["this is a long sentence"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + assert!(is_lines_equal(&table, 17 + 2 + 2)); + + table + }, + // 'sentence' doesn't have a space ' sentence' because we use left alignment + "| &str |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +test_table!( + max_width_wrapped_keep_words_4, + { + let table = Matrix::iter(vec!["this"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::wrap(10).keep_words())) + .to_string(); + + assert!(is_lines_equal(&table, 8)); + + table + }, + "| &str |" + "|------|" + "| this |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_0, + { + let table = Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + AnsiStr::ansi_strip(&table).to_string() + }, + "| String |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_0_1, + Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())), + "| String |" + "|-------------------|" + "| \u{1b}[32m\u{1b}[40mthis is a long \u{1b}[39m\u{1b}[49m |" + "| \u{1b}[32m\u{1b}[40msentence\u{1b}[39m\u{1b}[49m |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_1, + { + let table = Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + AnsiStr::ansi_strip(&table).to_string() + }, + "| String |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_1_1, + Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())), + "| String |" + "|-------------------|" + "| \u{1b}[32m\u{1b}[40mthis is a long \u{1b}[39m\u{1b}[49m |" + "| \u{1b}[32m\u{1b}[40msentence\u{1b}[39m\u{1b}[49m |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_2, + { + let table = Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + AnsiStr::ansi_strip(&table).to_string() + }, + "| String |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_2_1, + Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())), + "| String |" + "|-------------------|" + "| \u{1b}[32m\u{1b}[40mthis is a long \u{1b}[39m\u{1b}[49m |" + "| \u{1b}[32m\u{1b}[40msentence\u{1b}[39m\u{1b}[49m |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_3, + { + let table = Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + AnsiStr::ansi_strip(&table).to_string() + }, + "| String |" + "|-------------------|" + "| this is a long |" + "| sentence |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_3_1, + Matrix::iter(vec!["this is a long sentence".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())), + "| String |" + "|-------------------|" + "| \u{1b}[32m\u{1b}[40mthis is a long \u{1b}[39m\u{1b}[49m |" + "| \u{1b}[32m\u{1b}[40m sentence\u{1b}[39m\u{1b}[49m |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_4, + { + let table = Matrix::iter(vec!["this".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::wrap(10).keep_words())) + .to_string(); + + AnsiStr::ansi_strip(&table).to_string() + }, + "| String |" + "|--------|" + "| this |" +); + +#[cfg(feature = "color")] +test_table!( + max_width_wrapped_keep_words_color_4_1, + Matrix::iter(vec!["this".on_black().green().to_string()]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::wrap(10).keep_words())), + "| String |" + "|--------|" + "| \u{1b}[32;40mthis\u{1b}[0m |" +); + +test_table!( + max_width_wrapped_keep_words_long_word, + Matrix::iter(["this is a long sentencesentencesentence"]) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())), + "| &str |" + "|-------------------|" + "| this is a long se |" + "| ntencesentencesen |" + "| tence |" +); + +#[cfg(feature = "color")] +#[test] +fn max_width_wrapped_keep_words_long_word_color() { + let data = vec!["this is a long sentencesentencesentence" + .on_black() + .green() + .to_string()]; + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Modify::new(Segment::all()).with(Width::wrap(17).keep_words())) + .to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + "| String |" + "|-------------------|" + "| this is a long se |" + "| ntencesentencesen |" + "| tence |" + ) + ); + + assert_eq!( + table, + static_table!( + "| String |" + "|-------------------|" + "| \u{1b}[32m\u{1b}[40mthis is a long se\u{1b}[39m\u{1b}[49m |" + "| \u{1b}[32m\u{1b}[40mntencesentencesen\u{1b}[39m\u{1b}[49m |" + "| \u{1b}[32m\u{1b}[40mtence\u{1b}[39m\u{1b}[49m |" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn max_width_keep_words_1() { + use tabled::settings::style::HorizontalLine; + + let table = Matrix::iter(["asdf"]) + .with(Width::wrap(7).keep_words()) + .to_string(); + + assert_eq!( + table, + static_table!( + "+-----+" + "| &st |" + "| r |" + "+-----+" + "| asd |" + "| f |" + "+-----+" + ) + ); + + let table = Matrix::iter(["qweqw eqwe"]) + .with(Width::wrap(8).keep_words()) + .to_string(); + + assert_eq!( + table, + static_table!( + "+------+" + "| &str |" + "+------+" + "| qweq |" + "| w |" + "| eqwe |" + "+------+" + ) + ); + + let table = Matrix::iter([ + ["123 45678", "qweqw eqwe", "..."], + ["0", "1", "..."], + ["0", "1", "..."], + ]) + .with( + Style::modern() + .remove_horizontal() + .horizontals([HorizontalLine::new(1, Style::modern().get_horizontal())]), + ) + .with(Width::wrap(21).keep_words().priority::<PriorityMax>()) + .with(Alignment::center()) + .to_string(); + + assert_eq!( + table, + static_table!( + "┌──────┬──────┬─────┐" + "│ 0 │ 1 │ 2 │" + "├──────┼──────┼─────┤" + "│ 123 │ qweq │ ... │" + "│ 4567 │ w │ │" + "│ 8 │ eqwe │ │" + "│ 0 │ 1 │ ... │" + "│ 0 │ 1 │ ... │" + "└──────┴──────┴─────┘" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn max_width_wrapped_collored() { + let data = &[ + "asd".red().to_string(), + "zxc2".blue().to_string(), + "asdasd".on_black().green().to_string(), + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::wrap(2))) + .to_string(); + + assert_eq!( + table, + "| St |\n| ri |\n| ng |\n|----|\n| \u{1b}[31mas\u{1b}[39m |\n| \u{1b}[31md\u{1b}[39m |\n| \u{1b}[34mzx\u{1b}[39m |\n| \u{1b}[34mc2\u{1b}[39m |\n| \u{1b}[32m\u{1b}[40mas\u{1b}[39m\u{1b}[49m |\n| \u{1b}[32m\u{1b}[40mda\u{1b}[39m\u{1b}[49m |\n| \u{1b}[32m\u{1b}[40msd\u{1b}[39m\u{1b}[49m |" + ); +} + +#[test] +fn dont_change_content_if_width_is_less_then_max_width() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::truncate(1000).suffix("..."))) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn max_width_with_emoji() { + let data = &["🤠", "😳🥵🥶😱😨", "🚴🏻♀️🚴🏻🚴🏻♂️🚵🏻♀️🚵🏻🚵🏻♂️"]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::truncate(6).suffix("..."))) + .to_string(); + + assert_eq!( + table, + static_table!( + "| &str |" + "|--------|" + "| 🤠 |" + "| 😳�... |" + "| 🚴�... |" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn color_chars_are_stripped() { + let data = &[ + "asd".red().to_string(), + "zxc".blue().to_string(), + "asdasd".on_black().green().to_string(), + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Width::truncate(4).suffix("..."))) + .to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + "| S... |" + "|------|" + "| asd |" + "| zxc |" + "| a... |" + ) + ); + + assert_eq!( + table, + "| S... |\n|------|\n| \u{1b}[31masd\u{1b}[39m |\n| \u{1b}[34mzxc\u{1b}[39m |\n| \u{1b}[32;40ma\u{1b}[39m\u{1b}[49m... |", + ); +} + +#[test] +fn min_width() { + let mut table = Matrix::table(3, 3); + table + .with(Style::markdown()) + .with(Modify::new(Rows::single(0)).with(MinWidth::new(12))); + + assert_eq!( + table.to_string(), + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|--------------|--------------|--------------|--------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ), + ); + + table.with(Modify::new(Segment::all()).with(TrimStrategy::None)); + + assert_eq!( + table.to_string(), + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|--------------|--------------|--------------|--------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ), + ); +} + +#[test] +fn min_width_with_filler() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::single(0)).with(MinWidth::new(12).fill_with('.'))) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N........... | column 0.... | column 1.... | column 2.... |" + "|--------------|--------------|--------------|--------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn min_width_one_column() { + let mut table = Matrix::table(3, 3); + table + .with(Style::markdown()) + .with(Modify::new((0, 0)).with(MinWidth::new(5))); + + assert_eq!( + table.to_string(), + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|-------|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); + + table.with(Modify::new(Segment::all()).with(TrimStrategy::None)); + + assert_eq!( + table.to_string(), + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|-------|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn min_width_on_smaller_content() { + assert_eq!( + Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Rows::single(0)).with(MinWidth::new(1))) + .to_string(), + Matrix::new(3, 3).with(Style::markdown()).to_string() + ); +} + +#[test] +fn min_with_max_width() { + let mut table = Matrix::table(3, 3); + table + .with(Style::markdown()) + .with(Modify::new(Rows::single(0)).with(MinWidth::new(3))) + .with(Modify::new(Rows::single(0)).with(Width::truncate(3))); + + assert_eq!( + table.to_string(), + static_table!( + "| N | col | col | col |" + "|-----|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); + + table.with(Modify::new(Segment::all()).with(TrimStrategy::None)); + + assert_eq!( + table.to_string(), + static_table!( + "| N | col | col | col |" + "|-----|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn min_with_max_width_truncate_suffix() { + let mut table = Matrix::table(3, 3); + table + .with(Style::markdown()) + .with(Modify::new(Rows::single(0)).with(MinWidth::new(3))) + .with(Modify::new(Rows::single(0)).with(Width::truncate(3).suffix("..."))); + + assert_eq!( + table.to_string(), + static_table!( + "| N | ... | ... | ... |" + "|-----|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); + + table.with(Modify::new(Segment::all()).with(TrimStrategy::None)); + + assert_eq!( + table.to_string(), + static_table!( + "| N | ... | ... | ... |" + "|-----|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn min_with_max_width_truncate_suffix_limit_replace() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Rows::single(0)).with( + Width::truncate(3) + .suffix("...") + .suffix_limit(SuffixLimit::Replace('x')), + ), + ) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | xxx | xxx | xxx |" + "|---|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn min_with_max_width_truncate_suffix_limit_cut() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Rows::single(0)).with( + Width::truncate(3) + .suffix("qwert") + .suffix_limit(SuffixLimit::Cut), + ), + ) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | qwe | qwe | qwe |" + "|---|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn min_with_max_width_truncate_suffix_limit_ignore() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with( + Modify::new(Rows::single(0)).with( + Width::truncate(3) + .suffix("qwert") + .suffix_limit(SuffixLimit::Ignore), + ), + ) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | col | col | col |" + "|---|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn min_with_max_width_truncate_suffix_try_color() { + let data = &[ + "asd".red().to_string(), + "zxc".blue().to_string(), + "asdasd".on_black().green().to_string(), + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Width::truncate(7).suffix("..").suffix_try_color(true)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| S.. |" + "|-----|" + "| \u{1b}[31masd\u{1b}[39m |" + "| \u{1b}[34mzxc\u{1b}[39m |" + "| \u{1b}[32;40ma\u{1b}[39m\u{1b}[49m\u{1b}[32m\u{1b}[40m..\u{1b}[39m\u{1b}[49m |" + ) + ); +} + +#[cfg(feature = "color")] +#[test] +fn min_width_color() { + let data = &[ + "asd".red().to_string(), + "zxc".blue().to_string(), + "asdasd".on_black().green().to_string(), + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(MinWidth::new(10))) + .to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + "| String |" + "|------------|" + "| asd |" + "| zxc |" + "| asdasd |" + ) + ); + + assert_eq!( + table, + "| String |\n|------------|\n| \u{1b}[31masd\u{1b}[39m |\n| \u{1b}[34mzxc\u{1b}[39m |\n| \u{1b}[32;40masdasd\u{1b}[0m |", + ); +} + +#[cfg(feature = "color")] +#[test] +fn min_width_color_with_smaller_then_width() { + let data = &[ + "asd".red().to_string(), + "zxc".blue().to_string(), + "asdasd".on_black().green().to_string(), + ]; + + assert_eq!( + Matrix::iter(data) + .with(Modify::new(Segment::all()).with(MinWidth::new(1))) + .to_string(), + Matrix::iter(data).to_string() + ); +} + +#[test] +fn total_width_big() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Width::truncate(80)) + .with(MinWidth::new(80)) + .to_string(); + + assert_eq!(string_width_multiline(&table), 80); + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|--------------|---------------------|--------------------|--------------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); + + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(TrimStrategy::None)) + .with(Settings::new(Width::truncate(80), Width::increase(80))) + .to_string(); + + assert_eq!(string_width_multiline(&table), 80); + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|--------------|---------------------|--------------------|--------------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn total_width_big_with_panel() { + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with( + Modify::new(Segment::all()) + .with(Alignment::center()) + .with(Padding::zero()), + ) + .with(Style::markdown()) + .with(Width::truncate(80)) + .with(MinWidth::new(80)) + .to_string(); + + assert!(is_lines_equal(&table, 80)); + assert_eq!( + table, + static_table!( + "| Hello World |" + "|--------------|---------------------|--------------------|--------------------|" + "| N | column 0 | column 1 | column 2 |" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn total_width_big_with_panel_with_wrapping_doesnt_affect_increase() { + let table1 = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::wrap(80)) + .with(MinWidth::new(80)) + .to_string(); + + let table2 = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(80)) + .with(MinWidth::new(80)) + .to_string(); + + assert_eq!(table1, table2); +} + +#[test] +fn total_width_small() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Width::truncate(14)) + .with(MinWidth::new(14)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | | | c |" + "|--|--|--|---|" + "| | | | 0 |" + "| | | | 1 |" + "| | | | 2 |" + ) + ); + assert!(is_lines_equal(&table, 14)); +} + +#[test] +fn total_width_smaller_then_content() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Width::truncate(8)) + .with(MinWidth::new(8)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | | | |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); +} + +#[test] +fn total_width_small_with_panel() { + let table = Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(20)) + .with(MinWidth::new(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | co | co | col |" + "|--|----|----|-----|" + "| | 0- | 0- | 0-2 |" + "| | 1- | 1- | 1-2 |" + "| | 2- | 2- | 2-2 |" + ) + ); + assert!(is_lines_equal(&table, 20)); + + let table = Matrix::iter(Vec::<usize>::new()) + .with(Panel::horizontal(0, "Hello World")) + .with( + Modify::new(Segment::all()) + .with(Alignment::center()) + .with(Padding::zero()), + ) + .with(Width::truncate(5)) + .with(MinWidth::new(5)) + .to_string(); + + assert_eq!( + table, + static_table!("+---+" "|Hel|" "+---+" "|usi|" "+---+") + ); + assert!(is_lines_equal(&table, 5)); + + let table = Matrix::table(1, 2) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(20)) + .with(MinWidth::new(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello World |" + "|--|-------|-------|" + "| | colum | colum |" + "| | 0-0 | 0-1 |" + ) + ); + assert!(is_lines_equal(&table, 20)); + + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(20)) + .with(MinWidth::new(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello World |" + "|--|----|----|-----|" + "| | co | co | col |" + "| | 0- | 0- | 0-2 |" + "| | 1- | 1- | 1-2 |" + "| | 2- | 2- | 2-2 |" + ) + ); + assert!(is_lines_equal(&table, 20)); + + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(6)) + .with(MinWidth::new(6)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello Wor |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); + assert!(is_lines_equal(&table, 13)); + + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(14)) + .with(MinWidth::new(14)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello Worl |" + "|--|--|--|---|" + "| | | | c |" + "| | | | 0 |" + "| | | | 1 |" + "| | | | 2 |" + ) + ); + assert!(is_lines_equal(&table, 14)); + + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World 123")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::truncate(14)) + .with(MinWidth::new(14)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello Worl |" + "|--|--|--|---|" + "| | | | c |" + "| | | | 0 |" + "| | | | 1 |" + "| | | | 2 |" + ) + ); + assert!(is_lines_equal(&table, 14)); +} + +#[cfg(feature = "color")] +#[test] +fn total_width_wrapping() { + let table = Matrix::new(3, 3) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::wrap(20)) + .with(MinWidth::new(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | co | co | col |" + "| | lu | lu | umn |" + "| | mn | mn | 2 |" + "| | 0 | 1 | |" + "|--|----|----|-----|" + "| | 0- | 0- | 0-2 |" + "| | 0 | 1 | |" + "| | 1- | 1- | 1-2 |" + "| | 0 | 1 | |" + "| | 2- | 2- | 2-2 |" + "| | 0 | 1 | |" + ) + ); + assert!(is_lines_equal(&table, 20)); + + let table = Matrix::new(3, 3) + .insert((3, 2), "some loong string") + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::wrap(20).keep_words()) + .with(MinWidth::new(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | | column | |" + "| | | 1 | |" + "|--|--|---------|--|" + "| | | 0-1 | |" + "| | | 1-1 | |" + "| | | some | |" + "| | | loong | |" + "| | | string | |" + ) + ); + assert!(is_lines_equal(&table, 20)); +} + +#[test] +fn total_width_small_with_panel_using_wrapping() { + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::wrap(20)) + .with(MinWidth::new(20)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello World |" + "|--|----|----|-----|" + "| | co | co | col |" + "| | lu | lu | umn |" + "| | mn | mn | 2 |" + "| | 0 | 1 | |" + "| | 0- | 0- | 0-2 |" + "| | 0 | 1 | |" + "| | 1- | 1- | 1-2 |" + "| | 0 | 1 | |" + "| | 2- | 2- | 2-2 |" + "| | 0 | 1 | |" + ) + ); + assert!(is_lines_equal(&table, 20)); + + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::wrap(14)) + .with(MinWidth::new(14)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello Worl |" + "| d |" + "|--|--|--|---|" + "| | | | c |" + "| | | | o |" + "| | | | l |" + "| | | | u |" + "| | | | m |" + "| | | | n |" + "| | | | |" + "| | | | 2 |" + "| | | | 0 |" + "| | | | - |" + "| | | | 2 |" + "| | | | 1 |" + "| | | | - |" + "| | | | 2 |" + "| | | | 2 |" + "| | | | - |" + "| | | | 2 |" + ) + ); + assert!(is_lines_equal(&table, 14)); + + let table = Matrix::new(3, 3) + .with(Panel::horizontal(0, "Hello World 123")) + .with(Modify::new(Segment::all()).with(Alignment::center())) + .with(Style::markdown()) + .with(Width::wrap(14)) + .with(MinWidth::new(14)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| Hello Worl |" + "| d 123 |" + "|--|--|--|---|" + "| | | | c |" + "| | | | o |" + "| | | | l |" + "| | | | u |" + "| | | | m |" + "| | | | n |" + "| | | | |" + "| | | | 2 |" + "| | | | 0 |" + "| | | | - |" + "| | | | 2 |" + "| | | | 1 |" + "| | | | - |" + "| | | | 2 |" + "| | | | 2 |" + "| | | | - |" + "| | | | 2 |" + ) + ); + assert!(is_lines_equal(&table, 14)); +} + +#[test] +fn max_width_with_span() { + let mut table = Matrix::new(3, 3).insert((1, 1), "a long string").to_table(); + table + .with(Style::psql()) + .with(Modify::new((1, 1)).with(Span::column(2))) + .with(Modify::new((2, 2)).with(Span::column(2))); + + table.with(Width::truncate(40)); + + assert_eq!( + table.to_string(), + static_table!( + " N | column 0 | column 1 | column 2 " + "---+----------+----------+----------" + " 0 | a long string | 0-2 " + " 1 | 1-0 | 1-1 " + " 2 | 2-0 | 2-1 | 2-2 " + ) + ); + assert!(is_lines_equal(&table.to_string(), 36)); + + table.with(Width::truncate(20)); + + assert_eq!( + table.to_string(), + static_table!( + " | col | col | col " + "--+-----+-----+-----" + " | a long st | 0-2 " + " | 1-0 | 1-1 " + " | 2-0 | 2-1 | 2-2 " + ) + ); + assert!(is_lines_equal(&table.to_string(), 20)); + + table.with(Width::truncate(10)); + + assert_eq!( + table.to_string(), + static_table!( + " | | | " + "--+--+--+--" + " | a l | " + " | | 1-1 " + " | | | " + ) + ); + assert!(is_lines_equal(&table.to_string(), 11)); +} + +#[test] +fn min_width_works_with_right_alignment() { + let json = r#" + { + "some": "random", + "json": [ + { "1": "2" }, + { "1": "2" }, + { "1": "2" } + ] + } + "#; + + let mut table = Matrix::iter([json]); + table + .with(Style::markdown()) + .with( + Modify::new(Segment::all()) + .with(Alignment::right()) + .with(TrimStrategy::None), + ) + .with(MinWidth::new(50)); + + assert_eq!(string_width_multiline(&table.to_string()), 50); + assert_eq!( + table.to_string(), + static_table!( + "| &str |" + "|------------------------------------------------|" + "| |" + "| { |" + "| \"some\": \"random\", |" + "| \"json\": [ |" + "| { \"1\": \"2\" }, |" + "| { \"1\": \"2\" }, |" + "| { \"1\": \"2\" } |" + "| ] |" + "| } |" + "| |" + ) + ); + + table + .with(Modify::new(Segment::all()).with(TrimStrategy::Horizontal)) + .with(MinWidth::new(50)); + + assert_eq!( + table.to_string(), + static_table!( + r#"| &str |"# + r#"|------------------------------------------------|"# + r#"| |"# + r#"| { |"# + r#"| "some": "random", |"# + r#"| "json": [ |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" } |"# + r#"| ] |"# + r#"| } |"# + r#"| |"# + ) + ); + assert!(is_lines_equal(&table.to_string(), 50)); + + table + .with(Modify::new(Segment::all()).with(TrimStrategy::Both)) + .with(MinWidth::new(50)); + + assert_eq!( + table.to_string(), + static_table!( + r#"| &str |"# + r#"|------------------------------------------------|"# + r#"| { |"# + r#"| "some": "random", |"# + r#"| "json": [ |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" } |"# + r#"| ] |"# + r#"| } |"# + r#"| |"# + r#"| |"# + ) + ); + assert!(is_lines_equal(&table.to_string(), 50)); + + let mut table = Matrix::iter([json]); + table + .with(Style::markdown()) + .with( + Modify::new(Segment::all()) + .with(Alignment::center()) + .with(TrimStrategy::None), + ) + .with(MinWidth::new(50)); + + assert_eq!( + table.to_string(), + static_table!( + "| &str |" + "|------------------------------------------------|" + "| |" + "| { |" + "| \"some\": \"random\", |" + "| \"json\": [ |" + "| { \"1\": \"2\" }, |" + "| { \"1\": \"2\" }, |" + "| { \"1\": \"2\" } |" + "| ] |" + "| } |" + "| |" + ) + ); + assert_eq!(string_width_multiline(&table.to_string()), 50); + + table + .with(Modify::new(Segment::all()).with(TrimStrategy::Horizontal)) + .with(MinWidth::new(50)); + + assert_eq!( + table.to_string(), + static_table!( + r#"| &str |"# + r#"|------------------------------------------------|"# + r#"| |"# + r#"| { |"# + r#"| "some": "random", |"# + r#"| "json": [ |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" } |"# + r#"| ] |"# + r#"| } |"# + r#"| |"# + ) + ); + assert!(is_lines_equal(&table.to_string(), 50)); + + table + .with(Modify::new(Segment::all()).with(TrimStrategy::Both)) + .with(MinWidth::new(50)); + + assert_eq!( + table.to_string(), + static_table!( + r#"| &str |"# + r#"|------------------------------------------------|"# + r#"| { |"# + r#"| "some": "random", |"# + r#"| "json": [ |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" }, |"# + r#"| { "1": "2" } |"# + r#"| ] |"# + r#"| } |"# + r#"| |"# + r#"| |"# + ) + ); + assert!(is_lines_equal(&table.to_string(), 50)); +} + +#[test] +fn min_width_with_span_1() { + let data = [ + ["0", "1"], + ["a long string which will affect min width logic", ""], + ["2", "3"], + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new((1, 0)).with(Span::column(2))) + .with(MinWidth::new(100)) + .to_string(); + + assert_eq!(string_width_multiline(&table), 100); + assert_eq!( + table, + static_table!( + "| 0 | 1 |" + "|------------------------------------------------------------------------|-------------------------|" + "| 0 |" + "| a long string which will affect min width logic | |" + "| 2 | 3 |" + ) + ); + assert!(is_lines_equal(&table, 100)); +} + +#[test] +fn min_width_with_span_2() { + let data = [ + ["0", "1"], + ["a long string which will affect min width logic", ""], + ["2", "3"], + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new((2, 0)).with(Span::column(2))) + .with(MinWidth::new(100)) + .to_string(); + + assert_eq!(string_width_multiline(&table), 100); + assert_eq!( + table, + static_table!( + "| 0 | 1 |" + "|-------------------------------------------------|------------------------------------------------|" + "| 0 | 1 |" + "| a long string which will affect min width logic |" + "| 2 | 3 |" + ) + ); +} + +#[test] +fn justify_width_constant_test() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Justify::new(3)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | col | col | col |" + "|-----|-----|-----|-----|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn justify_width_constant_different_sizes_test() { + let table = Matrix::new(3, 3) + .insert((1, 1), "Hello World") + .insert((3, 2), "multi\nline string\n") + .with(Style::markdown()) + .with(Justify::new(3)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | col | col | col |" + "|-----|-----|-----|-----|" + "| 0 | Hel | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | mul | 2-2 |" + ) + ); +} + +#[test] +fn justify_width_constant_0_test() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Justify::new(0)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | | | |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); +} + +#[test] +fn justify_width_min_test() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Justify::min()) + .to_string(); + + println!("{table}"); + + assert_eq!( + table, + static_table!( + "| N | c | c | c |" + "|---|---|---|---|" + "| 0 | 0 | 0 | 0 |" + "| 1 | 1 | 1 | 1 |" + "| 2 | 2 | 2 | 2 |" + ) + ); +} + +#[test] +fn justify_width_max_test() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Justify::max()) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|----------|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); +} + +#[test] +fn max_width_when_cell_has_tabs() { + let table = Matrix::new(3, 3) + .insert((2, 1), "\tHello\tWorld\t") + .with(TabSize::new(4)) + .with(Style::markdown()) + .with(Modify::new(Columns::new(..)).with(Width::truncate(1))) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | c | c | c |" + "|---|---|---|---|" + "| 0 | 0 | 0 | 0 |" + "| 1 | | 1 | 1 |" + "| 2 | 2 | 2 | 2 |" + ) + ); +} + +#[test] +fn max_width_table_when_cell_has_tabs() { + let table = Matrix::new(3, 3) + .insert((2, 1), "\tHello\tWorld\t") + .with(TabSize::new(4)) + .with(Style::markdown()) + .with(Width::truncate(15)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | co | | |" + "|--|----|--|--|" + "| | 0- | | |" + "| | | | |" + "| | 2- | | |" + ) + ); +} + +// WE GOT [["", "column 0", "column 1 ", "column 2 "], ["", "0-0 ", "0-1 ", "0-2 "], ["", "Hello World With Big Line; Here w", "1-1", "1-2"], ["", "2-0 ", "Hello World With Big L", "2-2"]] +// [2, 10, 11, 12] +// 40 55 40 + +// BEFORE ADJ [2, 10, 11, 12] + +// WE GOT [["", "column 0", "column 1", "column 2"], ["", "0-0", "0-1", "0-2"], ["", "Hello World With Big Line; Here w", "1-1", "1-2"], ["", "2-0", "Hello World With Big L", "2-2"]] +// [2, 11, 12, 11] +// 41 55 40 + +// adj [2, 10, 10, 10] + +#[test] +fn max_width_truncate_with_big_span() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line; Here we gooooooo") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(3))) + .with(Width::truncate(40)) + .to_string(); + + assert_eq!(string_width_multiline(&table), 40); + assert_eq!( + table, + static_table!( + "| | column 0 | column 1 | column 2 |" + "|--|-----------|-----------|-----------|" + "| | 0-0 | 0-1 | 0-2 |" + "| | Hello World With Big Line; Here w |" + "| | 2-0 | 2-1 | 2-2 |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line; Here we gooooooo") + .insert((3, 2), "Hello World With Big Line; Here") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(3))) + .with(Modify::new((3, 2)).with(Span::column(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|---|-----------|----------------|----------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | Hello World With Big Line; Here we gooooooo |" + "| 2 | 2-0 | Hello World With Big Line; Here |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line; Here we gooooooo") + .insert((3, 2), "Hello World With Big Line; Here") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(3))) + .with(Modify::new((3, 2)).with(Span::column(2))) + .with(Width::truncate(40)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | colum | column 1 | column 2 |" + "|--|-------|-------------|-------------|" + "| | 0-0 | 0-1 | 0-2 |" + "| | Hello World With Big Line; Here w |" + "| | 2-0 | Hello World With Big Line |" + ) + ); + assert_eq!(string_width_multiline(&table), 40); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line; Here we gooooooo") + .insert((3, 2), "Hello World With Big Line; Here") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(2))) + .with(Modify::new((3, 2)).with(Span::column(2))) + .with(Width::truncate(40)) + .to_string(); + + assert_eq!(string_width_multiline(&table), 40); + assert_eq!( + table, + static_table!( + "| | column 0 | column 1 | c |" + "|--|---------------|---------------|---|" + "| | 0-0 | 0-1 | 0 |" + "| | Hello World With Big Line; He | 1 |" + "| | 2-0 | Hello World With |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line; Here w") + .insert((3, 2), "Hello World With Big L") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(3))) + .with(Modify::new((3, 2)).with(Span::column(2))) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|---|----------|------------|-----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | Hello World With Big Line; Here w |" + "| 2 | 2-0 | Hello World With Big L |" + ) + ); +} + +#[test] +fn max_width_truncate_priority_max() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::truncate(35).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 35)); + assert_eq!( + table, + static_table!( + "| N | column | column | column |" + "|---|---------|---------|---------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | Hello W | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::truncate(20).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 20)); + assert_eq!( + table, + static_table!( + "| N | co | co | co |" + "|---|----|----|----|" + "| 0 | 0- | 0- | 0- |" + "| 1 | He | 1- | 1- |" + "| 2 | 2- | 2- | 2- |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::truncate(0).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 13)); + assert_eq!( + table, + static_table!( + "| | | | |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); +} + +#[test] +fn max_width_truncate_priority_max_with_span() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(2))) + .with(Width::truncate(15).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 15)); + assert_eq!( + table, + static_table!( + "| N | c | | |" + "|---|---|--|--|" + "| 0 | 0 | | |" + "| 1 | Hell | |" + "| 2 | 2 | | |" + ) + ); +} + +#[test] +fn max_width_wrap_priority_max() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::wrap(35).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 35)); + assert_eq!( + table, + static_table!( + "| N | column | column | column |" + "| | 0 | 1 | 2 |" + "|---|---------|---------|---------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | Hello W | 1-1 | 1-2 |" + "| | orld Wi | | |" + "| | th Big | | |" + "| | Line | | |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::wrap(20).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 20)); + assert_eq!( + table, + static_table!( + "| N | co | co | co |" + "| | lu | lu | lu |" + "| | mn | mn | mn |" + "| | 0 | 1 | 2 |" + "|---|----|----|----|" + "| 0 | 0- | 0- | 0- |" + "| | 0 | 1 | 2 |" + "| 1 | He | 1- | 1- |" + "| | ll | 1 | 2 |" + "| | o | | |" + "| | Wo | | |" + "| | rl | | |" + "| | d | | |" + "| | Wi | | |" + "| | th | | |" + "| | B | | |" + "| | ig | | |" + "| | L | | |" + "| | in | | |" + "| | e | | |" + "| 2 | 2- | 2- | 2- |" + "| | 0 | 1 | 2 |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::wrap(0).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 13)); + assert_eq!( + table, + static_table!( + "| | | | |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); +} + +#[test] +fn max_width_wrap_priority_max_with_span() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(2))) + .with(Width::wrap(15).priority::<PriorityMax>()) + .to_string(); + + assert!(is_lines_equal(&table, 15)); + assert_eq!( + table, + static_table!( + "| N | c | | |" + "| | o | | |" + "| | l | | |" + "| | u | | |" + "| | m | | |" + "| | n | | |" + "| | | | |" + "| | 0 | | |" + "|---|---|--|--|" + "| 0 | 0 | | |" + "| | - | | |" + "| | 0 | | |" + "| 1 | Hell | |" + "| | o Wo | |" + "| | rld | |" + "| | With | |" + "| | Big | |" + "| | Lin | |" + "| | e | |" + "| 2 | 2 | | |" + "| | - | | |" + "| | 0 | | |" + ) + ); +} + +#[test] +fn max_width_truncate_priority_min() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::truncate(35).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 35)); + assert_eq!( + table, + static_table!( + "| | column 0 | | |" + "|--|------------------------|--|--|" + "| | 0-0 | | |" + "| | Hello World With Big L | | |" + "| | 2-0 | | |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::truncate(20).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 20)); + assert_eq!( + table, + static_table!( + "| | column | | |" + "|--|---------|--|--|" + "| | 0-0 | | |" + "| | Hello W | | |" + "| | 2-0 | | |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::truncate(0).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 13)); + assert_eq!( + table, + static_table!( + "| | | | |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); +} + +#[test] +fn max_width_truncate_priority_min_with_span() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(2))) + .with(Width::truncate(15).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 15)); + assert_eq!( + table, + static_table!( + "| | | co | |" + "|--|--|----|--|" + "| | | 0- | |" + "| | Hello | |" + "| | | 2- | |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(2))) + .with(Width::truncate(17).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 17)); + assert_eq!( + table, + static_table!( + "| | | colu | |" + "|--|--|------|--|" + "| | | 0-1 | |" + "| | Hello W | |" + "| | | 2-1 | |" + ) + ); +} + +#[test] +fn max_width_wrap_priority_min() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::wrap(35).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 35)); + assert_eq!( + table, + static_table!( + "| | column 0 | | |" + "|--|------------------------|--|--|" + "| | 0-0 | | |" + "| | Hello World With Big L | | |" + "| | ine | | |" + "| | 2-0 | | |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::wrap(20).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 20)); + assert_eq!( + table, + static_table!( + "| | column | | |" + "| | 0 | | |" + "|--|---------|--|--|" + "| | 0-0 | | |" + "| | Hello W | | |" + "| | orld Wi | | |" + "| | th Big | | |" + "| | Line | | |" + "| | 2-0 | | |" + ) + ); + + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Width::wrap(0).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 13)); + assert_eq!( + table, + static_table!( + "| | | | |" + "|--|--|--|--|" + "| | | | |" + "| | | | |" + "| | | | |" + ) + ); +} + +#[test] +fn max_width_wrap_priority_min_with_span() { + let table = Matrix::new(3, 3) + .insert((2, 1), "Hello World With Big Line") + .with(Style::markdown()) + .with(Modify::new((2, 1)).with(Span::column(2))) + .with(Width::wrap(15).priority::<PriorityMin>()) + .to_string(); + + assert!(is_lines_equal(&table, 15)); + assert_eq!( + table, + static_table!( + "| | | co | |" + "| | | lu | |" + "| | | mn | |" + "| | | 1 | |" + "|--|--|----|--|" + "| | | 0- | |" + "| | | 1 | |" + "| | Hello | |" + "| | Worl | |" + "| | d Wit | |" + "| | h Big | |" + "| | Line | |" + "| | | 2- | |" + "| | | 1 | |" + ) + ); +} + +#[test] +fn min_width_priority_max() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(MinWidth::new(60).priority::<PriorityMax>()) + .to_string(); + + assert_eq!(string_width_multiline(&table), 60); + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|---|----------|----------|--------------------------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ), + ); +} + +#[test] +fn min_width_priority_min() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(MinWidth::new(60).priority::<PriorityMin>()) + .to_string(); + + assert_eq!(string_width_multiline(&table), 60); + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|--------------|--------------|--------------|-------------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ), + ); +} + +#[test] +fn max_width_tab_0() { + let table = + Matrix::iter(["\t\tTigre Ecuador\tOMYA Andina\t3824909999\tCalcium carbonate\tColombia\t"]) + .with(TabSize::new(4)) + .with(Style::markdown()) + .with(Width::wrap(60)) + .to_string(); + + assert!(is_lines_equal(&table, 60)); + assert_eq!( + table, + static_table!( + "| &str |" + "|----------------------------------------------------------|" + "| Tigre Ecuador OMYA Andina 3824909999 Ca |" + "| lcium carbonate Colombia |" + ) + ); +} + +#[test] +fn min_width_is_not_used_after_padding() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(MinWidth::new(60)) + .with(Modify::new((0, 0)).with(Padding::new(2, 2, 0, 0))) + .to_string(); + + assert_eq!(string_width_multiline(&table), 40); + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|-----|----------|----------|----------|" + "| 0 | 0-0 | 0-1 | 0-2 |" + "| 1 | 1-0 | 1-1 | 1-2 |" + "| 2 | 2-0 | 2-1 | 2-2 |" + ), + ); +} + +#[test] +fn min_width_is_used_after_margin() { + let table = Matrix::new(3, 3) + .with(Style::markdown()) + .with(Margin::new(1, 1, 1, 1)) + .with(Width::increase(60)) + .to_string(); + + assert_eq!(string_width_multiline(&table), 60); + assert_eq!( + table, + static_table!( + " " + " | N | column 0 | column 1 | column 2 | " + " |--------|---------------|---------------|---------------| " + " | 0 | 0-0 | 0-1 | 0-2 | " + " | 1 | 1-0 | 1-1 | 1-2 | " + " | 2 | 2-0 | 2-1 | 2-2 | " + " " + ), + ); +} + +#[test] +fn wrap_keeping_words_0() { + let data = vec![["Hello world"]]; + let table = tabled::Table::new(data) + .with(Width::wrap(8).keep_words()) + .to_string(); + + assert_eq!( + tabled::grid::util::string::string_width_multiline(&table), + 8 + ); + + assert_eq!( + table, + static_table!( + "+------+" + "| 0 |" + "+------+" + "| Hell |" + "| o wo |" + "| rld |" + "+------+" + ) + ); +} + +#[test] +fn cell_truncate_multiline() { + let table = Matrix::new(3, 3) + .insert((1, 1), "H\nel\nlo World") + .insert((3, 2), "multi\nline string\n") + .with(Style::markdown()) + .with( + Modify::new(Columns::new(1..2).not(Rows::single(0))) + .with(Width::truncate(1).multiline()), + ) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|---|----------|-------------|----------|" + "| 0 | H | 0-1 | 0-2 |" + "| | e | | |" + "| | l | | |" + "| 1 | 1 | 1-1 | 1-2 |" + "| 2 | 2 | multi | 2-2 |" + "| | | line string | |" + "| | | | |" + ) + ); +} + +#[test] +fn cell_truncate_multiline_with_suffix() { + let table = Matrix::new(3, 3) + .insert((1, 1), "H\nel\nlo World") + .insert((3, 2), "multi\nline string\n") + .with(Style::markdown()) + .with( + Modify::new(Columns::new(1..2).not(Rows::single(0))) + .with(Width::truncate(1).multiline().suffix(".")), + ) + .to_string(); + + assert_eq!( + table, + static_table!( + "| N | column 0 | column 1 | column 2 |" + "|---|----------|-------------|----------|" + "| 0 | . | 0-1 | 0-2 |" + "| | . | | |" + "| | . | | |" + "| 1 | . | 1-1 | 1-2 |" + "| 2 | . | multi | 2-2 |" + "| | | line string | |" + "| | | | |" + ) + ); +} + +#[test] +fn table_truncate_multiline() { + let table = Matrix::new(3, 3) + .insert((1, 1), "H\nel\nlo World") + .insert((3, 2), "multi\nline string\n") + .with(Style::markdown()) + .with(Width::truncate(20).multiline()) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | c | colu | co |" + "|--|---|------|----|" + "| | H | 0-1 | 0- |" + "| | e | | |" + "| | l | | |" + "| | 1 | 1-1 | 1- |" + "| | 2 | mult | 2- |" + "| | | line | |" + "| | | | |" + ) + ); +} + +#[test] +fn table_truncate_multiline_with_suffix() { + let table = Matrix::new(3, 3) + .insert((1, 1), "H\nel\nlo World") + .insert((3, 2), "multi\nline string\n") + .with(Style::markdown()) + .with(Width::truncate(20).suffix(".").multiline()) + .to_string(); + + assert_eq!( + table, + static_table!( + "| | . | col. | c. |" + "|--|---|------|----|" + "| | . | 0-1 | 0. |" + "| | . | | |" + "| | . | | |" + "| | . | 1-1 | 1. |" + "| | . | mul. | 2. |" + "| | | lin. | |" + "| | | . | |" + ) + ); +} + +#[cfg(feature = "derive")] +mod derived { + use super::*; + + use tabled::Tabled; + + #[test] + fn wrapping_as_total_multiline() { + #[derive(Tabled)] + struct D<'a>( + #[tabled(rename = "version")] &'a str, + #[tabled(rename = "published_date")] &'a str, + #[tabled(rename = "is_active")] &'a str, + #[tabled(rename = "major_feature")] &'a str, + ); + + let data = vec![ + D("0.2.1", "2021-06-23", "true", "#[header(inline)] attribute"), + D("0.2.0", "2021-06-19", "false", "API changes"), + D("0.1.4", "2021-06-07", "false", "display_with attribute"), + ]; + + let table = Matrix::iter(&data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Width::wrap(57)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| ver | published_d | is_act | major_feature |" + "| sio | ate | ive | |" + "| n | | | |" + "|-----|-------------|--------|--------------------------|" + "| 0.2 | 2021-06-23 | true | #[header(inline)] attrib |" + "| .1 | | | ute |" + "| 0.2 | 2021-06-19 | false | API changes |" + "| .0 | | | |" + "| 0.1 | 2021-06-07 | false | display_with attribute |" + "| .4 | | | |" + ) + ); + assert!(is_lines_equal(&table, 57)); + + let table = Matrix::iter(&data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Width::wrap(57).keep_words()) + .to_string(); + + assert_eq!( + table, + static_table!( + "| ver | published_d | is_act | major_feature |" + "| sio | ate | ive | |" + "| n | | | |" + "|-----|-------------|--------|--------------------------|" + "| 0.2 | 2021-06-23 | true | #[header(inline)] |" + "| .1 | | | attribute |" + "| 0.2 | 2021-06-19 | false | API changes |" + "| .0 | | | |" + "| 0.1 | 2021-06-07 | false | display_with attribute |" + "| .4 | | | |" + ) + ); + assert!(is_lines_equal(&table, 57)); + } + + #[cfg(feature = "color")] + #[test] + fn wrapping_as_total_multiline_color() { + #[derive(Tabled)] + struct D( + #[tabled(rename = "version")] String, + #[tabled(rename = "published_date")] String, + #[tabled(rename = "is_active")] String, + #[tabled(rename = "major_feature")] String, + ); + + let data = vec![ + D( + "0.2.1".red().to_string(), + "2021-06-23".red().on_truecolor(8, 10, 30).to_string(), + "true".to_string(), + "#[header(inline)] attribute" + .blue() + .on_color(AnsiColors::Green) + .to_string(), + ), + D( + "0.2.0".red().to_string(), + "2021-06-19".green().on_truecolor(8, 100, 30).to_string(), + "false".to_string(), + "API changes".yellow().to_string(), + ), + D( + "0.1.4".white().to_string(), + "2021-06-07".red().on_truecolor(8, 10, 30).to_string(), + "false".to_string(), + "display_with attribute" + .red() + .on_color(AnsiColors::Black) + .to_string(), + ), + ]; + + let table = Matrix::iter(&data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Width::wrap(57)) + .to_string(); + + assert_eq!( + table, + static_table!( + "| ver | published_d | is_act | major_feature |" + "| sio | ate | ive | |" + "| n | | | |" + "|-----|-------------|--------|--------------------------|" + "| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-23\u{1b}[39m\u{1b}[49m | true | \u{1b}[34m\u{1b}[42m#[header(inline)] attrib\u{1b}[39m\u{1b}[49m |" + "| \u{1b}[31m.1\u{1b}[39m | | | \u{1b}[34m\u{1b}[42mute\u{1b}[39m\u{1b}[49m |" + "| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;100;30m\u{1b}[32m2021-06-19\u{1b}[39m\u{1b}[49m | false | \u{1b}[33mAPI changes\u{1b}[39m |" + "| \u{1b}[31m.0\u{1b}[39m | | | |" + "| \u{1b}[37m0.1\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-07\u{1b}[39m\u{1b}[49m | false | \u{1b}[31;40mdisplay_with attribute\u{1b}[0m |" + "| \u{1b}[37m.4\u{1b}[39m | | | |" + ) + ); + assert_eq!(string_width_multiline(&table), 57); + + let table = Matrix::iter(&data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Width::wrap(57).keep_words()) + .to_string(); + + assert_eq!( + table, + static_table!( + "| ver | published_d | is_act | major_feature |" + "| sio | ate | ive | |" + "| n | | | |" + "|-----|-------------|--------|--------------------------|" + "| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-23\u{1b}[39m\u{1b}[49m | true | \u{1b}[34m\u{1b}[42m#[header(inline)] \u{1b}[39m\u{1b}[49m |" + "| \u{1b}[31m.1\u{1b}[39m | | | \u{1b}[34m\u{1b}[42mattribute\u{1b}[39m\u{1b}[49m |" + "| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;100;30m\u{1b}[32m2021-06-19\u{1b}[39m\u{1b}[49m | false | \u{1b}[33mAPI changes\u{1b}[39m |" + "| \u{1b}[31m.0\u{1b}[39m | | | |" + "| \u{1b}[37m0.1\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-07\u{1b}[39m\u{1b}[49m | false | \u{1b}[31;40mdisplay_with attribute\u{1b}[0m |" + "| \u{1b}[37m.4\u{1b}[39m | | | |" + ) + ); + assert_eq!(string_width_multiline(&table), 57); + } + + #[cfg(feature = "color")] + #[test] + fn truncating_as_total_multiline_color() { + #[derive(Tabled)] + struct D( + #[tabled(rename = "version")] String, + #[tabled(rename = "published_date")] String, + #[tabled(rename = "is_active")] String, + #[tabled(rename = "major_feature")] String, + ); + + let data = vec![ + D( + "0.2.1".red().to_string(), + "2021-06-23".red().on_truecolor(8, 10, 30).to_string(), + "true".to_string(), + "#[header(inline)] attribute" + .blue() + .on_color(AnsiColors::Green) + .to_string(), + ), + D( + "0.2.0".red().to_string(), + "2021-06-19".green().on_truecolor(8, 100, 30).to_string(), + "false".to_string(), + "API changes".yellow().to_string(), + ), + D( + "0.1.4".white().to_string(), + "2021-06-07".red().on_truecolor(8, 10, 30).to_string(), + "false".to_string(), + "display_with attribute" + .red() + .on_color(AnsiColors::Black) + .to_string(), + ), + ]; + + let table = Matrix::iter(data) + .with(Style::markdown()) + .with(Modify::new(Segment::all()).with(Alignment::left())) + .with(Width::truncate(57)) + .to_string(); + + assert_eq!( + ansi_str::AnsiStr::ansi_strip(&table), + static_table!( + "| ver | published_d | is_act | major_feature |" + "|-----|-------------|--------|--------------------------|" + "| 0.2 | 2021-06-23 | true | #[header(inline)] attrib |" + "| 0.2 | 2021-06-19 | false | API changes |" + "| 0.1 | 2021-06-07 | false | display_with attribute |" + ) + ); + + assert_eq!( + table, + "| ver | published_d | is_act | major_feature |\n|-----|-------------|--------|--------------------------|\n| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-23\u{1b}[39m\u{1b}[49m | true | \u{1b}[34;42m#[header(inline)] attrib\u{1b}[39m\u{1b}[49m |\n| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;100;30m\u{1b}[32m2021-06-19\u{1b}[39m\u{1b}[49m | false | \u{1b}[33mAPI changes\u{1b}[39m |\n| \u{1b}[37m0.1\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-07\u{1b}[39m\u{1b}[49m | false | \u{1b}[31;40mdisplay_with attribute\u{1b}[0m |" + ); + assert_eq!(string_width_multiline(&table), 57); + } + + #[cfg(feature = "color")] + fn format_osc8_hyperlink(url: &str, text: &str) -> String { + format!( + "{osc}8;;{url}{st}{text}{osc}8;;{st}", + url = url, + text = text, + osc = "\x1b]", + st = "\x1b\\" + ) + } + + #[cfg(feature = "color")] + #[test] + fn hyperlinks() { + #[derive(Tabled)] + struct Distribution { + name: String, + is_hyperlink: bool, + } + + let table = |text: &str| { + let data = [Distribution { + name: text.to_owned(), + is_hyperlink: true, + }]; + tabled::Table::new(data) + .with( + Modify::new(Segment::all()) + .with(Width::wrap(5).keep_words()) + .with(Alignment::left()), + ) + .to_string() + }; + + let text = format_osc8_hyperlink("https://www.debian.org/", "Debian"); + assert_eq!( + table(&text), + "+-------+-------+\n\ + | name | is_hy |\n\ + | | perli |\n\ + | | nk |\n\ + +-------+-------+\n\ + | \u{1b}]8;;https://www.debian.org/\u{1b}\\Debia\u{1b}]8;;\u{1b}\\ | true |\n\ + | \u{1b}]8;;https://www.debian.org/\u{1b}\\n\u{1b}]8;;\u{1b}\\ | |\n\ + +-------+-------+" + ); + + // if there's more text than a link it will be ignored + let text = format!( + "{} :link", + format_osc8_hyperlink("https://www.debian.org/", "Debian"), + ); + assert_eq!( + table(&text), + "+-------+-------+\n\ + | name | is_hy |\n\ + | | perli |\n\ + | | nk |\n\ + +-------+-------+\n\ + | Debia | true |\n\ + | n | |\n\ + | :link | |\n\ + +-------+-------+" + ); + + let text = format!( + "asd {} 2 links in a string {}", + format_osc8_hyperlink("https://www.debian.org/", "Debian"), + format_osc8_hyperlink("https://www.wikipedia.org/", "Debian"), + ); + assert_eq!( + table(&text), + static_table!( + "+-------+-------+" + "| name | is_hy |" + "| | perli |" + "| | nk |" + "+-------+-------+" + "| asd D | true |" + "| ebian | |" + "| 2 | |" + "| links | |" + "| in a | |" + "| stri | |" + "| ng De | |" + "| bian | |" + "+-------+-------+" + ) + ); + } + + #[cfg(feature = "color")] + #[test] + fn hyperlinks_with_color() { + use owo_colors::OwoColorize; + + #[derive(Tabled)] + struct Distribution { + name: String, + is_hyperlink: bool, + } + + let table = |text: &str| { + let data = [Distribution { + name: text.to_owned(), + is_hyperlink: true, + }]; + tabled::Table::new(data) + .with( + Modify::new(Segment::all()) + .with(Width::wrap(6).keep_words()) + .with(Alignment::left()), + ) + .to_string() + }; + + let text = format_osc8_hyperlink( + "https://www.debian.org/", + "Debian".red().to_string().as_str(), + ); + assert_eq!( + table(&text), + static_table!( + "+--------+--------+" + "| name | is_hyp |" + "| | erlink |" + "+--------+--------+" + "| \u{1b}]8;;https://www.debian.org/\u{1b}\\\u{1b}[31mDebian\u{1b}[39m\u{1b}]8;;\u{1b}\\ | true |" + "+--------+--------+" + ) + ); + + // if there's more text than a link it will be ignored + let text = format!( + "{} :link", + format_osc8_hyperlink("https://www.debian.org/", "Debian"), + ); + assert_eq!( + table(&text), + static_table!( + "+--------+--------+" + "| name | is_hyp |" + "| | erlink |" + "+--------+--------+" + "| Debian | true |" + "| :link | |" + "+--------+--------+" + ) + ); + + let text = format!( + "asd {} 2 links in a string {}", + format_osc8_hyperlink("https://www.debian.org/", "Debian"), + format_osc8_hyperlink("https://www.wikipedia.org/", "Debian"), + ); + assert_eq!( + table(&text), + static_table!( + "+--------+--------+" + "| name | is_hyp |" + "| | erlink |" + "+--------+--------+" + "| asd | true |" + "| Debian | |" + "| 2 | |" + "| links | |" + "| in a | |" + "| string | |" + "| | |" + "| Debian | |" + "+--------+--------+" + ) + ); + } +} |