diff --git a/Tests.hs b/Tests.hs index e14cbd358..f5fe3ef4e 100644 --- a/Tests.hs +++ b/Tests.hs @@ -656,78 +656,6 @@ tests = TestList [ ,"showMixedAmount" ~: do showMixedAmount (Mixed []) ~?= "0" - ,"showTransaction" ~: do - assertEqual "show a balanced transaction, eliding last amount" - (unlines - ["2007/01/28 coopportunity" - ," expenses:food:groceries $47.18" - ," assets:checking" - ,"" - ]) - (let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" - [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting (Just t) - ,Posting False "assets:checking" (Mixed [dollars (-47.18)]) "" RegularPosting (Just t) - ] "" - in showTransaction t) - assertEqual "show a balanced transaction, no eliding" - (unlines - ["2007/01/28 coopportunity" - ," expenses:food:groceries $47.18" - ," assets:checking $-47.18" - ,"" - ]) - (let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" - [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting (Just t) - ,Posting False "assets:checking" (Mixed [dollars (-47.18)]) "" RegularPosting (Just t) - ] "" - in showTransactionUnelided t) - -- document some cases that arise in debug/testing: - assertEqual "show an unbalanced transaction, should not elide" - (unlines - ["2007/01/28 coopportunity" - ," expenses:food:groceries $47.18" - ," assets:checking $-47.19" - ,"" - ]) - (showTransaction - (txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" - [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting Nothing - ,Posting False "assets:checking" (Mixed [dollars (-47.19)]) "" RegularPosting Nothing - ] "")) - assertEqual "show an unbalanced transaction with one posting, should not elide" - (unlines - ["2007/01/28 coopportunity" - ," expenses:food:groceries $47.18" - ,"" - ]) - (showTransaction - (txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" - [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting Nothing - ] "")) - assertEqual "show a transaction with one posting and a missing amount" - (unlines - ["2007/01/28 coopportunity" - ," expenses:food:groceries " - ,"" - ]) - (showTransaction - (txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" - [Posting False "expenses:food:groceries" missingamt "" RegularPosting Nothing - ] "")) - - assertEqual "show a transaction with a priced commodityless amount" - (unlines - ["2010/01/01 x" - ," a 1 @ $2" - ," b " - ,"" - ]) - (showTransaction - (txnTieKnot $ Transaction (parsedate "2010/01/01") Nothing False "" "x" "" - [Posting False "a" (Mixed [Amount unknown 1 (Just $ Mixed [Amount dollar{precision=0} 2 Nothing])]) "" RegularPosting Nothing - ,Posting False "b" missingamt "" RegularPosting Nothing - ] "")) - ,"someamount" ~: do let -- | compare a parse result with a MixedAmount, showing the debug representation for clarity assertMixedAmountParse parseresult mixedamount = diff --git a/hledger-lib/Ledger.hs b/hledger-lib/Ledger.hs index f4ac29564..5bb136285 100644 --- a/hledger-lib/Ledger.hs +++ b/hledger-lib/Ledger.hs @@ -47,7 +47,7 @@ tests_Ledger = TestList -- ,Ledger.Commodity.tests_Commodity Ledger.Dates.tests_Dates -- ,Ledger.IO.tests_IO - -- ,Ledger.Transaction.tests_Transaction + ,Ledger.Transaction.tests_Transaction -- ,Ledger.Ledger.tests_Ledger -- ,Ledger.Parse.tests_Parse -- ,Ledger.Journal.tests_Journal diff --git a/hledger-lib/Ledger/Transaction.hs b/hledger-lib/Ledger/Transaction.hs index 0258357c9..f366a3cfd 100644 --- a/hledger-lib/Ledger/Transaction.hs +++ b/hledger-lib/Ledger/Transaction.hs @@ -12,6 +12,7 @@ import Ledger.Types import Ledger.Dates import Ledger.Posting import Ledger.Amount +import Ledger.Commodity (dollars, dollar, unknown) instance Show Transaction where show = showTransactionUnelided @@ -140,3 +141,86 @@ txnTieKnot t@Transaction{tpostings=ps} = t{tpostings=map (settxn t) ps} settxn :: Transaction -> Posting -> Posting settxn t p = p{ptransaction=Just t} +tests_Transaction = TestList [ + "showTransaction" ~: do + assertEqual "show a balanced transaction, eliding last amount" + (unlines + ["2007/01/28 coopportunity" + ," expenses:food:groceries $47.18" + ," assets:checking" + ,"" + ]) + (let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" + [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting (Just t) + ,Posting False "assets:checking" (Mixed [dollars (-47.18)]) "" RegularPosting (Just t) + ] "" + in showTransaction t) + + ,"showTransaction" ~: do + assertEqual "show a balanced transaction, no eliding" + (unlines + ["2007/01/28 coopportunity" + ," expenses:food:groceries $47.18" + ," assets:checking $-47.18" + ,"" + ]) + (let t = Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" + [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting (Just t) + ,Posting False "assets:checking" (Mixed [dollars (-47.18)]) "" RegularPosting (Just t) + ] "" + in showTransactionUnelided t) + + -- document some cases that arise in debug/testing: + ,"showTransaction" ~: do + assertEqual "show an unbalanced transaction, should not elide" + (unlines + ["2007/01/28 coopportunity" + ," expenses:food:groceries $47.18" + ," assets:checking $-47.19" + ,"" + ]) + (showTransaction + (txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" + [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting Nothing + ,Posting False "assets:checking" (Mixed [dollars (-47.19)]) "" RegularPosting Nothing + ] "")) + + ,"showTransaction" ~: do + assertEqual "show an unbalanced transaction with one posting, should not elide" + (unlines + ["2007/01/28 coopportunity" + ," expenses:food:groceries $47.18" + ,"" + ]) + (showTransaction + (txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" + [Posting False "expenses:food:groceries" (Mixed [dollars 47.18]) "" RegularPosting Nothing + ] "")) + + ,"showTransaction" ~: do + assertEqual "show a transaction with one posting and a missing amount" + (unlines + ["2007/01/28 coopportunity" + ," expenses:food:groceries " + ,"" + ]) + (showTransaction + (txnTieKnot $ Transaction (parsedate "2007/01/28") Nothing False "" "coopportunity" "" + [Posting False "expenses:food:groceries" missingamt "" RegularPosting Nothing + ] "")) + + ,"showTransaction" ~: do + assertEqual "show a transaction with a priced commodityless amount" + (unlines + ["2010/01/01 x" + ," a 1 @ $2" + ," b " + ,"" + ]) + (showTransaction + (txnTieKnot $ Transaction (parsedate "2010/01/01") Nothing False "" "x" "" + [Posting False "a" (Mixed [Amount unknown 1 (Just $ Mixed [Amount dollar{precision=0} 2 Nothing])]) "" RegularPosting Nothing + ,Posting False "b" missingamt "" RegularPosting Nothing + ] "")) + + ]