Lines
98.56 %
Functions
100 %
Branches
use forms::Row;
use super::*;
use crate::widgets::grid::{GridLine, ItemKind};
use crate::widgets::{AmountWidget, SelectOption};
fn items<'a>(grid: &'a Grid<'a>, line: usize) -> &'a [Item<'a>] {
match &grid.lines[line] {
GridLine::Cells { items, .. } => items,
GridLine::Text { .. } => &[],
}
fn texts<'a>(grid: &'a Grid<'a>, line: usize) -> Vec<&'a str> {
items(grid, line)
.iter()
.map(|item| item.text.as_str())
.collect()
fn accounts() -> Vec<SelectOption> {
["Cash", "Bank"]
.map(|label| SelectOption {
id: label.to_lowercase(),
label: label.to_string(),
})
.to_vec()
fn tag(row_key: &str, name: &str) -> Row {
Row::from([("row", row_key), ("name", name)].map(|(k, v)| (k.to_string(), v.to_string())))
fn tagged() -> SplitsWidget {
let mut sw = SplitsWidget::default();
let split = |key: &str| Row::from([("key".to_string(), key.to_string())]);
sw.load_rows(&[split("r1"), split("r2")]);
sw.load_tags(Some(&[tag("r1", "memo")]));
sw
#[test]
fn a_split_is_a_line_of_cells_with_its_number_and_the_arrow() {
sw.set_account_options(accounts());
let grid = sw.grid(true, None, None);
assert_eq!(grid.lines.len(), 1);
let cells = texts(&grid, 0);
assert_eq!(cells[..5], ["1.", "#0", "Cash", "→", "Cash"]);
assert_eq!(cells[7..], ["", "", ""]);
fn only_the_focused_cell_of_an_active_field_is_focused() {
let sw = SplitsWidget {
col_focus: COL_TO,
..Default::default()
};
let focused = |sw: &SplitsWidget, active| -> Vec<usize> {
let grid = sw.grid(active, None, None);
(0..items(&grid, 0).len())
.filter(|&at| items(&grid, 0)[at].focused)
assert_eq!(focused(&sw, true), [4]);
assert!(focused(&sw, false).is_empty());
fn the_kinds_follow_the_columns() {
let splits = SplitsWidget::default();
let grid = splits.grid(true, None, None);
let kinds: Vec<ItemKind> = items(&grid, 0).iter().map(|item| item.kind).collect();
use ItemKind::{Label, Select, Text};
assert_eq!(
kinds,
[
Label, Label, Select, Label, Select, Select, Select, Text, Label, Label
]
);
fn a_typed_amount_carries_its_cursor() {
sw.rows[0].value = AmountWidget::with_value("125");
sw.col_focus = COL_VALUE;
let value = &items(&grid, 0)[7];
(value.text.as_str(), value.cursor, value.focused),
("125", 3, true)
fn the_conversion_shows_when_filled_differing_or_focused() {
items(&sw.grid(true, None, None), 0)[9].kind,
ItemKind::Label
sw.focus_cell(0, "to-amount");
assert_eq!(items(&sw.grid(true, None, None), 0)[9].kind, ItemKind::Text);
sw.focus_cell(0, "value");
sw.rows[0].to_amount = AmountWidget::with_value("9");
assert_eq!(texts(&sw.grid(true, None, None), 0)[8..10], ["→", "9"]);
fn a_split_line_shows_its_tag_count() {
let sw = tagged();
assert_eq!(texts(&grid, 0).get(1), Some(&"#1"));
assert_eq!(texts(&grid, 1).get(1), Some(&"#0"));
fn the_open_tag_list_sits_under_its_split_with_the_focus_in_it() {
let mut sw = tagged();
sw.open_tags();
let grid = sw.grid(true, Some("suggest"), None);
assert_eq!(grid.lines.len(), 5);
assert_eq!(texts(&grid, 1), ["Name", "Value", "Description", "Leg"]);
assert_eq!(texts(&grid, 2)[0], "memo");
assert!(items(&grid, 2)[0].focused);
assert!(items(&grid, 0).iter().all(|item| !item.focused));
assert_eq!(grid.focus_line, 2);
assert!(matches!(&grid.lines[3], GridLine::Text { text, .. } if text == "suggest"));
assert!(texts(&grid, 4)[0].starts_with("2."));
fn an_empty_open_list_names_the_key_that_adds_a_tag() {
let note = |grid: &Grid| match grid.lines.last() {
Some(GridLine::Text { text, .. }) => text.clone(),
_ => String::new(),
note(&sw.grid(true, None, Some("C-n"))),
"(no tags: C-n adds one)"
assert_eq!(note(&sw.grid(true, None, None)), "(no tags)");
fn the_view_follows_the_focused_split() {
sw.add_row();
assert_eq!(sw.grid(true, None, None).focus_line, 2);
sw.prev_row();
assert_eq!(sw.grid(true, None, None).focus_line, 1);
fn the_conversion_is_not_shown_for_a_focused_to_amount_while_the_tags_are_open() {
assert_eq!(items(&grid, 0)[8].text, "");
assert_eq!(items(&grid, 0)[9].kind, ItemKind::Label);
fn the_grid_width_is_the_same_whichever_commodity_the_cell_shows() {
sw.set_commodity_options(accounts());
let none = sw.grid(true, None, None).width();
sw.rows[0].from_commodity.select_id("cash");
assert_eq!(sw.grid(true, None, None).width(), none);
fn typing_an_amount_up_to_the_reserve_does_not_change_the_grid_width() {
let empty = sw.grid(true, None, None).width();
sw.rows[0].value = AmountWidget::with_value("5829.00");
assert_eq!(sw.grid(true, None, None).width(), empty);