From 257ce573830e4bdcf8131f77e517e5636c77f39c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 10 Jan 2015 22:15:21 -0800 Subject: [PATCH] fix balance accumulation across assertions (#195) A sequence of balance assertions asserting first one commodity, then another, then the first again, was not working. --- hledger-lib/Hledger/Data/Journal.hs | 9 ++++----- tests/journal/balance-assertions.test | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 9948bc154..27088deb6 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -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 @@ -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 diff --git a/tests/journal/balance-assertions.test b/tests/journal/balance-assertions.test index 03aba61ae..7007b6d88 100755 --- a/tests/journal/balance-assertions.test +++ b/tests/journal/balance-assertions.test @@ -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, #