2023-04-26 20:56:10 +02:00
|
|
|
#![allow(dead_code)]
|
2022-12-15 15:47:04 +01:00
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
use nu_table::{string_width, NuTable, TableConfig};
|
|
|
|
use tabled::grid::records::vec_records::CellInfo;
|
2022-12-15 15:47:04 +01:00
|
|
|
|
|
|
|
pub struct TestCase {
|
|
|
|
cfg: TableConfig,
|
|
|
|
termwidth: usize,
|
|
|
|
expected: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCase {
|
|
|
|
pub fn new(cfg: TableConfig, termwidth: usize, expected: Option<String>) -> Self {
|
|
|
|
Self {
|
|
|
|
cfg,
|
|
|
|
termwidth,
|
|
|
|
expected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
type Data = Vec<Vec<CellInfo<String>>>;
|
|
|
|
|
|
|
|
pub fn test_table<I: IntoIterator<Item = TestCase>>(data: Data, tests: I) {
|
2022-12-15 15:47:04 +01:00
|
|
|
for (i, test) in tests.into_iter().enumerate() {
|
|
|
|
let actual = create_table(data.clone(), test.cfg.clone(), test.termwidth);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual, test.expected,
|
|
|
|
"\nfail i={:?} termwidth={}",
|
|
|
|
i, test.termwidth
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(table) = actual {
|
|
|
|
assert!(string_width(&table) <= test.termwidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
pub fn create_table(data: Data, config: TableConfig, termwidth: usize) -> Option<String> {
|
|
|
|
let table = NuTable::from(data);
|
2022-12-15 15:47:04 +01:00
|
|
|
table.draw(config, termwidth)
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
pub fn create_row(count_columns: usize) -> Vec<CellInfo<String>> {
|
2022-12-15 15:47:04 +01:00
|
|
|
let mut row = Vec::with_capacity(count_columns);
|
|
|
|
for i in 0..count_columns {
|
2023-04-26 20:56:10 +02:00
|
|
|
row.push(CellInfo::new(i.to_string()));
|
2022-12-15 15:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
row
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
pub fn cell(text: &str) -> CellInfo<String> {
|
|
|
|
CellInfo::new(text.to_string())
|
2022-12-15 15:47:04 +01:00
|
|
|
}
|