Lines
98.78 %
Functions
100 %
Branches
use ratatui::layout::Rect;
use super::*;
use crate::widgets::grid::{Grid, GridLine};
use crate::widgets::{SelectOption, SelectWidget};
fn column(floor: u16, want: u16, flex: bool) -> Column {
Column { floor, want, flex }
}
fn line(items: Vec<Item<'static>>, frozen: usize, focus: usize) -> Grid<'static> {
let mut grid = Grid::default();
let items = items
.into_iter()
.enumerate()
.map(|(at, item)| if at < frozen { item.frozen() } else { item })
.collect();
grid.push_cells(0, items);
if let Some(GridLine::Cells { items, .. }) = grid.lines.first_mut() {
items[focus].focused = true;
grid
#[test]
fn without_a_room_every_column_has_the_width_it_wants() {
let columns = [column(4, 9, true), column(3, 3, false)];
assert_eq!(resolve(&columns, None), [9, 3]);
fn flexible_columns_share_what_the_fixed_ones_leave_within_their_bounds() {
let columns = [
column(10, 30, true),
column(5, 5, false),
column(10, 12, true),
];
// The floors and two gaps leave 13 of 40 cells: 6 each and the spare one to the first.
assert_eq!(resolve(&columns, Some(40)), [17, 5, 12]);
assert_eq!(resolve(&columns, Some(20)), [10, 5, 10]);
assert_eq!(resolve(&columns, Some(200)), [30, 5, 12]);
fn a_fixed_text_cell_keeps_its_width_whatever_it_holds() {
let long = Item::text("123456789012345678901234", 24, true).fixed(12);
assert_eq!(long.width(), 12);
assert_eq!(Item::text("", 0, false).fixed(12).width(), 12);
fn a_fixed_select_does_not_track_its_widest_option() {
let mut select = SelectWidget::new();
let label = "a very long option label".to_string();
select.set_options(vec![SelectOption {
id: "x".to_string(),
label,
}]);
assert_eq!(Item::select(&select, false).fixed(9).width(), 9);
assert!(Item::select(&select, false).width() > 9);
fn the_frozen_columns_stay_while_the_rest_shifts_to_show_the_focused_cell() {
let items = ["a", "b", "c", "d", "e"].map(|text| Item::label(text).fixed(8));
let grid = line(items.to_vec(), 2, 4);
let rects = grid.cell_rects(Rect::new(0, 0, 30, 1), 0);
assert_eq!(rects[0][0], Rect::new(0, 0, 8, 1));
assert_eq!(rects[0][1], Rect::new(9, 0, 8, 1));
assert_eq!(rects[0][2].width, 0, "scrolled under the frozen columns");
assert_eq!(rects[0][4], Rect::new(22, 0, 8, 1));
fn a_focused_frozen_cell_shifts_nothing() {
let grid = line(items.to_vec(), 2, 1);
assert_eq!(rects[0][2], Rect::new(18, 0, 8, 1));
fn a_room_below_the_floors_leaves_every_column_at_its_floor() {
column(8, 20, true),
assert_eq!(resolve(&columns, Some(10)), [10, 5, 8]);
fn an_odd_spare_cell_goes_to_the_first_flexible_column() {
column(4, 20, true),
assert_eq!(resolve(&columns, Some(18)), [6, 5, 5]);
fn a_column_mixing_flex_and_fit_items_takes_the_widest_of_each() {
let mut column = Column::default();
column.absorb(&Item::label("ab").flex(6, 12));
column.absorb(&Item::label("a longer fit label"));
assert_eq!((column.floor, column.want, column.flex), (18, 18, true));