From 4d3f8359721be2b44b6f659794a40ef1da0a4db9 Mon Sep 17 00:00:00 2001 From: rbasso Date: Mon, 12 Sep 2016 15:04:10 +0900 Subject: [PATCH] say: Rewrite tests to use hspec with fail-fast. - Rewrite tests to use `hspec`. - Remote upper-limit test. --- exercises/say/package.yaml | 2 +- exercises/say/test/Tests.hs | 88 ++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/exercises/say/package.yaml b/exercises/say/package.yaml index 92a3bdb12..7cac30cbf 100644 --- a/exercises/say/package.yaml +++ b/exercises/say/package.yaml @@ -17,4 +17,4 @@ tests: source-dirs: test dependencies: - say - - HUnit + - hspec diff --git a/exercises/say/test/Tests.hs b/exercises/say/test/Tests.hs index ff28e0317..e73712b5b 100644 --- a/exercises/say/test/Tests.hs +++ b/exercises/say/test/Tests.hs @@ -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 ) + ]