From b3da48aaa0742d2aceddbdc7f10251f30dd688a0 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 17 Jul 2014 15:23:03 -0700 Subject: [PATCH] identify the commodity in balance assertion errors Now that balance assertions are checking only a single commodity, it can be confusing. Eg say all your amounts are in dollars, an assertion like "= 0" checked the dollar balance in hledger 0.23 but always succeeds in hledger 0.24. When an assertion fails, we now report which commodity was checked to help troubleshooting. --- hledger-lib/Hledger/Data/Journal.hs | 8 ++++---- hledger-lib/Hledger/Read/JournalReader.hs | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 17c0b9ce3..9d5c634dd 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -440,16 +440,16 @@ checkBalanceAssertion (errs,startbal) ps p = last ps assertion = pbalanceassertion p Just assertedbal = dbg2 "assertedbal" assertion + assertedcomm = dbg2 "assertedcomm" $ maybe "" acommodity $ headMay $ amounts assertedbal fullbal = dbg2 "fullbal" $ sum $ [dbg2 "startbal" startbal] ++ map pamount ps - singlebal = dbg2 "singlebal" $ - let c = maybe "" acommodity $ headMay $ amounts assertedbal - in filterMixedAmount (\a -> acommodity a == c) fullbal + singlebal = dbg2 "singlebal" $ filterMixedAmount (\a -> acommodity a == assertedcomm) fullbal bal = singlebal -- check single-commodity balance like Ledger; maybe add == FULLBAL later - err = printf "Balance assertion failed for account %s on %s\n%sAfter posting:\n %s\nexpected commodity balance is %s, calculated balance was %s." + err = printf "Balance assertion failed for account %s on %s\n%sAfter posting:\n %s\nexpected balance in commodity \"%s\" is %s, calculated balance was %s." (paccount p) (show $ postingDate p) (maybe "" (("In transaction:\n"++).show) $ ptransaction p) (show p) + assertedcomm (showMixedAmount assertedbal) (showMixedAmount singlebal) diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 2e919f961..204a7551f 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -513,7 +513,7 @@ postingp = do account <- modifiedaccountname let (ptype, account') = (accountNamePostingType account, unbracket account) amount <- spaceandamountormissing - massertion <- balanceassertion + massertion <- partialbalanceassertion _ <- fixedlotprice many spacenonewline ctx <- getState @@ -720,8 +720,8 @@ priceamount = return $ UnitPrice a)) <|> return NoPrice -balanceassertion :: GenParser Char JournalContext (Maybe MixedAmount) -balanceassertion = +partialbalanceassertion :: GenParser Char JournalContext (Maybe MixedAmount) +partialbalanceassertion = try (do many spacenonewline char '=' @@ -730,6 +730,16 @@ balanceassertion = return $ Just $ Mixed [a]) <|> return Nothing +-- balanceassertion :: GenParser Char JournalContext (Maybe MixedAmount) +-- balanceassertion = +-- try (do +-- many spacenonewline +-- string "==" +-- many spacenonewline +-- a <- amountp -- XXX should restrict to a simple amount +-- return $ Just $ Mixed [a]) +-- <|> return Nothing + -- http://ledger-cli.org/3.0/doc/ledger3.html#Fixing-Lot-Prices fixedlotprice :: GenParser Char JournalContext (Maybe Amount) fixedlotprice =