|
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 #-} |
4 | 2 |
|
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) |
9 | 6 |
|
10 |
| -testCase :: String -> Assertion -> Test |
11 |
| -testCase label assertion = TestLabel label (TestCase assertion) |
| 7 | +import Say (inEnglish) |
12 | 8 |
|
13 | 9 | 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`. |
16 | 24 |
|
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