Lines
97.63 %
Functions
100 %
Branches
use super::*;
use crate::modal::Modal;
use crate::widgets::SelectOption;
use crate::widgets::splits::{COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_COMM, COL_VALUE};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Buffer;
use ratatui::layout::Position;
const SPLITS: usize = 3;
pub(super) fn options(labels: &[&str]) -> Vec<SelectOption> {
labels
.iter()
.map(|label| SelectOption {
id: label.to_lowercase(),
label: (*label).to_string(),
})
.collect()
}
fn splits_form(edit: impl FnOnce(&mut crate::widgets::SplitsWidget)) -> Form {
let mut form = Form::transaction_create();
form.focus = SPLITS;
let Widget::Splits(sw) = &mut form.fields[SPLITS].widget else {
panic!("field {SPLITS} is the splits");
};
sw.set_account_options(options(&["Cash", "Bank", "Food"]));
sw.set_commodity_options(options(&["USD", "EUR"]));
edit(sw);
form
pub(super) fn drawn(form: Form, size: (u16, u16)) -> (Buffer, Option<Position>) {
let mut app = crate::test_support::app();
app.overlays.push(Modal::Form(form));
let mut terminal = Terminal::new(TestBackend::new(size.0, size.1)).expect("test terminal");
terminal
.draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
.expect("the frame draws");
let backend = terminal.backend();
let cursor = backend.cursor_visible().then(|| backend.cursor_position());
(backend.buffer().clone(), cursor)
pub(super) fn row_text(buf: &Buffer, y: u16) -> String {
(0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect()
pub(super) fn find(buf: &Buffer, text: &str) -> Vec<Position> {
let mut found = Vec::new();
for y in 0..buf.area.height {
let row = row_text(buf, y);
for (byte, _) in row.match_indices(text) {
let x = u16::try_from(row[..byte].chars().count()).unwrap_or(0);
found.push(Position::new(x, y));
found
pub(super) fn role_of(buf: &Buffer, at: Position) -> Option<tvui::Role> {
let theme = crate::test_support::dn_theme();
let style = buf[at].style();
[tvui::Role::InputFocused, tvui::Role::Input]
.into_iter()
.find(|role| {
let want = theme.style(*role);
(style.fg, style.bg) == (want.fg, want.bg)
/// The first cell of the first row of the grid in `buf`, where the row number is.
fn first_row(buf: &Buffer) -> Position {
find(buf, "1.")[0]
#[test]
fn the_focused_cell_is_input_focused_the_others_input_and_no_marks_are_left() {
let form = splits_form(|sw| sw.col_focus = COL_TO);
let (buf, _) = drawn(form, (120, 36));
let row = first_row(&buf);
let text = row_text(&buf, row.y);
assert!(!text.contains(['[', ']', '▸']), "{text}");
let cash = find(&buf, "Cash");
let (from, to) = (cash[0], cash[1]);
assert_eq!((from.y, to.y), (row.y, row.y));
assert_eq!(role_of(&buf, from), Some(tvui::Role::Input));
assert_eq!(role_of(&buf, to), Some(tvui::Role::InputFocused));
assert_eq!(buf[(from.x + 8, from.y)].symbol(), " ".to_string());
fn an_unfocused_grid_field_has_no_focused_cell() {
let mut form = splits_form(|_| {});
form.focus = 0;
let from = find(&buf, "Cash")[0];
fn the_cursor_is_at_the_edit_position_of_an_amount_cell() {
let form = splits_form(|sw| {
sw.col_focus = COL_VALUE;
for c in "125".chars() {
if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) =
sw.focused_subwidget_mut()
{
amount.insert_char(c);
if let Some(crate::widgets::FocusedSubWidget::Amount(amount)) = sw.focused_subwidget_mut() {
amount.as_editor_mut().move_left();
});
let (buf, cursor) = drawn(form, (120, 36));
let typed = find(&buf, "125")[0];
assert_eq!(cursor, Some(Position::new(typed.x + 2, typed.y)));
assert_eq!(role_of(&buf, typed), Some(tvui::Role::InputFocused));
fn no_cursor_on_a_closed_select_cell() {
let (_, cursor) = drawn(splits_form(|_| {}), (120, 36));
assert_eq!(cursor, None);
fn an_open_select_cell_lists_its_options_under_that_cell() {
let mut xs = Vec::new();
for col in [COL_FROM, COL_TO, COL_FROM_COMM, COL_TO_COMM] {
sw.col_focus = col;
sw.open_select();
let focused: Vec<u16> = (0..buf.area.width)
.filter(|&x| role_of(&buf, Position::new(x, row.y)) == Some(tvui::Role::InputFocused))
.collect();
let cell = focused[0];
assert_eq!(buf[(cell, row.y + 1)].symbol(), "┌", "column {col}");
xs.push(cell);
let mut distinct = xs.clone();
distinct.sort_unstable();
distinct.dedup();
assert_eq!(distinct.len(), 4, "{xs:?}");
fn an_open_select_cell_shows_its_filter_with_the_cursor_after_it() {
sw.col_focus = COL_TO;
sw.edit_select(|select| select.filter_push('b'));
let cell = (0..buf.area.width)
.find(|&x| role_of(&buf, Position::new(x, row.y)) == Some(tvui::Role::InputFocused))
.expect("a focused cell");
assert_eq!(buf[(cell + 1, row.y)].symbol(), "b");
assert_eq!(cursor, Some(Position::new(cell + 2, row.y)));
fn the_tag_grid_draws_under_its_split_further_in() {
sw.open_tags();
sw.add_tag();
let header = find(&buf, "Name")
.find(|at| at.y == row.y + 1)
.expect("the tag header is under the split");
assert_eq!(header.x, row.x + 4);
fn the_grid_scrolls_by_rows_to_keep_the_focused_row_in_view() {
let form = splits_form(|sw| (0..19).for_each(|_| sw.add_row()));
let (buf, _) = drawn(form, (80, 24));
assert!(find(&buf, " 1.").is_empty());
let last = find(&buf, "20.");
assert_eq!(last.len(), 1);
assert_eq!(
role_of(&buf, Position::new(last[0].x + 9, last[0].y)),
Some(tvui::Role::InputFocused)
);
fn the_rows_grid_draws_its_cells_with_the_focus_colour() {
let tags = form
.fields
.position(|field| matches!(field.widget, Widget::Rows(_)))
.expect("the transaction form has a rows field");
form.focus = tags;
let Widget::Rows(rw) = &mut form.fields[tags].widget else {
panic!("a rows field");
rw.add_row();
let header = find(&buf, "Name")[0];
let cell = Position::new(header.x, header.y + 1);
assert_eq!(role_of(&buf, cell), Some(tvui::Role::InputFocused));
assert_eq!(cursor, Some(Position::new(cell.x + 1, cell.y)));
fn typed_amount(sw: &mut crate::widgets::SplitsWidget, text: &str) {
for c in text.chars() {
fn a_long_amount_shows_whole_focused_and_unfocused() {
for col in [COL_VALUE, COL_FROM] {
typed_amount(sw, "1234567");
assert_eq!(find(&buf, "1234567").len(), 1, "focus on column {col}");
fn a_long_tag_value_shows_whole() {
.expect("a rows field");
rw.col_focus = 1;
if let Some(editor) = rw.focused_editor_mut() {
"groceries".chars().for_each(|c| editor.insert_char(c));
let at = find(&buf, "groceries");
assert_eq!(at.len(), 1);
assert_eq!(cursor, Some(Position::new(at[0].x + 9, at[0].y)));
fn an_open_select_with_a_long_filter_shows_it_whole() {
"Banana"
.chars()
.for_each(|c| sw.edit_select(|s| s.filter_push(c)));
let at = find(&buf, "Banana");
assert_eq!(at[0].y, row.y);
assert_eq!(cursor, Some(Position::new(at[0].x + 6, row.y)));
fn the_cursor_of_a_squeezed_select_cell_stays_inside_the_cell() {
"x".repeat(80)
let (buf, cursor) = drawn(form, (50, 24));
let cursor = cursor.expect("the cursor shows");
let focused: Vec<u16> = (0..50)
.filter(|&x| role_of(&buf, Position::new(x, cursor.y)) == Some(tvui::Role::InputFocused))
assert!(focused.contains(&cursor.x), "{cursor:?} {focused:?}");
fn dialog_x(buf: &Buffer) -> u16 {
find(buf, "╔")[0].x
fn the_dialog_stays_put_while_an_amount_is_typed_up_to_the_reserve() {
let (empty, _) = drawn(splits_form(|_| {}), (80, 24));
let form = splits_form(|sw| typed_amount(sw, "5829.00"));
let (typed, _) = drawn(form, (80, 24));
assert_eq!(dialog_x(&typed), dialog_x(&empty));
assert_eq!(find(&typed, "5829.00").len(), 1);
let form = splits_form(|sw| typed_amount(sw, "1234567"));
assert_eq!(find(&drawn(form, (80, 24)).0, "1234567").len(), 1);
fn converting(sw: &mut crate::widgets::SplitsWidget, key: &str) {
sw.col_focus = COL_FROM_COMM;
sw.edit_select(|select| select.select_id("usd"));
sw.col_focus = COL_TO_COMM;
sw.edit_select(|select| select.select_id("eur"));
sw.focus_cell(0, key);
fn a_to_amount_cell_past_the_edge_is_shifted_into_view_with_its_cursor() {
let form = splits_form(|sw| converting(sw, "to-amount"));
let (buf, cursor) = drawn(form, (80, 24));
let row = find(&buf, "1.")[0].y;
let focused: Vec<u16> = (0..80)
.filter(|&x| role_of(&buf, Position::new(x, row)) == Some(tvui::Role::InputFocused))
assert!(focused.len() >= 10, "{focused:?}");
assert_eq!((cursor.y, cursor.x), (row, focused[0] + 1));
let form = splits_form(|sw| converting(sw, "from"));
find(&buf, "1.").len(),
1,
"the row starts at its own left again"
fn a_cell_cut_at_the_left_by_the_shift_is_not_drawn_at_all() {
sw.edit_select(|select| select.select_id("bank"));
converting(sw, "to-amount");
let (buf, _) = drawn(form, (60, 24));
let rows: String = (0..buf.area.height)
.map(|y| row_text(&buf, y))
.collect::<Vec<_>>()
.join("\n");
assert!(!rows.contains("Ban"), "{rows}");
fn a_note_with_tildes_is_drawn_as_typed() {
let mut grid = crate::widgets::grid::Grid::default();
grid.push_note(0, "a~b~c");
let rects = grid.cell_rects(ratatui::layout::Rect::new(0, 0, 10, 1), 0);
let mut terminal = Terminal::new(TestBackend::new(10, 1)).expect("test terminal");
.draw(|frame| {
let screen = frame.area();
let at = tvui::Place::from(screen);
let mut hits = crate::hits::Hits::default();
crate::draw::canvas::draw_placed(frame, at, screen, &mut hits, |canvas, _, _| {
super::grid::draw_grid(canvas, &grid, &rects, &theme);
.expect("the grid draws");
assert_eq!(row_text(terminal.backend().buffer(), 0).trim_end(), "a~b~c");