2023-04-26 20:56:10 +02:00
|
|
|
#![allow(dead_code)]
|
2022-12-15 15:47:04 +01:00
|
|
|
|
2023-08-04 20:50:47 +02:00
|
|
|
use nu_table::{string_width, NuTable, NuTableConfig};
|
2024-08-24 00:35:42 +02:00
|
|
|
use tabled::grid::records::vec_records::Text;
|
2022-12-15 15:47:04 +01:00
|
|
|
|
|
|
|
pub struct TestCase {
|
2023-08-04 20:50:47 +02:00
|
|
|
cfg: NuTableConfig,
|
2022-12-15 15:47:04 +01:00
|
|
|
termwidth: usize,
|
|
|
|
expected: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestCase {
|
2023-08-04 20:50:47 +02:00
|
|
|
pub fn new(cfg: NuTableConfig, termwidth: usize, expected: Option<String>) -> Self {
|
2022-12-15 15:47:04 +01:00
|
|
|
Self {
|
|
|
|
cfg,
|
|
|
|
termwidth,
|
|
|
|
expected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-24 00:35:42 +02:00
|
|
|
type Data = Vec<Vec<Text<String>>>;
|
2023-04-26 20:56:10 +02:00
|
|
|
|
|
|
|
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-08-04 20:50:47 +02:00
|
|
|
pub fn create_table(data: Data, config: NuTableConfig, termwidth: usize) -> Option<String> {
|
2023-04-26 20:56:10 +02:00
|
|
|
let table = NuTable::from(data);
|
2022-12-15 15:47:04 +01:00
|
|
|
table.draw(config, termwidth)
|
|
|
|
}
|
|
|
|
|
2024-08-24 00:35:42 +02:00
|
|
|
pub fn create_row(count_columns: usize) -> Vec<Text<String>> {
|
2022-12-15 15:47:04 +01:00
|
|
|
let mut row = Vec::with_capacity(count_columns);
|
|
|
|
for i in 0..count_columns {
|
2024-08-24 00:35:42 +02:00
|
|
|
row.push(Text::new(i.to_string()));
|
2022-12-15 15:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
row
|
|
|
|
}
|
|
|
|
|
2024-08-24 00:35:42 +02:00
|
|
|
pub fn cell(text: &str) -> Text<String> {
|
|
|
|
Text::new(text.to_string())
|
2022-12-15 15:47:04 +01:00
|
|
|
}
|