22
33module Test.Hspec.Formatters.Codewars (newFormatter , escapeLF ) where
44
5- import Data.Text (pack , replace , unpack )
6- import Text.Printf (printf )
7- import Data.IORef
85import Control.Monad.IO.Class
6+ import Data.IORef
7+ import Data.Text (pack , replace , unpack )
8+ import Test.Hspec.Core.Formatters.V2
9+ ( FailureReason (.. ),
10+ Formatter (.. ),
11+ Item (.. ),
12+ Result (.. ),
13+ Seconds (.. ),
14+ formatException ,
15+ getRealTime ,
16+ silent ,
17+ writeLine ,
18+ )
919import Test.Hspec.Core.Util (Path )
10-
11- import Test.Hspec.Core.Formatters.V2 (
12- FailureReason (.. ),
13- Formatter (.. ),
14- Item (.. ),
15- Seconds (.. ),
16- Result (.. ),
17- formatException ,
18- silent ,
19- writeLine ,
20- getRealTime
21- )
20+ import Text.Printf (printf )
2221
2322getName :: Path -> String
2423getName (_, req) = escapeLF req
2524
2625newFormatter :: IO Formatter
2726newFormatter = do
28- times <- newIORef ([ ] :: [Seconds ])
29- pure $ silent
30- {
31- formatterGroupStarted = \ path -> do
32- writeLine " "
33- startedOn <- getRealTime
34- liftIO $ modifyIORef times (startedOn : )
35- writeLine $ " <DESCRIBE::>" ++ (getName path)
36- , formatterGroupDone = \ _ -> do
37- writeLine " "
38- ts <- liftIO $ readIORef times
39- now <- getRealTime
40- let startedOn = head ts
41- let duration = now - startedOn
42- writeLine $ " <COMPLETEDIN::>" ++ (formatToMillis $ duration)
43- liftIO $ modifyIORef times tail
44- , formatterItemStarted = \ path -> do
45- writeLine " "
46- writeLine $ " <IT::>" ++ (getName path)
47- , formatterItemDone = \ _ item -> do
48- writeLine " "
49- writeLine $ reportItem item
50- writeLine " "
51- writeLine $ " <COMPLETEDIN::>" ++ (formatToMillis $ itemDuration item)
52- }
27+ times <- newIORef ([] :: [Seconds ])
28+ pure $
29+ silent
30+ { formatterGroupStarted = \ path -> do
31+ writeLine " "
32+ startedOn <- getRealTime
33+ liftIO $ modifyIORef times (startedOn : )
34+ writeLine $ " <DESCRIBE::>" ++ (getName path),
35+ formatterGroupDone = \ _ -> do
36+ writeLine " "
37+ ts <- liftIO $ readIORef times
38+ now <- getRealTime
39+ let startedOn = head ts
40+ let duration = now - startedOn
41+ writeLine $ " <COMPLETEDIN::>" ++ (formatToMillis $ duration)
42+ liftIO $ modifyIORef times tail ,
43+ formatterItemStarted = \ path -> do
44+ writeLine " "
45+ writeLine $ " <IT::>" ++ (getName path),
46+ formatterItemDone = \ _ item -> do
47+ writeLine " "
48+ writeLine $ reportItem item
49+ writeLine " "
50+ writeLine $ " <COMPLETEDIN::>" ++ (formatToMillis $ itemDuration item)
51+ }
5352
5453reportItem :: Item -> String
5554reportItem item =
5655 case itemResult item of
5756 Success -> " <PASSED::>Test Passed"
5857 Failure _ reason -> reasonAsString reason
59- Pending _ Nothing -> " <FAILED::>Test pending: no reason given"
60- Pending _ (Just msg) -> " <FAILED::>Test pending: " ++ (escapeLF msg)
58+ Pending _ Nothing -> " <FAILED::>Test pending: no reason given"
59+ Pending _ (Just msg) -> " <FAILED::>Test pending: " ++ (escapeLF msg)
6160
6261reasonAsString :: FailureReason -> String
6362reasonAsString reason =
@@ -73,7 +72,6 @@ reasonAsString reason =
7372 Error (Just s) err ->
7473 " <ERROR::>" ++ (escapeLF s) ++ " <:LF:>" ++ (escapeLF $ formatException err)
7574
76-
7775formatToMillis :: Seconds -> String
7876formatToMillis (Seconds s) = printf " %.3f" (s * 1000 )
7977
0 commit comments