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

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

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

            
31
#[test]
32
1
fn a_choice_writes_its_options_and_default() {
33
1
    assert_eq!(
34
1
        descriptor_sexpr(&FORM),
35
1
        concat!(
36
            "(:name \"choice-test\" :title \"Choice test\" :action query :fields (",
37
            "(:key \"period\" :label \"Period\" :kind choice ",
38
            ":options ((\"month\" \"Month\") (\"year\" \"Year \\\"all\\\"\")) :default \"month\" :required nil) ",
39
            "(:key \"mode\" :label \"Mode\" :kind choice ",
40
            ":options ((\"all\" \"All\") (\"any\" \"Any\")) :required t)))"
41
        )
42
    );
43
1
}
44

            
45
#[test]
46
1
fn the_choice_descriptor_matches_the_shared_fixture() {
47
1
    let fixture = include_str!("fixtures/descriptor-choice.sexp");
48
1
    assert_eq!(descriptor_sexpr(&FORM), fixture.trim_end_matches('\n'));
49
1
}
50

            
51
#[test]
52
1
fn only_the_report_transaction_and_script_forms_emit_a_choice() {
53
29
    fn emits(fields: &[Field]) -> bool {
54
113
        fields.iter().any(|field| match field.kind {
55
6
            Kind::Choice { .. } => true,
56
8
            Kind::Rows { fields, .. } => emits(fields),
57
99
            _ => false,
58
113
        })
59
29
    }
60
1
    let names: Vec<&str> = all()
61
1
        .iter()
62
21
        .filter(|form| emits(form.fields))
63
6
        .map(|form| form.name)
64
        .collect();
65
1
    assert_eq!(
66
1
        names,
67
1
        [
68
1
            "transaction-create",
69
1
            "transaction-edit",
70
1
            "script-create",
71
1
            "script-edit",
72
1
            "report-activity",
73
1
            "report-category"
74
1
        ]
75
    );
76
1
}
77

            
78
#[test]
79
1
fn a_text_check_leaves_a_choice_value_alone() {
80
1
    let mut values = forms::FormValues::default();
81
1
    values.set("period", "month");
82
1
    values.set("mode", "any");
83
1
    assert!(values.check(&FORM).is_empty());
84
1
}
85

            
86
#[test]
87
1
fn a_split_tag_side_is_a_choice_whose_blank_key_means_both_legs() {
88
1
    let rows = forms::TRANSACTION_CREATE
89
1
        .fields
90
1
        .iter()
91
5
        .find(|field| field.key == "split-tags")
92
1
        .map(|field| field.kind);
93
1
    let Some(Kind::Rows { fields, .. }) = rows else {
94
        panic!("split-tags is not rows");
95
    };
96
5
    let side = fields.iter().find(|field| field.key == "side");
97
1
    let Some(Kind::Choice { options, default }) = side.map(|field| field.kind) else {
98
        panic!("side is not a choice");
99
    };
100
3
    let keys: Vec<&str> = options.iter().map(|(key, _)| *key).collect();
101
1
    assert_eq!(keys, ["", "from", "to"]);
102
1
    assert_eq!(default, Some(""));
103
1
    let sexpr = descriptor_sexpr(&forms::TRANSACTION_CREATE);
104
1
    assert!(
105
1
        sexpr.contains(":options ((\"\" \"Both accounts\")"),
106
        "{sexpr}"
107
    );
108
1
}