Lines
100 %
Functions
Branches
use ratatui::style::Style;
use super::*;
const PRESETS: [&str; 2] = ["dn", "turbo"];
fn theme(preset: &str) -> Theme {
Theme::preset(preset).expect("the preset parses")
}
const OK: Button<'static> = Button {
label: "O~K~",
default: false,
};
fn drawn(button: &Button<'_>, theme: &Theme, look: Look, width: u16) -> Buffer {
let mut buf = Buffer::empty(Rect::new(0, 0, width, HEIGHT));
button.render(Rect::new(0, 0, width, HEIGHT), &mut buf, theme, look);
buf
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
fn style_at(
buf: &Buffer,
x: u16,
y: u16,
) -> (Option<ratatui::style::Color>, Option<ratatui::style::Color>) {
let style = buf[(x, y)].style();
(style.fg, style.bg)
fn pair(style: Style) -> (Option<ratatui::style::Color>, Option<ratatui::style::Color>) {
#[test]
fn the_width_is_ten_or_the_label_and_four() {
assert_eq!(OK.width(), 10);
let cancel = Button {
label: "~C~ancel",
..OK
assert_eq!(cancel.width(), 10);
let long = Button {
label: "~S~ave and close",
assert_eq!(long.width(), 18);
fn the_up_button_has_the_face_the_right_shadow_and_the_lower_shadow() {
for preset in PRESETS {
let theme = theme(preset);
let buf = drawn(&OK, &theme, Look::default(), 10);
assert_eq!(row(&buf, 0), " OK \u{2584}", "{preset}");
assert_eq!(
row(&buf, 1),
format!(" {}", "\u{2580}".repeat(8)),
"{preset}"
);
let shadow = pair(theme.style(Role::ButtonShadow));
let face = pair(theme.style(Role::Button));
assert_eq!(style_at(&buf, 0, 0), shadow, "{preset} blank column");
assert_eq!(style_at(&buf, 9, 0), shadow, "{preset} right shadow");
for x in 1..9 {
assert_ne!(style_at(&buf, x, 0), shadow, "{preset} face {x}");
assert_eq!(style_at(&buf, 1, 0), face, "{preset} face");
for x in 0..10 {
assert_eq!(style_at(&buf, x, 1), shadow, "{preset} lower {x}");
fn the_pressed_button_loses_its_shadow_and_moves_one_column_right() {
let look = Look {
pressed: true,
..Look::default()
let buf = drawn(&OK, &theme, look, 10);
assert_eq!(row(&buf, 0), " OK ", "{preset}");
assert_eq!(row(&buf, 1), " ".repeat(10), "{preset}");
assert_eq!(style_at(&buf, 0, 0), shadow, "{preset}");
assert_eq!(style_at(&buf, 1, 0), shadow, "{preset}");
assert_eq!(style_at(&buf, 2, 0), face, "{preset}");
assert_eq!(style_at(&buf, 9, 0), face, "{preset}");
fn the_label_is_centred_on_the_face() {
let buf = drawn(&cancel, &theme("dn"), Look::default(), 10);
assert_eq!(row(&buf, 0), " Cancel \u{2584}");
let wide = drawn(&OK, &theme("dn"), Look::default(), 20);
row(&wide, 0),
format!("{}OK{}\u{2584}", " ".repeat(9), " ".repeat(8))
fn the_face_takes_its_role_from_the_state_and_the_hotkey_is_lit() {
let default = Button {
default: true,
let states = [
(OK, false, Role::Button),
(default, false, Role::ButtonDefault),
(default, true, Role::ButtonFocus),
(OK, true, Role::ButtonFocus),
];
for (button, focused, role) in states {
focused,
pressed: false,
let buf = drawn(&button, &theme, look, 10);
let face = theme.style(role);
assert_eq!(style_at(&buf, 1, 0), pair(face), "{preset} {role:?}");
assert_eq!(style_at(&buf, 4, 0), pair(face), "{preset} {role:?} O");
let hot = (theme.style(Role::ButtonHotkey).fg, face.bg);
assert_eq!(style_at(&buf, 5, 0), hot, "{preset} {role:?} K");
fn a_button_in_a_narrow_or_off_screen_area_does_not_panic() {
let theme = theme("dn");
let narrow = drawn(&OK, &theme, Look::default(), 4);
assert_eq!(row(&narrow, 0), " O\u{2584}");
let mut small = Buffer::empty(Rect::new(0, 0, 6, 1));
OK.render(Rect::new(0, 0, 10, 2), &mut small, &theme, Look::default());
assert_eq!(row(&small, 0), " OK");
let tiny = drawn(&OK, &theme, Look::default(), 2);
assert_eq!(row(&tiny, 0), " ");