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
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)

View File

@ -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 =