1
//! Tests for the select-reference resolver.
2

            
3
use forms::Source;
4

            
5
use super::super::{FormOption, resolve as resolve_in};
6
use crate::CommandError;
7

            
8
const CASH: &str = "550e8400-e29b-41d4-a716-446655440001";
9
const OTHER_CASH: &str = "550e8400-e29b-41d4-a716-446655440002";
10
const ODD: &str = "550e8400-e29b-41d4-a716-446655440003";
11

            
12
28
fn option(id: &str, label: &str, alias: &str) -> FormOption {
13
28
    FormOption {
14
28
        id: id.to_string(),
15
28
        label: label.to_string(),
16
28
        alias: alias.to_string(),
17
28
        commodity: None,
18
28
    }
19
28
}
20

            
21
7
fn options() -> Vec<FormOption> {
22
7
    vec![
23
7
        option(CASH, "Assets:Cash", "Cash"),
24
7
        option(OTHER_CASH, "Other:Cash", "Cash"),
25
7
        option(ODD, "A¦B", "A|B"),
26
    ]
27
7
}
28

            
29
11
fn resolve(options: &[FormOption], flag: &str, input: &str) -> Result<String, CommandError> {
30
11
    resolve_in(options, Source::Accounts, flag, input)
31
11
}
32

            
33
4
fn message(result: Result<String, CommandError>) -> String {
34
4
    result.map_or_else(|e| e.to_string(), |id| format!("resolved {id}"))
35
4
}
36

            
37
#[test]
38
1
fn an_own_uuid_passes() {
39
1
    assert_eq!(
40
1
        resolve(&options(), "--to", CASH).ok().as_deref(),
41
1
        Some(CASH)
42
    );
43
1
}
44

            
45
#[test]
46
1
fn an_exact_label_maps_to_its_id() {
47
1
    let result = resolve(&options(), "--to", "Other:Cash");
48
1
    assert_eq!(result.ok().as_deref(), Some(OTHER_CASH));
49
1
}
50

            
51
#[test]
52
1
fn pipe_and_broken_bar_both_map() {
53
1
    for input in ["A|B", "A¦B"] {
54
2
        let result = resolve(&options(), "--to", input);
55
2
        assert_eq!(result.ok().as_deref(), Some(ODD), "{input}");
56
    }
57
1
}
58

            
59
#[test]
60
1
fn a_unique_alias_maps_to_its_id() {
61
1
    let only = [option(CASH, "Assets:Cash", "Cash")];
62
1
    assert_eq!(resolve(&only, "--to", "Cash").ok().as_deref(), Some(CASH));
63
1
}
64

            
65
#[test]
66
1
fn an_ambiguous_alias_lists_the_candidates() {
67
1
    let text = message(resolve(&options(), "--parent", "Cash"));
68
1
    assert!(text.contains("--parent"), "{text}");
69
1
    assert!(text.contains("Assets:Cash, Other:Cash"), "{text}");
70
1
}
71

            
72
#[test]
73
1
fn unknown_input_names_the_flag() {
74
1
    let text = message(resolve(&options(), "--row to", "Nowhere"));
75
1
    assert!(
76
1
        text.contains("--row to") && text.contains("Nowhere"),
77
        "{text}"
78
    );
79
1
}
80

            
81
#[test]
82
1
fn a_uuid_that_is_not_an_option_is_unknown() {
83
1
    let text = message(resolve(
84
1
        &options(),
85
1
        "--to",
86
1
        "550e8400-e29b-41d4-a716-446655449999",
87
1
    ));
88
1
    assert!(text.contains("matches no entry"), "{text}");
89
1
}
90

            
91
#[test]
92
1
fn a_path_label_matches_its_pipe_alias_both_ways() {
93
1
    let options = [option(ODD, "Parent:A¦B", "A|B")];
94
1
    for input in ["A|B", "A¦B"] {
95
2
        let result = resolve(&options, "--to", input);
96
2
        assert_eq!(result.ok().as_deref(), Some(ODD), "{input}");
97
    }
98
1
}
99

            
100
#[test]
101
1
fn commodities_match_case_insensitively() {
102
1
    let options = [option(CASH, "EUR", "EUR"), option(ODD, "USD", "USD")];
103
1
    let result = resolve_in(&options, Source::Commodities, "--row from-commodity", "eur");
104
1
    assert_eq!(result.ok().as_deref(), Some(CASH));
105
1
    let accounts = resolve(&options, "--to", "eur");
106
1
    assert!(accounts.is_err(), "accounts stay case-sensitive");
107
1
}
108

            
109
#[test]
110
1
fn a_case_only_commodity_alias_pair_is_ambiguous() {
111
1
    let options = [
112
1
        option(CASH, "USD ‹5500›", "USD"),
113
1
        option(OTHER_CASH, "usd ‹5501›", "usd"),
114
1
    ];
115
1
    let result = resolve_in(&options, Source::Commodities, "--row from-commodity", "Usd");
116
1
    let text = message(result);
117
1
    assert!(
118
1
        text.contains("ambiguous") && text.contains("USD ‹5500›, usd ‹5501›"),
119
        "{text}"
120
    );
121
1
}
122

            
123
#[test]
124
1
fn map_values_names_the_field_with_the_given_prefix() {
125
1
    let mut values = forms::FormValues::default();
126
1
    values.set("target", "Nowhere");
127
1
    let fetched = vec![(Source::Commodities, vec![option(CASH, "USD", "USD")])];
128
2
    let name = |prefix| {
129
2
        super::super::map_values(&forms::REPORT_BALANCE, &values, &fetched, prefix)
130
2
            .err()
131
2
            .map(|e| e.to_string())
132
            .unwrap_or_default()
133
2
    };
134
1
    assert!(name("--").contains("--target"), "{}", name("--"));
135
1
    assert!(!name("").contains("--target"), "{}", name(""));
136
1
}