journal: make numbers parsing more strict

- Hunt down adjacent punctuations with altering char.
- Add some tests dedicated to parsing amounts.
This commit is contained in:
Mykola Orliuk 2017-01-18 14:06:46 +02:00 committed by Simon Michael
parent 53ad035b24
commit bcf7a1add5
2 changed files with 113 additions and 1 deletions

View File

@ -531,7 +531,7 @@ numberp = do
-- interspersed with periods, commas, or both -- interspersed with periods, commas, or both
-- ptrace "numberp" -- ptrace "numberp"
sign <- signp sign <- signp
parts <- some $ choice' [some digitChar, some $ char ',', some $ char '.'] parts <- some $ choice' [some digitChar, some $ oneOf ['.', ',']]
dbg8 "numberp parsed" (sign,parts) `seq` return () dbg8 "numberp parsed" (sign,parts) `seq` return ()
-- check the number is well-formed and identify the decimal point and digit -- check the number is well-formed and identify the decimal point and digit

112
tests/journal/numbers.test Normal file
View File

@ -0,0 +1,112 @@
# Simple case
hledger bal -f - --no-total
<<<
2017/1/1
a 1,000.00 EUR
b -1,000.00 EUR
>>>
1,000.00 EUR a
-1,000.00 EUR b
>>>2
>>>=0
# No digits before decimal sep
hledger bal -f - --no-total
<<<
2017/1/1
a .01 EUR
b
>>>
0.01 EUR a
-0.01 EUR b
>>>2
>>>=0
# No digits after decimal sep
hledger bal -f - --no-total
<<<
2017/1/1
a 1. EUR
b
>>>
1 EUR a
-1 EUR b
>>>2
>>>=0
# No digits at all
hledger bal -f -
<<<
2017/1/1
a . EUR
b
>>>
>>>=1
# Omitted decimals
hledger bal -f -
<<<
2017/1/1
a 1,000 EUR
b -1,000.00 EUR
>>>
>>>=1
# Big prices
hledger bal -f - --no-total
<<<
2017/1/1
a -1 BTC @ 24,840 UAH
b 24,840.00 UAH
>>>
>>>=1
# adjacent punctuation chars
hledger bal -f -
<<<
2017/1/1
a 1,,0,,0.0 EUR
b -1..0..0,0 EUR
>>>
>>>=1
# adjacent punctuation chars of different types
hledger bal -f -
<<<
2017/1/1
a 1,0,.0 EUR
b -1.0.,0 EUR
>>>
>>>=1
# separator chars vary
hledger bal -f -
<<<
2017/1/1
a 1.0,0.0,0 EUR
b -1,0.0,0.0 EUR
>>>
>>>=1
# number begins with a decimal char
hledger bal -f -
<<<
2017/1/1
a .1 EUR
b -.1 EUR
>>>
0.1 EUR a
-0.1 EUR b
--------------------
0
>>>2
>>>=0
# number begins with a separator char
hledger bal -f -
<<<
2017/1/1
a ,100.0 EUR
b -,100.0 EUR
>>>
>>>=1