Lines
100 %
Functions
Branches
use super::*;
fn theme() -> Theme {
Theme::preset("dn").expect("the dn preset parses")
}
fn screen() -> Rect {
Rect::new(0, 0, 40, 20)
fn cells(buf: &Buffer, y: u16, from: u16, to: u16) -> String {
(from..to).map(|x| buf[(x, y)].symbol()).collect()
fn labels(n: usize) -> Vec<String> {
(0..n).map(|i| format!("Opt{i}")).collect()
fn filled() -> Buffer {
let mut buf = Buffer::empty(Rect::new(0, 0, 30, 12));
for position in buf.area.positions() {
buf[position].set_char('x');
buf
fn drawn_in(items: &[String], state: &mut ListState, area: Rect) -> Buffer {
let mut buf = filled();
DropDown::new(&theme(), items).render(area, &mut buf, state);
fn drawn(items: &[String], selected: usize, area: Rect) -> Buffer {
let mut state = ListState::default().with_selected(Some(selected));
drawn_in(items, &mut state, area)
#[test]
fn the_box_goes_below_the_anchor_at_its_column_and_width() {
let area = dropdown_area(Rect::new(5, 3, 12, 1), 4, screen());
assert_eq!(area, Rect::new(5, 4, 12, 6));
fn the_box_goes_above_when_there_is_no_room_below() {
let area = dropdown_area(Rect::new(5, 17, 12, 1), 4, screen());
assert_eq!(area, Rect::new(5, 11, 12, 6));
fn the_box_is_cut_to_the_larger_side_when_neither_fits() {
let small = Rect::new(0, 0, 40, 8);
assert_eq!(
dropdown_area(Rect::new(0, 2, 10, 1), 20, small),
Rect::new(0, 3, 10, 5)
);
dropdown_area(Rect::new(0, 6, 10, 1), 20, small),
Rect::new(0, 0, 10, 6)
fn the_box_is_kept_inside_a_narrow_screen() {
let area = dropdown_area(Rect::new(30, 2, 20, 1), 2, screen());
assert_eq!(area, Rect::new(20, 3, 20, 4));
let narrow = dropdown_area(Rect::new(3, 2, 20, 1), 2, Rect::new(0, 0, 10, 10));
assert_eq!(narrow, Rect::new(0, 3, 10, 4));
fn the_box_under_the_last_row_goes_above() {
let area = dropdown_area(Rect::new(0, 19, 10, 1), 3, screen());
assert_eq!(area, Rect::new(0, 14, 10, 5));
fn rows_are_framed_with_the_selected_one_in_its_role() {
let items = labels(3);
let buf = drawn(&items, 1, Rect::new(2, 1, 10, 5));
assert_eq!(cells(&buf, 1, 2, 12), "┌────────┐");
assert_eq!(cells(&buf, 2, 2, 12), "│ Opt0 │");
assert_eq!(cells(&buf, 3, 2, 12), "│ Opt1 │");
assert_eq!(cells(&buf, 4, 2, 12), "│ Opt2 │");
assert_eq!(cells(&buf, 5, 2, 12), "└────────┘");
let theme = theme();
assert_eq!(buf[(4, 2)].style().bg, theme.style(Role::List).bg);
assert_eq!(buf[(4, 3)].style().fg, theme.style(Role::ListSelected).fg);
assert_eq!(buf[(10, 3)].style().bg, theme.style(Role::ListSelected).bg);
fn the_list_scrolls_to_keep_the_selected_row_in_view() {
let items = labels(20);
let buf = drawn(&items, 15, Rect::new(0, 0, 10, 5));
assert_eq!(cells(&buf, 3, 0, 9), "│ Opt15 ");
let selected = theme().style(Role::ListSelected);
assert_eq!(buf[(2, 3)].style().fg, selected.fg);
assert_eq!(buf[(2, 3)].style().bg, selected.bg);
assert_eq!(buf[(2, 2)].style().bg, theme().style(Role::List).bg);
fn the_box_casts_a_shadow_right_and_below() {
let items = labels(2);
let buf = drawn(&items, 0, Rect::new(2, 1, 10, 4));
let shade = theme().style(Role::Shadow);
assert_eq!(buf[(12, 2)].style().bg, shade.bg);
assert_eq!(buf[(13, 3)].style().bg, shade.bg);
assert_eq!(buf[(5, 5)].style().bg, shade.bg);
assert_eq!(buf[(5, 6)].symbol(), "x");
fn a_box_partly_off_the_buffer_is_clipped() {
let buf = drawn(&items, 0, Rect::new(25, 10, 10, 6));
assert_eq!(cells(&buf, 10, 25, 30), "┌───┐");
fn the_window_stays_put_while_the_selection_moves_inside_it() {
let area = Rect::new(0, 0, 10, 5);
let mut state = ListState::default().with_selected(Some(0));
drawn_in(&items, &mut state, area);
for selected in [1, 2, 1] {
state.select(Some(selected));
let buf = drawn_in(&items, &mut state, area);
assert_eq!(state.offset(), 0);
assert_eq!(cells(&buf, 1, 0, 10), "│ Opt0 ▲");
fn the_window_scrolls_by_the_minimum_past_either_edge() {
state.select(Some(3));
assert_eq!(state.offset(), 1);
state.select(Some(2));
state.select(Some(0));
fn an_offset_past_the_last_page_is_pulled_back_to_it() {
let items = labels(6);
let mut state = ListState::default().with_offset(5).with_selected(Some(5));
let buf = drawn_in(&items, &mut state, Rect::new(0, 0, 10, 5));
assert_eq!(state.offset(), 3);
assert_eq!(cells(&buf, 3, 1, 9), " Opt5 ");
fn the_scrollbar_shows_on_the_right_border_only_when_items_exceed_the_rows() {
let long = drawn(&labels(20), 0, area);
assert_eq!(long[(9, 1)].symbol(), "\u{25B2}");
assert_eq!(long[(9, 3)].symbol(), "\u{25BC}");
long[(9, 2)].style().fg,
theme.style(Role::ScrollbarThumb).fg
long[(9, 2)].style().bg,
theme.style(Role::ScrollbarThumb).bg
let short = drawn(&labels(3), 0, area);
assert_eq!(short[(9, 1)].symbol(), "\u{2502}");
assert_eq!(short[(9, 2)].symbol(), "\u{2502}");
fn the_scrollbar_thumb_follows_the_window() {
let area = Rect::new(0, 0, 10, 9);
let thumb = |buf: &Buffer| (2..7).find(|&y| buf[(9, y)].symbol() != "\u{2592}");
let top = drawn(&labels(20), 0, area);
let end = drawn(&labels(20), 19, area);
assert!(
thumb(&top) < thumb(&end),
"{:?} {:?}",
thumb(&top),
thumb(&end)
let y = thumb(&end).expect("the thumb is drawn");
let thumb = theme.style(Role::ScrollbarThumb);
assert_eq!(end[(9, y)].style().bg, thumb.bg);
assert_eq!(end[(9, y)].style().fg, thumb.fg);
fn the_rows_are_the_area_inside_the_border() {
DropDown::rows(Rect::new(2, 1, 10, 5)),
Rect::new(3, 2, 8, 3)
fn the_option_rows_are_the_rows_inside_the_border_numbered_from_the_top() {
let area = Rect::new(2, 3, 10, 5);
DropDown::option_rows(area, 4, 20),
[
(4, Rect::new(3, 4, 8, 1)),
(5, Rect::new(3, 5, 8, 1)),
(6, Rect::new(3, 6, 8, 1)),
]
fn a_short_list_has_a_row_per_option_and_none_past_the_end() {
let area = Rect::new(0, 0, 10, 8);
assert_eq!(DropDown::option_rows(area, 0, 2).len(), 2);
assert_eq!(DropDown::option_rows(area, 5, 6).len(), 1);
assert!(DropDown::option_rows(area, 6, 6).is_empty());
fn the_option_rows_match_the_rows_the_list_draws() {
let items = labels(10);
let area = Rect::new(2, 1, 12, 6);
let mut state = ListState::default().with_selected(Some(7));
for (index, row) in DropDown::option_rows(area, state.offset(), items.len()) {
let text = cells(&buf, row.y, row.x, row.right());
text.trim_start().starts_with(&items[index]),
"{index} {text:?}"