Lines
97.25 %
Functions
100 %
Branches
//! Type-ahead on the tag cells of every form that carries a tags list.
use forms::{FormValues, Scope, Suggest};
use crate::form::Form;
use crate::widgets::{Hint, Widget};
fn names(scope: Scope) -> Hint {
Hint::Names(Suggest::TagNames(scope))
}
fn values(scope: Scope, tag: &str) -> Hint {
Hint::Values(Suggest::TagValues(scope), tag.to_string())
fn strings(items: &[&str]) -> Vec<String> {
items.iter().map(ToString::to_string).collect()
fn on_tags(mut form: Form) -> Form {
form.focus = form
.fields
.iter()
.position(|field| field.key == Some("tags"))
.expect("the form has a tags field");
if let Some(Widget::Rows(rows)) = form.focused_field_mut().map(|field| &mut field.widget) {
rows.add_row();
form
fn on_split_tags(mut form: Form) -> Form {
.position(|field| matches!(field.widget, Widget::Splits(_)))
.expect("the form has splits");
if let Some(Widget::Splits(sw)) = form.focused_field_mut().map(|field| &mut field.widget) {
sw.open_tags();
sw.add_tag();
fn type_in(form: &mut Form, text: &str) {
let editor = form
.focused_rows_mut()
.and_then(|rows| rows.focused_editor_mut());
if let Some(editor) = editor {
text.chars().for_each(|c| editor.insert_char(c));
fn next_cell(form: &mut Form) {
if let Some(rows) = form.focused_rows_mut() {
rows.advance_cell();
fn focused_text(form: &mut Form, key: &str) -> Option<String> {
form.focused_rows_mut()
.map(|rows| rows.focused_text(key).to_string())
fn answer(form: &mut Form, hint: &Hint, seq: u64, items: &[&str]) {
form.hint_asked(hint, seq);
form.hint_arrived(hint, seq, strings(items));
fn transaction() -> Form {
on_tags(Form::transaction_create())
fn account() -> Form {
on_tags(Form::account_edit(&FormValues::default()))
fn commodity() -> Form {
on_tags(Form::commodity_edit(&FormValues::default()))
#[test]
fn a_tag_name_cell_of_each_scope_asks_for_its_own_names() {
let cases = [
(transaction(), Scope::Transaction),
(account(), Scope::Account),
(commodity(), Scope::Commodity),
];
for (form, scope) in cases {
assert_eq!(form.wanted_hint(), Some(names(scope)));
fn names_are_offered_by_prefix_and_completed_by_the_key() {
for mut form in [transaction(), account(), commodity()] {
let hint = form.wanted_hint().expect("names are wanted");
answer(&mut form, &hint, 1, &["category", "trip", "cash"]);
type_in(&mut form, "c");
assert_eq!(form.candidates(), ["category", "cash"]);
form.complete_hint();
assert_eq!(focused_text(&mut form, "name").as_deref(), Some("category"));
fn a_name_is_asked_once_per_scope_for_the_form_lifetime() {
let mut form = transaction();
form.hint_asked(&names(Scope::Transaction), 1);
assert_eq!(form.wanted_hint(), None, "asked");
type_in(&mut form, "x");
assert_eq!(form.wanted_hint(), None, "no request per keystroke");
fn values_follow_the_name_typed_in_the_same_row() {
for (mut form, scope) in cases {
type_in(&mut form, "category");
next_cell(&mut form);
let hint = values(scope, "category");
assert_eq!(form.wanted_hint(), Some(hint.clone()));
answer(&mut form, &hint, 2, &["food", "fuel"]);
type_in(&mut form, "fu");
assert_eq!(form.candidates(), ["fuel"]);
assert_eq!(focused_text(&mut form, "value").as_deref(), Some("fuel"));
fn the_value_cell_without_a_name_asks_nothing() {
assert_eq!(form.wanted_hint(), None);
fn another_name_asks_for_its_own_values_and_does_not_borrow_the_old() {
let old = values(Scope::Transaction, "category");
answer(&mut form, &old, 3, &["food"]);
rows.retreat_cell();
type_in(&mut form, "2");
assert_eq!(
form.wanted_hint(),
Some(values(Scope::Transaction, "category2"))
);
assert!(form.candidates().is_empty());
fn a_stale_answer_is_dropped() {
let hint = names(Scope::Transaction);
form.hint_asked(&hint, 4);
form.hint_arrived(&hint, 3, strings(&["old"]));
form.hint_arrived(&hint, 4, strings(&["category", "trip"]));
assert_eq!(form.candidates(), ["category", "trip"]);
fn split_and_transaction_scopes_do_not_share_answers() {
let mut form = on_split_tags(Form::transaction_create());
assert_eq!(form.wanted_hint(), Some(names(Scope::Split)));
answer(&mut form, &names(Scope::Split), 1, &["memo"]);
assert_eq!(form.candidates(), ["memo"]);
let mut other = on_tags(Form::transaction_create());
other.hint_asked(&names(Scope::Split), 1);
assert_eq!(other.wanted_hint(), Some(names(Scope::Transaction)));
fn repeated_completion_cycles_through_the_matches_of_the_original_prefix() {
answer(
&mut form,
&names(Scope::Split),
1,
&["food", "fuel", "rent"],
type_in(&mut form, "f");
let mut filled = || {
focused_text(&mut form, "name")
};
let cycle = [filled(), filled(), filled()];
cycle,
[
Some("food".into()),
Some("fuel".into()),
Some("food".into())
]
fn the_suggestion_line_lists_what_fits() {
assert_eq!(form.suggestion_line(None), None);
answer(&mut form, &names(Scope::Transaction), 1, &["food", "fuel"]);
form.suggestion_line(Some("C-o".to_string())).as_deref(),
Some("suggest (C-o): food, fuel")
form.suggestion_line(None).as_deref(),
Some("suggest: food, fuel")
fn the_hint_names_its_form_options_source() {
assert_eq!(names(Scope::Split).source(), "split-tag-names");
values(Scope::Account, "a:b").source(),
"account-tag-values:a:b"