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 -- testing
-- | Get a Test's label, or the empty string. -- | Get a Test's label, or the empty string.
tname :: Test -> String testName :: Test -> String
tname (TestLabel n _) = n testName (TestLabel n _) = n
tname _ = "" testName _ = ""
-- | Flatten a Test containing TestLists into a list of single tests. -- | Flatten a Test containing TestLists into a list of single tests.
tflatten :: Test -> [Test] flattenTests :: Test -> [Test]
tflatten (TestLabel _ t@(TestList _)) = tflatten t flattenTests (TestLabel _ t@(TestList _)) = flattenTests t
tflatten (TestList ts) = concatMap tflatten ts flattenTests (TestList ts) = concatMap flattenTests ts
tflatten t = [t] flattenTests t = [t]
-- | Filter TestLists in a Test, recursively, preserving the structure. -- | Filter TestLists in a Test, recursively, preserving the structure.
tfilter :: (Test -> Bool) -> Test -> Test filterTests :: (Test -> Bool) -> Test -> Test
tfilter p (TestLabel l ts) = TestLabel l (tfilter p ts) filterTests p (TestLabel l ts) = TestLabel l (filterTests p ts)
tfilter p (TestList ts) = TestList $ filter (any p . tflatten) $ map (tfilter p) ts filterTests p (TestList ts) = TestList $ filter (any p . flattenTests) $ map (filterTests p) ts
tfilter _ t = t filterTests _ t = t
-- | Simple way to assert something is some expected value, with no label. -- | Simple way to assert something is some expected value, with no label.
is :: (Eq a, Show a) => a -> a -> Assertion 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 matchedaddon) && "binary-filename" `in_` (rawopts_ opts) = putStrLn $ binaryfilename progname
| null cmd = putStr $ showModeHelp mainmode' | null cmd = putStr $ showModeHelp mainmode'
| cmd `isPrefixOf` "add" = showModeHelpOr addmode $ journalFilePathFromOpts opts >>= ensureJournalFileExists >> withJournalDo opts add | 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`) ["accounts","balance"] = showModeHelpOr accountsmode $ withJournalDo opts balance
| any (cmd `isPrefixOf`) ["entries","print"] = showModeHelpOr entriesmode $ withJournalDo opts print' | any (cmd `isPrefixOf`) ["entries","print"] = showModeHelpOr entriesmode $ withJournalDo opts print'
| any (cmd `isPrefixOf`) ["postings","register"] = showModeHelpOr postingsmode $ withJournalDo opts register | 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. -- | Run unit tests and exit with success or failure.
runtests :: CliOpts -> IO () test' :: CliOpts -> IO ()
runtests opts = do test' opts = do
(hunitcounts,_) <- runtests' opts results <- runTests opts
if errors hunitcounts > 0 || (failures hunitcounts > 0) if errors results > 0 || failures results > 0
then exitFailure then exitFailure
else exitWith ExitSuccess else exitWith ExitSuccess
-- | Run unit tests and exit on failure. -- | Run all or just the matched unit tests and return their HUnit result counts.
runTestsOrExit :: CliOpts -> IO () runTests :: CliOpts -> IO Counts
runTestsOrExit opts = do runTests = liftM (fst . flip (,) 0) . runTestTT . flatTests
(hunitcounts,_) <- runtests' opts
when (errors hunitcounts > 0 || (failures hunitcounts > 0)) $ exitFailure
runtests' :: Num b => CliOpts -> IO (Counts, b) -- | Run all or just the matched unit tests until the first failure or
runtests' opts = liftM (flip (,) 0) $ runTestTT ts -- error, returning the name of the problem test if any.
where runTestsTillFailure :: CliOpts -> IO (Maybe String)
ts = TestList $ filter matchname $ tflatten tests_Hledger_Cli -- show flat test names runTestsTillFailure opts = undefined -- do
-- ts = tfilter matchname $ TestList tests -- show hierarchical test names -- let ts = flatTests opts
matchname = matchpats (patterns_ $ reportopts_ opts) . tname -- 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