From 6ef755c1330c6e11476ea2e63b8388ad29efb4ef Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 12 Jun 2010 21:43:03 -0400 Subject: Added support for assert, check and expr directives These can occur in many places: ; Within an automated transaction, the assert is evaluated every time ; a posting is matched, with the expression context set to the ; matching posting. = /Food/ assert account("Expenses:Food").total >= $100 2010-06-12 Sample Expenses:Food $100 Assets:Checking ; At file scope, the expression is evaluated with "global" scope. assert account("Expenses:Food").total == $100 ; At the top of a transction, the assertion's scope is the ; transaction. After a posting, the scope is that posting. Note ; however that account totals are only adjusted after successful ; parsing of a transaction, which means that all the assertions below ; are true, even though it appears as though the middle posting should ; affect the total immediately (which is not the case). 2010-06-12 Sample 2 assert account("Expenses:Food").total == $100 Expenses:Food $50 assert account("Expenses:Food").total == $100 Assets:Checking assert account("Expenses:Food").total == $100 --- src/xact.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/xact.cc') diff --git a/src/xact.cc b/src/xact.cc index eeb487d9..0bf1fc2c 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -691,6 +691,21 @@ void auto_xact_t::extend_xact(xact_base_t& xact, current_year); } } + if (check_exprs) { + foreach (check_expr_pair& pair, *check_exprs) { + if (pair.second == auto_xact_t::EXPR_GENERAL) { + pair.first.calc(bound_scope); + } + else if (! pair.first.calc(bound_scope).to_boolean()) { + if (pair.second == auto_xact_t::EXPR_ASSERTION) { + throw_(parse_error, + _("Transaction assertion failed: %1" << pair.first)); + } else { + warning_(_("Transaction check failed: %1" << pair.first)); + } + } + } + } foreach (post_t * post, posts) { amount_t post_amount; -- cgit v1.2.3