Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

say: Rewrite tests to use hspec with fail-fast. #292

Merged
merged 1 commit into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
]