1
use forms::Row;
2

            
3
use super::*;
4
use crate::widgets::grid::{GridLine, ItemKind};
5
use crate::widgets::{AmountWidget, SelectOption};
6

            
7
37
fn items<'a>(grid: &'a Grid<'a>, line: usize) -> &'a [Item<'a>] {
8
37
    match &grid.lines[line] {
9
37
        GridLine::Cells { items, .. } => items,
10
        GridLine::Text { .. } => &[],
11
    }
12
37
}
13

            
14
7
fn texts<'a>(grid: &'a Grid<'a>, line: usize) -> Vec<&'a str> {
15
7
    items(grid, line)
16
7
        .iter()
17
58
        .map(|item| item.text.as_str())
18
        .collect()
19
7
}
20

            
21
2
fn accounts() -> Vec<SelectOption> {
22
2
    ["Cash", "Bank"]
23
        .map(|label| SelectOption {
24
4
            id: label.to_lowercase(),
25
4
            label: label.to_string(),
26
4
        })
27
        .to_vec()
28
2
}
29

            
30
2
fn tag(row_key: &str, name: &str) -> Row {
31
4
    Row::from([("row", row_key), ("name", name)].map(|(k, v)| (k.to_string(), v.to_string())))
32
2
}
33

            
34
2
fn tagged() -> SplitsWidget {
35
2
    let mut sw = SplitsWidget::default();
36
4
    let split = |key: &str| Row::from([("key".to_string(), key.to_string())]);
37
2
    sw.load_rows(&[split("r1"), split("r2")]);
38
2
    sw.load_tags(Some(&[tag("r1", "memo")]));
39
2
    sw
40
2
}
41

            
42
#[test]
43
1
fn a_split_is_a_line_of_cells_with_its_number_and_the_arrow() {
44
1
    let mut sw = SplitsWidget::default();
45
1
    sw.set_account_options(accounts());
46
1
    let grid = sw.grid(true, None, None);
47
1
    assert_eq!(grid.lines.len(), 1);
48
1
    let cells = texts(&grid, 0);
49
1
    assert_eq!(cells[..5], ["1.", "#0", "Cash", "→", "Cash"]);
50
1
    assert_eq!(cells[7..], ["", "", ""]);
51
1
}
52

            
53
#[test]
54
1
fn only_the_focused_cell_of_an_active_field_is_focused() {
55
1
    let sw = SplitsWidget {
56
1
        col_focus: COL_TO,
57
1
        ..Default::default()
58
1
    };
59
2
    let focused = |sw: &SplitsWidget, active| -> Vec<usize> {
60
2
        let grid = sw.grid(active, None, None);
61
2
        (0..items(&grid, 0).len())
62
20
            .filter(|&at| items(&grid, 0)[at].focused)
63
            .collect()
64
2
    };
65
1
    assert_eq!(focused(&sw, true), [4]);
66
1
    assert!(focused(&sw, false).is_empty());
67
1
}
68

            
69
#[test]
70
1
fn the_kinds_follow_the_columns() {
71
1
    let splits = SplitsWidget::default();
72
1
    let grid = splits.grid(true, None, None);
73
10
    let kinds: Vec<ItemKind> = items(&grid, 0).iter().map(|item| item.kind).collect();
74
    use ItemKind::{Label, Select, Text};
75
1
    assert_eq!(
76
1
        kinds,
77
1
        [
78
1
            Label, Label, Select, Label, Select, Select, Select, Text, Label, Label
79
1
        ]
80
    );
81
1
}
82

            
83
#[test]
84
1
fn a_typed_amount_carries_its_cursor() {
85
1
    let mut sw = SplitsWidget::default();
86
1
    sw.rows[0].value = AmountWidget::with_value("125");
87
1
    sw.col_focus = COL_VALUE;
88
1
    let grid = sw.grid(true, None, None);
89
1
    let value = &items(&grid, 0)[7];
90
1
    assert_eq!(
91
1
        (value.text.as_str(), value.cursor, value.focused),
92
1
        ("125", 3, true)
93
    );
94
1
}
95

            
96
#[test]
97
1
fn the_conversion_shows_when_filled_differing_or_focused() {
98
1
    let mut sw = SplitsWidget::default();
99
1
    assert_eq!(
100
1
        items(&sw.grid(true, None, None), 0)[9].kind,
101
1
        ItemKind::Label
102
    );
103
1
    sw.focus_cell(0, "to-amount");
104
1
    assert_eq!(items(&sw.grid(true, None, None), 0)[9].kind, ItemKind::Text);
105
1
    sw.focus_cell(0, "value");
106
1
    sw.rows[0].to_amount = AmountWidget::with_value("9");
107
1
    assert_eq!(texts(&sw.grid(true, None, None), 0)[8..10], ["→", "9"]);
108
1
}
109

            
110
#[test]
111
1
fn a_split_line_shows_its_tag_count() {
112
1
    let sw = tagged();
113
1
    let grid = sw.grid(true, None, None);
114
1
    assert_eq!(texts(&grid, 0).get(1), Some(&"#1"));
115
1
    assert_eq!(texts(&grid, 1).get(1), Some(&"#0"));
116
1
}
117

            
118
#[test]
119
1
fn the_open_tag_list_sits_under_its_split_with_the_focus_in_it() {
120
1
    let mut sw = tagged();
121
1
    sw.open_tags();
122
1
    let grid = sw.grid(true, Some("suggest"), None);
123
1
    assert_eq!(grid.lines.len(), 5);
124
1
    assert_eq!(texts(&grid, 1), ["Name", "Value", "Description", "Leg"]);
125
1
    assert_eq!(texts(&grid, 2)[0], "memo");
126
1
    assert!(items(&grid, 2)[0].focused);
127
10
    assert!(items(&grid, 0).iter().all(|item| !item.focused));
128
1
    assert_eq!(grid.focus_line, 2);
129
1
    assert!(matches!(&grid.lines[3], GridLine::Text { text, .. } if text == "suggest"));
130
1
    assert!(texts(&grid, 4)[0].starts_with("2."));
131
1
}
132

            
133
#[test]
134
1
fn an_empty_open_list_names_the_key_that_adds_a_tag() {
135
1
    let mut sw = SplitsWidget::default();
136
1
    sw.open_tags();
137
2
    let note = |grid: &Grid| match grid.lines.last() {
138
2
        Some(GridLine::Text { text, .. }) => text.clone(),
139
        _ => String::new(),
140
2
    };
141
1
    assert_eq!(
142
1
        note(&sw.grid(true, None, Some("C-n"))),
143
1
        "(no tags: C-n adds one)"
144
    );
145
1
    assert_eq!(note(&sw.grid(true, None, None)), "(no tags)");
146
1
}
147

            
148
#[test]
149
1
fn the_view_follows_the_focused_split() {
150
1
    let mut sw = SplitsWidget::default();
151
1
    sw.add_row();
152
1
    sw.add_row();
153
1
    assert_eq!(sw.grid(true, None, None).focus_line, 2);
154
1
    sw.prev_row();
155
1
    assert_eq!(sw.grid(true, None, None).focus_line, 1);
156
1
}
157

            
158
#[test]
159
1
fn the_conversion_is_not_shown_for_a_focused_to_amount_while_the_tags_are_open() {
160
1
    let mut sw = SplitsWidget::default();
161
1
    sw.focus_cell(0, "to-amount");
162
1
    sw.open_tags();
163
1
    let grid = sw.grid(true, None, None);
164
1
    assert_eq!(items(&grid, 0)[8].text, "");
165
1
    assert_eq!(items(&grid, 0)[9].kind, ItemKind::Label);
166
1
}
167

            
168
#[test]
169
1
fn the_grid_width_is_the_same_whichever_commodity_the_cell_shows() {
170
1
    let mut sw = SplitsWidget::default();
171
1
    sw.set_commodity_options(accounts());
172
1
    let none = sw.grid(true, None, None).width();
173
1
    sw.rows[0].from_commodity.select_id("cash");
174
1
    assert_eq!(sw.grid(true, None, None).width(), none);
175
1
}
176

            
177
#[test]
178
1
fn typing_an_amount_up_to_the_reserve_does_not_change_the_grid_width() {
179
1
    let mut sw = SplitsWidget::default();
180
1
    let empty = sw.grid(true, None, None).width();
181
1
    sw.rows[0].value = AmountWidget::with_value("5829.00");
182
1
    assert_eq!(sw.grid(true, None, None).width(), empty);
183
1
}