From 32d9365fc2fec0a946f401e7d7e5037b7b8dc866 Mon Sep 17 00:00:00 2001 From: Nikolay Orlyuk Date: Wed, 11 Jan 2017 06:44:10 +0200 Subject: [PATCH] Ensure showTransaction produce a valid journal (#466) - Make output of print to be a valid journal - Partially reverts 419f5f2a2 Fixes simonmichael/hledger#465 --- bin/hledger-equity.hs | 4 ++-- hledger-lib/Hledger/Data/Transaction.hs | 26 ++++++++++++++----------- tests/misc/amount-rendering.test | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/bin/hledger-equity.hs b/bin/hledger-equity.hs index 9ad0835f2..674ba42e5 100755 --- a/bin/hledger-equity.hs +++ b/bin/hledger-equity.hs @@ -71,10 +71,10 @@ main = do q = queryFromOpts today ropts_ (acctbals,_) = balanceReport ropts_ q j balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals - ps = [posting{paccount=a, pamount=Mixed [b]} | (a,_,_,Mixed bs) <- acctbals, b <- bs] + ps = [posting{paccount=a, pamount=b} | (a,_,_,b) <- acctbals] ++ [posting{paccount="equity:opening balances", pamount=balancingamt}] enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q - nps = [posting{paccount=a, pamount=negate (Mixed [b])} | (a,_,_,Mixed bs) <- acctbals, b <- bs] + nps = [posting{paccount=a, pamount=negate b} | (a,_,_,b) <- acctbals] ++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}] putStr $ showTransaction (nulltransaction{tdate=addDays (-1) enddate, tdescription="closing balances", tpostings=nps}) putStr $ showTransaction (nulltransaction{tdate=enddate, tdescription="opening balances", tpostings=ps}) diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 2fedaa627..9e95c9dba 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -132,7 +132,8 @@ tests_showTransactionUnelided = [ `gives` unlines [ "2012/05/14=2012/05/15 (code) desc ; tcomment1", " ; tcomment2", - " $1.00", + " * a $1.00", + " ; pcomment2", " * a 2.00h", " ; pcomment2", "" @@ -186,11 +187,12 @@ postingsAsLines elide onelineamounts t ps | otherwise = concatMap (postingAsLines False onelineamounts ps) ps postingAsLines :: Bool -> Bool -> [Posting] -> Posting -> [String] -postingAsLines elideamount onelineamounts ps p = - postinglines +postingAsLines elideamount onelineamounts ps p = concat [ + postingblock ++ newlinecomments + | postingblock <- postingblocks] where - postinglines = map rstrip $ lines $ concatTopPadded [account, " ", amount, assertion, samelinecomment] + postingblocks = [map rstrip $ lines $ concatTopPadded [account, " ", amount, assertion, samelinecomment] | amount <- shownAmounts] assertion = maybe "" ((" = " ++) . showAmount) $ pbalanceassertion p account = indent $ @@ -200,10 +202,10 @@ postingAsLines elideamount onelineamounts ps p = acctwidth = maximum $ map (textWidth . paccount) ps -- currently prices are considered part of the amount string when right-aligning amounts - amount - | elideamount = "" - | onelineamounts = fitString (Just amtwidth) Nothing False False $ showMixedAmountOneLine $ pamount p - | otherwise = fitStringMulti (Just amtwidth) Nothing False False $ showMixedAmount $ pamount p + shownAmounts + | elideamount = [""] + | onelineamounts = [fitString (Just amtwidth) Nothing False False $ showMixedAmountOneLine $ pamount p] + | otherwise = map (fitStringMulti (Just amtwidth) Nothing False False . showAmount ) . amounts $ pamount p where amtwidth = maximum $ 12 : map (strWidth . showMixedAmount . pamount) ps -- min. 12 for backwards compatibility @@ -222,8 +224,8 @@ showPostingLine p = tests_postingAsLines = [ "postingAsLines" ~: do - let p `gives` ls = assertEqual "" ls (postingAsLines False False [p] p) - posting `gives` [" 0"] + let p `gives` ls = assertEqual (show p) ls (postingAsLines False False [p] p) + posting `gives` [] posting{ pstatus=Cleared, paccount="a", @@ -233,7 +235,9 @@ tests_postingAsLines = [ ptags=[("ptag1","val1"),("ptag2","val2")] } `gives` [ - " $1.00", + " * a $1.00 ; pcomment1", + " ; pcomment2", + " ; tag3: val3 ", " * a 2.00h ; pcomment1", " ; pcomment2", " ; tag3: val3 " diff --git a/tests/misc/amount-rendering.test b/tests/misc/amount-rendering.test index 80a76492c..b5b06d89c 100644 --- a/tests/misc/amount-rendering.test +++ b/tests/misc/amount-rendering.test @@ -10,7 +10,7 @@ hledger -f - print 2010/01/01 a EUR 1 ; a euro b USD 1 ; a dollar - EUR -1 + c EUR -1 ; a euro and a dollar c USD -1 ; a euro and a dollar >>>=0