Skip to content

Commit

Permalink
Merge pull request #292 from rbasso/hspec-say
Browse files Browse the repository at this point in the history
say: Rewrite tests to use hspec with fail-fast.
  • Loading branch information
rbasso authored Sep 15, 2016
2 parents d474b52 + 4d3f835 commit 66c8f4c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 50 deletions.
2 changes: 1 addition & 1 deletion exercises/say/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- say
- HUnit
- hspec
88 changes: 39 additions & 49 deletions exercises/say/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,54 +1,44 @@
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
import Say (inEnglish)
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

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

testCase :: String -> Assertion -> Test
testCase label assertion = TestLabel label (TestCase assertion)
import Say (inEnglish)

main :: IO ()
main = exitProperly $ runTestTT $ TestList
[ TestList inEnglishTests ]
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "say" $
describe "inEnglish" $ for_ cases test
where

test (n, expected) = it description assertion
where
description = show n
assertion = inEnglish n `shouldBe` expected

-- As of 2016-09-12, there was no reference file
-- for the test cases in `exercism/x-common`.

inEnglishTests :: [Test]
inEnglishTests =
[ testCase "zero" $
Just "zero" @=? inEnglish (0::Int)
, testCase "one" $
Just "one" @=? inEnglish (1::Integer)
, testCase "fourteen" $
Just "fourteen" @=? inEnglish (14::Int)
, testCase "twenty" $
Just "twenty" @=? inEnglish (20::Int)
, testCase "twenty-two" $
Just "twenty-two" @=? inEnglish (22::Int)
, testCase "one hundred" $
Just "one hundred" @=? inEnglish (100::Int)
, testCase "one hundred twenty-three" $
Just "one hundred twenty-three" @=? inEnglish (123::Int)
, testCase "one thousand" $
Just "one thousand" @=? inEnglish (1000::Int)
, testCase "one thousand two hundred thirty-four" $
Just "one thousand two hundred thirty-four" @=? inEnglish (1234::Int)
, testCase "one million" $
Just "one million" @=? inEnglish (1000000::Int)
, testCase "one million two" $
Just "one million two" @=? inEnglish (1000002::Int)
, testCase "one million two thousand three hundred forty-five" $
Just "one million two thousand three hundred forty-five" @=?
inEnglish (1002345::Int)
, testCase "one billion" $
Just "one billion" @=? inEnglish (1000000000::Int)
, testCase "a big number" $
Just "nine hundred eighty-seven billion six hundred fifty-four million \
\three hundred twenty-one thousand one hundred twenty-three" @=?
inEnglish (987654321123::Integer)
, testCase "lower bound" $
Nothing @=? inEnglish (-1::Integer)
, testCase "upper bound" $
Nothing @=? inEnglish (1000000000000::Integer)
]
cases = [ ( 0, Just "zero" )
, ( 1, Just "one" )
, ( 14, Just "fourteen" )
, ( 20, Just "twenty" )
, ( 22, Just "twenty-two" )
, ( 100, Just "one hundred" )
, ( 123, Just "one hundred twenty-three" )
, ( 1000, Just "one thousand" )
, ( 1234, Just "one thousand two hundred thirty-four")
, ( 1000000, Just "one million" )
, ( 1000002, Just "one million two" )
, ( 1002345, Just "one million two thousand three \
\hundred forty-five" )
, ( 1000000000, Just "one billion" )
, ( 987654321123, Just "nine hundred eighty-seven billion \
\six hundred fifty-four million \
\three hundred twenty-one thousand \
\one hundred twenty-three" )
, ( -1, Nothing )
]

0 comments on commit 66c8f4c

Please sign in to comment.