add: more tests, bugfixes for add's commodity/precision handling; make all tests pass

This commit is contained in:
Simon Michael 2010-11-16 15:08:06 +00:00
parent 4e672f1722
commit ea903ffe6e
5 changed files with 83 additions and 20 deletions

View File

@ -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)
getpostingsandvalidate
-- fragile
-- | Read postings from the command line until . is entered, using any
-- provided historical postings and the journal context to guess defaults.
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)
| otherwise = Nothing
where Just ps = historicalps'
defaultamountstr | isJust bestmatch' = Just $ showMixedAmountWithPrecision maxprecision $ pamount $ fromJust bestmatch'
defaultamountstr | isJust bestmatch' = Just historicalamountstr
| n > 1 = Just balancingamountstr
| 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
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
historicalps'' = if defaultamtused then historicalps' else Nothing
commodityadded | showMixedAmountWithPrecision maxprecision amount == amountstr = Nothing
| otherwise = maybe Nothing (Just . commodity) $ headMay $ amounts amount
commodityadded | c == cwithnodef = Nothing
| otherwise = c
where c = maybemixedamountcommodity amount
cwithnodef = maybemixedamountcommodity amount'
maybemixedamountcommodity = maybe Nothing (Just . commodity) . headMay . amounts
p = nullposting{paccount=stripbrackets account,
pamount=amount,
ptype=postingtype account}

View File

@ -2,4 +2,4 @@
bin/hledger -f nosuch.journal add
<<<
2009/1/32
>>>2 /date .*: date .*/
>>> /date .*: date .*/

View File

@ -1,4 +1,4 @@
bin/hledger -f nosuch.journal add
<<<
>>>2 /date .*: description/
>>> /date .*: description/

View File

@ -8,4 +8,4 @@ a
b
0.5
c
>>>2 /amount 3 \[-0.75\]/
>>> /amount 3 \[-0.75\]/

View File

@ -1,15 +1,71 @@
# add should use the (final) default commodity if any
# disabled as add currently requires ctrl-c to terminate
# simple add with no existing journal, no commodity entered
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
# <<<
# 2010/1/1
# 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
<<<
# 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/