Fix parsing of blank/empty balances + testcases (#539)

This commit is contained in:
Dmitry Astapov 2017-04-18 17:07:30 +01:00 committed by Simon Michael
parent 451f9d7307
commit c4c4be5fad
2 changed files with 29 additions and 3 deletions

View File

@ -664,9 +664,10 @@ transactionFromCsvRecord sourcepos rules record = t
_ -> "expenses:unknown"
account1 = T.pack $ maybe "" render (mfieldtemplate "account1") `or` defaccount1
account2 = T.pack $ maybe "" render (mfieldtemplate "account2") `or` defaccount2
balance = maybe Nothing parsebalance $ mfieldtemplate "balance"
parsebalance "" = Nothing
parsebalance str = Just $ either (balanceerror str) id $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack $ (currency++) $ negateIfParenthesised $ render str
balance = maybe Nothing (parsebalance.render) $ mfieldtemplate "balance"
parsebalance str
| all isSpace str = Nothing
| otherwise = Just $ either (balanceerror str) id $ runParser (evalStateT (amountp <* eof) mempty) "" $ T.pack $ (currency++) $ negateIfParenthesised str
balanceerror str err = error' $ unlines
["error: could not parse \""++str++"\" as balance amount"
,showRecord record

View File

@ -67,4 +67,29 @@
>>>2 /using conversion rules file.*t.rules/
>>>=0
# 7. read CSV with blank balance field
rm -rf t.rules$$; printf 'fields date, description, amount, balance\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\n' >t.rules$$; echo '10/2009/09,Flubber Co,50,123\n11/2009/09,Blubber Co,60,' | hledger -f- print --rules-file t.rules$$; rm -rf t.rules$$
>>>
2009/09/10 Flubber Co
income:unknown $-50
assets:myacct $50 = $123
2009/09/11 Blubber Co
income:unknown $-60
assets:myacct $60
>>>2 /using conversion rules file.*t.rules/
>>>=0
# 8. read CSV with empty balance field
rm -rf t.rules$$; printf 'fields date, description, amount, balance\ndate-format %%d/%%Y/%%m\ncurrency $\naccount1 assets:myacct\n' >t.rules$$; echo '10/2009/09,Flubber Co,50,123\n11/2009/09,Blubber Co,60, ' | hledger -f- print --rules-file t.rules$$; rm -rf t.rules$$
>>>
2009/09/10 Flubber Co
income:unknown $-50
assets:myacct $50 = $123
2009/09/11 Blubber Co
income:unknown $-60
assets:myacct $60
>>>2 /using conversion rules file.*t.rules/
>>>=0