Skip to content

Commit 66c8f4c

Browse files
authored
Merge pull request #292 from rbasso/hspec-say
say: Rewrite tests to use hspec with fail-fast.
2 parents d474b52 + 4d3f835 commit 66c8f4c

File tree

2 files changed

+40
-50
lines changed

2 files changed

+40
-50
lines changed

exercises/say/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ tests:
1717
source-dirs: test
1818
dependencies:
1919
- say
20-
- HUnit
20+
- hspec

exercises/say/test/Tests.hs

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,44 @@
1-
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
2-
import System.Exit (ExitCode(..), exitWith)
3-
import Say (inEnglish)
1+
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
42

5-
exitProperly :: IO Counts -> IO ()
6-
exitProperly m = do
7-
counts <- m
8-
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
3+
import Data.Foldable (for_)
4+
import Test.Hspec (Spec, describe, it, shouldBe)
5+
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
96

10-
testCase :: String -> Assertion -> Test
11-
testCase label assertion = TestLabel label (TestCase assertion)
7+
import Say (inEnglish)
128

139
main :: IO ()
14-
main = exitProperly $ runTestTT $ TestList
15-
[ TestList inEnglishTests ]
10+
main = hspecWith defaultConfig {configFastFail = True} specs
11+
12+
specs :: Spec
13+
specs = describe "say" $
14+
describe "inEnglish" $ for_ cases test
15+
where
16+
17+
test (n, expected) = it description assertion
18+
where
19+
description = show n
20+
assertion = inEnglish n `shouldBe` expected
21+
22+
-- As of 2016-09-12, there was no reference file
23+
-- for the test cases in `exercism/x-common`.
1624

17-
inEnglishTests :: [Test]
18-
inEnglishTests =
19-
[ testCase "zero" $
20-
Just "zero" @=? inEnglish (0::Int)
21-
, testCase "one" $
22-
Just "one" @=? inEnglish (1::Integer)
23-
, testCase "fourteen" $
24-
Just "fourteen" @=? inEnglish (14::Int)
25-
, testCase "twenty" $
26-
Just "twenty" @=? inEnglish (20::Int)
27-
, testCase "twenty-two" $
28-
Just "twenty-two" @=? inEnglish (22::Int)
29-
, testCase "one hundred" $
30-
Just "one hundred" @=? inEnglish (100::Int)
31-
, testCase "one hundred twenty-three" $
32-
Just "one hundred twenty-three" @=? inEnglish (123::Int)
33-
, testCase "one thousand" $
34-
Just "one thousand" @=? inEnglish (1000::Int)
35-
, testCase "one thousand two hundred thirty-four" $
36-
Just "one thousand two hundred thirty-four" @=? inEnglish (1234::Int)
37-
, testCase "one million" $
38-
Just "one million" @=? inEnglish (1000000::Int)
39-
, testCase "one million two" $
40-
Just "one million two" @=? inEnglish (1000002::Int)
41-
, testCase "one million two thousand three hundred forty-five" $
42-
Just "one million two thousand three hundred forty-five" @=?
43-
inEnglish (1002345::Int)
44-
, testCase "one billion" $
45-
Just "one billion" @=? inEnglish (1000000000::Int)
46-
, testCase "a big number" $
47-
Just "nine hundred eighty-seven billion six hundred fifty-four million \
48-
\three hundred twenty-one thousand one hundred twenty-three" @=?
49-
inEnglish (987654321123::Integer)
50-
, testCase "lower bound" $
51-
Nothing @=? inEnglish (-1::Integer)
52-
, testCase "upper bound" $
53-
Nothing @=? inEnglish (1000000000000::Integer)
54-
]
25+
cases = [ ( 0, Just "zero" )
26+
, ( 1, Just "one" )
27+
, ( 14, Just "fourteen" )
28+
, ( 20, Just "twenty" )
29+
, ( 22, Just "twenty-two" )
30+
, ( 100, Just "one hundred" )
31+
, ( 123, Just "one hundred twenty-three" )
32+
, ( 1000, Just "one thousand" )
33+
, ( 1234, Just "one thousand two hundred thirty-four")
34+
, ( 1000000, Just "one million" )
35+
, ( 1000002, Just "one million two" )
36+
, ( 1002345, Just "one million two thousand three \
37+
\hundred forty-five" )
38+
, ( 1000000000, Just "one billion" )
39+
, ( 987654321123, Just "nine hundred eighty-seven billion \
40+
\six hundred fifty-four million \
41+
\three hundred twenty-one thousand \
42+
\one hundred twenty-three" )
43+
, ( -1, Nothing )
44+
]

0 commit comments

Comments
 (0)