#[ allow( unused_imports ) ]
use super::*;
use the_module::
{
AsTable,
WithRef,
filter,
print,
output_format,
};
use std::
{
borrow::Cow,
};
#[ test ]
fn basic()
{
let test_objects = test_object::test_objects_gen();
let _as_table : AsTable< '_, Vec< test_object::TestObject >, usize, test_object::TestObject, str> = AsTable::new( &test_objects );
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
println!( "{}", &output );
let exp = r#"│ id │ created_at │ file_ids │ tools │
─────────────────────────────────────────────────────────────────────────────
│ 1 │ 1627845583 │ [ │ │
│ │ │ "file1", │ │
│ │ │ "file2", │ │
│ │ │ ] │ │
│ 2 │ 13 │ [ │ [ │
│ │ │ "file3", │ { │
│ │ │ "file4\nmore details", │ "tool1": "value1", │
│ │ │ ] │ }, │
│ │ │ │ { │
│ │ │ │ "tool2": "value2", │
│ │ │ │ }, │
│ │ │ │ ] │"#;
a_id!( output.as_str(), exp );
}
#[ test ]
fn table_to_string()
{
use the_module::TableFormatter;
let test_objects = test_object::test_objects_gen();
let as_table : AsTable< '_, Vec< test_object::TestObject >, usize, test_object::TestObject, str> = AsTable::new( &test_objects );
let table_string = as_table.table_to_string();
println!( "\ntable_string\n{table_string}" );
assert!( table_string.contains( "id" ) );
assert!( table_string.contains( "created_at" ) );
assert!( table_string.contains( "file_ids" ) );
assert!( table_string.contains( "tools" ) );
println!( "" );
let as_table = AsTable::new( &test_objects );
let table_string = as_table.table_to_string();
assert!( table_string.contains( "id" ) );
assert!( table_string.contains( "created_at" ) );
assert!( table_string.contains( "file_ids" ) );
assert!( table_string.contains( "tools" ) );
println!( "\ntable_string\n{table_string}" );
}
#[ test ]
fn custom_format()
{
let test_objects = test_object::test_objects_gen();
let mut format = output_format::Table::default();
format.cell_prefix = "( ".into();
format.cell_postfix = " )".into();
format.cell_separator = "|".into();
format.row_prefix = ">".into();
format.row_postfix = "<".into();
format.row_separator = "\n".into();
let printer = print::Printer::with_format( &format );
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );
let result = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( result.is_ok() );
println!( "\noutput\n{output}" );
assert!( output.contains( "id" ) );
assert!( output.contains( "created_at" ) );
assert!( output.contains( "file_ids" ) );
assert!( output.contains( "tools" ) );
let exp = r#">( id )|( created_at )|( file_ids )|( tools )<
─────────────────────────────────────────────────────────────────────────────────────
>( 1 )|( 1627845583 )|( [ )|( )<
>( )|( )|( "file1", )|( )<
>( )|( )|( "file2", )|( )<
>( )|( )|( ] )|( )<
>( 2 )|( 13 )|( [ )|( [ )<
>( )|( )|( "file3", )|( { )<
>( )|( )|( "file4\nmore details", )|( "tool1": "value1", )<
>( )|( )|( ] )|( }, )<
>( )|( )|( )|( { )<
>( )|( )|( )|( "tool2": "value2", )<
>( )|( )|( )|( }, )<
>( )|( )|( )|( ] )<"#;
a_id!( output.as_str(), exp );
use the_module::TableFormatter;
let mut format = output_format::Table::default();
format.cell_prefix = "( ".into();
format.cell_postfix = " )".into();
format.cell_separator = "|".into();
format.row_prefix = ">".into();
format.row_postfix = "<".into();
format.row_separator = "\n".into();
let got = AsTable::new( &test_objects ).table_to_string_with_format( &format );
let exp = r#">( id )|( created_at )|( file_ids )|( tools )<
─────────────────────────────────────────────────────────────────────────────────────
>( 1 )|( 1627845583 )|( [ )|( )<
>( )|( )|( "file1", )|( )<
>( )|( )|( "file2", )|( )<
>( )|( )|( ] )|( )<
>( 2 )|( 13 )|( [ )|( [ )<
>( )|( )|( "file3", )|( { )<
>( )|( )|( "file4\nmore details", )|( "tool1": "value1", )<
>( )|( )|( ] )|( }, )<
>( )|( )|( )|( { )<
>( )|( )|( )|( "tool2": "value2", )<
>( )|( )|( )|( }, )<
>( )|( )|( )|( ] )<"#;
a_id!( got, exp );
}
#[ test ]
fn filter_col_none()
{
let test_objects = test_object::test_objects_gen();
let mut format = output_format::Table::default();
format.cell_prefix = "( ".into();
format.cell_postfix = " )".into();
format.cell_separator = "|".into();
format.row_prefix = ">".into();
format.row_postfix = "<".into();
format.row_separator = "\n".into();
let mut printer = print::Printer::with_format( &format );
printer.filter_col = &filter::None;
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );
let result = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( result.is_ok() );
println!( "\noutput\n{output}" );
let exp = r#"><
──
><
><"#;
a_id!( output.as_str(), exp );
}
#[ test ]
fn filter_col_callback()
{
let test_objects = test_object::test_objects_gen();
let mut format = output_format::Table::default();
format.cell_prefix = "( ".into();
format.cell_postfix = " )".into();
format.cell_separator = "|".into();
format.row_prefix = ">".into();
format.row_postfix = "<".into();
format.row_separator = "\n".into();
let mut printer = print::Printer::with_format( &format );
printer.filter_col = &| title : &str |
{
title != "tools"
};
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );
let result = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( result.is_ok() );
println!( "\noutput\n{output}" );
let exp = r#">( id )|( created_at )|( file_ids )<
──────────────────────────────────────────────────────
>( 1 )|( 1627845583 )|( [ )<
>( )|( )|( "file1", )<
>( )|( )|( "file2", )<
>( )|( )|( ] )<
>( 2 )|( 13 )|( [ )<
>( )|( )|( "file3", )<
>( )|( )|( "file4\nmore details", )<
>( )|( )|( ] )<"#;
a_id!( output.as_str(), exp );
}
#[ test ]
fn filter_row_none()
{
let test_objects = test_object::test_objects_gen();
let mut format = output_format::Table::default();
format.cell_prefix = "( ".into();
format.cell_postfix = " )".into();
format.cell_separator = "|".into();
format.row_prefix = ">".into();
format.row_postfix = "<".into();
format.row_separator = "\n".into();
let mut printer = print::Printer::with_format( &format );
printer.filter_row = &filter::None;
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );
let result = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( result.is_ok() );
println!( "\noutput\n{output}" );
let exp = r#""#;
a_id!( output.as_str(), exp );
}
#[ test ]
fn filter_row_callback()
{
let test_objects = test_object::test_objects_gen();
let mut format = output_format::Table::default();
format.cell_prefix = "( ".into();
format.cell_postfix = " )".into();
format.cell_separator = "|".into();
format.row_prefix = ">".into();
format.row_postfix = "<".into();
format.row_separator = "\n".into();
let mut printer = print::Printer::with_format( &format );
printer.filter_row = &| _typ, irow, _row : &[ ( Cow< '_, str >, [ usize ; 2 ] ) ] |
{
irow != 1
};
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );
let result = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( result.is_ok() );
println!( "\noutput\n{output}" );
let exp = r#">( id )|( created_at )|( file_ids )|( tools )<
─────────────────────────────────────────────────────────────────────────────────────
>( 2 )|( 13 )|( [ )|( [ )<
>( )|( )|( "file3", )|( { )<
>( )|( )|( "file4\nmore details", )|( "tool1": "value1", )<
>( )|( )|( ] )|( }, )<
>( )|( )|( )|( { )<
>( )|( )|( )|( "tool2": "value2", )<
>( )|( )|( )|( }, )<
>( )|( )|( )|( ] )<"#;
a_id!( output.as_str(), exp );
}
#[ test ]
fn no_subtract_with_overflow()
{
let test_objects = test_object::test_objects_gen_with_unicode();
let format = output_format::Table::default();
let printer = print::Printer::with_format( &format );
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );
let result = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( result.is_ok() );
}
#[ test ]
fn test_width_limiting()
{
use the_module::string;
for max_width in min_width()..max_width()
{
println!("max_width: {}", max_width);
let test_objects = test_object::test_objects_gen();
let as_table = AsTable::new( &test_objects );
let mut format = output_format::Table::default();
format.max_width = max_width;
let mut output = String::new();
let printer = print::Printer::with_format( &format );
let mut context = print::Context::new( &mut output, printer );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
for line in string::lines( &output )
{
assert_eq!( max_width, line.chars().count() );
}
}
}
#[ test ]
fn test_error_on_unsatisfiable_limit()
{
for max_width in 1..( min_width() )
{
println!( "max_width: {}", max_width );
let test_objects = test_object::test_objects_gen();
let as_table = AsTable::new( &test_objects );
let mut format = output_format::Table::default();
format.max_width = max_width;
let mut output = String::new();
let printer = print::Printer::with_format( &format );
let mut context = print::Context::new( &mut output, printer );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_err() );
}
}
#[ test ]
fn test_table_not_grows()
{
use the_module::string;
let expected_width = max_width();
for max_width in ( expected_width + 1 )..500
{
println!( "max_width: {}", max_width );
let test_objects = test_object::test_objects_gen();
let as_table = AsTable::new( &test_objects );
let mut format = output_format::Table::default();
format.max_width = max_width;
let mut output = String::new();
let printer = print::Printer::with_format( &format );
let mut context = print::Context::new( &mut output, printer );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
for line in string::lines( &output )
{
assert_eq!( expected_width, line.chars().count() );
}
}
}
fn min_width() -> usize
{
use the_module::Fields;
let format = output_format::Table::default();
let test_objects = test_object::test_objects_gen();
let col_count = test_objects[0].fields().count();
format.min_width( col_count )
}
fn max_width() -> usize
{
use the_module::string;
let test_objects = test_object::test_objects_gen();
let as_table = AsTable::new( &test_objects );
let format = output_format::Table::default();
let mut output = String::new();
let printer = print::Printer::with_format( &format );
let mut context = print::Context::new( &mut output, printer );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
for line in string::lines( &output )
{
return line.chars().count();
}
0
}
#[ test ]
fn ukrainian_chars()
{
let test_objects = test_object::test_objects_gen_with_unicode();
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
println!( "{}", &output );
let exp = r#"│ id │ created_at │ file_ids │ tools │
───────────────────────────────────────────────────────────────────────────────────────────────────────
│ Доміно │ 100 │ [ │ │
│ │ │ "файл1", │ │
│ │ │ "файл2", │ │
│ │ │ ] │ │
│ Інший юнікод │ 120 │ [] │ [ │
│ │ │ │ { │
│ │ │ │ "тулз1": "значення1", │
│ │ │ │ }, │
│ │ │ │ { │
│ │ │ │ "тулз2": "значення2", │
│ │ │ │ }, │
│ │ │ │ ] │"#;
a_id!( output.as_str(), exp );
}
#[ test ]
fn ukrainian_and_english_chars()
{
let test_objects = test_object::test_objects_gen_2_languages();
let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
println!( "{}", &output );
let exp = r#"│ id │ created_at │ file_ids │ tools │
────────────────────────────────────────────────────────────────────────────────────────────
│ Доміно │ 100 │ [ │ [ │
│ │ │ "файл1", │ { │
│ │ │ "файл2", │ "тулз1": "значення1", │
│ │ │ ] │ }, │
│ │ │ │ { │
│ │ │ │ "тулз2": "значення2", │
│ │ │ │ }, │
│ │ │ │ ] │
│ File │ 120 │ [ │ [ │
│ │ │ "file1", │ { │
│ │ │ "file2", │ "tools1": "value1", │
│ │ │ ] │ }, │
│ │ │ │ { │
│ │ │ │ "tools1": "value2", │
│ │ │ │ }, │
│ │ │ │ ] │"#;
a_id!( output.as_str(), exp );
}