|
1 | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | 2 |
|
3 | | -module Test.Hspec.Formatters.Codewars (codewars) where |
| 3 | +module Test.Hspec.Formatters.Codewars (codewars, escapeLF) where |
4 | 4 |
|
5 | 5 | import Data.Text (pack, replace, unpack) |
| 6 | +import Text.Printf (printf) |
| 7 | +import Data.IORef |
| 8 | +import Control.Monad.IO.Class |
| 9 | +import Test.Hspec.Core.Util (Path) |
6 | 10 |
|
7 | | -import Test.Hspec.Core.Formatters.V1 ( |
| 11 | +import Test.Hspec.Core.Formatters.V2 ( |
8 | 12 | FailureReason (..), |
9 | | - Formatter (..), |
| 13 | + Formatter (..), |
| 14 | + Item (..), |
| 15 | + Seconds (..), |
| 16 | + Result (..), |
10 | 17 | formatException, |
11 | 18 | silent, |
12 | 19 | writeLine, |
| 20 | + getRealTime |
13 | 21 | ) |
14 | 22 |
|
15 | | -codewars :: Formatter |
16 | | -codewars = |
17 | | - silent |
18 | | - { exampleGroupStarted = \_ name -> do |
| 23 | +getName :: Path -> String |
| 24 | +getName (_, req) = escapeLF req |
| 25 | + |
| 26 | +codewars :: IO Formatter |
| 27 | +codewars = 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 |
19 | 37 | writeLine "" |
20 | | - writeLine $ escapeLF $ "<DESCRIBE::>" ++ name |
21 | | - , exampleGroupDone = writeLine "\n<COMPLETEDIN::>" |
22 | | - , exampleSucceeded = \(_, name) _ -> do |
| 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 |
23 | 45 | writeLine "" |
24 | | - writeLine $ escapeLF $ "<IT::>" ++ name |
25 | | - writeLine "\n<PASSED::>Test Passed" |
26 | | - writeLine "\n<COMPLETEDIN::>" |
27 | | - , exampleFailed = \(_, name) _ reason -> do |
| 46 | + writeLine $ "<IT::>" ++ (getName path) |
| 47 | + ,formatterItemDone = \_ item -> do |
28 | 48 | writeLine "" |
29 | | - writeLine $ escapeLF $ "<IT::>" ++ name |
| 49 | + writeLine $ reportItem item |
30 | 50 | writeLine "" |
31 | | - writeLine $ escapeLF $ reasonAsString reason |
32 | | - writeLine "\n<COMPLETEDIN::>" |
| 51 | + writeLine $ "<COMPLETEDIN::>" ++ (formatToMillis $ itemDuration item) |
33 | 52 | } |
34 | 53 |
|
| 54 | +reportItem :: Item -> String |
| 55 | +reportItem item = |
| 56 | + case itemResult item of |
| 57 | + Success -> "<PASSED::>Test Passed" |
| 58 | + Failure _ reason -> reasonAsString reason |
| 59 | + Pending _ Nothing -> "<FAILED::>Test pending: no reason given" |
| 60 | + Pending _ (Just msg) -> "<FAILED::>Test pending: " ++ (escapeLF msg) |
| 61 | + |
35 | 62 | reasonAsString :: FailureReason -> String |
36 | 63 | reasonAsString reason = |
37 | 64 | case reason of |
38 | 65 | NoReason -> "<FAILED::>Test Failed" |
39 | | - Reason x -> "<FAILED::>" ++ x |
| 66 | + Reason x -> "<FAILED::>" ++ (escapeLF x) |
40 | 67 | ExpectedButGot Nothing expected got -> |
41 | | - "<FAILED::>Expected " ++ expected ++ " but got " ++ got |
| 68 | + "<FAILED::>expected: " ++ (escapeLF expected) ++ "<:LF:> but got: " ++ (escapeLF got) |
42 | 69 | ExpectedButGot (Just src) expected got -> |
43 | | - "<FAILED::>" ++ src ++ " expected " ++ expected ++ " but got " ++ got |
| 70 | + "<FAILED::>" ++ (escapeLF src) ++ "<:LF:>expected: " ++ (escapeLF expected) ++ "<:LF:> but got: " ++ (escapeLF got) |
44 | 71 | Error Nothing err -> |
45 | | - "<ERROR::>" ++ formatException err |
| 72 | + "<ERROR::>uncaught exception: " ++ (escapeLF $ formatException err) |
46 | 73 | Error (Just s) err -> |
47 | | - "<ERROR::>" ++ s ++ formatException err |
| 74 | + "<ERROR::>" ++ (escapeLF s) ++ "<:LF:>" ++ (escapeLF $ formatException err) |
| 75 | + |
| 76 | + |
| 77 | +formatToMillis :: Seconds -> String |
| 78 | +formatToMillis (Seconds s) = printf "%.3f" (s * 1000) |
48 | 79 |
|
49 | 80 | escapeLF :: String -> String |
50 | 81 | escapeLF = unpack . replace "\n" "<:LF:>" . pack |
0 commit comments