Skip to content

Commit

Permalink
fix balance accumulation across assertions (#195)
Browse files Browse the repository at this point in the history
A sequence of balance assertions asserting first one commodity, then
another, then the first again, was not working.
  • Loading branch information
simonmichael committed Jan 11, 2015
1 parent 5a4c881 commit 6a4f288
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 4 additions & 5 deletions hledger-lib/Hledger/Data/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,9 @@ checkBalanceAssertion :: ([String],MixedAmount) -> [Posting] -> ([String],MixedA
checkBalanceAssertion (errs,startbal) ps
| null ps = (errs,startbal)
| isNothing assertion = (errs,startbal)
|
-- bal' /= assertedbal -- MixedAmount's Eq instance currently gets confused by different precisions
not $ isReallyZeroMixedAmount (bal - assertedbal) = (errs++[err], bal)
| otherwise = (errs,bal)
| -- bal' /= assertedbal -- MixedAmount's Eq instance currently gets confused by different precisions
not $ isReallyZeroMixedAmount (bal - assertedbal) = (errs++[err], fullbal)
| otherwise = (errs,fullbal)
where
p = last ps
assertion = pbalanceassertion p
Expand All @@ -473,7 +472,7 @@ checkBalanceAssertion (errs,startbal) ps
-- assertion are discarded.
splitAssertions :: [Posting] -> [[Posting]]
splitAssertions ps
| null rest = [[]]
| null rest = []
| otherwise = (ps'++[head rest]):splitAssertions (tail rest)
where
(ps',rest) = break (isJust . pbalanceassertion) ps
Expand Down
15 changes: 14 additions & 1 deletion tests/journal/balance-assertions.test
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,20 @@ hledgerdev -f - stats
>>>2
>>>=0

# 7. what should happen here ? Currently,
# 7. balances should accumulate (#195)
hledgerdev -f - stats
<<<
1/1
(a) 1F = 1F
(a) 2G = 2G

1/2
(a) 3F = 4F
>>> !/assertion failed/
>>>2
>>>=0

# 8. what should happen here ? Currently,
# in a, 3.4 EUR @@ $5.6 and -3.4 EUR cancel out (wrong ?)
# in b,
#
Expand Down

0 comments on commit 6a4f288

Please sign in to comment.