use lv_tui::prelude::*;
use lv_tui::widgets::*;
fn has_text(buf: &Buffer, text: &str) -> bool {
let all: String = buf.cells.iter().map(|c| c.symbol.as_str()).collect();
all.contains(text)
}
fn col(title: &str, w: ColumnWidth) -> TableColumn {
TableColumn { title: Text::from(title), width: w, align: TextAlign::Left }
}
#[test]
fn table_renders_headers_and_data() {
let t = Table::new()
.columns(vec![col("Name", ColumnWidth::Fixed(10)), col("Size", ColumnWidth::Fixed(8))])
.rows_simple(vec![vec!["a.rs", "1KB"], vec!["b.rs", "2KB"]]);
let pilot = Pilot::new(t, 30, 8);
assert!(has_text(pilot.frame(), "Name"), "header visible");
assert!(has_text(pilot.frame(), "a.rs"), "row data visible");
}
#[test]
fn table_down_selects_row() {
let t = Table::new()
.columns(vec![col("Col", ColumnWidth::Fixed(10))])
.rows_simple(vec![vec!["A"], vec!["B"], vec!["C"]])
.select_style(Style::default().fg(Color::White).bg(Color::Black));
let mut pilot = Pilot::new(t, 20, 8);
pilot.press(Key::Down).unwrap(); assert!(has_text(pilot.frame(), "B"), "row B visible after Down");
}
#[test]
fn table_fixed_rows_keeps_header() {
let mut rows = Vec::new();
for i in 0..20 {
rows.push(vec![format!("row-{}", i)]);
}
let t = Table::new()
.columns(vec![col("Col", ColumnWidth::Fixed(10))])
.rows_simple(rows)
.fixed_rows(2) .select_style(Style::default().fg(Color::White).bg(Color::Black));
let mut pilot = Pilot::new(t, 20, 8);
for _ in 0..5 {
pilot.press(Key::Down).unwrap();
}
assert!(has_text(pilot.frame(), "Col"), "header should stay fixed");
}
#[test]
fn table_cell_mode_left_right() {
let t = Table::new()
.columns(vec![
col("A", ColumnWidth::Fixed(8)),
col("B", ColumnWidth::Fixed(8)),
col("C", ColumnWidth::Fixed(8)),
])
.rows_simple(vec![vec!["a1", "b1", "c1"], vec!["a2", "b2", "c2"]])
.cursor_type(CursorType::Cell)
.select_style(Style::default().fg(Color::White).bg(Color::Black));
let mut pilot = Pilot::new(t, 40, 8);
pilot.press(Key::Right).unwrap();
assert!(has_text(pilot.frame(), "b1"), "data still rendered after right");
}
#[test]
fn table_column_highlight() {
let t = Table::new()
.columns(vec![col("X", ColumnWidth::Fixed(10)), col("Y", ColumnWidth::Fixed(10))])
.rows_simple(vec![vec!["x1", "y1"]])
.column_highlight_style(Style::default().bold());
let pilot = Pilot::new(t, 30, 8);
assert!(has_text(pilot.frame(), "X"), "table renders with column highlight");
}
#[test]
fn table_cell_highlight() {
let t = Table::new()
.columns(vec![col("Name", ColumnWidth::Fixed(12))])
.rows_simple(vec![vec!["value"]])
.select_style(Style::default().fg(Color::White).bg(Color::Black))
.cell_highlight_style(Style::default().bold());
let mut pilot = Pilot::new(t, 20, 8);
pilot.press(Key::Down).unwrap();
assert!(has_text(pilot.frame(), "value"), "selected cell still renders");
}
#[test]
fn table_defaults_work() {
let t = Table::new()
.columns(vec![col("H", ColumnWidth::Fixed(6))])
.rows_simple(vec![vec!["d"]]);
let pilot = Pilot::new(t, 20, 6);
assert!(has_text(pilot.frame(), "H"), "default table renders");
assert!(has_text(pilot.frame(), "d"), "default data renders");
}