Skip to content

leap: Rewrite tests to use hspec with fail-fast. #212

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/leap/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ tests:
source-dirs: test
dependencies:
- leap
- HUnit
- hspec
74 changes: 51 additions & 23 deletions exercises/leap/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE RecordWildCards #-}

import Control.Monad (unless)
import System.Exit (exitFailure)

import Test.HUnit
( (~:)
, (~=?)
, Counts (failures, errors)
, Test (TestList)
, runTestTT
)
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import LeapYear (isLeapYear)

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

isLeapYearTests :: Test
isLeapYearTests = TestList $ map test cases
specs :: Spec
specs = describe "leap" $
describe "isLeapYear" $ for_ cases test
where
test (label, year, expected) = label ~: isLeapYear year ~=? expected
cases = [ ("leap year" , 1996, True )
, ("standard and odd year" , 1997, False)
, ("standard even year" , 1998, False)
, ("standard nineteenth century", 1900, False)
, ("standard eighteenth century", 1800, False)
, ("leap twenty fourth century" , 2400, True )
, ("leap y2k" , 2000, True ) ]

test Case{..} = it explanation assertion
where
explanation = unwords [show input, "-", description]
assertion = isLeapYear (fromIntegral input) `shouldBe` expected

-- Test cases adapted from `exercism/x-common/leap.json` on 2016-07-27.

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

cases :: [Case]
cases = [ Case { description = "leap year"
, input = 1996
, expected = True
}
, Case { description = "standard and odd year"
, input = 1997
, expected = False
}
, Case { description = "standard even year"
, input = 1998
, expected = False
}
, Case { description = "standard nineteenth century"
, input = 1900
, expected = False
}
, Case { description = "standard eighteenth century"
, input = 1800
, expected = False
}
, Case { description = "leap twenty fourth century"
, input = 2400
, expected = True
}
, Case { description = "leap y2k"
, input = 2000
, expected = True
}
]