1
//! A non-carried `tags` rows field gets `--tag` and `--clear-tags`; `--row` stays for splits.
2

            
3
use clap::error::ErrorKind;
4
use forms::{Action, Field, Form, Kind, TRANSACTION_EDIT};
5

            
6
use super::{form_command, values_of};
7

            
8
const CELLS: &[Field] = &[text("name"), text("value"), text("description")];
9

            
10
const fn text(key: &'static str) -> Field {
11
    Field {
12
        key,
13
        label: key,
14
        kind: Kind::Text {
15
            multiline: false,
16
            suggest: None,
17
        },
18
        required: false,
19
    }
20
}
21

            
22
static FORM: Form = Form {
23
    name: "test-free-rows",
24
    title: "Free rows",
25
    fields: &[
26
        TRANSACTION_EDIT.fields[2],
27
        TRANSACTION_EDIT.fields[5],
28
        Field {
29
            key: "tags",
30
            label: "Tags",
31
            kind: Kind::Rows {
32
                fields: CELLS,
33
                nested_under: None,
34
            },
35
            required: false,
36
        },
37
    ],
38
    action: Action::Mutation,
39
};
40

            
41
5
fn parse(args: &[&str]) -> Result<forms::FormValues, clap::Error> {
42
5
    let argv = std::iter::once("edit").chain(args.iter().copied());
43
5
    let matches = form_command("edit", "Edit", &FORM).try_get_matches_from(argv)?;
44
3
    Ok(values_of(&FORM, &matches))
45
5
}
46

            
47
#[test]
48
1
fn tag_and_clear_options_exist_beside_row() {
49
1
    let command = form_command("edit", "Edit", &FORM);
50
1
    let longs: Vec<_> = command
51
1
        .get_arguments()
52
4
        .filter_map(|a| a.get_long())
53
        .collect();
54
1
    assert_eq!(longs, ["date", "row", "tag", "clear-tags"]);
55
1
}
56

            
57
#[test]
58
1
fn no_tag_option_leaves_the_field_absent() -> Result<(), clap::Error> {
59
1
    assert!(parse(&[])?.rows("tags").is_none());
60
1
    Ok(())
61
1
}
62

            
63
#[test]
64
1
fn tag_options_give_the_rows() -> Result<(), clap::Error> {
65
1
    let values = parse(&[
66
1
        "--tag",
67
1
        "name=a,value=1",
68
1
        "--tag",
69
1
        "name=b,value=2,description=x\\,y",
70
1
        "--row",
71
1
        "value=5",
72
1
    ])?;
73
1
    let tags = values.rows("tags").unwrap_or_default();
74
1
    assert_eq!(tags.len(), 2);
75
1
    assert_eq!(tags[1].get("description").map(String::as_str), Some("x,y"));
76
1
    assert_eq!(values.rows("splits").map(<[_]>::len), Some(1));
77
1
    Ok(())
78
1
}
79

            
80
#[test]
81
1
fn clear_tags_sends_an_empty_list() -> Result<(), clap::Error> {
82
1
    assert_eq!(parse(&["--clear-tags"])?.rows("tags"), Some(&[][..]));
83
1
    Ok(())
84
1
}
85

            
86
#[test]
87
1
fn tag_with_clear_tags_is_a_usage_error() {
88
1
    let error = parse(&["--tag", "name=a", "--clear-tags"]).err();
89
1
    assert_eq!(error.map(|e| e.kind()), Some(ErrorKind::ArgumentConflict));
90
1
}
91

            
92
#[test]
93
1
fn a_tag_cell_is_checked_like_a_row_cell() {
94
1
    assert!(parse(&["--tag", "bogus=1"]).is_err());
95
1
}
96

            
97
#[test]
98
1
fn every_form_builds_a_command() {
99
1
    let forms = forms::all().iter().copied().chain([&FORM]);
100
1
    for form in forms {
101
22
        form_command("f", "F", form).debug_assert();
102
    }
103
1
}