Debian Codegraph @

Test/Tasty/HUnit/Steps.hs

raw
1 {-# LANGUAGE DeriveDataTypeable, BangPatterns #-}
2 module Test.Tasty.HUnit.Steps (testCaseSteps) where
3
4 import Control.Applicative
5 import Control.Exception
6 import Data.IORef
7 import Data.List (foldl')
8 import Data.Typeable (Typeable)
9 import Prelude -- Silence AMP import warnings
10 import Test.Tasty.HUnit.Orig
11 import Test.Tasty.Providers
12 import Test.Tasty.Runners (getTime)
13 import Text.Printf (printf)
14
15 newtype TestCaseSteps = TestCaseSteps ((String -> IO ()) -> Assertion)
16 deriving Typeable
17
18 instance IsTest TestCaseSteps where
19 run _ (TestCaseSteps assertionFn) yieldProgress = do
20 ref <- newIORef []
21
22 let
23 stepFn :: String -> IO ()
24 stepFn msg = do
25 tme <- getTime
26 -- The number of steps is not fixed, so we can't
27 -- provide the progress percentage.
28 -- We also don't provide the timings here, only
29 -- at the end.
30 yieldProgress (Progress msg 0)
31 atomicModifyIORef ref (\l -> ((tme,msg):l, ()))
32
33 hunitResult <- (Right <$> assertionFn stepFn) `catch`
34 \(SomeException ex) -> return $ Left (displayException ex)
35
36 endTime <- getTime
37
38 maxMsgLength <- foldl' max 0 . map (length . snd) <$> readIORef ref
39
40 let msgFormat = "%-" ++ show (min maxMsgLength 62) ++ "s (%.02fs)"
41
42 msgs <- snd . foldl'
43 (\(lastTime, acc) (curTime, msg) ->
44 let !duration = lastTime - curTime
45 !msg' = if duration >= 0.01 then printf msgFormat msg duration else msg
46 in (curTime, msg':acc))
47 (endTime, [])
48 <$> readIORef ref
49
50 return $
51 case hunitResult of
52
53 Right {} -> testPassed (unlines msgs)
54
55 Left errMsg -> testFailed $
56 if null msgs
57 then
58 errMsg
59 else
60 -- Indent the error msg w.r.t. step messages
61 unlines $
62 msgs ++ map (" " ++) (lines errMsg)
63
64 testOptions = return []
65
66 -- | Create a multi-step unit test.
67 --
68 -- Example:
69 --
70 -- >main = defaultMain $ testCaseSteps "Multi-step test" $ \step -> do
71 -- > step "Preparing..."
72 -- > -- do something
73 -- >
74 -- > step "Running part 1"
75 -- > -- do something
76 -- >
77 -- > step "Running part 2"
78 -- > -- do something
79 -- > assertFailure "BAM!"
80 -- >
81 -- > step "Running part 3"
82 -- > -- do something
83 --
84 -- The @step@ calls are mere annotations. They let you see which steps were
85 -- performed successfully, and which step failed.
86 --
87 -- You can think of @step@
88 -- as 'putStrLn', except 'putStrLn' would mess up the output with the
89 -- console reporter and get lost with the others.
90 --
91 -- For the example above, the output will be
92 --
93 -- >Multi-step test: FAIL
94 -- > Preparing...
95 -- > Running part 1
96 -- > Running part 2
97 -- > BAM!
98 -- >
99 -- >1 out of 1 tests failed (0.00s)
100 --
101 -- Note that:
102 --
103 -- * Tasty still treats this as a single test, even though it consists of
104 -- multiple steps.
105 --
106 -- * The execution stops after the first failure. When we are looking at
107 -- a failed test, we know that all /displayed/ steps but the last one were
108 -- successful, and the last one failed. The steps /after/ the failed one
109 -- are /not displayed/, since they didn't run.
110 testCaseSteps :: TestName -> ((String -> IO ()) -> Assertion) -> TestTree
111 testCaseSteps name = singleTest name . TestCaseSteps