Lines
98.56 %
Functions
98.51 %
Branches
100 %
use super::grid_tests::{drawn, find, options, role_of, row_text};
use super::*;
use crate::widgets::FocusedSubWidget;
use crate::widgets::SplitsWidget;
use crate::widgets::splits::{
COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE,
};
use ratatui::buffer::Buffer;
use ratatui::layout::Position;
const SPLITS: usize = 3;
const BANK: &str = "Assets:Bank:Checking";
const FOOD: &str = "Expenses:Food:Groceries";
const SIZES: [(u16, u16); 2] = [(100, 30), (80, 24)];
const COLUMNS: [usize; 6] = [
COL_FROM,
COL_TO,
COL_FROM_COMM,
COL_TO_COMM,
COL_VALUE,
COL_TO_AMOUNT,
];
fn form_of(loaded: bool, edit: impl FnOnce(&mut 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.add_row();
if loaded {
sw.set_account_options(options(&[BANK, FOOD, "Assets:Cash"]));
sw.set_commodity_options(options(&["USD", "Hungarian Forint (HUF)"]));
for (row, (from, to)) in [(BANK, FOOD), (FOOD, BANK)].into_iter().enumerate() {
sw.row_focus = row;
sw.col_focus = COL_FROM;
sw.edit_select(|select| select.select_id(&from.to_lowercase()));
sw.col_focus = COL_TO;
sw.edit_select(|select| select.select_id(&to.to_lowercase()));
}
sw.row_focus = 0;
sw.col_focus = COL_VALUE;
edit(sw);
form
fn two_splits(edit: impl FnOnce(&mut SplitsWidget)) -> Form {
form_of(true, edit)
fn line_of(buf: &Buffer, row: usize) -> u16 {
find(buf, &format!("{}.", row + 1))[0].y
/// The text of `line` with its runs of blanks folded to one.
fn folded(line: &str) -> String {
line.split_whitespace().collect::<Vec<_>>().join(" ")
fn split_line(buf: &Buffer, row: usize) -> String {
folded(&row_text(buf, line_of(buf, row)))
fn marks(buf: &Buffer, y: u16) -> Vec<u16> {
(0..buf.area.width)
.filter(|&x| buf[(x, y)].symbol() == "▼")
.collect()
/// Where the columns of split line `row` sit: its select marks.
fn shape(buf: &Buffer, row: usize) -> Vec<u16> {
marks(buf, line_of(buf, row))
fn type_amount(sw: &mut SplitsWidget, text: &str) {
for c in text.chars() {
if let Some(FocusedSubWidget::Amount(amount)) = sw.focused_subwidget_mut() {
amount.insert_char(c);
#[test]
fn the_100_column_dump_shows_the_row_number_the_tag_count_and_from() {
let (buf, _) = drawn(two_splits(|_| {}), (100, 30));
let dump: Vec<String> = (0..buf.area.height).map(|y| row_text(&buf, y)).collect();
assert!(
split_line(&buf, 0).contains("1. #0 Assets:Bank:Checking"),
"{dump:#?}"
);
split_line(&buf, 1).contains("2. #0 Expenses:Food:Groceries"),
fn the_80_column_dump_shows_the_row_number_the_tag_count_and_from() {
let (buf, _) = drawn(two_splits(|_| {}), (80, 24));
assert!(split_line(&buf, 0).contains("1. #0 …"), "{dump:#?}");
assert!(split_line(&buf, 1).contains("2. #0 …"), "{dump:#?}");
fn both_commodity_columns_are_equally_wide() {
let at = marks(&buf, line_of(&buf, 0));
assert_eq!(at.len(), 4, "{at:?}");
assert_eq!(at[3] - at[2], at[2] - at[1], "{at:?}");
assert_eq!(at[3] - at[2], 10, "{at:?}");
fn the_rows_share_every_column() {
for size in SIZES {
let (buf, _) = drawn(two_splits(|_| {}), size);
assert_eq!(shape(&buf, 0), shape(&buf, 1), "{size:?}");
fn widths_do_not_change_when_the_options_arrive() {
let (before, _) = drawn(form_of(false, |_| {}), size);
let (after, _) = drawn(two_splits(|_| {}), size);
assert_eq!(shape(&before, 0), shape(&after, 0), "{size:?}");
fn widths_do_not_change_while_typing() {
let (idle, _) = drawn(two_splits(|_| {}), size);
let (typed, _) = drawn(two_splits(|sw| type_amount(sw, "123456789012345678")), size);
assert_eq!(shape(&idle, 0), shape(&typed, 0), "{size:?}");
let (filtered, _) = drawn(
two_splits(|sw| {
sw.open_select();
sw.edit_select(|select| {
"Expenses:Food:Gro"
.chars()
.for_each(|c| select.filter_push(c))
});
}),
size,
assert_eq!(shape(&idle, 0), shape(&filtered, 0), "{size:?}");
fn the_conversion_slot_appears_only_when_needed() {
let arrows = |buf: &Buffer| row_text(buf, line_of(buf, 0)).matches('→').count();
let (idle, _) = drawn(two_splits(|_| {}), (100, 30));
let (shown, _) = drawn(two_splits(|sw| sw.col_focus = COL_TO_AMOUNT), (100, 30));
assert_eq!(arrows(&idle), 1);
assert_eq!(arrows(&shown), 2);
let (filled, _) = drawn(
sw.col_focus = COL_TO_AMOUNT;
type_amount(sw, "9");
(100, 30),
assert_eq!(arrows(&filled), 2);
fn the_tag_count_is_visible_with_long_names_and_counts_blank_rows() {
assert!(split_line(&buf, 0).contains("1. #0 "), "{size:?}");
let (tagged, _) = drawn(
sw.open_tags();
sw.add_tag();
assert!(split_line(&tagged, 0).contains("1. #1 "), "{size:?}");
assert!(split_line(&tagged, 1).contains("2. #0 "), "{size:?}");
fn the_row_number_and_from_stay_visible_on_every_focus() {
for col in COLUMNS {
let (buf, _) = drawn(two_splits(|sw| sw.col_focus = col), size);
let y = line_of(&buf, 0);
let text = split_line(&buf, 0);
let from = text.split_once("1. #0 ").map_or("", |(_, rest)| rest);
let shown = from.chars().next().is_some_and(|c| c != '▼' && c != '→');
assert!(shown, "{size:?} col {col}: {text}");
let focused = (0..buf.area.width)
.any(|x| role_of(&buf, Position::new(x, y)) == Some(tvui::Role::InputFocused));
focused,
"{size:?} col {col}: the focused cell is shown\n{text}"
fn inner(buf: &Buffer, row: usize) -> String {
let text = row_text(buf, line_of(buf, row));
let cells: Vec<&str> = text.split('║').collect();
cells
.get(1)
.map_or(String::new(), |inner| inner.trim_end().to_string())
fn the_100_column_split_lines_are_pinned() {
assert_eq!(
inner(&buf, 0),
" 1. #0 Assets:Bank:Checking ▼ → Expenses:Food:Groceries ▼ (none) ▼ same ▼"
inner(&buf, 1),
" 2. #0 Expenses:Food:Groceries ▼ → Assets:Bank:Checking ▼ (none) ▼ same ▼"
fn the_80_column_split_lines_are_pinned() {
" 1. #0 …Bank:Checking ▼ → …od:Groceries ▼ (none) ▼ same ▼"
fn the_leg_of_a_tag_row_scrolls_into_view_under_the_split() {
let form = two_splits(|sw| {
sw.focus_tag(0, "description");
if let Some(tags) = sw.tags_mut() {
tags.set_focused_text("a long description of the tag, wide enough to push the leg out");
sw.focus_tag(0, "side");
let (buf, _) = drawn(form, (80, 24));
let focused = |y| {
.any(|x| role_of(&buf, Position::new(x, y)) == Some(tvui::Role::InputFocused))
let y = line_of(&buf, 0) + 2;
assert!(focused(y), "{}", row_text(&buf, y));
const FORINT: &str = "hungarian forint (huf)";
fn converting(sw: &mut SplitsWidget) {
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(FORINT));
fn the_100_column_line_with_the_conversion_shown_is_pinned() {
let (buf, _) = drawn(two_splits(converting), (100, 30));
" 1. #0 …ts:Bank:Checking ▼ → …s:Food:Groceries ▼ USD ▼ Hungar►▼ →"
fn the_80_column_line_with_the_conversion_shown_is_pinned() {
let (buf, _) = drawn(two_splits(converting), (80, 24));
" 1. #0 …Checking ▼ → …roceries ▼ USD ▼ Hungar►▼ →"
fn a_to_account_cut_by_the_shift_is_clipped_not_hidden() {
let (buf, _) = drawn(
converting(sw);
(80, 24),
let text = row_text(&buf, y);
assert_eq!(marks(&buf, y).len(), 4, "{text}");
assert!(text.contains("ceries"), "{text}");