From 5320d55be62d55a2a2be5458e46b9352cd70edf0 Mon Sep 17 00:00:00 2001 From: Paul Fenwick Date: Sun, 12 Aug 2018 21:43:54 +1000 Subject: README: Document set_titles, use prettyformat::table, plus extra tests! README: - Mention `set_titles()` - Explicitly `use prettytable::format` - Expand sample code to call `set_titles()` and `add_row()`s. lib.rs: - Added test from README code using the longhand FormatBuilder - Added test from README using FORMAT_NO_LINESEP_WITH_TITLE --- src/lib.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index cf3e658..a3adbfe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -967,6 +967,63 @@ mod tests { assert_eq!(out, table.to_string().replace("\r\n", "\n")); } + #[test] + fn test_readme_format() { + + // The below is lifted from the README + + let mut table = Table::new(); + let format = format::FormatBuilder::new() + .column_separator('|') + .borders('|') + .separators(&[format::LinePosition::Top, + format::LinePosition::Bottom], + format::LineSeparator::new('-', '+', '+', '+')) + .padding(1, 1) + .build(); + table.set_format(format); + + table.set_titles(Row::new(vec![Cell::new("Title 1"), Cell::new("Title 2")])); + table.add_row(Row::new(vec![Cell::new("Value 1"), Cell::new("Value 2")])); + table.add_row(Row::new(vec![Cell::new("Value three"), Cell::new("Value four")])); + + let out = "\ ++-------------+------------+ +| Title 1 | Title 2 | +| Value 1 | Value 2 | +| Value three | Value four | ++-------------+------------+ +"; + + println!("{}", out); + println!("____"); + println!("{}", table.to_string().replace("\r\n","\n")); + assert_eq!(out, table.to_string().replace("\r\n","\n")); + } + + #[test] + fn test_readme_format_with_title() { + let mut table = Table::new(); + table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE); + + table.set_titles(Row::new(vec![Cell::new("Title 1"), Cell::new("Title 2")])); + table.add_row(Row::new(vec![Cell::new("Value 1"), Cell::new("Value 2")])); + table.add_row(Row::new(vec![Cell::new("Value three"), Cell::new("Value four")])); + + let out = "\ ++-------------+------------+ +| Title 1 | Title 2 | ++-------------+------------+ +| Value 1 | Value 2 | +| Value three | Value four | ++-------------+------------+ +"; + println!("{}", out); + println!("____"); + println!("{}", table.to_string().replace("\r\n","\n")); + assert_eq!(out, table.to_string().replace("\r\n","\n")); + } + #[cfg(feature = "csv")] mod csv { use Table; -- cgit v1.2.3