1
use std::sync::LazyLock;
2

            
3
use forms::{Action, Form, TRANSACTION_EDIT};
4

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

            
7
const KEYS: [&str; 5] = ["date", "note", "tags", "splits", "split-tags"];
8

            
9
static WITH_CARRIED: LazyLock<Form> = LazyLock::new(|| Form {
10
2
    name: "test-carried",
11
2
    title: "Carried",
12
2
    fields: KEYS
13
2
        .iter()
14
10
        .filter_map(|key| {
15
10
            TRANSACTION_EDIT
16
10
                .fields
17
10
                .iter()
18
50
                .find(|field| field.key == *key)
19
10
        })
20
        .copied()
21
        .collect::<Vec<_>>()
22
        .leak(),
23
2
    action: Action::Mutation,
24
2
});
25

            
26
2
fn command() -> clap::Command {
27
2
    form_command("edit", "Edit", &WITH_CARRIED)
28
2
}
29

            
30
#[test]
31
1
fn only_split_tags_of_the_nested_fields_have_an_option() {
32
1
    assert_eq!(WITH_CARRIED.fields.len(), KEYS.len());
33
1
    let command = command();
34
1
    let longs: Vec<_> = command
35
1
        .get_arguments()
36
7
        .filter_map(|a| a.get_long())
37
        .collect();
38
1
    assert_eq!(
39
1
        longs,
40
1
        [
41
1
            "date",
42
1
            "note",
43
1
            "tag",
44
1
            "clear-tags",
45
1
            "row",
46
1
            "split-tag",
47
1
            "clear-split-tags"
48
1
        ]
49
    );
50
1
    assert_eq!(
51
1
        TRANSACTION_EDIT.nested_keys().collect::<Vec<_>>(),
52
1
        ["split-tags"]
53
    );
54
1
    for rejected in ["--split-tags", "--tags-split", "--clear-split-tag"] {
55
3
        assert!(
56
3
            command
57
3
                .clone()
58
3
                .try_get_matches_from(["edit", rejected, "x"])
59
3
                .is_err(),
60
            "{rejected}"
61
        );
62
    }
63
1
    assert!(
64
1
        command
65
1
            .clone()
66
1
            .try_get_matches_from(["edit", "--split-tag", "1:a=b"])
67
1
            .is_ok()
68
    );
69
1
    assert!(
70
1
        command
71
1
            .clone()
72
1
            .try_get_matches_from(["edit", "--row", "value=1"])
73
1
            .is_ok()
74
    );
75
1
}
76

            
77
#[test]
78
1
fn carried_fields_stay_out_of_the_overrides() {
79
1
    let matches = command()
80
1
        .try_get_matches_from(["edit", "--note", "x", "--row", "value=1"])
81
1
        .unwrap();
82
1
    let values = values_of(&WITH_CARRIED, &matches);
83
1
    assert_eq!(values.text("note"), Some("x"));
84
1
    assert!(
85
1
        TRANSACTION_EDIT
86
1
            .nested_keys()
87
1
            .all(|key| values.rows(key).is_none())
88
    );
89
1
    assert_eq!(values.rows("splits").map(<[_]>::len), Some(1));
90
1
}
91

            
92
#[test]
93
1
fn transaction_forms_take_tag_options() {
94
1
    for form in [&forms::TRANSACTION_CREATE, &forms::TRANSACTION_EDIT] {
95
2
        let matches = form_command("f", "F", form)
96
2
            .try_get_matches_from(["f", "--tag", "name=trip,description=ski"])
97
2
            .unwrap();
98
2
        let tags = values_of(form, &matches);
99
2
        assert_eq!(tags.rows("tags").map(<[_]>::len), Some(1));
100
2
        let cleared = form_command("f", "F", form)
101
2
            .try_get_matches_from(["f", "--clear-tags"])
102
2
            .unwrap();
103
2
        assert_eq!(values_of(form, &cleared).rows("tags"), Some(&[][..]));
104
    }
105
1
}