Skip to content

minesweeper: Rewrite tests to use hspec with fail-fast. #265

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
Aug 10, 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/minesweeper/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- minesweeper
- HUnit
- hspec
104 changes: 48 additions & 56 deletions exercises/minesweeper/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,60 +1,52 @@
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Minesweeper (annotate)

exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

testCase :: String -> Assertion -> Test
testCase label assertion = TestLabel label (TestCase assertion)
specs :: Spec
specs = describe "minesweeper" $
describe "annotate" $ for_ cases test
where

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

clean :: [String] -> [String]
clean = map (map mineOrSpace)
where mineOrSpace '*' = '*'
mineOrSpace _ = ' '

check :: [String] -> Assertion
check board = board @=? annotate (clean board)

minesweeperTests :: [Test]
minesweeperTests =
[ testCase "zero size board" $ do
check []
, testCase "empty board" $ do
check [ " "
, " "
, " "
]
, testCase "board full of mines" $ do
check [ "***"
, "***"
, "***"
]
, testCase "surrounded" $ do
check [ "***"
, "*8*"
, "***"
]
, testCase "horizontal line" $ do
check [ "1*2*1" ]
, testCase "vertical line" $ do
check [ "1"
, "*"
, "2"
, "*"
, "1"
]
, testCase "cross" $ do
check [ " 2*2 "
, "25*52"
, "*****"
, "25*52"
, " 2*2 "
]
]
-- As of 2016-08-09, there was no reference file
-- for the test cases in `exercism/x-common`.

test (description, board) = it description assertion
where
assertion = annotate (clearBoard board) `shouldBe` board
clearBoard = map (map mineOrSpace)
mineOrSpace '*' = '*'
mineOrSpace _ = ' '

cases = [ ("zero size board" , [] )

, ("empty board" , [ " "
, " "
, " " ] )

, ("board full of mines" , [ "***"
, "***"
, "***" ] )

, ("surrounded", [ "***"
, "*8*"
, "***" ] )

, ("horizontal line", [ "1*2*1" ] )

, ("vertical line", [ "1"
, "*"
, "2"
, "*"
, "1" ] )

, ("cross", [ " 2*2 "
, "25*52"
, "*****"
, "25*52"
, " 2*2 " ] )
]