Lines
98.33 %
Functions
100 %
Branches
//! A submitted form stays where its dialog stood, whatever happens to it before the reply.
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
use crate::overlay::Placement;
use crate::test_support::{app, draw_frame, draw_into};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use tvui::Place;
/// The rect of the double-framed dialog in `buffer`.
pub(crate) fn dialog_rect(buffer: &Buffer) -> Option<Rect> {
let area = buffer.area;
let mut corners = (area.y..area.bottom())
.flat_map(|y| (area.x..area.right()).map(move |x| (x, y)))
.filter(|&at| matches!(buffer[at].symbol(), "╔" | "╝"));
let (left, top) = corners.next()?;
let (right, bottom) = corners.next_back()?;
Some(Rect::new(left, top, right + 1 - left, bottom + 1 - top))
}
pub(crate) fn rect_at(app: &App, width: u16, height: u16) -> Option<Rect> {
dialog_rect(&draw_frame(app, width, height).0)
/// An app with a form moved and sized away from where it opens.
pub(crate) fn moved_form() -> (App, Rect) {
let mut app = app();
app.overlays.push(Modal::Form(Form::commodity_create()));
draw_into(&mut app, 100, 30);
let auto = app.hits.dialog().expect("the form was drawn").auto;
let place = Place {
x: 7,
y: 4,
width: auto.width.saturating_sub(6).max(40),
height: auto.height + 3,
};
app.overlays.set_placement(Placement::of(place, auto));
let rect = rect_at(&app, 100, 30).expect("the moved form is drawn");
assert_eq!((rect.x, rect.y), (7, 4));
(app, rect)
pub(crate) fn submit(app: &mut App) {
let Some(Modal::Form(form)) = app.overlays.pop() else {
panic!("the form is open");
app.lock_pending_form(1, form);
#[test]
fn a_moved_and_sized_form_stays_put_while_pending() {
let (mut app, rect) = moved_form();
submit(&mut app);
assert_eq!(rect_at(&app, 100, 30), Some(rect));
fn the_pending_form_is_clamped_like_the_form_when_the_terminal_shrinks() {
let far = Place {
x: 85,
y: 26,
..Place::from(auto)
app.overlays.set_placement(Placement::of(far, auto));
let corner = |buffer: &Buffer| {
(area.y..area.bottom())
.find(|&at| buffer[at].symbol() == "╔")
let open = corner(&draw_frame(&app, 60, 20).0);
assert!(open.is_some_and(|at| at != (85, 26)), "the clamp moved it");
assert_eq!(corner(&draw_frame(&app, 60, 20).0), open);