Skip to content

hamming: Rewrite tests to use hspec with fail-fast. #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/hamming/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ tests:
source-dirs: test
dependencies:
- hamming
- HUnit
- hspec
35 changes: 17 additions & 18 deletions exercises/hamming/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}

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

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

import Hamming (distance)

main :: IO ()
main = do
counts <- runTestTT tests
unless (failures counts == 0 && errors counts == 0) exitFailure
main = hspecWith defaultConfig {configFastFail = True} specs

tests :: Test
tests = TestList $ map test cases
specs :: Spec
specs = describe "hamming" $
describe "distance" $ for_ cases test
where
test (Case {..}) = description
~: fmap fromIntegral (distance strand1 strand2)
~=? expected

-- Test cases adapted from x-common/hamming.json from 2016-03-13.
test Case{..} = it description assertion
where
assertion = expression `shouldBe` fromIntegral <$> expected
expression = distance strand1 strand2

-- Test cases adapted from `exercism/x-common/hamming.json` on 2016-07-12.

data Case = Case { description :: String
, strand1 :: String
, strand2 :: String
Expand Down