Skip to content

Commit d1a6905

Browse files
authored
Merge pull request #221 from rbasso/hspec-hamming
hamming: Rewrite tests to use hspec with fail-fast.
2 parents facac66 + f446464 commit d1a6905

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

exercises/hamming/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
- hamming
19-
- HUnit
19+
- hspec

exercises/hamming/test/Tests.hs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
22
{-# LANGUAGE RecordWildCards #-}
33

4-
import Control.Monad (unless)
5-
import System.Exit (exitFailure)
4+
import Data.Foldable (for_)
5+
import Test.Hspec (Spec, describe, it, shouldBe)
6+
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
67

7-
import Test.HUnit
8-
( (~:)
9-
, (~=?)
10-
, Counts (failures, errors)
11-
, Test (TestList)
12-
, runTestTT
13-
)
8+
-- base >= 4.8 re-exports Control.Applicative.(<$>).
9+
import Control.Applicative -- This is only need for <$>, if GHC < 7.10.
10+
import Prelude -- This trick avoids a warning if GHC >= 7.10.
1411

1512
import Hamming (distance)
1613

1714
main :: IO ()
18-
main = do
19-
counts <- runTestTT tests
20-
unless (failures counts == 0 && errors counts == 0) exitFailure
15+
main = hspecWith defaultConfig {configFastFail = True} specs
2116

22-
tests :: Test
23-
tests = TestList $ map test cases
17+
specs :: Spec
18+
specs = describe "hamming" $
19+
describe "distance" $ for_ cases test
2420
where
25-
test (Case {..}) = description
26-
~: fmap fromIntegral (distance strand1 strand2)
27-
~=? expected
2821

29-
-- Test cases adapted from x-common/hamming.json from 2016-03-13.
22+
test Case{..} = it description assertion
23+
where
24+
assertion = expression `shouldBe` fromIntegral <$> expected
25+
expression = distance strand1 strand2
26+
27+
-- Test cases adapted from `exercism/x-common/hamming.json` on 2016-07-12.
28+
3029
data Case = Case { description :: String
3130
, strand1 :: String
3231
, strand2 :: String

0 commit comments

Comments
 (0)