Skip to content

nth-prime: Rewrite tests to use hspec with fail-fast. #285

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
Sep 6, 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/nth-prime/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ tests:
source-dirs: test
dependencies:
- nth-prime
- HUnit
- hspec
9 changes: 7 additions & 2 deletions exercises/nth-prime/src/Example.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module Prime (nth) where

nth :: Int -> Integer
nth = (primes !!) . pred
nth :: Int -> Maybe Integer
nth x
| x <= 0 = Nothing
| otherwise = Just $ nth' x

nth' :: Int -> Integer
nth' = (primes !!) . pred

-- all primes > 5 are in the form of 6n+1 or 6n+5 (for n >= 1)
primes :: [Integer]
Expand Down
70 changes: 49 additions & 21 deletions exercises/nth-prime/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
import Test.HUnit ((@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
import Prime (nth)
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
import Prime (nth)

main :: IO ()
main = exitProperly $ runTestTT $ TestList
[ TestList primeTests ]

primeTests :: [Test]
primeTests = map TestCase
[ 2 @=? nth 1
, 3 @=? nth 2
, 13 @=? nth 6
, [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71] @=?
map nth [1..20]
, 7919 @=? nth 1000
, 104729 @=? nth 10000
, 104743 @=? nth 10001
]
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "nth-prime" $
describe "nth" $ for_ cases test
where

test Case{..} = it description assertion
where
assertion = nth (fromIntegral input) `shouldBe` expected

-- As of 2016-09-06, there was no reference file for the test cases in
-- `exercism/x-common`, so we adapted the test cases available in the
-- pull request `exercism/x-common#332`.

data Case = Case { description :: String
, input :: Integer
, expected :: Maybe Integer
}

cases :: [Case]
cases = [ Case { description = "first prime"
, input = 1
, expected = Just 2
}
, Case { description = "second prime"
, input = 2
, expected = Just 3
}
, Case { description = "sixth prime"
, input = 6
, expected = Just 13
}
, Case { description = "big prime"
, input = 10001
, expected = Just 104743
}
, Case { description = "there is no zeroth prime"
, input = 0
, expected = Nothing
}
]