1
use forms::{Action, Field, Form, Kind};
2

            
3
use super::{form_command, values_of};
4

            
5
const FIELDS: &[Field] = &[
6
    Field {
7
        key: "period",
8
        label: "Period",
9
        kind: Kind::Choice {
10
            options: &[("month", "Month"), ("year", "Year")],
11
            default: Some("month"),
12
        },
13
        required: false,
14
    },
15
    Field {
16
        key: "mode",
17
        label: "Mode",
18
        kind: Kind::Choice {
19
            options: &[("all", "All"), ("any", "Any")],
20
            default: None,
21
        },
22
        required: true,
23
    },
24
];
25

            
26
const FORM: Form = Form {
27
    name: "choice-test",
28
    title: "Choice test",
29
    fields: FIELDS,
30
    action: Action::Query,
31
};
32

            
33
#[test]
34
1
fn a_known_key_is_accepted() -> Result<(), clap::Error> {
35
1
    let matches = form_command("q", "Q", &FORM).try_get_matches_from(["q", "--period", "year"])?;
36
1
    let values = values_of(&FORM, &matches);
37
1
    assert_eq!(values.text("period"), Some("year"));
38
1
    assert_eq!(values.text("mode"), None);
39
1
    Ok(())
40
1
}
41

            
42
#[test]
43
1
fn an_unknown_key_is_a_clap_error() {
44
1
    let error = form_command("q", "Q", &FORM)
45
1
        .try_get_matches_from(["q", "--mode", "some"])
46
1
        .err();
47
1
    assert_eq!(
48
1
        error.map(|e| e.kind()),
49
1
        Some(clap::error::ErrorKind::InvalidValue)
50
    );
51
1
}
52

            
53
#[test]
54
1
fn a_label_is_not_a_key() {
55
1
    let result = form_command("q", "Q", &FORM).try_get_matches_from(["q", "--period", "Year"]);
56
1
    assert!(result.is_err());
57
1
}
58

            
59
#[test]
60
1
fn help_lists_the_options_and_the_default() {
61
1
    let help = form_command("q", "Q", &FORM).render_long_help().to_string();
62
1
    assert!(
63
1
        help.contains("One of: month (Month), year (Year); default: month"),
64
        "{help}"
65
    );
66
1
    assert!(help.contains("One of: all (All), any (Any)"), "{help}");
67
1
    assert!(help.contains("[possible values: month, year]"), "{help}");
68
1
}