Skip to content

Commit 653e29d

Browse files
authored
Merge pull request #8 from codewars/test-outputs
2 parents bbb4481 + a5e3875 commit 653e29d

File tree

5 files changed

+151
-46
lines changed

5 files changed

+151
-46
lines changed

hspec-formatters-codewars.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ test-suite hspec-formatters-codewars-test
3838
type: exitcode-stdio-1.0
3939
main-is: Main.hs
4040
other-modules:
41-
ExampleSpec
41+
Helper
4242
Spec
43+
Test.Hspec.Formatters.CodewarsSpec
4344
Paths_hspec_formatters_codewars
4445
hs-source-dirs:
4546
test
@@ -50,6 +51,7 @@ test-suite hspec-formatters-codewars-test
5051
, hspec >=2.8
5152
, hspec-core >=2.8
5253
, hspec-formatters-codewars
54+
, silently
5355
, text
5456
default-language: Haskell2010
5557
build-tool-depends: hspec-discover:hspec-discover

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ tests:
4949
- hspec-core >= 2.8
5050
- hspec-formatters-codewars
5151
- QuickCheck
52+
- silently
5253
verbatim:
5354
build-tool-depends:
5455
hspec-discover:hspec-discover

test/ExampleSpec.hs

Lines changed: 0 additions & 45 deletions
This file was deleted.

test/Helper.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Helper (captureLines, normalizeSummary) where
2+
3+
import Data.Char
4+
import Data.List (isPrefixOf)
5+
import System.IO.Silently
6+
7+
captureLines :: IO a -> IO [String]
8+
captureLines = fmap lines . capture_
9+
10+
-- Replace duration in summary with zeroes
11+
normalizeSummary :: [String] -> [String]
12+
normalizeSummary = map f
13+
where
14+
f x
15+
| "<COMPLETEDIN::>" `isPrefixOf` x = map g x
16+
| otherwise = x
17+
g x
18+
| isNumber x = '0'
19+
| otherwise = x
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
module Test.Hspec.Formatters.CodewarsSpec (spec) where
2+
3+
import Helper (captureLines, normalizeSummary)
4+
import Test.Hspec
5+
import Test.Hspec.Core.Formatters.V2 (formatterToFormat)
6+
import qualified Test.Hspec.Core.Runner as H
7+
import qualified Test.Hspec.Core.Spec as H
8+
import Test.Hspec.Formatters.Codewars (newFormatter)
9+
10+
runSpecWithCodewars :: Spec -> IO [String]
11+
runSpecWithCodewars s =
12+
do
13+
codewars <- newFormatter
14+
let config = H.defaultConfig {H.configFormat = Just $ formatterToFormat codewars}
15+
captureLines $ H.hspecWithResult config s
16+
>>= (return . normalizeSummary)
17+
18+
spec :: Spec
19+
spec = do
20+
describe "Codewars Formatter" $ do
21+
it "handles passing tests" $ do
22+
r <- runSpecWithCodewars $ do
23+
H.describe "Example" $ do
24+
H.it "success" (H.Result "" H.Success)
25+
r
26+
`shouldBe` [ "",
27+
"<DESCRIBE::>Example",
28+
"",
29+
"<IT::>success",
30+
"",
31+
"<PASSED::>Test Passed",
32+
"",
33+
"<COMPLETEDIN::>0.000",
34+
"",
35+
"<COMPLETEDIN::>0.000"
36+
]
37+
38+
it "handles failing tests" $ do
39+
r <- runSpecWithCodewars $ do
40+
H.describe "parsing" $ do
41+
H.it "can parse integers and fail" $ do
42+
read "10" `shouldBe` (11 :: Int)
43+
H.it "can parse floating-point numbers and fail" $ do
44+
read "2.5" `shouldBe` (2.6 :: Float)
45+
r
46+
`shouldBe` [ "",
47+
"<DESCRIBE::>parsing",
48+
"",
49+
"<IT::>can parse integers and fail",
50+
"",
51+
"<FAILED::>expected: 11<:LF:> but got: 10",
52+
"",
53+
"<COMPLETEDIN::>0.000",
54+
"",
55+
"<IT::>can parse floating-point numbers and fail",
56+
"",
57+
"<FAILED::>expected: 2.6<:LF:> but got: 2.5",
58+
"",
59+
"<COMPLETEDIN::>0.000",
60+
"",
61+
"<COMPLETEDIN::>0.000"
62+
]
63+
64+
it "handles crashing tests" $ do
65+
r <- runSpecWithCodewars $ do
66+
H.it "crash" $ do
67+
read (last $ tail $ ["10"]) `shouldBe` (11 :: Int)
68+
r
69+
`shouldBe` [ "",
70+
"<IT::>crash",
71+
"",
72+
"<ERROR::>uncaught exception: ErrorCall<:LF:>Prelude.last: empty list",
73+
"",
74+
"<COMPLETEDIN::>0.000"
75+
]
76+
77+
it "handles pending tests" $ do
78+
r <- runSpecWithCodewars $ do
79+
H.it "not implemented" $ do
80+
H.pending
81+
H.it "not implemented, with a message" $ do
82+
H.pendingWith "Not implemented yet"
83+
84+
r
85+
`shouldBe` [ "",
86+
"<IT::>not implemented",
87+
"",
88+
"<FAILED::>Test pending: no reason given",
89+
"",
90+
"<COMPLETEDIN::>0.000",
91+
"",
92+
"<IT::>not implemented, with a message",
93+
"",
94+
"<FAILED::>Test pending: Not implemented yet",
95+
"",
96+
"<COMPLETEDIN::>0.000"
97+
]
98+
99+
it "handles multiline names" $ do
100+
r <- runSpecWithCodewars $ do
101+
H.describe "Can present\nmultiline titles\nof groups" $ do
102+
H.it "Can present\nmultiline titles\nof items" $ do
103+
read "10" `shouldBe` (10 :: Int)
104+
r
105+
`shouldBe` [ "",
106+
"<DESCRIBE::>Can present<:LF:>multiline titles<:LF:>of groups",
107+
"",
108+
"<IT::>Can present<:LF:>multiline titles<:LF:>of items",
109+
"",
110+
"<PASSED::>Test Passed",
111+
"",
112+
"<COMPLETEDIN::>0.000",
113+
"",
114+
"<COMPLETEDIN::>0.000"
115+
]
116+
117+
it "handles multiline assertions" $ do
118+
r <- runSpecWithCodewars $ do
119+
H.it "present multiline assertions" $ do
120+
"10" `shouldBe` "10, but\nbroken into\nmultiple lines"
121+
r
122+
`shouldBe` [ "",
123+
"<IT::>present multiline assertions",
124+
"",
125+
"<FAILED::>expected: \"10, but\\nbroken into\\nmultiple lines\"<:LF:> but got: \"10\"",
126+
"",
127+
"<COMPLETEDIN::>0.000"
128+
]

0 commit comments

Comments
 (0)