refactor test runner

This commit is contained in:
Simon Michael 2012-05-14 19:23:12 +00:00
parent db4d853a3f
commit 1654776f4a
3 changed files with 34 additions and 27 deletions

View File

@ -347,21 +347,21 @@ getCurrentLocalTime = do
-- testing
-- | Get a Test's label, or the empty string.
tname :: Test -> String
tname (TestLabel n _) = n
tname _ = ""
testName :: Test -> String
testName (TestLabel n _) = n
testName _ = ""
-- | Flatten a Test containing TestLists into a list of single tests.
tflatten :: Test -> [Test]
tflatten (TestLabel _ t@(TestList _)) = tflatten t
tflatten (TestList ts) = concatMap tflatten ts
tflatten t = [t]
flattenTests :: Test -> [Test]
flattenTests (TestLabel _ t@(TestList _)) = flattenTests t
flattenTests (TestList ts) = concatMap flattenTests ts
flattenTests t = [t]
-- | Filter TestLists in a Test, recursively, preserving the structure.
tfilter :: (Test -> Bool) -> Test -> Test
tfilter p (TestLabel l ts) = TestLabel l (tfilter p ts)
tfilter p (TestList ts) = TestList $ filter (any p . tflatten) $ map (tfilter p) ts
tfilter _ t = t
filterTests :: (Test -> Bool) -> Test -> Test
filterTests p (TestLabel l ts) = TestLabel l (filterTests p ts)
filterTests p (TestList ts) = TestList $ filter (any p . flattenTests) $ map (filterTests p) ts
filterTests _ t = t
-- | Simple way to assert something is some expected value, with no label.
is :: (Eq a, Show a) => a -> a -> Assertion

View File

@ -75,7 +75,7 @@ main = do
| (null matchedaddon) && "binary-filename" `in_` (rawopts_ opts) = putStrLn $ binaryfilename progname
| null cmd = putStr $ showModeHelp mainmode'
| cmd `isPrefixOf` "add" = showModeHelpOr addmode $ journalFilePathFromOpts opts >>= ensureJournalFileExists >> withJournalDo opts add
| cmd `isPrefixOf` "test" = showModeHelpOr testmode $ runtests opts
| cmd `isPrefixOf` "test" = showModeHelpOr testmode $ test' opts
| any (cmd `isPrefixOf`) ["accounts","balance"] = showModeHelpOr accountsmode $ withJournalDo opts balance
| any (cmd `isPrefixOf`) ["entries","print"] = showModeHelpOr entriesmode $ withJournalDo opts print'
| any (cmd `isPrefixOf`) ["postings","register"] = showModeHelpOr postingsmode $ withJournalDo opts register

View File

@ -37,22 +37,29 @@ import Hledger.Cli
-- | Run unit tests and exit with success or failure.
runtests :: CliOpts -> IO ()
runtests opts = do
(hunitcounts,_) <- runtests' opts
if errors hunitcounts > 0 || (failures hunitcounts > 0)
test' :: CliOpts -> IO ()
test' opts = do
results <- runTests opts
if errors results > 0 || failures results > 0
then exitFailure
else exitWith ExitSuccess
-- | Run unit tests and exit on failure.
runTestsOrExit :: CliOpts -> IO ()
runTestsOrExit opts = do
(hunitcounts,_) <- runtests' opts
when (errors hunitcounts > 0 || (failures hunitcounts > 0)) $ exitFailure
-- | Run all or just the matched unit tests and return their HUnit result counts.
runTests :: CliOpts -> IO Counts
runTests = liftM (fst . flip (,) 0) . runTestTT . flatTests
runtests' :: Num b => CliOpts -> IO (Counts, b)
runtests' opts = liftM (flip (,) 0) $ runTestTT ts
where
ts = TestList $ filter matchname $ tflatten tests_Hledger_Cli -- show flat test names
-- ts = tfilter matchname $ TestList tests -- show hierarchical test names
matchname = matchpats (patterns_ $ reportopts_ opts) . tname
-- | Run all or just the matched unit tests until the first failure or
-- error, returning the name of the problem test if any.
runTestsTillFailure :: CliOpts -> IO (Maybe String)
runTestsTillFailure opts = undefined -- do
-- let ts = flatTests opts
-- results = liftM (fst . flip (,) 0) $ runTestTT $
-- firstproblem = find (\counts -> )
-- | All or pattern-matched tests, as a flat list to show simple names.
flatTests opts = TestList $ filter (matcherFromOpts opts) $ flattenTests tests_Hledger_Cli
-- | All or pattern-matched tests, in the original suites to show hierarchical names.
hierarchicalTests opts = filterTests (matcherFromOpts opts) tests_Hledger_Cli
matcherFromOpts opts = matchpats (patterns_ $ reportopts_ opts) . testName