From 1e3291af4885e33c84d03222363b79442de93054 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 15 Oct 2008 06:32:52 +0000 Subject: [PATCH] focus on tests in Tests.hs, clean up --- Ledger/Account.hs | 3 - Ledger/AccountName.hs | 3 - Ledger/Amount.hs | 3 - Ledger/Commodity.hs | 3 - Ledger/Entry.hs | 3 - Ledger/Ledger.hs | 3 - Ledger/Parse.hs | 3 - Ledger/RawLedger.hs | 3 - Ledger/RawTransaction.hs | 3 - Ledger/TimeLog.hs | 3 - Ledger/Transaction.hs | 3 - PrintCommand.hs | 3 - RegisterCommand.hs | 3 - Tests.hs | 140 ++++++++++++++++++--------------------- 14 files changed, 63 insertions(+), 116 deletions(-) diff --git a/Ledger/Account.hs b/Ledger/Account.hs index b36a826d9..113af60a7 100644 --- a/Ledger/Account.hs +++ b/Ledger/Account.hs @@ -13,9 +13,6 @@ import Ledger.Types import Ledger.Amount -accounttests = TestList [ - ] - instance Show Account where show (Account a ts b) = printf "Account %s with %d txns and %s balance" a (length ts) (show b) diff --git a/Ledger/AccountName.hs b/Ledger/AccountName.hs index 551186591..b62cf9fd0 100644 --- a/Ledger/AccountName.hs +++ b/Ledger/AccountName.hs @@ -11,9 +11,6 @@ import Ledger.Utils import Ledger.Types -accountnametests = TestList [ - ] - sepchar = ':' accountNameComponents :: AccountName -> [String] diff --git a/Ledger/Amount.hs b/Ledger/Amount.hs index 0d447199f..96682a0ee 100644 --- a/Ledger/Amount.hs +++ b/Ledger/Amount.hs @@ -43,9 +43,6 @@ import Ledger.Types import Ledger.Commodity -amounttests = TestList [ - ] - instance Show Amount where show = showAmount -- | Get the string representation of an amount, based on its commodity's diff --git a/Ledger/Commodity.hs b/Ledger/Commodity.hs index 44f45eda6..67fbc3ddd 100644 --- a/Ledger/Commodity.hs +++ b/Ledger/Commodity.hs @@ -13,9 +13,6 @@ import Ledger.Utils import Ledger.Types -commoditytests = TestList [ - ] - -- for nullamt, autoamt, etc. unknown = Commodity {symbol="",side=L,spaced=False,comma=False,precision=0,rate=1} diff --git a/Ledger/Entry.hs b/Ledger/Entry.hs index e699c2f82..92314a211 100644 --- a/Ledger/Entry.hs +++ b/Ledger/Entry.hs @@ -13,9 +13,6 @@ import Ledger.RawTransaction import Ledger.Amount -entrytests = TestList [ - ] - instance Show Entry where show = showEntryDescription {- diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index 08856da7f..07bb95828 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -19,9 +19,6 @@ import Ledger.RawLedger import Ledger.Entry -ledgertests = TestList [ - ] - instance Show Ledger where show l = printf "Ledger with %d entries, %d accounts\n%s" ((length $ entries $ rawledger l) + diff --git a/Ledger/Parse.hs b/Ledger/Parse.hs index f1ca305dc..68582015f 100644 --- a/Ledger/Parse.hs +++ b/Ledger/Parse.hs @@ -21,9 +21,6 @@ import Ledger.Commodity import Ledger.TimeLog -parsertests = TestList [ - ] - -- utils parseLedgerFile :: String -> IO (Either ParseError RawLedger) diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index 34827862f..5cf8bee35 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -15,9 +15,6 @@ import Ledger.Entry import Ledger.Transaction -rawledgertests = TestList [ - ] - instance Show RawLedger where show l = printf "RawLedger with %d entries, %d accounts: %s" ((length $ entries l) + diff --git a/Ledger/RawTransaction.hs b/Ledger/RawTransaction.hs index 8c88b1456..63a0bef1f 100644 --- a/Ledger/RawTransaction.hs +++ b/Ledger/RawTransaction.hs @@ -12,9 +12,6 @@ import Ledger.Types import Ledger.Amount -rawtransactiontests = TestList [ - ] - instance Show RawTransaction where show = showLedgerTransaction showLedgerTransaction :: RawTransaction -> String diff --git a/Ledger/TimeLog.hs b/Ledger/TimeLog.hs index c12d227b0..0dfd9d7ab 100644 --- a/Ledger/TimeLog.hs +++ b/Ledger/TimeLog.hs @@ -14,9 +14,6 @@ import Ledger.Commodity import Ledger.Amount -timelogtests = TestList [ - ] - instance Show TimeLogEntry where show t = printf "%s %s %s" (show $ tlcode t) (tldatetime t) (tlcomment t) diff --git a/Ledger/Transaction.hs b/Ledger/Transaction.hs index b02bf4eba..7fdb0b7ab 100644 --- a/Ledger/Transaction.hs +++ b/Ledger/Transaction.hs @@ -14,9 +14,6 @@ import Ledger.RawTransaction import Ledger.Amount -transactiontests = TestList [ - ] - instance Show Transaction where show (Transaction eno d desc a amt) = unwords [d,desc,a,show amt] diff --git a/PrintCommand.hs b/PrintCommand.hs index 91da98ce7..9c6b2375a 100644 --- a/PrintCommand.hs +++ b/PrintCommand.hs @@ -10,9 +10,6 @@ import Ledger import Options -printcommandtests = TestList [ - ] - -- | Print ledger entries in standard format. print' :: [Opt] -> [String] -> Ledger -> IO () print' opts args l = putStr $ showEntries opts args l diff --git a/RegisterCommand.hs b/RegisterCommand.hs index 85539e9a5..4cc93f019 100644 --- a/RegisterCommand.hs +++ b/RegisterCommand.hs @@ -10,9 +10,6 @@ import Ledger import Options -registercommandtests = TestList [ - ] - -- | Print a register report. register :: [Opt] -> [String] -> Ledger -> IO () register opts args l = putStr $ showTransactionsWithBalances opts args l diff --git a/Tests.hs b/Tests.hs index 8629c6360..4b4e45b7b 100644 --- a/Tests.hs +++ b/Tests.hs @@ -15,80 +15,71 @@ import RegisterCommand -- quickcheck = mapM quickCheck ([ -- ] :: [Bool]) -runtests = runTestTT alltests +runtests = runTestTT $ tconcat [unittests, functests] -alltests = concattests [ - tests - ,accounttests - ,accountnametests - ,amounttests - ,balancecommandtests - ,commoditytests - ,entrytests - ,ledgertests - ,parsertests - ,printcommandtests - ,rawledgertests - ,rawtransactiontests - ,registercommandtests - ,timelogtests - ] - where - concattests = foldr (\(TestList as) (TestList bs) -> TestList (as ++ bs)) (TestList []) +tconcat :: [Test] -> Test +tconcat = foldr (\(TestList as) (TestList bs) -> TestList (as ++ bs)) (TestList []) -tests = - TestList +------------------------------------------------------------------------------ +unittests = TestList +-- NB assertequal arguments on a new line have to be indented at least +-- one space, contrary to haskell-mode's auto indent [ - "display dollar amount" ~: show (dollars 1) ~?= "$1.00" + "show dollars" ~: show (dollars 1) ~?= "$1.00" - ,"display time amount" ~: show (hours 1) ~?= "1.0h" + ,"show hours" ~: show (hours 1) ~?= "1.0h" --- ,"amount precision" ~: do --- let a1 = dollars 1.23 --- let a2 = Amount (comm "$") (-1.23) 2 --- let a3 = Amount (comm "$") (-1.23) 3 --- assertequal (Amount (comm "$") 0 1) (a1 + a2) --- assertequal (Amount (comm "$") 0 1) (a1 + a3) --- assertequal (Amount (comm "$") (-2.46) 2) (a2 + a3) --- assertequal (Amount (comm "$") (-2.46) 3) (a3 + a3) --- -- sum adds 0, with Amount fromIntegral's default precision of 2 --- assertequal (Amount (comm "$") 0 1) (sum [a1,a2]) --- assertequal (Amount (comm "$") (-2.46) 2) (sum [a2,a3]) --- assertequal (Amount (comm "$") (-2.46) 2) (sum [a3,a3]) + ,"amount arithmetic" ~: do + let a1 = dollars 1.23 + let a2 = Amount (comm "$") (-1.23) + let a3 = Amount (comm "$") (-1.23) + assertequal (Amount (comm "$") 0) (a1 + a2) + assertequal (Amount (comm "$") 0) (a1 + a3) + assertequal (Amount (comm "$") (-2.46)) (a2 + a3) + assertequal (Amount (comm "$") (-2.46)) (a3 + a3) + assertequal (Amount (comm "$") (-2.46)) (sum [a2,a3]) + assertequal (Amount (comm "$") (-2.46)) (sum [a3,a3]) + assertequal (Amount (comm "$") 0) (sum [a1,a2,a3,-a3]) - ,"ledgertransaction" ~: do - assertparseequal rawtransaction1 (parsewith ledgertransaction rawtransaction1_str) + ,"ledgertransaction" ~: do + assertparseequal rawtransaction1 (parsewith ledgertransaction rawtransaction1_str) + + ,"ledgerentry" ~: do + assertparseequal entry1 (parsewith ledgerentry entry1_str) - ,"ledgerentry" ~: do - assertparseequal entry1 (parsewith ledgerentry entry1_str) + ,"autofillEntry" ~: do + assertequal + (dollars (-47.18)) + (tamount $ last $ etransactions $ autofillEntry entry1) + + ,"punctuatethousands" ~: punctuatethousands "" @?= "" + ,"punctuatethousands" ~: punctuatethousands "1234567.8901" @?= "1,234,567.8901" + ,"punctuatethousands" ~: punctuatethousands "-100" @?= "-100" - ,"autofillEntry" ~: do - assertequal - (dollars (-47.18)) - (tamount $ last $ etransactions $ autofillEntry entry1) + ,"expandAccountNames" ~: do + assertequal + ["assets","assets:cash","assets:checking","expenses","expenses:vacation"] + (expandAccountNames ["assets:cash","assets:checking","expenses:vacation"]) - ,"punctuatethousands" ~: punctuatethousands "" @?= "" - ,"punctuatethousands" ~: punctuatethousands "1234567.8901" @?= "1,234,567.8901" - ,"punctuatethousands" ~: punctuatethousands "-100" @?= "-100" + ,"ledgerAccountNames" ~: do + assertequal + ["assets","assets:cash","assets:checking","assets:saving","equity","equity:opening balances", + "expenses","expenses:food","expenses:food:dining","expenses:phone","expenses:vacation", + "liabilities","liabilities:credit cards","liabilities:credit cards:discover"] + (accountnames ledger7) - ,"expandAccountNames" ~: do - assertequal - ["assets","assets:cash","assets:checking","expenses","expenses:vacation"] - (expandAccountNames ["assets:cash","assets:checking","expenses:vacation"]) + ,"cacheLedger" ~: do + assertequal 15 (length $ Map.keys $ accountmap $ cacheLedger rawledger7 ) - ,"ledgerAccountNames" ~: do - assertequal - ["assets","assets:cash","assets:checking","assets:saving","equity","equity:opening balances", - "expenses","expenses:food","expenses:food:dining","expenses:phone","expenses:vacation", - "liabilities","liabilities:credit cards","liabilities:credit cards:discover"] - (accountnames ledger7) + ,"transactionamount" ~: do + assertparseequal (dollars 47.18) (parsewith transactionamount " $47.18") + assertparseequal (Amount (Commodity {symbol="$",side=L,spaced=False,comma=False,precision=0,rate=1}) 1) (parsewith transactionamount " $1.") + ] - ,"cacheLedger" ~: do - assertequal 15 (length $ Map.keys $ accountmap $ cacheLedger rawledger7 ) - - ,"transactionamount" ~: do - assertparseequal (dollars 47.18) (parsewith transactionamount " $47.18") - assertparseequal (Amount (Commodity {symbol="$",side=L,spaced=False,comma=False,precision=0,rate=1}) 1) (parsewith transactionamount " $1.") +------------------------------------------------------------------------------ +functests = TestList + [ + balancecommandtests ] balancecommandtests = @@ -103,8 +94,7 @@ balancecommandtests = \ $1 liabilities\n\ \" --" (showBalanceReport [] [] l) - , - + , "balance report with showsubs" ~: do l <- ledgerfromfile "sample.ledger" assertequal @@ -120,8 +110,7 @@ balancecommandtests = \ $1 liabilities:debts\n\ \" --" (showBalanceReport [ShowSubs] [] l) - , - + , "balance report with account pattern o" ~: do l <- ledgerfromfile "sample.ledger" assertequal @@ -131,8 +120,7 @@ balancecommandtests = \ $-1\n\ \" --" (showBalanceReport [] ["o"] l) - , - + , "balance report with account pattern o and showsubs" ~: do l <- ledgerfromfile "sample.ledger" assertequal @@ -144,8 +132,7 @@ balancecommandtests = \ $-1\n\ \" --" (showBalanceReport [ShowSubs] ["o"] l) - , - + , "balance report with account pattern a" ~: do l <- ledgerfromfile "sample.ledger" assertequal @@ -158,8 +145,7 @@ balancecommandtests = \ $-1\n\ \" --" (showBalanceReport [] ["a"] l) - , - + , "balance report with account pattern e" ~: do l <- ledgerfromfile "sample.ledger" assertequal @@ -170,9 +156,9 @@ balancecommandtests = \ $1 liabilities:debts\n\ \" --" (showBalanceReport [] ["e"] l) - , - - "balance report with unmatched parent of two matched subaccounts" ~: do + , + "balance report with unmatched parent of two matched subaccounts" ~: + do l <- ledgerfromfile "sample.ledger" assertequal " $-2 assets:cash\n\ @@ -197,8 +183,8 @@ balancecommandtests = assertparseequal :: (Show a, Eq a) => a -> (Either ParseError a) -> Assertion assertparseequal expected parsed = either printParseError (assertequal expected) parsed - --- test data +------------------------------------------------------------------------------ +-- data rawtransaction1_str = " expenses:food:dining $10.00\n"