New: Assertion directives

159 views
Skip to first unread message

John Wiegley

unread,
Jun 13, 2010, 12:50:08 AM6/13/10
to ledge...@googlegroups.com
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
; matched posting.
= /Food/
assert account("Expenses:Food").total >= $100

2010-06-12 Sample
Expenses:Food $100
Assets:Checking

; At file scope, the expression is evaluated within "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 first 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

John

Jeroen De Vlieger

unread,
Oct 9, 2012, 6:03:26 AM10/9/12
to ledge...@googlegroups.com
this doesn't seem to be documented in the 3.0 documentation, is that correct?
I was looking for exactly this in it and failed to find the anser I needed till I came across this post.

Chapter 11 on value expressions is also a bit chaotic and unclear IMHO.
I read it twice and I still can't make head nor tails of it.

with kind regards,

Jeroen

Jeroen De Vlieger

unread,
Oct 9, 2012, 6:27:43 AM10/9/12
to ledge...@googlegroups.com
hmm though I got it to work, but it doesn't  :-s

the following works

2012/10/09 t
   a  € 2
   b

2012/10/09 t
  a  € 4
  b

2012/10/09 t
  c  € 5
  a

2012/10/09 t
  a:1  € 3
  z

check account("a").total == € 4
check account("a:1").total == € 3


But adding an intermediate check breaks the whole thing.
Why?

2012/10/09 t
   a  € 2
   b

2012/10/09 t
  a  € 4
  b

2012/10/09 t
  c  € 5
  a

check account("a").total == € 1

2012/10/09 t
  a:1  € 3
  z

check account("a").total == € 4
check account("a:1").total == € 3

Jeroen




Jeroen De Vlieger

unread,
Oct 9, 2012, 6:32:02 AM10/9/12
to ledge...@googlegroups.com
The following does seems to work


2012/10/09 t
   a  € 2
   b

2012/10/09 t
  a  € 4
  b

2012/10/09 t
  c  € 5
  a

2012/10/09 assertion
  a  € 0 = € 1


2012/10/09 t
  a:1  € 3
  z

2012/10/09 assertion
  a  € 0 = € 1
  a:1  € 0 = € 3


but it feels like a hack since the assert and check statements are, I think, designed for such tasks.

comments? Idea?

with kind regard


Jeroen

On Sunday, June 13, 2010 6:50:08 AM UTC+2, John Wiegley wrote:

Craig Earls

unread,
Oct 9, 2012, 9:24:56 AM10/9/12
to ledge...@googlegroups.com
CHapter 11 reflects my very rudimentary understanding of the value expression language.  I agree that it is the weakest section by far.


On Tue, Oct 9, 2012 at 3:03 AM, Jeroen De Vlieger <fly...@gmail.com> wrote:
this doesn't seem to be documented in the 3.0 documentation, is that correct?
I was looking for exactly this in it and failed to find the anser I needed till I came across this post.

Chapter 11 on value expressions is also a bit chaotic and unclear IMHO.
I read it twice and I still can't make head nor tails of it.


--
Craig, Corona De Tucson, AZ
enderw88.wordpress.com

Alexis

unread,
Nov 19, 2012, 5:59:50 PM11/19/12
to ledge...@googlegroups.com
Hi,
I came across the same problem and got it partially working by
using amount instead of total:

% cat test.dat
2012/10/09 t
a € 2
b

2012/10/09 t
a € 4
b

2012/10/09 t
c € 5
a

check account("a").amount == € 1

2012/10/09 t
a:1 € 3
z

check account("a").amount == € 4
check account("a:1").amount == € 3

% ledger -f test.dat bal
Warning: "test.dat", line 19: Check failed: account("a").amount == € 4
€ 4 a
€ 3 1
€ -6 b
€ 5 c
€ -3 z
--------------------
0

What I find furthermore interesting is how the account's
total() and amount() develop during a register report.
Is this behaviour intended? Is it correct?
Or could this be a bug?

%ledger -f test.dat reg --total 'account("a").total()'
09.10.2012 t a € 2 € 2
b € -2 € 2
09.10.2012 t a € 4 € 2 ; I'd expect € 6 here
b € -4 € 2
09.10.2012 t c € 5 € 2
a € -5 € -3 ; I'd expect € 1 here
09.10.2012 t a:1 € 3 € -3
z € -3 € -3

%ledger -f test.dat reg --total 'account("a").amount()'
09.10.2012 t a € 2 € 2
b € -2 € 2
09.10.2012 t a € 4 € 2
b € -4 € 2
09.10.2012 t c € 5 € 2
a € -5 € 2
09.10.2012 t a:1 € 3 € 2
z

Alexis

Fjan

unread,
Jul 1, 2015, 2:43:27 PM7/1/15
to ledge...@googlegroups.com

The behaviour of the total() function confused me a lot, it has non idempotent side effects. Posting this here in case someone else comes to this thread looking for info. I can't claim to fully understand what's going on, but an assert in another ledger file that is parsed before it can alter the result of the total() function, in practice eval print( 'account("a").total') will show you the totals are summed but only if they appear in different scopes. So I think it's safe to claim:

- You should have at most one assert on total in your ledger files
 assert  'account("a").total' == 10

- You probably instead want to us amount, which does not have side effects
 assert  'account("a").amount' == 10

If anyone else has a better understanding of what is going on, and why multiple asserts can influence each other I would be very interested to know.
-j
Reply all
Reply to author
Forward
0 new messages