tests cleanup
This commit is contained in:
		
							parent
							
								
									30fe7ac225
								
							
						
					
					
						commit
						db52c7d0e5
					
				
							
								
								
									
										118
									
								
								Tests.hs
									
									
									
									
									
								
							
							
						
						
									
										118
									
								
								Tests.hs
									
									
									
									
									
								
							| @ -2,9 +2,12 @@ | |||||||
| module Tests | module Tests | ||||||
| where | where | ||||||
| 
 | 
 | ||||||
| import Test.HUnit |  | ||||||
| import Test.QuickCheck |  | ||||||
| import Text.ParserCombinators.Parsec | import Text.ParserCombinators.Parsec | ||||||
|  | import Test.QuickCheck | ||||||
|  | import Test.HUnit | ||||||
|  | -- trying to make "*Tests> test" work | ||||||
|  | -- hiding (test) | ||||||
|  | --import qualified Test.HUnit (Test.HUnit.test) | ||||||
| 
 | 
 | ||||||
| import Options | import Options | ||||||
| import Types | import Types | ||||||
| @ -12,20 +15,29 @@ import Parse | |||||||
| 
 | 
 | ||||||
| -- sample data | -- sample data | ||||||
| 
 | 
 | ||||||
| sample_entry = "\ | transaction1_str  = "  expenses:food:dining  $10.00\n" | ||||||
|  | 
 | ||||||
|  | transaction1 = Transaction "expenses:food:dining" (Amount "$" 10) | ||||||
|  | 
 | ||||||
|  | entry1_str = "\ | ||||||
|  | \2007/01/28 coopportunity\n\ | ||||||
|  | \  expenses:food:groceries                 $47.18\n\ | ||||||
|  | \  assets:checking\n\ | ||||||
|  | \\n" --" | ||||||
|  | 
 | ||||||
|  | entry1 = | ||||||
|  |     (Entry "2007/01/28" False "" "coopportunity"  | ||||||
|  |                [Transaction "expenses:food:groceries" (Amount "$" 47.18),  | ||||||
|  |                 Transaction "assets:checking" (Amount "$" (-47.18))]) | ||||||
|  | 
 | ||||||
|  | entry2_str = "\ | ||||||
| \2007/01/27 * joes diner\n\ | \2007/01/27 * joes diner\n\ | ||||||
| \  expenses:food:dining                    $10.00\n\ | \  expenses:food:dining                    $10.00\n\ | ||||||
| \  expenses:gifts                          $10.00\n\ | \  expenses:gifts                          $10.00\n\ | ||||||
| \  assets:checking                        $-20.00\n\ | \  assets:checking                        $-20.00\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_entry2 = "\ | entry3_str = "\ | ||||||
| \2007/01/28 coopportunity\n\ |  | ||||||
| \  expenses:food:groceries                 $47.18\n\ |  | ||||||
| \  assets:checking\n\ |  | ||||||
| \\n" --" |  | ||||||
| 
 |  | ||||||
| sample_entry3 = "\ |  | ||||||
| \2007/01/01 * opening balance\n\ | \2007/01/01 * opening balance\n\ | ||||||
| \    assets:cash                                $4.82\n\ | \    assets:cash                                $4.82\n\ | ||||||
| \    equity:opening balances\n\ | \    equity:opening balances\n\ | ||||||
| @ -39,19 +51,19 @@ sample_entry3 = "\ | |||||||
| \  assets:checking\n\ | \  assets:checking\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_periodic_entry = "\ | periodic_entry1_str = "\ | ||||||
| \~ monthly from 2007/2/2\n\ | \~ monthly from 2007/2/2\n\ | ||||||
| \  assets:saving            $200.00\n\ | \  assets:saving            $200.00\n\ | ||||||
| \  assets:checking\n\ | \  assets:checking\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_periodic_entry2 = "\ | periodic_entry2_str = "\ | ||||||
| \~ monthly from 2007/2/2\n\ | \~ monthly from 2007/2/2\n\ | ||||||
| \  assets:saving            $200.00         ;auto savings\n\ | \  assets:saving            $200.00         ;auto savings\n\ | ||||||
| \  assets:checking\n\ | \  assets:checking\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_periodic_entry3 = "\ | periodic_entry3_str = "\ | ||||||
| \~ monthly from 2007/01/01\n\ | \~ monthly from 2007/01/01\n\ | ||||||
| \    assets:cash                                $4.82\n\ | \    assets:cash                                $4.82\n\ | ||||||
| \    equity:opening balances\n\ | \    equity:opening balances\n\ | ||||||
| @ -61,11 +73,7 @@ sample_periodic_entry3 = "\ | |||||||
| \    equity:opening balances\n\ | \    equity:opening balances\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_transaction  = "  expenses:food:dining  $10.00\n" | ledger_str = "\ | ||||||
| 
 |  | ||||||
| sample_transaction2 = "  assets:checking\n" |  | ||||||
| 
 |  | ||||||
| sample_ledger = "\ |  | ||||||
| \\n\ | \\n\ | ||||||
| \2007/01/27 * joes diner\n\ | \2007/01/27 * joes diner\n\ | ||||||
| \  expenses:food:dining                    $10.00\n\ | \  expenses:food:dining                    $10.00\n\ | ||||||
| @ -79,30 +87,30 @@ sample_ledger = "\ | |||||||
| \\n\ | \\n\ | ||||||
| \" --" | \" --" | ||||||
| 
 | 
 | ||||||
| sample_ledger2 = "\ | ledger2_str = "\ | ||||||
| \;comment\n\ | \;comment\n\ | ||||||
| \2007/01/27 * joes diner\n\ | \2007/01/27 * joes diner\n\ | ||||||
| \  expenses:food:dining                    $10.00\n\ | \  expenses:food:dining                    $10.00\n\ | ||||||
| \  assets:checking                        $-47.18\n\ | \  assets:checking                        $-47.18\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_ledger3 = "\ | ledger3_str = "\ | ||||||
| \2007/01/27 * joes diner\n\ | \2007/01/27 * joes diner\n\ | ||||||
| \  expenses:food:dining                    $10.00\n\ | \  expenses:food:dining                    $10.00\n\ | ||||||
| \;intra-entry comment\n\ | \;intra-entry comment\n\ | ||||||
| \  assets:checking                        $-47.18\n\ | \  assets:checking                        $-47.18\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_ledger4 = "\ | ledger4_str = "\ | ||||||
| \!include \"somefile\"\n\ | \!include \"somefile\"\n\ | ||||||
| \2007/01/27 * joes diner\n\ | \2007/01/27 * joes diner\n\ | ||||||
| \  expenses:food:dining                    $10.00\n\ | \  expenses:food:dining                    $10.00\n\ | ||||||
| \  assets:checking                        $-47.18\n\ | \  assets:checking                        $-47.18\n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_ledger5 = "" | ledger5_str = "" | ||||||
| 
 | 
 | ||||||
| sample_ledger6 = "\ | ledger6_str = "\ | ||||||
| \~ monthly from 2007/1/21\n\ | \~ monthly from 2007/1/21\n\ | ||||||
| \    expenses:entertainment  $16.23        ;netflix\n\ | \    expenses:entertainment  $16.23        ;netflix\n\ | ||||||
| \    assets:checking\n\ | \    assets:checking\n\ | ||||||
| @ -112,7 +120,7 @@ sample_ledger6 = "\ | |||||||
| \;     equity:opening balances                         \n\ | \;     equity:opening balances                         \n\ | ||||||
| \\n" --" | \\n" --" | ||||||
| 
 | 
 | ||||||
| sample_ledger7 = "\ | ledger7_str = "\ | ||||||
| \2007/01/01 * opening balance\n\ | \2007/01/01 * opening balance\n\ | ||||||
| \    assets:cash                                $4.82\n\ | \    assets:cash                                $4.82\n\ | ||||||
| \    equity:opening balances                         \n\ | \    equity:opening balances                         \n\ | ||||||
| @ -192,8 +200,6 @@ ledger7 = Ledger [] [] | |||||||
| --     expenses:food:dining                       $6.48 | --     expenses:food:dining                       $6.48 | ||||||
| --     assets:checking | --     assets:checking | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| -- utils | -- utils | ||||||
| 
 | 
 | ||||||
| assertParseEqual :: (Show a, Eq a) => a -> (Either ParseError a) -> Assertion | assertParseEqual :: (Show a, Eq a) => a -> (Either ParseError a) -> Assertion | ||||||
| @ -206,61 +212,53 @@ assertEqual' e a = assertEqual "" e a | |||||||
| 
 | 
 | ||||||
| parse' p ts = parse p "" ts | parse' p ts = parse p "" ts | ||||||
| 
 | 
 | ||||||
|      |  | ||||||
| -- hunit tests | -- hunit tests | ||||||
| 
 | 
 | ||||||
| --   parseTest ledgertransaction sample_transaction2 | --   parseTest ledgerentry entry2_str | ||||||
| --   parseTest ledgerentry sample_entry2 | --   parseTest ledgerentry entry3_str | ||||||
| --   parseTest ledgerentry sample_entry3 | --   parseTest ledgerperiodicentry periodic_entry1_str | ||||||
| --   parseTest ledgerperiodicentry sample_periodic_entry | --   parseTest ledgerperiodicentry periodic_entry2_str | ||||||
| --   parseTest ledgerperiodicentry sample_periodic_entry2 | --   parseTest ledgerperiodicentry periodic_entry3_str | ||||||
| --   parseTest ledgerperiodicentry sample_periodic_entry3 | --   parseTest ledger ledger_str | ||||||
| --   parseTest ledger sample_ledger | --   parseTest ledger ledger2_str | ||||||
| --   parseTest ledger sample_ledger2 | --   parseTest ledger ledger3_str | ||||||
| --   parseTest ledger sample_ledger3 | --   parseTest ledger ledger4_str | ||||||
| --   parseTest ledger sample_ledger4 | --   parseTest ledger ledger5_str | ||||||
| --   parseTest ledger sample_ledger5 | --   parseTest ledger ledger6_str | ||||||
| --   parseTest ledger sample_ledger6 | --   parseTest ledger periodic_entry1_str | ||||||
| --   parseTest ledger sample_periodic_entry | --   parseTest ledger periodic_entry2_str | ||||||
| --   parseTest ledger sample_periodic_entry2 |  | ||||||
| --   parseLedgerFile ledgerFilePath >>= printParseResult | --   parseLedgerFile ledgerFilePath >>= printParseResult | ||||||
| 
 | 
 | ||||||
| test_parse_ledgertransaction :: Assertion | test_parse_ledgertransaction :: Assertion | ||||||
| test_parse_ledgertransaction = | test_parse_ledgertransaction = | ||||||
|     assertParseEqual |     assertParseEqual transaction1 (parse' ledgertransaction transaction1_str)       | ||||||
|       (Transaction "expenses:food:dining" (Amount "$" 10)) |  | ||||||
|       (parse' ledgertransaction sample_transaction) |  | ||||||
| 
 |  | ||||||
| entry2 = |  | ||||||
|     (Entry "2007/01/28" False "" "coopportunity"  |  | ||||||
|                [Transaction "expenses:food:groceries" (Amount "$" 47.18),  |  | ||||||
|                 Transaction "assets:checking" (Amount "$" (-47.18))]) |  | ||||||
| 
 | 
 | ||||||
| test_parse_ledgerentry = | test_parse_ledgerentry = | ||||||
|     assertParseEqual entry2 (parse' ledgerentry sample_entry2) |     assertParseEqual entry1 (parse' ledgerentry entry1_str) | ||||||
| 
 | 
 | ||||||
| test_autofill_entry =  | test_autofill_entry =  | ||||||
|     assertEqual' |     assertEqual' | ||||||
|       (Amount "$" (-47.18)) |       (Amount "$" (-47.18)) | ||||||
|       (amount $ last $ transactions $ autofill entry2) |       (amount $ last $ transactions $ autofill entry1) | ||||||
| 
 | 
 | ||||||
| hunittests = TestList [ | tests = TestList [ | ||||||
|                        test "test_parse_ledgertransaction" test_parse_ledgertransaction |                    t "test_parse_ledgertransaction" test_parse_ledgertransaction | ||||||
|                       , test "test_parse_ledgerentry" test_parse_ledgerentry |                  , t "test_parse_ledgerentry" test_parse_ledgerentry | ||||||
|                       , test "test_autofill_entry" test_autofill_entry |                  , t "test_autofill_entry" test_autofill_entry | ||||||
|                  ]  |                  ]  | ||||||
|     where test label fn = TestLabel label $ TestCase fn |     where t label fn = TestLabel label $ TestCase fn | ||||||
| 
 | 
 | ||||||
| hunittests2 = Test.HUnit.test [ | tests2 = Test.HUnit.test [ | ||||||
|                           "test1" ~: assertEqual "2 equals 2" 2 2 |                           "test1" ~: assertEqual "2 equals 2" 2 2 | ||||||
|                               ] |                               ] | ||||||
| 
 | 
 | ||||||
| -- quickcheck properties | -- quickcheck properties | ||||||
| 
 | 
 | ||||||
| prop1 = 1 == 1 | prop1 = 1 == 1 | ||||||
| 
 |  | ||||||
| --prop_test_parse_ledgertransaction = | --prop_test_parse_ledgertransaction = | ||||||
| --     (Transaction "expenses:food:dining" (Amount "$" 10)) ==  | --     (Transaction "expenses:food:dining" (Amount "$" 10)) ==  | ||||||
| --     (parse' ledgertransaction sample_transaction)) | --     (parse' ledgertransaction transaction_str)) | ||||||
| -- how ? |  | ||||||
| 
 | 
 | ||||||
|  | props = [ | ||||||
|  |          prop1 | ||||||
|  |         ] | ||||||
							
								
								
									
										20
									
								
								hledger.hs
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								hledger.hs
									
									
									
									
									
								
							| @ -1,13 +1,15 @@ | |||||||
| #!/usr/bin/runhaskell | #!/usr/bin/env runhaskell | ||||||
| -- hledger - ledger-compatible money management utilities (& haskell study) | -- hledger - ledger-compatible money management utilities (& haskell study) | ||||||
| -- GPLv3, (c) Simon Michael & contributors,  | -- GPLv3, (c) Simon Michael & contributors,  | ||||||
| -- John Wiegley's ledger is at http://newartisans.com/ledger.html . | -- John Wiegley's ledger is at http://newartisans.com/ledger.html . | ||||||
| 
 | 
 | ||||||
| module Main where | module Main -- almost all IO is handled here | ||||||
|  | where | ||||||
| 
 | 
 | ||||||
| import System (getArgs) | import System (getArgs) | ||||||
| import Data.List (isPrefixOf) | import Data.List (isPrefixOf) | ||||||
| import Test.HUnit (runTestTT) | import Test.HUnit (runTestTT) | ||||||
|  | import Test.QuickCheck (quickCheck) | ||||||
| import Text.ParserCombinators.Parsec (parseFromFile, ParseError) | import Text.ParserCombinators.Parsec (parseFromFile, ParseError) | ||||||
| 
 | 
 | ||||||
| import Options | import Options | ||||||
| @ -18,21 +20,23 @@ import Tests | |||||||
| main :: IO () | main :: IO () | ||||||
| main = do | main = do | ||||||
|   (opts, args) <- (getArgs >>= getOptions) |   (opts, args) <- (getArgs >>= getOptions) | ||||||
|   test |  | ||||||
|   if args == [] |   if args == [] | ||||||
|     then register [] |     then register [] | ||||||
|     else |     else | ||||||
|       let (command, args) = (head args, tail args) in |       let (command, args') = (head args, tail args) in | ||||||
|       if "reg" `isPrefixOf` command then register args |       if "reg" `isPrefixOf` command then (register args') | ||||||
|       else if "bal" `isPrefixOf` command then balance args |       else if "bal" `isPrefixOf` command then balance args' | ||||||
|  |            else if "test" `isPrefixOf` command then test | ||||||
|                 else error "could not recognise your command" |                 else error "could not recognise your command" | ||||||
| 
 | 
 | ||||||
| -- commands | -- commands | ||||||
| 
 | 
 | ||||||
| test :: IO ()       | test :: IO ()       | ||||||
| test = do | test = do | ||||||
|   runTestTT hunittests |   putStrLn "hunit " | ||||||
| --  quickCheck prop1 |   runTestTT tests | ||||||
|  |   putStr "quickcheck " | ||||||
|  |   mapM quickCheck props | ||||||
|   return () |   return () | ||||||
| 
 | 
 | ||||||
| register :: [String] -> IO () | register :: [String] -> IO () | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user