Lines
97.1 %
Functions
100 %
Branches
//! Up and Down walk the dialog controls from a one-line field; a button walks them with all four arrows.
use super::tests::{ctrl, key, press};
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
use crate::widgets::splits::COL_VALUE;
use crate::widgets::{DateWidget, SelectOption, SelectWidget, Widget};
use ratatui::crossterm::event::KeyCode;
const PRESETS: [&str; 3] = ["cua", "emacs", "vim"];
fn form_app(preset: &str, form: Form) -> App {
let mut app = crate::test_support::app_with_preset(preset);
app.overlays.push(Modal::Form(form));
app
}
fn form_of(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
other => panic!("not a form: {other:?}"),
fn focus(app: &App) -> usize {
form_of(app).focus
fn set_focus(app: &mut App, focus: usize) {
if let Some(form) = app.overlays.form_mut() {
form.focus = focus;
fn row_focus(app: &App) -> usize {
match &form_of(app).fields[3].widget {
Widget::Splits(sw) => sw.row_focus,
_ => panic!("no splits"),
fn buttons_start(app: &App) -> usize {
form_of(app).fields.len()
#[test]
fn down_and_up_on_a_text_field_walk_the_controls_and_wrap() {
for preset in PRESETS {
let mut app = form_app(preset, Form::config_set());
let first_button = buttons_start(&app);
press(&mut app, key(KeyCode::Down));
assert_eq!(focus(&app), 1, "{preset}");
press(&mut app, key(KeyCode::Up));
assert_eq!(focus(&app), 0, "{preset}");
assert_eq!(
focus(&app),
first_button + 1,
"{preset} wraps to the last button"
);
assert_eq!(focus(&app), 0, "{preset} wraps to the first field");
fn left_and_right_on_a_text_field_still_move_the_cursor() {
super::tests::type_text(&mut app, "ab");
press(&mut app, key(KeyCode::Left));
super::tests::type_text(&mut app, "x");
assert_eq!(super::tests::focused_text(&app), "axb", "{preset}");
press(&mut app, key(KeyCode::Right));
super::tests::type_text(&mut app, "y");
assert_eq!(super::tests::focused_text(&app), "axby", "{preset}");
fn control_up_and_down_step_a_date_and_the_arrows_leave_it() {
let mut form = Form::config_set();
form.fields[0].widget = Widget::Date(DateWidget::with_value("2026-01-10"));
let mut app = form_app(preset, form);
press(&mut app, ctrl(KeyCode::Down));
form_of(&app).fields[0].widget.value(),
"2026-01-11",
"{preset}"
press(&mut app, ctrl(KeyCode::Up));
"2026-01-09",
press(&mut app, key(KeyCode::Tab));
fn down_from_the_date_of_a_transaction_reaches_the_buttons_through_every_field() {
let mut app = form_app(preset, Form::transaction_create());
let fields = buttons_start(&app);
let mut seen = vec![focus(&app)];
while focus(&app) < fields {
seen.push(focus(&app));
assert_eq!(seen[1], 1, "{preset} the note follows the date");
assert!(
seen.windows(2).all(|w| w[1] == w[0] + 1),
"{preset} {seen:?}"
assert_eq!(focus(&app), fields + 1, "{preset}");
fn select_form() -> Form {
let mut select = SelectWidget::new();
select.set_options(
["alpha", "beta", "gamma"]
.map(|name| SelectOption {
id: name.to_string(),
label: name.to_string(),
})
.to_vec(),
form.fields[0].widget = Widget::Select(select);
form
fn a_closed_select_is_left_by_down_and_opened_by_enter_or_space() {
let mut app = form_app(preset, select_form());
for opener in [KeyCode::Enter, KeyCode::Char(' ')] {
press(&mut app, key(opener));
assert!(select_open(&app), "{preset} {opener:?}");
let before = select_index(&app);
assert_ne!(select_index(&app), before, "{preset}");
press(&mut app, key(KeyCode::Esc));
assert!(!select_open(&app), "{preset}");
assert_eq!(focus(&app), buttons_start(&app) + 1, "{preset} left upward");
set_focus(&mut app, 0);
fn select_widget(app: &App) -> &SelectWidget {
match &form_of(app).fields[0].widget {
Widget::Select(select) => select,
_ => panic!("a select"),
fn select_open(app: &App) -> bool {
select_widget(app).open
fn select_index(app: &App) -> usize {
select_widget(app).focused_in_filtered()
fn up_and_down_move_inside_the_splits_and_leave_at_its_first_and_last_row() {
let mut form = Form::transaction_create();
let splits = 3;
if let Widget::Splits(sw) = &mut form.fields[splits].widget {
sw.add_row();
sw.row_focus = 0;
sw.col_focus = COL_VALUE;
form.focus = splits;
assert_eq!(focus(&app), splits, "{preset}");
assert_eq!(row_focus(&app), 1, "{preset}");
assert_eq!(row_focus(&app), 0, "{preset}");
assert_eq!(focus(&app), splits - 1, "{preset}");
splits + 1,
"{preset} leaves below the last row"
fn up_and_down_move_between_the_lines_of_a_text_area_and_leave_at_its_ends() {
if let Some(note) = form.fields[1].widget.as_text_mut() {
note.replace_buffer("one\ntwo");
form.focus = 1;
assert_eq!(focus(&app), 1, "{preset} inside the text");
assert_eq!(focus(&app), 2, "{preset} below the last line");
set_focus(&mut app, 1);
assert_eq!(focus(&app), 0, "{preset} above the first line");
fn the_arrows_walk_the_buttons_and_back_into_the_fields() {
let ok = buttons_start(&app);
set_focus(&mut app, ok);
assert_eq!(focus(&app), ok + 1, "{preset} right");
assert_eq!(focus(&app), ok, "{preset} left");
assert_eq!(focus(&app), ok + 1, "{preset} down");
assert_eq!(focus(&app), ok, "{preset} up");
assert_eq!(focus(&app), ok - 1, "{preset} back to the last field");
set_focus(&mut app, ok + 1);
assert_eq!(focus(&app), 0, "{preset} on to the first field");
fn enter_on_a_button_reached_by_the_arrows_presses_it() {
let close = buttons_start(&app) + 1;
for _ in 0..close {
assert_eq!(focus(&app), close, "{preset}");
press(&mut app, key(KeyCode::Enter));
assert!(app.overlays.top().is_none(), "{preset}");