Lines
97.46 %
Functions
100 %
Branches
//! The cells and the drop-down of a form are recorded where the frame drew them.
use crate::app::App;
use crate::form::Form;
use crate::hits::{Hits, Surface, Target};
use crate::modal::Modal;
use crate::test_support::draw_frame;
use crate::widgets::grid::CellId;
use crate::widgets::splits::{
COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE,
};
use crate::widgets::{SelectOption, Widget};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
const SPLITS: usize = 3;
fn options(n: usize) -> Vec<SelectOption> {
(0..n)
.map(|i| SelectOption {
id: format!("id{i}"),
label: format!("label{i}"),
})
.collect()
}
/// A transaction form with the splits focused and `n` account options in every select.
fn transaction(n: usize) -> App {
let mut form = Form::transaction_create();
form.focus = SPLITS;
if let Some(Widget::Splits(splits)) = form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
splits.set_account_options(options(n));
splits.set_commodity_options(options(n));
let mut app = crate::test_support::app();
app.overlays.push(Modal::Form(form));
app
fn splits(app: &mut App) -> &mut crate::widgets::SplitsWidget {
match app.overlays.top_mut() {
Some(Modal::Form(form)) => match form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
Some(Widget::Splits(splits)) => splits,
_ => panic!("no splits"),
},
_ => panic!("no form"),
fn cells(hits: &Hits) -> Vec<(Rect, CellId)> {
hits.entries()
.iter()
.filter_map(|(rect, target)| match target {
Target::Cell { field, cell } if *field == SPLITS => Some((*rect, *cell)),
_ => None,
fn text(buffer: &Buffer, rect: Rect) -> String {
(rect.x..rect.right())
.map(|x| buffer[(x, rect.y)].symbol())
fn cols(cells: &[(Rect, CellId)], row: usize) -> Vec<usize> {
let of_row = cells
.filter(|(_, cell)| cell.row == row && !cell.tag);
of_row.map(|(_, cell)| cell.col).collect()
#[test]
fn a_splits_row_records_its_cells_left_to_right_and_not_its_labels() {
let app = transaction(3);
let (buffer, hits) = draw_frame(&app, 160, 30);
let found = cells(&hits);
assert_eq!(
cols(&found, 0),
[COL_FROM, COL_TO, COL_FROM_COMM, COL_TO_COMM, COL_VALUE]
);
let xs: Vec<u16> = found.iter().map(|(rect, _)| rect.x).collect();
assert!(xs.windows(2).all(|pair| pair[0] < pair[1]), "{xs:?}");
assert!(text(&buffer, found[0].0).contains("label0"));
fn the_conversion_amount_is_a_cell_while_it_shows() {
let mut app = transaction(3);
splits(&mut app).col_focus = COL_TO_AMOUNT;
let (_, hits) = draw_frame(&app, 160, 30);
assert_eq!(cols(&cells(&hits), 0).last(), Some(&COL_TO_AMOUNT));
fn a_shifted_row_records_the_cells_where_the_shift_put_them() {
let (buffer, hits) = draw_frame(&app, 50, 30);
let at = |col| found.iter().find(|(_, cell)| cell.col == col);
let (rect, _) = at(COL_TO_AMOUNT).expect("the focused cell is drawn whole");
assert!(text(&buffer, *rect).trim().is_empty());
assert!(at(COL_FROM).is_some(), "the frozen From stays: {found:?}");
assert!(at(COL_TO).is_none(), "{found:?}");
assert!(rect.right() <= 50);
fn an_open_tag_list_records_tag_cells_apart_from_the_split_cells() {
splits(&mut app).add_tag();
splits(&mut app).open_tags();
assert!(found.iter().any(|(_, cell)| cell.tag && cell.row == 0));
assert!(found.iter().any(|(_, cell)| !cell.tag));
fn an_open_select_records_its_options_on_top_of_a_box_that_is_not_a_block() {
splits(&mut app).open_select();
let entries = hits.entries();
let options: Vec<_> = entries
.filter(|(_, target)| matches!(target, Target::Option(_)))
.collect();
assert_eq!(options.len(), 3);
for (index, (rect, target)) in options.iter().enumerate() {
assert_eq!(*target, Target::Option(index));
assert!(text(&buffer, *rect).contains(&format!("label{index}")));
let boxed = entries
.find(|(_, target)| *target == Target::Body(Surface::Options));
assert!(boxed.is_some_and(|(rect, _)| rect.contains(options[0].0.as_position())));
hits.hit_at(options[0].0.as_position())
.map(|(_, target, _)| target),
Some(Target::Option(0))
fn a_long_list_records_the_window_it_shows_and_a_bar() {
let mut app = transaction(20);
let shown: Vec<_> = hits
.entries()
.filter_map(|(_, target)| match target {
Target::Option(index) => Some(*index),
assert_eq!(shown, (0..8).collect::<Vec<_>>());
let bar: Vec<_> = entries
.filter(|(_, target)| matches!(target, Target::Scroll(Surface::Options, _)))
assert!(!bar.is_empty());