1
- import Test.HUnit (Assertion , (@=?) , runTestTT , Test (.. ), Counts (.. ))
1
+ import Data.Char (toUpper )
2
+ import Test.Hspec (Spec , describe , it , shouldBe )
3
+ import Test.Hspec.Runner (configFastFail , defaultConfig , hspecWith )
4
+
2
5
import Accumulate (accumulate )
3
- import System.Exit (ExitCode (.. ), exitWith )
4
- import Data.Char (toUpper )
5
6
6
- exitProperly :: IO Counts -> IO ()
7
- exitProperly m = do
8
- counts <- m
9
- exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
7
+ main :: IO ()
8
+ main = hspecWith defaultConfig {configFastFail = True } specs
10
9
11
- testCase :: String -> Assertion -> Test
12
- testCase label assertion = TestLabel label ( TestCase assertion)
10
+ specs :: Spec
11
+ specs = describe " accumulate " $ do
13
12
14
- main :: IO ()
15
- main = exitProperly $ runTestTT $ TestList
16
- [ TestList accumulateTests ]
17
-
18
- square :: Int -> Int
19
- square x = x * x
20
-
21
- accumulateTests :: [Test ]
22
- accumulateTests =
23
- [ testCase " empty accumulation" $
24
- [] @=? accumulate square []
25
- , testCase " accumulate squares" $
26
- [1 , 4 , 9 ] @=? accumulate square [1 , 2 , 3 ]
27
- , testCase " accumulate upcases" $
28
- [" HELLO" , " WORLD" ] @=? accumulate (map toUpper) [" hello" , " world" ]
29
- , testCase " accumulate reversed strings" $
30
- [" eht" , " kciuq" , " nworb" , " xof" , " cte" ] @=?
31
- accumulate reverse [" the" , " quick" , " brown" , " fox" , " etc" ]
32
- , testCase " accumulate recursively" $
33
- [[" a1" , " a2" , " a3" ], [" b1" , " b2" , " b3" ], [" c1" , " c2" , " c3" ]] @=?
34
- accumulate (\ c -> accumulate ((c: ) . show ) ([1 , 2 , 3 ] :: [Int ])) " abc"
35
- , testCase " accumulate non-strict" $
36
- [" nice work!" ] @=?
37
- take 1 (accumulate id
38
- (" nice work!" :
39
- error " accumulate should be even lazier, don't use reverse!" ))
40
- ]
13
+ -- As of 2016-07-27, there was no reference file
14
+ -- for the test cases in `exercism/x-common`.
15
+
16
+ let square x = x * x :: Int
17
+
18
+ it " empty accumulation" $
19
+ accumulate square []
20
+ `shouldBe` []
21
+
22
+ it " accumulate squares" $
23
+ accumulate square [1 , 2 , 3 ]
24
+ `shouldBe` [1 , 4 , 9 ]
25
+
26
+ it " accumulate upcases" $
27
+ accumulate (map toUpper) [" hello" , " world" ]
28
+ `shouldBe` [" HELLO" , " WORLD" ]
29
+
30
+ it " accumulate reversed strings" $
31
+ accumulate reverse [" the" , " quick" , " brown" , " fox" , " etc" ]
32
+ `shouldBe` [" eht" , " kciuq" , " nworb" , " xof" , " cte" ]
33
+
34
+ it " accumulate recursively" $
35
+ accumulate (\ c -> accumulate ((c: ) . show ) ([1 , 2 , 3 ] :: [Int ])) " abc"
36
+ `shouldBe` [[" a1" , " a2" , " a3" ], [" b1" , " b2" , " b3" ], [" c1" , " c2" , " c3" ]]
37
+
38
+ it " accumulate non-strict" $
39
+ take 1 (accumulate id (" nice work!" : error " accumulate should be even lazier, don't use reverse!" ))
40
+ `shouldBe` [" nice work!" ]
0 commit comments