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