Lines
99.62 %
Functions
100 %
Branches
use super::*;
use crate::{close_area, title_area, zoom_area};
use ratatui::buffer::Buffer;
const WIDTHS: [Constraint; 2] = [Constraint::Length(4), Constraint::Fill(1)];
fn theme() -> Theme {
Theme::preset("dn").expect("the dn preset parses")
}
fn drawn(panel: Panel, area: Rect) -> Buffer {
let mut buf = Buffer::empty(area);
panel.render(area, &mut buf);
buf
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
fn state(active: bool) -> FrameState {
let mode = match active {
true => FrameMode::Active,
false => FrameMode::Passive,
};
FrameState {
mode,
..FrameState::default()
#[test]
fn an_active_panel_joins_its_dividers_to_the_double_frame() {
let theme = theme();
let panel = Panel::new(&theme, "", state(true)).widths(&WIDTHS);
let buf = drawn(panel, Rect::new(0, 0, 12, 4));
assert_eq!(row(&buf, 0), "╔═[■]╤═[↑]═╗");
assert_eq!(row(&buf, 1), "║ │ ║");
assert_eq!(row(&buf, 2), "║ │ ║");
assert_eq!(row(&buf, 3), "╚════╧════─┘");
fn a_passive_panel_joins_its_dividers_to_the_single_frame() {
let panel = Panel::new(&theme, "", state(false)).widths(&WIDTHS);
let buf = drawn(panel, Rect::new(0, 0, 12, 3));
assert_eq!(row(&buf, 0), "┌────┬─────┐");
assert_eq!(row(&buf, 2), "└────┴─────┘");
fn columns_are_the_inner_widths_with_a_gap_for_the_divider() {
let area = Rect::new(2, 1, 12, 4);
let columns = panel.columns(area);
assert_eq!(columns, [Rect::new(3, 2, 4, 2), Rect::new(8, 2, 5, 2)]);
assert_eq!(Panel::inner(area), Rect::new(3, 2, 10, 2));
fn a_panel_without_widths_has_no_dividers() {
let buf = drawn(Panel::new(&theme, "", state(true)), Rect::new(0, 0, 8, 3));
assert_eq!(row(&buf, 0), "╔══════╗");
assert_eq!(row(&buf, 1), "║ ║");
fn the_info_sits_right_aligned_in_the_bottom_border_in_the_header_role() {
let area = Rect::new(0, 0, 24, 3);
let mut buf = drawn(Panel::new(&theme, "", state(true)).widths(&WIDTHS), area);
render_info(&theme, area, &mut buf, state(true), "1–20 of 48");
assert_eq!(row(&buf, 2), "╚════╧════ 1–20 of 48 ─┘");
let header = theme.style(Role::PanelHeader);
assert_eq!(buf[(11, 2)].style().fg, header.fg);
assert_eq!(buf[(11, 2)].style().bg, header.bg);
fn the_info_styles_only_its_own_cells_not_the_border() {
let mut buf = drawn(Panel::new(&theme, "", state(true)), area);
let frame = buf[(2, 2)].style();
assert_eq!(buf[(2, 2)].style(), frame);
assert_eq!(buf[(11, 2)].style().fg, theme.style(Role::PanelHeader).fg);
assert_ne!(buf[(2, 2)].style().fg, buf[(11, 2)].style().fg);
fn a_long_info_is_clipped_inside_the_frame() {
let area = Rect::new(0, 0, 12, 3);
render_info(
&theme,
area,
&mut buf,
state(true),
"1–20 of 48 and a very long tail",
);
assert_eq!(row(&buf, 2).chars().count(), 12);
assert!(row(&buf, 2).starts_with('╚'));
assert!(row(&buf, 2).ends_with('┘'));
assert!(row(&buf, 1).ends_with('║'));
fn bar(rows: usize, view: usize, top: usize, height: u16) -> Option<String> {
let area = Rect::new(0, 0, 1, height + 2);
render_scrollbar(&theme, area, &mut buf, (rows, view, top));
let column: String = (1..=height)
.map(|y| buf[(0, y)].symbol().to_string())
.collect();
(rows > view).then_some(column)
fn the_scrollbar_is_absent_while_the_rows_fit() {
assert_eq!(bar(10, 10, 0, 12), None);
assert_eq!(bar(3, 10, 0, 12), None);
let area = Rect::new(0, 0, 1, 5);
render_scrollbar(&theme(), area, &mut buf, (3, 10, 0));
assert_eq!(buf, Buffer::empty(area));
fn the_track_is_the_right_border_inside_the_corners() {
assert_eq!(
scrollbar_track(Rect::new(0, 0, 20, 8)),
Rect::new(19, 1, 1, 6)
assert_eq!(scrollbar_track(Rect::new(4, 2, 0, 8)).width, 0);
fn the_thumb_follows_the_top_row_from_the_first_end_to_the_last() {
assert_eq!(bar(40, 10, 0, 12).as_deref(), Some("▲■■■▒▒▒▒▒▒▒▼"));
assert_eq!(bar(40, 10, 15, 12).as_deref(), Some("▲▒▒▒▒■■■▒▒▒▼"));
assert_eq!(bar(40, 10, 30, 12).as_deref(), Some("▲▒▒▒▒▒▒▒■■■▼"));
fn the_scrollbar_roles_style_the_track_and_the_thumb() {
let area = Rect::new(0, 0, 1, 14);
render_scrollbar(&theme, area, &mut buf, (40, 10, 0));
let (page, thumb) = (
theme.style(Role::Scrollbar),
theme.style(Role::ScrollbarThumb),
let colours = |y| (buf[(0, y)].style().fg, buf[(0, y)].style().bg);
assert_eq!(colours(1), (page.fg, page.bg));
assert_eq!(colours(2), (thumb.fg, thumb.bg));
assert_eq!(colours(8), (page.fg, page.bg));
fn dividers_and_junctions_take_the_frame_role_in_every_preset() {
for preset in ["dn", "turbo"] {
let theme = Theme::preset(preset).expect("preset parses");
for (active, role) in [(true, Role::PanelFrame), (false, Role::PanelFramePassive)] {
let panel = Panel::new(&theme, "", state(active)).widths(&WIDTHS);
let frame = theme.style(role);
for y in 0..4 {
let style = buf[(5, y)].style();
(style.fg, style.bg),
(frame.fg, frame.bg),
"{preset} {active} {y}"
fn the_rows_are_inside_the_frame_below_the_header() {
assert_eq!(Panel::rows(Rect::new(2, 1, 20, 8)), Rect::new(3, 3, 18, 5));
assert_eq!(Panel::rows(Rect::new(0, 0, 20, 2)).height, 0);
fn the_range_counts_from_one_and_is_absent_without_rows() {
assert_eq!(range_text(0..20, 48).as_deref(), Some("1\u{2013}20 of 48"));
range_text(22..30, 30).as_deref(),
Some("23\u{2013}30 of 30")
assert_eq!(range_text(5..5, 30), None);
fn bar_symbol(part: BarPart) -> &'static str {
match part {
BarPart::Up => "\u{25B2}",
BarPart::Down => "\u{25BC}",
BarPart::Thumb => "\u{25A0}",
BarPart::PageUp | BarPart::PageDown => "\u{2592}",
fn the_bar_parts_cover_the_cells_the_rendered_bar_draws() {
let shapes = [(30, 10), (100, 7), (11, 10), (40, 1), (9, 0)];
for height in [1u16, 2, 3, 4, 5, 8, 12, 30] {
for (rows, view) in shapes {
for top in [0, 1, (rows - view) / 2, rows - view] {
let area = Rect::new(0, 0, 6, height + 2);
let parts = scrollbar_parts(area, (rows, view, top));
let track = scrollbar_track(area);
assert_eq!(parts.is_empty(), height <= 2, "{height} {rows}/{view}");
let drawn = if parts.is_empty() { 0 } else { track.height };
for y in track.top()..track.top() + drawn {
let hits: Vec<_> = parts
.iter()
.filter(|(cell, _)| cell.contains((track.x, y).into()))
let [(_, part)] = hits[..] else {
panic!("{height} {rows}/{view}@{top}: row {y} holds {hits:?}");
buf[(track.x, y)].symbol(),
bar_symbol(*part),
"{height} {rows}/{view}@{top} {y} {part:?}"
fn a_bar_is_split_into_arrows_page_areas_and_a_thumb_in_order() {
let area = Rect::new(0, 0, 4, 12);
let parts: Vec<_> = scrollbar_parts(area, (20, 10, 5)).into_iter().collect();
let kinds: Vec<_> = parts.iter().map(|(_, part)| *part).collect();
kinds,
[
BarPart::Up,
BarPart::PageUp,
BarPart::Thumb,
BarPart::PageDown,
BarPart::Down
]
assert_eq!(parts.first().map(|(cell, _)| cell.y), Some(track.y));
parts.last().map(|(cell, _)| cell.bottom()),
Some(track.bottom())
fn a_bar_without_hidden_rows_has_no_parts() {
assert!(scrollbar_parts(Rect::new(0, 0, 4, 12), (10, 10, 0)).is_empty());
assert!(scrollbar_parts(Rect::new(0, 0, 4, 12), (3, 10, 0)).is_empty());
fn a_passive_panel_info_runs_to_the_corner_margin() {
let mut buf = drawn(Panel::new(&theme, "", state(false)), area);
render_info(&theme, area, &mut buf, state(false), "1–20 of 48");
assert_eq!(row(&buf, 2), "└────────── 1–20 of 48 ┘");
fn a_narrow_info_row_draws_nothing_and_does_not_panic() {
for width in 0..6 {
let area = Rect::new(0, 0, width, 3);
render_info(&theme, area, &mut buf, state(false), "info");
render_info(&theme, area, &mut buf, state(true), "info");
fn a_dragging_panel_draws_single_dividers_in_the_icon_colour() {
let dragging = FrameState {
mode: FrameMode::Dragging,
let panel = Panel::new(&theme, "", dragging).widths(&WIDTHS);
assert_eq!(row(&buf, 0), "┌─[■]┬─[↑]─┐");
let icon = theme.style(Role::FrameIcon);
assert_eq!((style.fg, style.bg), (icon.fg, icon.bg), "{y}");
fn the_dividers_never_land_on_the_icons_title_or_number() {
let area = Rect::new(0, 0, 16, 4);
let widths = [Constraint::Fill(1); 4];
let numbered = FrameState {
number: Some(5),
..state(true)
let panel = Panel::new(&theme, "A", numbered).widths(&widths);
let gaps: Vec<u16> = columns.iter().map(|column| column.right()).collect();
let buf = drawn(panel, area);
let close = close_area(area, numbered).expect("fits");
let zoom = zoom_area(area, numbered).expect("fits");
assert!(
gaps.iter().any(|x| close.contains((*x, 0).into())),
"{gaps:?}"
gaps.iter().any(|x| zoom.contains((*x, 0).into())),
let cells = |rect: Rect| -> String {
rect.positions()
.map(|p| buf[p].symbol().to_string())
assert_eq!(cells(close), "[■]");
assert_eq!(cells(zoom), "[↑]");
assert_eq!(buf[(9, 0)].symbol(), "5");
assert_eq!(cells(title_area(area, "A").expect("fits")), "A");