Lines
100 %
Functions
Branches
use super::*;
fn tag(entities: &[Entity], name: &str, values: &[&str]) -> Item {
Item::Tag {
entities: entities.to_vec(),
name: name.to_owned(),
values: values.iter().map(|v| (*v).to_owned()).collect(),
}
fn account(id: &str, subtree: bool) -> Item {
Item::Account {
id: id.to_owned(),
subtree,
fn text(logic: Logic, items: &[Item]) -> Option<String> {
group_sexpr(logic, items).expect("buildable")
#[test]
fn a_tag_row_is_one_leaf_per_entity() {
let row = tag(&[Entity::Transaction], "category", &["food"]);
assert_eq!(
text(Logic::And, &[row]).as_deref(),
Some(r#"(transaction-tag= "category" "food")"#)
);
fn every_entity_has_its_operator() {
for (entity, operator) in [
(Entity::Account, "account-tag="),
(Entity::Transaction, "transaction-tag="),
(Entity::Split, "split-tag="),
] {
let row = tag(&[entity], "n", &["v"]);
let want = format!(r#"({operator} "n" "v")"#);
assert_eq!(text(Logic::And, &[row]).as_deref(), Some(want.as_str()));
fn several_entities_and_values_expand_to_or() {
let row = tag(&[Entity::Account, Entity::Split], "t", &["a", "b"]);
Some(
r#"(or (or (account-tag= "t" "a") (account-tag= "t" "b")) (or (split-tag= "t" "a") (split-tag= "t" "b")))"#
)
fn repeated_entities_build_one_condition_each() {
let row = tag(&[Entity::Split; 3], "t", &["a"]);
Some(r#"(split-tag= "t" "a")"#)
fn accounts_use_equality_or_subtree() {
text(Logic::And, &[account(" u1 ", false)]).as_deref(),
Some(r#"(account= "u1")"#)
text(Logic::And, &[account("u1", true)]).as_deref(),
Some(r#"(account-subtree "u1")"#)
fn groups_combine_their_children() {
let items = [account("a", false), account("b", true)];
text(Logic::And, &items).as_deref(),
Some(r#"(and (account= "a") (account-subtree "b"))"#)
text(Logic::Or, &items).as_deref(),
Some(r#"(or (account= "a") (account-subtree "b"))"#)
fn not_wraps_exactly_one_child() {
text(Logic::Not, &[account("a", false)]).as_deref(),
Some(r#"(not (account= "a"))"#)
let two = [account("a", false), account("b", false)];
group_sexpr(Logic::Not, &two),
Err(Fault {
path: vec![],
reason: Reason::NotArity(2)
})
fn nested_groups_nest() {
let inner = Item::Group {
logic: Logic::Or,
items: vec![account("a", false), account("b", false)],
};
let outer = [Item::Group {
logic: Logic::Not,
items: vec![inner],
}];
text(Logic::And, &outer).as_deref(),
Some(r#"(not (or (account= "a") (account= "b")))"#)
fn an_empty_builder_is_no_filter() {
assert_eq!(text(Logic::And, &[]), None);
assert_eq!(text(Logic::Not, &[]), None);
fn blank_rows_and_empty_groups_are_refused_not_dropped() {
let items = [
tag(&[], "", &[]),
account(" ", true),
Item::Group {
items: vec![],
},
];
for (at, item) in items.into_iter().enumerate() {
assert!(
group_sexpr(Logic::And, std::slice::from_ref(&item)).is_err(),
"{at}"
assert!(item_sexpr(&item).is_err(), "{at}");
fn a_tag_without_entity_is_unbuildable() {
let row = tag(&[], "category", &["food"]);
group_sexpr(Logic::And, &[row]),
path: vec![0],
reason: Reason::TagWithoutEntity
fn a_malformed_child_is_unbuildable_at_any_depth() {
let bad = Item::Group {
let outer = [account("c", false), bad];
group_sexpr(Logic::And, &outer),
path: vec![1],
fn names_and_values_are_escaped() {
let row = tag(&[Entity::Split], r#"a"b"#, &[r"c\d"]);
Some(r#"(split-tag= "a\"b" "c\\d")"#)
fn entities_and_logic_parse_their_wire_names() {
assert_eq!(Entity::parse("split"), Some(Entity::Split));
assert_eq!(Entity::parse("account"), Some(Entity::Account));
assert_eq!(Entity::parse("transaction"), Some(Entity::Transaction));
assert_eq!(Entity::parse("x"), None);
assert_eq!(Logic::parse("not"), Some(Logic::Not));
assert_eq!(Logic::parse("or"), Some(Logic::Or));
assert_eq!(Logic::parse("and"), Some(Logic::And));
assert_eq!(Logic::parse("AND"), None);
fn wire_names_round_trip_through_parse() {
for entity in Entity::ALL {
assert_eq!(Entity::parse(entity.name()), Some(entity));
for logic in [Logic::And, Logic::Or, Logic::Not] {
assert_eq!(Logic::parse(logic.operator()), Some(logic));
fn ids(list: &[&str]) -> Vec<String> {
list.iter().map(|id| (*id).to_owned()).collect()
fn amount(comparison: Comparison, numer: i64, denom: i64) -> Item {
Item::Amount {
comparison,
value: Amount::new(numer, denom).expect("amount"),
fn id_rows_write_their_operators() {
let cases = [
(
Item::AccountIn {
ids: ids(&["a", "b"]),
r#"(account-in "a" "b")"#,
),
(Item::AccountIn { ids: ids(&["a"]) }, r#"(account-in "a")"#),
Item::CounterpartyEq { id: "c".into() },
r#"(counterparty= "c")"#,
Item::CounterpartyIn {
ids: ids(&["c", "d"]),
r#"(counterparty-in "c" "d")"#,
(Item::CommodityEq { id: "m".into() }, r#"(commodity= "m")"#),
Item::CommodityIn { ids: ids(&["m"]) },
r#"(commodity-in "m")"#,
for (row, want) in cases {
assert_eq!(text(Logic::And, &[row]).as_deref(), Some(want));
fn list_ids_are_trimmed_when_written() {
let row = Item::AccountIn {
ids: ids(&[" a ", "b"]),
Some(r#"(account-in "a" "b")"#)
fn amount_rows_write_integers_fractions_and_negatives() {
(amount(Comparison::Gt, 100, 1), "(amount> 100)"),
(amount(Comparison::Lt, 1, 2), "(amount< 1/2)"),
(amount(Comparison::Eq, -3, 4), "(amount= -3/4)"),
(amount(Comparison::Gt, -5, 1), "(amount> -5)"),
(amount(Comparison::Eq, 0, 1), "(amount= 0)"),
fn comparison_operators_are_the_grammar_names() {
assert_eq!(Comparison::Gt.operator(), "amount>");
assert_eq!(Comparison::Lt.operator(), "amount<");
assert_eq!(Comparison::Eq.operator(), "amount=");
fn new_rows_nest_in_groups() {
items: vec![
Item::CommodityEq { id: "m".into() },
amount(Comparison::Lt, 5, 1),
],
let rows = [Item::CounterpartyEq { id: "c".into() }, inner];
text(Logic::Not, &rows[..1]).as_deref(),
Some(r#"(not (counterparty= "c"))"#)
text(Logic::And, &rows).as_deref(),
Some(r#"(and (counterparty= "c") (or (commodity= "m") (amount< 5)))"#)