-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from rbasso/hspec-say
say: Rewrite tests to use hspec with fail-fast.
- Loading branch information
Showing
2 changed files
with
40 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ tests: | |
source-dirs: test | ||
dependencies: | ||
- say | ||
- HUnit | ||
- hspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
] |