Lines
100 %
Functions
Branches
//! The areas a frame records are the ones it drew.
use super::body_area;
use crate::app::App;
use crate::hits::{Hits, Target};
use crate::modal::{ConfirmAction, Modal};
use crate::test_support::draw_frame;
use crate::view::Tab;
use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use tvui::{MenuBar, MenuItem, MenuPopup, Panel, StatusBar, plain};
const SCREEN: Rect = Rect::new(0, 0, 80, 24);
fn text(buffer: &Buffer, rect: Rect) -> String {
(rect.x..rect.right())
.map(|x| buffer[(x, rect.y)].symbol())
.collect()
}
fn targets(hits: &Hits, pick: impl Fn(Target) -> bool) -> Vec<(Rect, Target)> {
hits.entries()
.iter()
.filter(|(_, target)| pick(*target))
.copied()
fn accounts() -> App {
let mut app = crate::test_support::app();
app.switch_tab(Tab::Accounts);
let row = |i: usize| format!(r#"(:account :id "u{i}" :name "acc{i}" :parent "")"#);
let rows: Vec<String> = (0..3).map(row).collect();
app.accounts_mut()
.on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
app
fn is_row(target: Target) -> bool {
matches!(target, Target::Row(_))
#[test]
fn a_list_records_each_row_where_it_draws_it() {
let (buffer, hits) = draw_frame(&accounts(), SCREEN.width, SCREEN.height);
let body = Panel::rows(body_area(SCREEN));
let rows = targets(&hits, is_row);
assert_eq!(rows.len(), 3);
for (i, (rect, target)) in rows.into_iter().enumerate() {
let y = body.y + u16::try_from(i).expect("small");
assert_eq!(
(rect, target),
(Rect::new(body.x, y, body.width, 1), Target::Row(i))
);
assert!(text(&buffer, rect).contains(&format!("acc{i}")), "{i}");
fn the_menu_bar_records_its_title_and_a_closed_menu_no_backdrop() {
let bar = Rect::new(0, 0, SCREEN.width, 1);
let titles = targets(&hits, |t| matches!(t, Target::MenuTitle(_)));
titles,
MenuBar::title_areas(bar, &["~V~iew", "~W~indow", "~O~ptions"])
.into_iter()
.enumerate()
.map(|(index, area)| (area, Target::MenuTitle(index)))
.collect::<Vec<_>>()
assert_eq!(text(&buffer, titles[0].0), " View ");
assert_eq!(text(&buffer, titles[1].0), " Window ");
assert!(targets(&hits, |t| t == Target::MenuBackdrop).is_empty());
fn an_open_menu_records_a_backdrop_and_each_item_on_top_of_the_view() {
let mut app = accounts();
app.open_menu();
let (buffer, hits) = draw_frame(&app, SCREEN.width, SCREEN.height);
targets(&hits, |t| t == Target::MenuBackdrop),
vec![(SCREEN, Target::MenuBackdrop)]
let entries = app.menu_entries();
let items: Vec<MenuItem> = entries
.map(|entry| MenuItem {
label: &entry.label,
key: &entry.key,
marked: entry.marked,
enabled: entry.enabled,
})
.collect();
let anchor = MenuBar::title_areas(Rect::new(0, 0, SCREEN.width, 1), &["~V~iew", "~W~indow"])[0];
let found = targets(&hits, |t| matches!(t, Target::MenuItem(_)));
let want: Vec<_> = MenuPopup::item_areas(anchor, &items)
.map(|(i, rect)| (rect, Target::MenuItem(i)))
assert_eq!(found, want);
assert_eq!(found.len(), Tab::ALL.len());
assert!(text(&buffer, found[0].0).contains("Accounts"));
let over_a_row = found[0].0;
let top = hits.hit_at(Position::new(over_a_row.x, over_a_row.y));
assert_eq!(top.map(|(_, target, _)| target), Some(Target::MenuItem(0)));
fn the_status_bar_records_each_hint_it_draws_with_its_intent() {
let app = accounts();
let hinted = crate::keymap::hinted(&app);
let pairs: Vec<_> = hinted.iter().map(crate::keymap::Hint::pair).collect();
let found = targets(&hits, |t| matches!(t, Target::Hint(_)));
assert!(!found.is_empty());
let bar = Rect::new(0, SCREEN.bottom() - 1, SCREEN.width, 1);
let areas = StatusBar::item_areas(bar, &pairs);
assert!(found.len() <= areas.len());
for ((rect, target), (index, area)) in found.iter().zip(areas) {
assert_eq!((*rect, *target), (area, Target::Hint(hinted[index].intent)));
let (key, label) = &pairs[index];
assert_eq!(text(&buffer, *rect), format!("{key} {label}"));
fn a_status_message_replaces_the_hints() {
app.set_status("saved");
let (_, hits) = draw_frame(&app, SCREEN.width, SCREEN.height);
assert!(targets(&hits, |t| matches!(t, Target::Hint(_))).is_empty());
fn confirm() -> App {
app.overlays.push(Modal::Confirm {
prompt: "Delete it?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
fn a_confirm_blocks_the_screen_and_records_yes_and_no_where_drawn() {
let (buffer, hits) = draw_frame(&confirm(), SCREEN.width, SCREEN.height);
let buttons = targets(&hits, |t| matches!(t, Target::Button(_)));
assert_eq!(buttons.len(), 2);
assert!(text(&buffer, buttons[0].0).contains("Yes"));
assert!(text(&buffer, buttons[1].0).contains("No"));
assert_eq!(buttons[1].1, Target::Button(1));
let blocks = targets(&hits, |t| t == Target::Block);
assert_eq!(blocks.last(), Some(&(SCREEN, Target::Block)));
let entries = hits.entries();
let first_row = entries.iter().position(|(_, t)| is_row(*t)).expect("rows");
let block = entries
.position(|entry| *entry == (SCREEN, Target::Block))
.expect("block");
assert!(first_row < block, "the modal lies over the view");
fn a_form_records_its_fields_and_buttons_where_drawn() {
let form = crate::form::Form::transaction_create();
let labels: Vec<&str> = form.fields.iter().map(|f| f.label).collect();
app.overlays.push(Modal::Form(form));
let fields = targets(&hits, |t| matches!(t, Target::Field(_)));
assert!(fields.len() >= 3, "{fields:?}");
for (rect, target) in &fields {
let Target::Field(i) = target else { continue };
let drawn = text(&buffer, Rect { height: 1, ..*rect });
assert!(
drawn.contains(plain(labels[*i]).trim_end_matches(':')),
"{i}: {drawn}"
assert!(text(&buffer, buttons[0].0).contains("OK"));
assert!(text(&buffer, buttons[1].0).contains("Close"));
let at = hits.hit_at(Position::new(buttons[0].0.x, buttons[0].0.y));
assert_eq!(at.map(|(_, t, _)| t), Some(Target::Button(0)));