1
use super::*;
2

            
3
9
fn tag(entities: &[Entity], name: &str, values: &[&str]) -> Item {
4
    Item::Tag {
5
9
        entities: entities.to_vec(),
6
9
        name: name.to_owned(),
7
9
        values: values.iter().map(|v| (*v).to_owned()).collect(),
8
    }
9
9
}
10

            
11
13
fn account(id: &str, subtree: bool) -> Item {
12
13
    Item::Account {
13
13
        id: id.to_owned(),
14
13
        subtree,
15
13
    }
16
13
}
17

            
18
29
fn text(logic: Logic, items: &[Item]) -> Option<String> {
19
29
    group_sexpr(logic, items).expect("buildable")
20
29
}
21

            
22
#[test]
23
1
fn a_tag_row_is_one_leaf_per_entity() {
24
1
    let row = tag(&[Entity::Transaction], "category", &["food"]);
25
1
    assert_eq!(
26
1
        text(Logic::And, &[row]).as_deref(),
27
1
        Some(r#"(transaction-tag= "category" "food")"#)
28
    );
29
1
}
30

            
31
#[test]
32
1
fn every_entity_has_its_operator() {
33
1
    for (entity, operator) in [
34
1
        (Entity::Account, "account-tag="),
35
1
        (Entity::Transaction, "transaction-tag="),
36
1
        (Entity::Split, "split-tag="),
37
1
    ] {
38
3
        let row = tag(&[entity], "n", &["v"]);
39
3
        let want = format!(r#"({operator} "n" "v")"#);
40
3
        assert_eq!(text(Logic::And, &[row]).as_deref(), Some(want.as_str()));
41
    }
42
1
}
43

            
44
#[test]
45
1
fn several_entities_and_values_expand_to_or() {
46
1
    let row = tag(&[Entity::Account, Entity::Split], "t", &["a", "b"]);
47
1
    assert_eq!(
48
1
        text(Logic::And, &[row]).as_deref(),
49
1
        Some(
50
1
            r#"(or (or (account-tag= "t" "a") (account-tag= "t" "b")) (or (split-tag= "t" "a") (split-tag= "t" "b")))"#
51
1
        )
52
    );
53
1
}
54

            
55
#[test]
56
1
fn repeated_entities_build_one_condition_each() {
57
1
    let row = tag(&[Entity::Split; 3], "t", &["a"]);
58
1
    assert_eq!(
59
1
        text(Logic::And, &[row]).as_deref(),
60
1
        Some(r#"(split-tag= "t" "a")"#)
61
    );
62
1
}
63

            
64
#[test]
65
1
fn accounts_use_equality_or_subtree() {
66
1
    assert_eq!(
67
1
        text(Logic::And, &[account(" u1 ", false)]).as_deref(),
68
1
        Some(r#"(account= "u1")"#)
69
    );
70
1
    assert_eq!(
71
1
        text(Logic::And, &[account("u1", true)]).as_deref(),
72
1
        Some(r#"(account-subtree "u1")"#)
73
    );
74
1
}
75

            
76
#[test]
77
1
fn groups_combine_their_children() {
78
1
    let items = [account("a", false), account("b", true)];
79
1
    assert_eq!(
80
1
        text(Logic::And, &items).as_deref(),
81
1
        Some(r#"(and (account= "a") (account-subtree "b"))"#)
82
    );
83
1
    assert_eq!(
84
1
        text(Logic::Or, &items).as_deref(),
85
1
        Some(r#"(or (account= "a") (account-subtree "b"))"#)
86
    );
87
1
}
88

            
89
#[test]
90
1
fn not_wraps_exactly_one_child() {
91
1
    assert_eq!(
92
1
        text(Logic::Not, &[account("a", false)]).as_deref(),
93
1
        Some(r#"(not (account= "a"))"#)
94
    );
95
1
    let two = [account("a", false), account("b", false)];
96
1
    assert_eq!(
97
1
        group_sexpr(Logic::Not, &two),
98
1
        Err(Fault {
99
1
            path: vec![],
100
1
            reason: Reason::NotArity(2)
101
1
        })
102
    );
103
1
}
104

            
105
#[test]
106
1
fn nested_groups_nest() {
107
1
    let inner = Item::Group {
108
1
        logic: Logic::Or,
109
1
        items: vec![account("a", false), account("b", false)],
110
1
    };
111
1
    let outer = [Item::Group {
112
1
        logic: Logic::Not,
113
1
        items: vec![inner],
114
1
    }];
115
1
    assert_eq!(
116
1
        text(Logic::And, &outer).as_deref(),
117
1
        Some(r#"(not (or (account= "a") (account= "b")))"#)
118
    );
119
1
}
120

            
121
#[test]
122
1
fn an_empty_builder_is_no_filter() {
123
1
    assert_eq!(text(Logic::And, &[]), None);
124
1
    assert_eq!(text(Logic::Not, &[]), None);
125
1
}
126

            
127
#[test]
128
1
fn blank_rows_and_empty_groups_are_refused_not_dropped() {
129
1
    let items = [
130
1
        tag(&[], "", &[]),
131
1
        account("  ", true),
132
1
        Item::Group {
133
1
            logic: Logic::Not,
134
1
            items: vec![],
135
1
        },
136
1
    ];
137
1
    for (at, item) in items.into_iter().enumerate() {
138
3
        assert!(
139
3
            group_sexpr(Logic::And, std::slice::from_ref(&item)).is_err(),
140
            "{at}"
141
        );
142
3
        assert!(item_sexpr(&item).is_err(), "{at}");
143
    }
144
1
}
145

            
146
#[test]
147
1
fn a_tag_without_entity_is_unbuildable() {
148
1
    let row = tag(&[], "category", &["food"]);
149
1
    assert_eq!(
150
1
        group_sexpr(Logic::And, &[row]),
151
1
        Err(Fault {
152
1
            path: vec![0],
153
1
            reason: Reason::TagWithoutEntity
154
1
        })
155
    );
156
1
}
157

            
158
#[test]
159
1
fn a_malformed_child_is_unbuildable_at_any_depth() {
160
1
    let bad = Item::Group {
161
1
        logic: Logic::Not,
162
1
        items: vec![account("a", false), account("b", false)],
163
1
    };
164
1
    let outer = [account("c", false), bad];
165
1
    assert_eq!(
166
1
        group_sexpr(Logic::And, &outer),
167
1
        Err(Fault {
168
1
            path: vec![1],
169
1
            reason: Reason::NotArity(2)
170
1
        })
171
    );
172
1
}
173

            
174
#[test]
175
1
fn names_and_values_are_escaped() {
176
1
    let row = tag(&[Entity::Split], r#"a"b"#, &[r"c\d"]);
177
1
    assert_eq!(
178
1
        text(Logic::And, &[row]).as_deref(),
179
1
        Some(r#"(split-tag= "a\"b" "c\\d")"#)
180
    );
181
1
}
182

            
183
#[test]
184
1
fn entities_and_logic_parse_their_wire_names() {
185
1
    assert_eq!(Entity::parse("split"), Some(Entity::Split));
186
1
    assert_eq!(Entity::parse("account"), Some(Entity::Account));
187
1
    assert_eq!(Entity::parse("transaction"), Some(Entity::Transaction));
188
1
    assert_eq!(Entity::parse("x"), None);
189
1
    assert_eq!(Logic::parse("not"), Some(Logic::Not));
190
1
    assert_eq!(Logic::parse("or"), Some(Logic::Or));
191
1
    assert_eq!(Logic::parse("and"), Some(Logic::And));
192
1
    assert_eq!(Logic::parse("AND"), None);
193
1
}
194

            
195
#[test]
196
1
fn wire_names_round_trip_through_parse() {
197
1
    for entity in Entity::ALL {
198
3
        assert_eq!(Entity::parse(entity.name()), Some(entity));
199
    }
200
1
    for logic in [Logic::And, Logic::Or, Logic::Not] {
201
3
        assert_eq!(Logic::parse(logic.operator()), Some(logic));
202
    }
203
1
}
204

            
205
5
fn ids(list: &[&str]) -> Vec<String> {
206
8
    list.iter().map(|id| (*id).to_owned()).collect()
207
5
}
208

            
209
6
fn amount(comparison: Comparison, numer: i64, denom: i64) -> Item {
210
6
    Item::Amount {
211
6
        comparison,
212
6
        value: Amount::new(numer, denom).expect("amount"),
213
6
    }
214
6
}
215

            
216
#[test]
217
1
fn id_rows_write_their_operators() {
218
1
    let cases = [
219
1
        (
220
1
            Item::AccountIn {
221
1
                ids: ids(&["a", "b"]),
222
1
            },
223
1
            r#"(account-in "a" "b")"#,
224
1
        ),
225
1
        (Item::AccountIn { ids: ids(&["a"]) }, r#"(account-in "a")"#),
226
1
        (
227
1
            Item::CounterpartyEq { id: "c".into() },
228
1
            r#"(counterparty= "c")"#,
229
1
        ),
230
1
        (
231
1
            Item::CounterpartyIn {
232
1
                ids: ids(&["c", "d"]),
233
1
            },
234
1
            r#"(counterparty-in "c" "d")"#,
235
1
        ),
236
1
        (Item::CommodityEq { id: "m".into() }, r#"(commodity= "m")"#),
237
1
        (
238
1
            Item::CommodityIn { ids: ids(&["m"]) },
239
1
            r#"(commodity-in "m")"#,
240
1
        ),
241
1
    ];
242
1
    for (row, want) in cases {
243
6
        assert_eq!(text(Logic::And, &[row]).as_deref(), Some(want));
244
    }
245
1
}
246

            
247
#[test]
248
1
fn list_ids_are_trimmed_when_written() {
249
1
    let row = Item::AccountIn {
250
1
        ids: ids(&[" a ", "b"]),
251
1
    };
252
1
    assert_eq!(
253
1
        text(Logic::And, &[row]).as_deref(),
254
1
        Some(r#"(account-in "a" "b")"#)
255
    );
256
1
}
257

            
258
#[test]
259
1
fn amount_rows_write_integers_fractions_and_negatives() {
260
1
    let cases = [
261
1
        (amount(Comparison::Gt, 100, 1), "(amount> 100)"),
262
1
        (amount(Comparison::Lt, 1, 2), "(amount< 1/2)"),
263
1
        (amount(Comparison::Eq, -3, 4), "(amount= -3/4)"),
264
1
        (amount(Comparison::Gt, -5, 1), "(amount> -5)"),
265
1
        (amount(Comparison::Eq, 0, 1), "(amount= 0)"),
266
1
    ];
267
1
    for (row, want) in cases {
268
5
        assert_eq!(text(Logic::And, &[row]).as_deref(), Some(want));
269
    }
270
1
}
271

            
272
#[test]
273
1
fn comparison_operators_are_the_grammar_names() {
274
1
    assert_eq!(Comparison::Gt.operator(), "amount>");
275
1
    assert_eq!(Comparison::Lt.operator(), "amount<");
276
1
    assert_eq!(Comparison::Eq.operator(), "amount=");
277
1
}
278

            
279
#[test]
280
1
fn new_rows_nest_in_groups() {
281
1
    let inner = Item::Group {
282
1
        logic: Logic::Or,
283
1
        items: vec![
284
1
            Item::CommodityEq { id: "m".into() },
285
1
            amount(Comparison::Lt, 5, 1),
286
1
        ],
287
1
    };
288
1
    let rows = [Item::CounterpartyEq { id: "c".into() }, inner];
289
1
    assert_eq!(
290
1
        text(Logic::Not, &rows[..1]).as_deref(),
291
1
        Some(r#"(not (counterparty= "c"))"#)
292
    );
293
1
    assert_eq!(
294
1
        text(Logic::And, &rows).as_deref(),
295
1
        Some(r#"(and (counterparty= "c") (or (commodity= "m") (amount< 5)))"#)
296
    );
297
1
}