Skip to content

Commit 5fad15c

Browse files
authored
Merge pull request #213 from rbasso/hspec-accumulate
accumulate: Rewrite tests to use hspec with fail-fast.
2 parents 794f2af + aa67cad commit 5fad15c

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

exercises/accumulate/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ tests:
1616
source-dirs: test
1717
dependencies:
1818
- accumulate
19-
- HUnit
19+
- hspec

exercises/accumulate/test/Tests.hs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
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+
25
import Accumulate (accumulate)
3-
import System.Exit (ExitCode(..), exitWith)
4-
import Data.Char (toUpper)
56

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
109

11-
testCase :: String -> Assertion -> Test
12-
testCase label assertion = TestLabel label (TestCase assertion)
10+
specs :: Spec
11+
specs = describe "accumulate" $ do
1312

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

Comments
 (0)