Lines
100 %
Functions
Branches
use ratatui::style::Style;
use super::*;
use crate::{Role, scrollbar_track};
fn theme() -> Theme {
Theme::preset("dn").expect("the dn preset parses")
}
const BUTTONS: [Button<'static>; 2] = [
Button {
label: "~O~K",
default: true,
},
label: "~C~ancel",
default: false,
];
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
fn drawn(area: Rect, screen: Rect) -> Buffer {
let mut buf = Buffer::empty(screen);
Dialog::new(&theme(), "Form", &BUTTONS).render(area, &mut buf);
buf
#[test]
fn the_dialog_draws_frame_title_and_buttons_bottom_centred() {
let buf = drawn(Rect::new(0, 0, 30, 7), Rect::new(0, 0, 34, 8));
assert_eq!(row(&buf, 0), "╔═[■]═══════ Form ═══════════╗ ");
assert_eq!(row(&buf, 3), "║ ║ ");
assert_eq!(row(&buf, 4), "║ OK ▄ Cancel ▄ ║ ");
assert_eq!(row(&buf, 5), "║ ▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀ ║ ");
assert_eq!(row(&buf, 6), "╚════════════════════════════╝ ");
fn the_default_button_and_hotkeys_use_their_roles() {
let theme = theme();
let ok = theme.style(Role::ButtonDefault);
let cancel = theme.style(Role::Button);
let hot = theme.style(Role::ButtonHotkey);
assert_eq!(buf[(5, 4)].style().bg, ok.bg);
assert_eq!(buf[(9, 4)].style().fg, ok.fg);
assert_eq!(buf[(8, 4)].style().fg, hot.fg);
assert_eq!(buf[(17, 4)].style().bg, cancel.bg);
assert_eq!(buf[(18, 4)].style().fg, hot.fg);
assert_eq!(buf[(1, 4)].style().bg, theme.style(Role::Dialog).bg);
fn the_focused_button_is_drawn_in_the_focus_role() {
let mut buf = Buffer::empty(Rect::new(0, 0, 34, 8));
let dialog = Dialog::new(&theme, "Form", &BUTTONS).focused(Some(1));
dialog.render(Rect::new(0, 0, 30, 7), &mut buf);
let focus = theme.style(Role::ButtonFocus);
assert_eq!(buf[(17, 4)].style().bg, focus.bg);
assert_eq!(buf[(5, 4)].style().bg, theme.style(Role::Button).bg);
fn the_pressed_button_is_drawn_down_and_the_others_stay_up() {
let dialog = Dialog::new(&theme, "Form", &BUTTONS).pressed(Some(1));
assert_eq!(row(&buf, 4), "║ OK ▄ Cancel ║ ");
assert_eq!(row(&buf, 5), "║ ▀▀▀▀▀▀▀▀ ║ ");
fn the_dialog_casts_the_shadow() {
let shadow = Style::reset().patch(theme.style(Role::Shadow));
assert_eq!(buf[(30, 3)].style().bg, shadow.bg);
assert_eq!(buf[(31, 3)].style().bg, shadow.bg);
assert_eq!(buf[(10, 7)].style().bg, shadow.bg);
fn the_body_excludes_the_frame_and_the_button_row() {
let area = Rect::new(2, 1, 30, 10);
assert_eq!(Dialog::body_area(area), Rect::new(3, 2, 28, 5));
assert_eq!(
Dialog::body_area(Rect::new(0, 0, 4, 3)),
Rect::new(1, 1, 2, 0)
);
fn the_buttons_are_centred_in_the_last_two_inner_rows() {
let areas = Dialog::button_areas(Rect::new(0, 0, 30, 7), &BUTTONS, ButtonRow::Centred);
assert_eq!(areas, [Rect::new(4, 4, 10, 2), Rect::new(16, 4, 10, 2)]);
fn a_dialog_too_short_for_buttons_has_none() {
assert!(Dialog::button_areas(Rect::new(0, 0, 30, 3), &BUTTONS, ButtonRow::Centred).is_empty());
fn button(label: &'static str) -> Button<'static> {
label,
fn xs(area: Rect, count: usize, row: ButtonRow) -> Vec<(u16, u16, u16)> {
let all = [button("~O~K"), button("~C~ancel"), button("~H~elp")];
Dialog::button_areas(area, &all[..count], row)
.iter()
.map(|rect| (rect.x, rect.width, rect.y))
fn a_centred_row_puts_one_two_and_three_buttons_in_the_middle() {
let area = Rect::new(0, 0, 44, 8);
let centred = ButtonRow::Centred;
assert_eq!(xs(area, 1, centred), [(17, 10, 5)]);
assert_eq!(xs(area, 2, centred), [(11, 10, 5), (23, 10, 5)]);
assert_eq!(xs(area, 3, centred), [(5, 10, 5), (17, 10, 5), (29, 10, 5)]);
fn a_bottom_right_row_ends_one_column_before_the_frame() {
let right = ButtonRow::BottomRight;
assert_eq!(xs(area, 1, right), [(32, 10, 5)]);
assert_eq!(xs(area, 2, right), [(20, 10, 5), (32, 10, 5)]);
assert_eq!(xs(area, 3, right), [(8, 10, 5), (20, 10, 5), (32, 10, 5)]);
fn a_row_wider_than_the_dialog_is_cut_off_at_the_frame() {
let area = Rect::new(0, 0, 30, 8);
let cut = [(1, 10, 5), (13, 10, 5), (25, 4, 5)];
assert_eq!(xs(area, 3, ButtonRow::Centred), cut);
assert_eq!(xs(area, 3, ButtonRow::BottomRight), cut);
xs(area, 2, ButtonRow::BottomRight),
[(6, 10, 5), (18, 10, 5)]
let areas = Dialog::button_areas(Rect::new(0, 0, 12, 8), &BUTTONS, ButtonRow::Centred);
assert_eq!(areas[0], Rect::new(1, 5, 10, 2));
assert_eq!(areas[1].area(), 0);
fn the_row_width_is_the_buttons_and_the_gaps() {
assert_eq!(Dialog::button_row_width(&BUTTONS), 22);
assert_eq!(Dialog::button_row_width(&BUTTONS[..1]), 10);
assert_eq!(Dialog::button_row_width(&[]), 0);
fn a_click_hits_the_face_without_the_blank_column_the_shadow_column_and_the_shadow_row() {
let face = Button::face(Rect::new(4, 4, 10, 2));
assert_eq!(face, Rect::new(5, 4, 8, 1));
assert_eq!(Button::face(Rect::new(0, 0, 1, 2)).width, 0);
fn the_scrollbar_runs_along_the_body_rows_of_the_right_frame() {
let area = Rect::new(0, 0, 30, 10);
let mut buf = Buffer::empty(Rect::new(0, 0, 34, 11));
Dialog::new(&theme(), "Form", &BUTTONS)
.scrollbar(40, 5, 0)
.render(area, &mut buf);
let body = Dialog::body_area(area);
assert_eq!(body.height, 5);
assert_eq!(buf[(29, 1)].symbol(), "\u{25B2}");
assert_eq!(buf[(29, 5)].symbol(), "\u{25BC}");
assert_eq!(buf[(29, 6)].symbol(), "\u{2551}");
assert_eq!(buf[(29, 0)].symbol(), "\u{2557}");
let thumb = theme().style(Role::ScrollbarThumb);
assert_eq!(buf[(29, 2)].style().bg, thumb.bg);
assert_eq!(buf[(29, 2)].style().fg, thumb.fg);
fn no_scrollbar_when_the_body_fits() {
.scrollbar(6, 6, 0)
assert_eq!(buf[(29, 1)].symbol(), "\u{2551}");
fn the_scrollbar_frame_is_the_right_frame_from_the_corner_row_to_the_one_below_the_body() {
let frame = Dialog::scrollbar_frame(Rect::new(0, 0, 30, 10));
assert_eq!((frame.y, frame.bottom()), (0, 7));
assert_eq!(scrollbar_track(frame), Rect::new(29, 1, 1, 5));
fn a_row_as_wide_as_a_u16_places_without_overflow() {
let label = "x".repeat(70_000);
let huge = [Button {
label: &label,
}];
assert_eq!(Dialog::button_row_width(&huge), u16::MAX);
for row in [ButtonRow::Centred, ButtonRow::BottomRight] {
let areas = Dialog::button_areas(area, &huge, row);
assert_eq!(areas, [Rect::new(1, 5, 28, 2)]);
fn the_dialog_frame_has_a_close_icon_and_no_zoom_grow_or_number_in_both_presets() {
for name in ["dn", "turbo"] {
let theme = Theme::preset(name).expect("the preset parses");
let mut buf = Buffer::empty(Rect::new(0, 0, 30, 7));
Dialog::new(&theme, "Form", &BUTTONS).render(Rect::new(0, 0, 30, 7), &mut buf);
let top: String = (0..30).map(|x| buf[(x, 0)].symbol().to_string()).collect();
assert_eq!(top, "╔═[■]═══════ Form ═══════════╗", "{name}");
buf[(2, 0)].style().fg,
theme.style(Role::DialogFrame).fg,
"{name}"
buf[(3, 0)].style().fg,
theme.style(Role::DialogIcon).fg,
buf[(3, 0)].style().bg,
theme.style(Role::Dialog).bg,
buf[(4, 0)].style().fg,
assert_eq!(buf[(25, 0)].symbol(), "═", "{name}: no zoom");
assert_eq!(buf[(28, 6)].symbol(), "═", "{name}: no grow mark");
assert_eq!(buf[(23, 0)].symbol(), "═", "{name}: no number");
fn the_close_area_is_the_icon_and_min_size_holds_the_buttons_and_a_body_row() {
Dialog::close_area(Rect::new(4, 2, 30, 7)),
Some(Rect::new(6, 2, 3, 1))
assert_eq!(Dialog::close_area(Rect::new(0, 0, 11, 7)), None);
let (width, height) = Dialog::min_size(&BUTTONS, ButtonRow::BottomRight);
assert_eq!(height, 6);
let buttons = Dialog::button_areas(
Rect::new(0, 0, width, height),
&BUTTONS,
ButtonRow::BottomRight,
assert!(buttons.iter().all(|area| area.width > 0));
fn min_size_is_one_column_wider_for_a_right_hand_row() {
let row = Dialog::button_row_width(&BUTTONS);
assert_eq!(Dialog::min_size(&BUTTONS, ButtonRow::Centred).0, row + 2);
Dialog::min_size(&BUTTONS, ButtonRow::BottomRight).0,
row + 3
fn a_dragging_dialog_has_a_single_line_frame_and_its_icon_in_one_colour() {
Dialog::new(&theme, "Form", &BUTTONS)
.dragging(true)
.render(Rect::new(0, 0, 30, 7), &mut buf);
assert_eq!(buf[(0, 0)].symbol(), "┌");
assert_eq!(buf[(2, 0)].symbol(), "[");
assert_eq!(buf[(2, 0)].style().fg, theme.style(Role::DialogIcon).fg);
const FULL: Rect = Rect::new(0, 0, 100, 40);
fn fitted_is_as_tall_as_its_rows_and_centred() {
let area = Dialog::fitted((3, 2), &BUTTONS, ButtonRow::Centred, FULL);
assert_eq!(area.height, 2 + 2 + Dialog::BUTTON_ROWS);
assert_eq!(area.x, (FULL.width - area.width) / 2);
assert_eq!(area.y, (FULL.height - area.height) / 2);
fn fitted_follows_the_host_offset() {
let host = Rect::new(10, 5, 100, 40);
let area = Dialog::fitted((3, 2), &BUTTONS, ButtonRow::Centred, host);
assert_eq!(area.x, host.x + (host.width - area.width) / 2);
assert_eq!(area.y, host.y + (host.height - area.height) / 2);
fn fitted_widens_to_a_long_line_and_stays_as_narrow_as_its_buttons_otherwise() {
let wide = Dialog::fitted((80, 1), &BUTTONS, ButtonRow::Centred, FULL);
assert_eq!(wide.width, 84);
let short = Dialog::fitted((3, 1), &BUTTONS, ButtonRow::Centred, FULL);
assert_eq!(short.width, 24);
fn fitted_is_content_sized_on_a_wide_screen() {
let screen = Rect::new(0, 0, 200, 50);
let area = Dialog::fitted((10, 3), &BUTTONS, ButtonRow::Centred, screen);
assert_eq!(area.width, 24);
fn fitted_never_exceeds_the_host_even_when_it_is_too_small() {
let tall = Dialog::fitted((3, 100), &BUTTONS, ButtonRow::Centred, FULL);
assert_eq!(tall.height, FULL.height);
let narrow = Rect::new(0, 0, 10, 40);
let area = Dialog::fitted((3, 1), &BUTTONS, ButtonRow::Centred, narrow);
assert_eq!(area.width, narrow.width);
assert_eq!(area.x, 0);
fn fitted_gives_a_right_hand_row_one_more_column() {
let host = Rect::new(0, 0, 30, 40);
let centred = Dialog::fitted((0, 1), &BUTTONS, ButtonRow::Centred, host);
let right = Dialog::fitted((0, 1), &BUTTONS, ButtonRow::BottomRight, host);
assert_eq!(centred.width, 24);
assert_eq!(right.width, 25);