1
//! Type-ahead on the tag cells of every form that carries a tags list.
2

            
3
use forms::{FormValues, Scope, Suggest};
4

            
5
use crate::form::Form;
6
use crate::widgets::{Hint, Widget};
7

            
8
12
fn names(scope: Scope) -> Hint {
9
12
    Hint::Names(Suggest::TagNames(scope))
10
12
}
11

            
12
6
fn values(scope: Scope, tag: &str) -> Hint {
13
6
    Hint::Values(Suggest::TagValues(scope), tag.to_string())
14
6
}
15

            
16
12
fn strings(items: &[&str]) -> Vec<String> {
17
12
    items.iter().map(ToString::to_string).collect()
18
12
}
19

            
20
15
fn on_tags(mut form: Form) -> Form {
21
15
    form.focus = form
22
15
        .fields
23
15
        .iter()
24
45
        .position(|field| field.key == Some("tags"))
25
15
        .expect("the form has a tags field");
26
15
    if let Some(Widget::Rows(rows)) = form.focused_field_mut().map(|field| &mut field.widget) {
27
15
        rows.add_row();
28
    }
29
15
    form
30
15
}
31

            
32
2
fn on_split_tags(mut form: Form) -> Form {
33
2
    form.focus = form
34
2
        .fields
35
2
        .iter()
36
8
        .position(|field| matches!(field.widget, Widget::Splits(_)))
37
2
        .expect("the form has splits");
38
2
    if let Some(Widget::Splits(sw)) = form.focused_field_mut().map(|field| &mut field.widget) {
39
2
        sw.open_tags();
40
2
        sw.add_tag();
41
    }
42
2
    form
43
2
}
44

            
45
13
fn type_in(form: &mut Form, text: &str) {
46
13
    let editor = form
47
13
        .focused_rows_mut()
48
13
        .and_then(|rows| rows.focused_editor_mut());
49
13
    if let Some(editor) = editor {
50
44
        text.chars().for_each(|c| editor.insert_char(c));
51
    }
52
13
}
53

            
54
6
fn next_cell(form: &mut Form) {
55
6
    if let Some(rows) = form.focused_rows_mut() {
56
6
        rows.advance_cell();
57
    }
58
6
}
59

            
60
9
fn focused_text(form: &mut Form, key: &str) -> Option<String> {
61
9
    form.focused_rows_mut()
62
9
        .map(|rows| rows.focused_text(key).to_string())
63
9
}
64

            
65
10
fn answer(form: &mut Form, hint: &Hint, seq: u64, items: &[&str]) {
66
10
    form.hint_asked(hint, seq);
67
10
    form.hint_arrived(hint, seq, strings(items));
68
10
}
69

            
70
8
fn transaction() -> Form {
71
8
    on_tags(Form::transaction_create())
72
8
}
73

            
74
3
fn account() -> Form {
75
3
    on_tags(Form::account_edit(&FormValues::default()))
76
3
}
77

            
78
3
fn commodity() -> Form {
79
3
    on_tags(Form::commodity_edit(&FormValues::default()))
80
3
}
81

            
82
#[test]
83
1
fn a_tag_name_cell_of_each_scope_asks_for_its_own_names() {
84
1
    let cases = [
85
1
        (transaction(), Scope::Transaction),
86
1
        (account(), Scope::Account),
87
1
        (commodity(), Scope::Commodity),
88
1
    ];
89
1
    for (form, scope) in cases {
90
3
        assert_eq!(form.wanted_hint(), Some(names(scope)));
91
    }
92
1
}
93

            
94
#[test]
95
1
fn names_are_offered_by_prefix_and_completed_by_the_key() {
96
1
    for mut form in [transaction(), account(), commodity()] {
97
3
        let hint = form.wanted_hint().expect("names are wanted");
98
3
        answer(&mut form, &hint, 1, &["category", "trip", "cash"]);
99
3
        type_in(&mut form, "c");
100
3
        assert_eq!(form.candidates(), ["category", "cash"]);
101
3
        form.complete_hint();
102
3
        assert_eq!(focused_text(&mut form, "name").as_deref(), Some("category"));
103
    }
104
1
}
105

            
106
#[test]
107
1
fn a_name_is_asked_once_per_scope_for_the_form_lifetime() {
108
1
    let mut form = transaction();
109
1
    form.hint_asked(&names(Scope::Transaction), 1);
110
1
    assert_eq!(form.wanted_hint(), None, "asked");
111
1
    type_in(&mut form, "x");
112
1
    assert_eq!(form.wanted_hint(), None, "no request per keystroke");
113
1
}
114

            
115
#[test]
116
1
fn values_follow_the_name_typed_in_the_same_row() {
117
1
    let cases = [
118
1
        (transaction(), Scope::Transaction),
119
1
        (account(), Scope::Account),
120
1
        (commodity(), Scope::Commodity),
121
1
    ];
122
1
    for (mut form, scope) in cases {
123
3
        type_in(&mut form, "category");
124
3
        next_cell(&mut form);
125
3
        let hint = values(scope, "category");
126
3
        assert_eq!(form.wanted_hint(), Some(hint.clone()));
127
3
        answer(&mut form, &hint, 2, &["food", "fuel"]);
128
3
        type_in(&mut form, "fu");
129
3
        assert_eq!(form.candidates(), ["fuel"]);
130
3
        form.complete_hint();
131
3
        assert_eq!(focused_text(&mut form, "value").as_deref(), Some("fuel"));
132
    }
133
1
}
134

            
135
#[test]
136
1
fn the_value_cell_without_a_name_asks_nothing() {
137
1
    let mut form = transaction();
138
1
    next_cell(&mut form);
139
1
    assert_eq!(form.wanted_hint(), None);
140
1
}
141

            
142
#[test]
143
1
fn another_name_asks_for_its_own_values_and_does_not_borrow_the_old() {
144
1
    let mut form = transaction();
145
1
    type_in(&mut form, "category");
146
1
    next_cell(&mut form);
147
1
    let old = values(Scope::Transaction, "category");
148
1
    answer(&mut form, &old, 3, &["food"]);
149
1
    if let Some(rows) = form.focused_rows_mut() {
150
1
        rows.retreat_cell();
151
    }
152
1
    type_in(&mut form, "2");
153
1
    next_cell(&mut form);
154
1
    assert_eq!(
155
1
        form.wanted_hint(),
156
1
        Some(values(Scope::Transaction, "category2"))
157
    );
158
1
    assert!(form.candidates().is_empty());
159
1
}
160

            
161
#[test]
162
1
fn a_stale_answer_is_dropped() {
163
1
    let mut form = transaction();
164
1
    let hint = names(Scope::Transaction);
165
1
    form.hint_asked(&hint, 4);
166
1
    form.hint_arrived(&hint, 3, strings(&["old"]));
167
1
    assert!(form.candidates().is_empty());
168
1
    form.hint_arrived(&hint, 4, strings(&["category", "trip"]));
169
1
    assert_eq!(form.candidates(), ["category", "trip"]);
170
1
}
171

            
172
#[test]
173
1
fn split_and_transaction_scopes_do_not_share_answers() {
174
1
    let mut form = on_split_tags(Form::transaction_create());
175
1
    assert_eq!(form.wanted_hint(), Some(names(Scope::Split)));
176
1
    answer(&mut form, &names(Scope::Split), 1, &["memo"]);
177
1
    assert_eq!(form.candidates(), ["memo"]);
178
1
    let mut other = on_tags(Form::transaction_create());
179
1
    other.hint_asked(&names(Scope::Split), 1);
180
1
    assert_eq!(other.wanted_hint(), Some(names(Scope::Transaction)));
181
1
}
182

            
183
#[test]
184
1
fn repeated_completion_cycles_through_the_matches_of_the_original_prefix() {
185
1
    let mut form = on_split_tags(Form::transaction_create());
186
1
    answer(
187
1
        &mut form,
188
1
        &names(Scope::Split),
189
1
        1,
190
1
        &["food", "fuel", "rent"],
191
1
    );
192
1
    type_in(&mut form, "f");
193
3
    let mut filled = || {
194
3
        form.complete_hint();
195
3
        focused_text(&mut form, "name")
196
3
    };
197
1
    let cycle = [filled(), filled(), filled()];
198
1
    assert_eq!(
199
1
        cycle,
200
1
        [
201
1
            Some("food".into()),
202
1
            Some("fuel".into()),
203
1
            Some("food".into())
204
1
        ]
205
    );
206
1
}
207

            
208
#[test]
209
1
fn the_suggestion_line_lists_what_fits() {
210
1
    let mut form = transaction();
211
1
    assert_eq!(form.suggestion_line(None), None);
212
1
    answer(&mut form, &names(Scope::Transaction), 1, &["food", "fuel"]);
213
1
    assert_eq!(
214
1
        form.suggestion_line(Some("C-o".to_string())).as_deref(),
215
1
        Some("suggest (C-o): food, fuel")
216
    );
217
1
    assert_eq!(
218
1
        form.suggestion_line(None).as_deref(),
219
1
        Some("suggest: food, fuel")
220
    );
221
1
}
222

            
223
#[test]
224
1
fn the_hint_names_its_form_options_source() {
225
1
    assert_eq!(names(Scope::Split).source(), "split-tag-names");
226
1
    assert_eq!(
227
1
        values(Scope::Account, "a:b").source(),
228
1
        "account-tag-values:a:b"
229
    );
230
1
}