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.
This commit is contained in:
Simon Michael 2014-07-17 15:23:03 -07:00
parent 528913d1bd
commit b3da48aaa0
2 changed files with 17 additions and 7 deletions

View File

@ -440,16 +440,16 @@ checkBalanceAssertion (errs,startbal) ps
p = last ps p = last ps
assertion = pbalanceassertion p assertion = pbalanceassertion p
Just assertedbal = dbg2 "assertedbal" assertion Just assertedbal = dbg2 "assertedbal" assertion
assertedcomm = dbg2 "assertedcomm" $ maybe "" acommodity $ headMay $ amounts assertedbal
fullbal = dbg2 "fullbal" $ sum $ [dbg2 "startbal" startbal] ++ map pamount ps fullbal = dbg2 "fullbal" $ sum $ [dbg2 "startbal" startbal] ++ map pamount ps
singlebal = dbg2 "singlebal" $ singlebal = dbg2 "singlebal" $ filterMixedAmount (\a -> acommodity a == assertedcomm) fullbal
let c = maybe "" acommodity $ headMay $ amounts assertedbal
in filterMixedAmount (\a -> acommodity a == c) fullbal
bal = singlebal -- check single-commodity balance like Ledger; maybe add == FULLBAL later 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) (paccount p)
(show $ postingDate p) (show $ postingDate p)
(maybe "" (("In transaction:\n"++).show) $ ptransaction p) (maybe "" (("In transaction:\n"++).show) $ ptransaction p)
(show p) (show p)
assertedcomm
(showMixedAmount assertedbal) (showMixedAmount assertedbal)
(showMixedAmount singlebal) (showMixedAmount singlebal)

View File

@ -513,7 +513,7 @@ postingp = do
account <- modifiedaccountname account <- modifiedaccountname
let (ptype, account') = (accountNamePostingType account, unbracket account) let (ptype, account') = (accountNamePostingType account, unbracket account)
amount <- spaceandamountormissing amount <- spaceandamountormissing
massertion <- balanceassertion massertion <- partialbalanceassertion
_ <- fixedlotprice _ <- fixedlotprice
many spacenonewline many spacenonewline
ctx <- getState ctx <- getState
@ -720,8 +720,8 @@ priceamount =
return $ UnitPrice a)) return $ UnitPrice a))
<|> return NoPrice <|> return NoPrice
balanceassertion :: GenParser Char JournalContext (Maybe MixedAmount) partialbalanceassertion :: GenParser Char JournalContext (Maybe MixedAmount)
balanceassertion = partialbalanceassertion =
try (do try (do
many spacenonewline many spacenonewline
char '=' char '='
@ -730,6 +730,16 @@ balanceassertion =
return $ Just $ Mixed [a]) return $ Just $ Mixed [a])
<|> return Nothing <|> 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 -- http://ledger-cli.org/3.0/doc/ledger3.html#Fixing-Lot-Prices
fixedlotprice :: GenParser Char JournalContext (Maybe Amount) fixedlotprice :: GenParser Char JournalContext (Maybe Amount)
fixedlotprice = fixedlotprice =