Lines
95.5 %
Functions
100 %
Branches
//! The transaction-create form against its shared descriptor.
use cli_core::render::FieldError;
use cli_core::transaction::submit_call;
use forms::TRANSACTION_CREATE;
use crate::form::{Field, Form};
use crate::widgets::splits::{COL_FROM_COMM, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE};
use crate::widgets::{DateWidget, FocusedSubWidget, SelectOption, SplitsWidget, Widget};
fn opt(id: &str, label: &str) -> SelectOption {
SelectOption {
id: id.to_string(),
label: label.to_string(),
}
fn error(field: &str, message: &str) -> FieldError {
FieldError {
field: field.to_string(),
message: message.to_string(),
fn splits_mut(form: &mut Form) -> &mut SplitsWidget {
match form.fields.iter_mut().find(|f| f.key == Some("splits")) {
Some(Field {
widget: Widget::Splits(sw),
..
}) => sw,
other => panic!("splits field must hold Splits, got {other:?}"),
fn type_in(sw: &mut SplitsWidget, col: usize, text: &str) {
sw.col_focus = col;
if let Some(FocusedSubWidget::Amount(aw)) = sw.focused_subwidget_mut() {
text.chars().for_each(|c| aw.insert_char(c));
fn pick_eur_to_commodity(sw: &mut SplitsWidget) {
sw.col_focus = COL_TO_COMM;
if let Some(FocusedSubWidget::Select(select)) = sw.focused_subwidget_mut() {
select.next();
fn pick_usd_from_commodity(sw: &mut SplitsWidget) {
sw.col_focus = COL_FROM_COMM;
fn two_row_form() -> Form {
let mut form = Form::transaction_create();
form.fields[0].widget = Widget::Date(DateWidget::with_value("2024-06-15T10:00"));
if let Widget::Text(note) = &mut form.fields[1].widget {
"lunch".chars().for_each(|c| note.insert_char(c));
let sw = splits_mut(&mut form);
sw.set_account_options(vec![opt("acc-1", "Cash"), opt("acc-2", "Food")]);
sw.set_commodity_options(vec![opt("c-1", "USD"), opt("c-2", "EUR")]);
pick_usd_from_commodity(sw);
type_in(sw, COL_VALUE, "50");
sw.add_row();
pick_eur_to_commodity(sw);
type_in(sw, COL_TO_AMOUNT, "45");
form
#[test]
fn fields_are_the_descriptor_fields() {
let form = Form::transaction_create();
let labels: Vec<&str> = form.fields.iter().map(|f| f.label).collect();
let expected: Vec<&str> = TRANSACTION_CREATE
.fields
.iter()
.filter(|f| !f.is_nested())
.map(|f| f.label)
.collect();
assert_eq!(labels, expected);
fn submit_payload_of_a_two_row_cross_currency_entry_is_exact() {
let values = two_row_form().values();
let expected = concat!(
r#"(submit-form "transaction-create" "(:date \"2024-06-15T10:00\" :note \"lunch\" :splits ("#,
r#"(:from \"acc-1\" :to \"acc-1\" :from-commodity \"c-1\" :value \"50\") "#,
r#"(:from \"acc-1\" :to \"acc-1\" :from-commodity \"c-1\" :to-commodity \"c-2\" :value \"50\" :to-amount \"45\")"#,
r#"))")"#
);
assert_eq!(submit_call(&TRANSACTION_CREATE, &values), expected);
fn empty_cells_are_left_out_of_the_values() {
let values = Form::transaction_create().values();
let row = &values.rows("splits").expect("splits rows")[0];
assert!(!row.contains_key("value"));
assert!(!row.contains_key("to-amount"));
fn rejections_are_listed_with_labels_and_focus_the_first_cell() {
let mut form = two_row_form();
form.show_errors(&[
error("splits.1.to-amount", "is required"),
error("note", "too long"),
error("splits.0", "bad hidden data"),
error("", "unreadable payload"),
error("elsewhere", "kept raw"),
]);
assert_eq!(
form.errors,
[
"Row 2 \u{b7} To amount: is required",
"Note: too long",
"Row 1: bad hidden data",
"unreadable payload",
"elsewhere: kept raw",
]
assert_eq!(form.focus, 3);
assert_eq!((sw.row_focus, sw.col_focus), (1, COL_TO_AMOUNT));
fn a_field_rejection_focuses_the_field() {
form.focus = 3;
form.show_errors(&[error("date", "invalid date")]);
assert_eq!(form.errors, ["Date: invalid date"]);
assert_eq!(form.focus, 0);
fn a_form_level_rejection_keeps_the_focus() {
form.focus = 1;
form.show_errors(&[error("", "unreadable payload")]);
assert_eq!(form.errors, ["unreadable payload"]);
assert_eq!(form.focus, 1);