add: more tests, bugfixes for add's commodity/precision handling; make all tests pass
This commit is contained in:
parent
4e672f1722
commit
ea903ffe6e
@ -92,6 +92,7 @@ getTransaction j opts args defaultDate = do
|
|||||||
hPutStr stderr $ concatMap (\(n,t) -> printf "[%3d%%] %s" (round $ n*100 :: Int) (show t)) $ take 3 historymatches)
|
hPutStr stderr $ concatMap (\(n,t) -> printf "[%3d%%] %s" (round $ n*100 :: Int) (show t)) $ take 3 historymatches)
|
||||||
getpostingsandvalidate
|
getpostingsandvalidate
|
||||||
|
|
||||||
|
-- fragile
|
||||||
-- | Read postings from the command line until . is entered, using any
|
-- | Read postings from the command line until . is entered, using any
|
||||||
-- provided historical postings and the journal context to guess defaults.
|
-- provided historical postings and the journal context to guess defaults.
|
||||||
getPostings :: JournalContext -> (AccountName -> Bool) -> Maybe [Posting] -> [Posting] -> InputT IO [Posting]
|
getPostings :: JournalContext -> (AccountName -> Bool) -> Maybe [Posting] -> [Posting] -> InputT IO [Posting]
|
||||||
@ -111,16 +112,22 @@ getPostings ctx accept historicalps enteredps = do
|
|||||||
| n <= length ps = Just $ ps !! (n-1)
|
| n <= length ps = Just $ ps !! (n-1)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
where Just ps = historicalps'
|
where Just ps = historicalps'
|
||||||
defaultamountstr | isJust bestmatch' = Just $ showMixedAmountWithPrecision maxprecision $ pamount $ fromJust bestmatch'
|
defaultamountstr | isJust bestmatch' = Just historicalamountstr
|
||||||
| n > 1 = Just balancingamountstr
|
| n > 1 = Just balancingamountstr
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
where balancingamountstr = showMixedAmountWithPrecision maxprecision $ negate $ sumMixedAmountsPreservingHighestPrecision $ map pamount enteredrealps
|
where
|
||||||
|
historicalamountstr = showMixedAmountWithPrecision maxprecision $ pamount $ fromJust bestmatch'
|
||||||
|
balancingamountstr = showMixedAmountWithPrecision maxprecision $ negate $ sumMixedAmountsPreservingHighestPrecision $ map pamount enteredrealps
|
||||||
amountstr <- askFor (printf "amount %d" n) defaultamountstr validateamount
|
amountstr <- askFor (printf "amount %d" n) defaultamountstr validateamount
|
||||||
let amount = setMixedAmountPrecision maxprecision $ fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr
|
let amount = fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr
|
||||||
|
amount' = fromparse $ runParser (someamount <|> return missingamt) nullctx "" amountstr
|
||||||
defaultamtused = Just (showMixedAmount amount) == defaultamountstr
|
defaultamtused = Just (showMixedAmount amount) == defaultamountstr
|
||||||
historicalps'' = if defaultamtused then historicalps' else Nothing
|
historicalps'' = if defaultamtused then historicalps' else Nothing
|
||||||
commodityadded | showMixedAmountWithPrecision maxprecision amount == amountstr = Nothing
|
commodityadded | c == cwithnodef = Nothing
|
||||||
| otherwise = maybe Nothing (Just . commodity) $ headMay $ amounts amount
|
| otherwise = c
|
||||||
|
where c = maybemixedamountcommodity amount
|
||||||
|
cwithnodef = maybemixedamountcommodity amount'
|
||||||
|
maybemixedamountcommodity = maybe Nothing (Just . commodity) . headMay . amounts
|
||||||
p = nullposting{paccount=stripbrackets account,
|
p = nullposting{paccount=stripbrackets account,
|
||||||
pamount=amount,
|
pamount=amount,
|
||||||
ptype=postingtype account}
|
ptype=postingtype account}
|
||||||
|
|||||||
@ -2,4 +2,4 @@
|
|||||||
bin/hledger -f nosuch.journal add
|
bin/hledger -f nosuch.journal add
|
||||||
<<<
|
<<<
|
||||||
2009/1/32
|
2009/1/32
|
||||||
>>>2 /date .*: date .*/
|
>>> /date .*: date .*/
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
bin/hledger -f nosuch.journal add
|
bin/hledger -f nosuch.journal add
|
||||||
<<<
|
<<<
|
||||||
|
|
||||||
>>>2 /date .*: description/
|
>>> /date .*: description/
|
||||||
|
|||||||
@ -8,4 +8,4 @@ a
|
|||||||
b
|
b
|
||||||
0.5
|
0.5
|
||||||
c
|
c
|
||||||
>>>2 /amount 3 \[-0.75\]/
|
>>> /amount 3 \[-0.75\]/
|
||||||
|
|||||||
@ -1,15 +1,71 @@
|
|||||||
# add should use the (final) default commodity if any
|
# simple add with no existing journal, no commodity entered
|
||||||
# disabled as add currently requires ctrl-c to terminate
|
rm -f add-default-commodity-$$.j; bin/hledger -f add-default-commodity-$$.j add; rm -f add-default-commodity-$$.j
|
||||||
|
<<<
|
||||||
|
|
||||||
|
|
||||||
|
a
|
||||||
|
1000.0
|
||||||
|
b
|
||||||
|
|
||||||
|
.
|
||||||
|
>>> /^date \[.*\]: description \[\]: account 1: amount 1: account 2: amount 2 \[-1000\]: account 3: date \[.*\]: $/
|
||||||
#
|
#
|
||||||
# printf 'D £1000.00\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add; rm -f add-default-commodity-$$.j
|
# default commodity with greater precision
|
||||||
# <<<
|
printf 'D $1000.00\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j
|
||||||
# 2010/1/1
|
<<<
|
||||||
|
|
||||||
# a
|
|
||||||
# 1000
|
|
||||||
# b
|
|
||||||
# >>>
|
|
||||||
# 2010/01/01 y
|
|
||||||
# a £1000.00
|
|
||||||
# b £-1000.00
|
|
||||||
|
|
||||||
|
a
|
||||||
|
$1000.0
|
||||||
|
b
|
||||||
|
|
||||||
|
.
|
||||||
|
>>> /a +\$1000\.0/
|
||||||
|
#
|
||||||
|
# default commodity with less precision
|
||||||
|
printf 'D $1000.0\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j
|
||||||
|
<<<
|
||||||
|
|
||||||
|
|
||||||
|
a
|
||||||
|
$1000.00
|
||||||
|
b
|
||||||
|
|
||||||
|
.
|
||||||
|
>>> /a +\$1000\.00/
|
||||||
|
#
|
||||||
|
# existing commodity with greater precision
|
||||||
|
printf '2010/1/1\n a $1000.00\n b\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j
|
||||||
|
<<<
|
||||||
|
|
||||||
|
|
||||||
|
a
|
||||||
|
$1000.0
|
||||||
|
b
|
||||||
|
|
||||||
|
.
|
||||||
|
>>> /a +\$1000\.0/
|
||||||
|
#
|
||||||
|
# existing commodity with less precision
|
||||||
|
printf '2010/1/1\n a $1000.0\n b\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j
|
||||||
|
<<<
|
||||||
|
|
||||||
|
|
||||||
|
a
|
||||||
|
$1000.00
|
||||||
|
b
|
||||||
|
|
||||||
|
.
|
||||||
|
>>> /a +\$1000\.00/
|
||||||
|
#
|
||||||
|
# no commodity entered, the (most recent) default commodity should be applied
|
||||||
|
printf 'D $1000.0\nD £1,000.00\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j
|
||||||
|
<<<
|
||||||
|
2010/1/1
|
||||||
|
|
||||||
|
a
|
||||||
|
1000
|
||||||
|
b
|
||||||
|
|
||||||
|
.
|
||||||
|
>>> /a +£1,000.00/
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user