Lines
99.43 %
Functions
100 %
Branches
//! The key names in modal titles and the confirm text follow the keymap.
use super::*;
use crate::hits::Slot;
use crate::modal::{ConfirmAction, Csv, Draft, FilterBuilder};
fn app_with(overrides: &str) -> App {
let mut app = crate::test_support::app();
app.keys = match app.keys.overridden(overrides) {
Ok(keys) => keys,
Err(err) => panic!("the override parses: {err}"),
};
app
}
fn title(modal: &Modal, app: &App) -> String {
modal_content(modal, app)
.map(|content| content.0)
.unwrap_or_default()
fn draft() -> Modal {
Modal::Draft(Draft::new(Vec::new()))
#[test]
fn the_titles_name_their_dialog_and_leave_the_keys_to_the_bar() {
let app = crate::test_support::app();
let csv = Modal::Csv(Csv::new(String::new(), String::new()));
assert_eq!(title(&csv, &app), "Category CSV (saving is local only)");
let draft = Modal::Draft(Draft::new(Vec::new()));
assert_eq!(title(&draft, &app), "Template draft");
fn the_filter_builder_title_leaves_the_keys_to_the_bar() {
let builder = Modal::FilterBuilder(FilterBuilder::new("(and)"));
assert_eq!(title(&builder, &app), "Filter");
fn a_form_box_fits_its_lines_and_stays_on_the_screen() {
let full = Rect::new(0, 0, 80, 24);
let few = Text::from(vec![Line::from("a"), Line::from("b")]);
let area = fitted_rect(&few, &[], full);
assert_eq!((area.width, area.height), (5, 7));
assert_eq!((area.x, area.y), (37, 8));
let tall = Text::from(vec![Line::from("x".repeat(100)); 40]);
assert_eq!(fitted_rect(&tall, &[], full), full);
fn labels(modal: &Modal, app: &App) -> Vec<(&'static str, bool)> {
buttons(modal, app)
.iter()
.map(|b| (b.label, b.default))
.collect()
fn confirm(action: ConfirmAction) -> Modal {
Modal::Confirm {
focus: None,
prompt: String::new(),
action,
fn every_kind() -> Vec<Modal> {
vec![
Modal::Csv(Csv::new(String::new(), String::new())),
Modal::Draft(Draft::new(Vec::new())),
Modal::FilterBuilder(FilterBuilder::new("(and)")),
Modal::Form(crate::form::Form::tag_create()),
confirm(ConfirmAction::DeleteTransaction(String::new())),
confirm(ConfirmAction::DiscardForm),
]
fn each_modal_kind_has_its_buttons_and_the_enter_default() {
assert_eq!(labels(&draft(), &app), [("~C~lose", true)]);
let help = Modal::Draft(Draft::new(Vec::new()));
assert_eq!(labels(&help, &app), [("~C~lose", true)]);
let form = Modal::Form(crate::form::Form::tag_create());
assert_eq!(labels(&form, &app), [("~O~K", true), ("~C~lose", false)]);
assert_eq!(
labels(&builder, &app),
[("A~p~ply", false), ("~C~lose", false)]
);
let delete = confirm(ConfirmAction::DeleteTransaction(String::new()));
assert_eq!(labels(&delete, &app), [("~Y~es", true), ("~N~o", false)]);
let discard = confirm(ConfirmAction::DiscardForm);
assert_eq!(labels(&discard, &app), [("~Y~es", false), ("~N~o", true)]);
fn the_default_follows_the_enter_binding() {
let app = app_with("modal-view: Enter = none; modal-view: F9 = close");
assert_eq!(labels(&draft(), &app), [("~C~lose", false)]);
/// The keys a button's highlighted letter types: a plain key, or Alt with it in a form.
fn hotkey_chord(modal: &Modal, label: &str) -> String {
let letter = tvui::hotkey(label)
.map(|c| c.to_ascii_lowercase())
.unwrap_or(' ');
match modal {
Modal::Form(_) => format!("M-{letter}"),
_ => letter.to_string(),
fn every_button_hotkey_presses_its_intent_in_the_modal_chain() {
for modal in every_kind() {
let keys = keymap::in_modal(&app, &modal);
for &(label, intent) in modal.button_specs() {
let chord = hotkey_chord(&modal, label);
assert_eq!(keys.bound(&chord), Some(intent), "{label} {chord}");
fn a_text_box_is_as_wide_as_three_buttons_need_when_the_text_is_short() {
let button = |label| Button {
label,
default: false,
let three = [button("~Y~es"), button("~N~o"), button("~A~ll")];
let full = Rect::new(0, 0, 50, 24);
let short = Text::from("Sure?");
assert_eq!(fitted_rect(&short, &[], full).width, 9);
assert_eq!(fitted_rect(&short, &three, full).width, 36);
fn opened_slot(modal: Modal) -> Slot {
app.overlays.push(modal);
let (_, hits) = crate::test_support::draw_frame(&app, 120, 40);
hits.dialog().expect("a dialog was drawn")
fn draft_of(lines: usize) -> Modal {
let lines = (0..lines).map(|n| format!("line {n}")).collect();
Modal::Draft(Draft::new(lines))
fn a_short_draft_opens_as_tall_as_its_lines_not_the_host() {
let slot = opened_slot(draft_of(6));
let expected = 6 + 2 + Dialog::BUTTON_ROWS;
assert_eq!(slot.auto.height, expected);
assert!(slot.auto.height < slot.host.height);
fn a_draft_taller_than_the_host_is_capped_at_the_host_and_scrolls() {
let slot = opened_slot(draft_of(200));
assert_eq!(slot.auto.height, slot.host.height);
fn an_empty_dialog_opens_at_its_minimum_size() {
for modal in [
draft_of(0),
] {
let slot = opened_slot(modal);
assert!(slot.auto.width >= slot.min.0 && slot.auto.height >= slot.min.1);
slot.auto.y,
(slot.host.height - slot.auto.height) / 2 + slot.host.y
fn the_quit_confirmation_opens_as_a_forty_by_nine_message_box() {
app.overlays
.push(Modal::confirm("Quit?", ConfirmAction::Quit));
let (_, hits) = crate::test_support::draw_frame(&app, 80, 24);
let slot = hits.dialog().expect("a dialog was drawn");
assert_eq!((slot.auto.width, slot.auto.height), (40, 9));
assert_eq!(slot.auto.x, slot.host.x + (slot.host.width - 40) / 2);
assert_eq!(slot.auto.y, slot.host.y + (slot.host.height - 9) / 2);
fn a_long_confirmation_wraps_inside_the_message_area() {
let (from, to) = (
"Assets:Bank:Checking:Personal:Main",
"Assets:Bank:Savings:Personal:Main",
let prompt = format!("Merge {from} into {to}? Every split moves and the source is deleted.");
let len = prompt.chars().count();
.push(Modal::confirm(prompt, ConfirmAction::DiscardForm));
let (buffer, hits) = crate::test_support::draw_frame(&app, 80, 24);
let area = slot.auto;
assert_eq!(usize::from(area.height), len / 33 + 7);
let text = |dx: u16, dy: u16| buffer[(area.x + dx, area.y + dy)].symbol().to_string();
assert_eq!(text(3, 2), "M");
assert_ne!(text(3, 3), " ");
for dy in 2..area.height - 3 {
assert_eq!(text(38, dy), " ", "row {dy}");
fn the_filter_builder_is_content_sized_on_a_big_screen() {
app.overlays.push(Modal::FilterBuilder(FilterBuilder::new(
r#"(and (amount> 1) (account= "a"))"#,
)));
let (_, hits) = crate::test_support::draw_frame(&app, 200, 50);
(slot.auto.width, slot.auto.height),
(24, 4 + 2 + Dialog::BUTTON_ROWS)