Lines
100 %
Functions
Branches
use super::items::{Item, labelled_id, parse_items, resolve_id};
fn option(id: &str, label: &str, alias: Option<&str>) -> Item {
Item {
id: Some(id.into()),
label: label.into(),
alias: alias.map(Into::into),
path: None,
commodity: None,
}
#[test]
fn options_parse_with_their_optional_fields() {
let body = r#"[{"id":"a","label":"Assets:Cash","alias":"Cash","commodity":"c1"},
{"id":"b","label":"Idle"}]"#;
let items = parse_items(body).expect("parse");
assert_eq!(items[0].alias.as_deref(), Some("Cash"));
assert_eq!(items[0].commodity.as_deref(), Some("c1"));
assert_eq!(items[1].label, "Idle");
assert_eq!(items[1].commodity, None);
fn tag_suggestions_use_the_name_as_label() {
let items = parse_items(r#"[{"name":"category","value":null}]"#).expect("parse");
assert_eq!(items[0].label, "category");
assert_eq!(items[0].id, None);
fn matching_covers_label_and_alias_in_any_case_never_the_id() {
let item = option("1b4e28ba", "Assets:Cash", Some("Wallet"));
assert!(item.matches("assets"));
assert!(item.matches("wall"));
assert!(item.matches(""));
assert!(!item.matches("bank"));
assert!(!item.matches("1b4e"));
fn only_an_exact_unique_label_has_an_id() {
let items = [
option("a", "Cash ‹abcd›", Some("Cash")),
option("b", "Cash ‹ef01›", Some("Cash")),
option("c", "Bank", None),
];
assert_eq!(labelled_id(&items, "Cash ‹ef01›"), Some("b"));
assert_eq!(labelled_id(&items, " Bank "), Some("c"));
assert_eq!(labelled_id(&items, "Cash"), None);
assert_eq!(labelled_id(&items, "bank"), None);
assert_eq!(labelled_id(&items, "c"), None);
fn the_path_is_matched_and_shown_only_when_it_differs() {
let mut item = option("a", "JR", Some("JR"));
item.path = Some("Transportation:JR".into());
assert!(item.matches("transport"));
assert_eq!(item.context(), Some("Transportation:JR"));
item.path = Some("JR".into());
assert_eq!(item.context(), None);
fn a_step_matching_several_options_leaves_the_text_unresolved() {
assert_eq!(resolve_id(&items, "Cash"), None);
assert_eq!(resolve_id(&items, "b"), Some("b"));
fn text_names_an_option_by_id_alias_or_label() {
option("a", "Dollar", Some("Buck")),
option("b", "Euro", None),
for text in ["a", "buck", "Buck", " Buck", "Dollar"] {
assert_eq!(resolve_id(&items, text), Some("a"));
assert_eq!(resolve_id(&items, "euro"), Some("b"));
assert_eq!(resolve_id(&items, "USD"), None);