Skip to content

saddle-points: Rewrite tests to use hspec with fail-fast. #291

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 13, 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/saddle-points/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ tests:
source-dirs: test
dependencies:
- saddle-points
- HUnit
- hspec
87 changes: 43 additions & 44 deletions exercises/saddle-points/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import Test.HUnit (Assertion, (@=?), runTestTT, Test(..), Counts(..))
import System.Exit (ExitCode(..), exitWith)
import Matrix (saddlePoints)
import Data.Array (listArray)
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
import Data.Array (listArray)
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

testCase :: String -> Assertion -> Test
testCase label assertion = TestLabel label (TestCase assertion)
import Matrix (saddlePoints)

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

matrixTests :: [Test]
matrixTests =
[ testCase "Example from README" $ do
let matrix = listArray ((0, 0), (2, 2))
[ 9, 8, 7
, 5, 3, 2
, 6, 6, 7 ]
[(1, 0)] @=? saddlePoints matrix
, testCase "no saddle point" $ do
let matrix = listArray ((0, 0), (1, 1))
[ 2, 1
, 1, 2 ]
[] @=? saddlePoints matrix
, testCase "a saddle point" $ do
let matrix = listArray ((0, 0), (1, 1))
[ 1, 2
, 3, 4 ]
[(0, 1)] @=? saddlePoints matrix
, testCase "another saddle point" $ do
let matrix = listArray ((0, 0), (2, 4))
[ 18, 3, 39, 19, 91
, 38, 10, 8, 77, 320
, 3, 4, 8, 6, 7 ]
[(2, 2)] @=? saddlePoints matrix
, testCase "multiple saddle points" $ do
let matrix = listArray ((0, 0), (2, 2))
[ 4, 5, 4
, 3, 5, 5
, 1, 5, 4 ]
[(0, 1), (1, 1), (2, 1)] @=? saddlePoints matrix
]
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "saddle-points" $
describe "saddlePoints" $ for_ cases test
where

test (description, xss, expected) = it description assertion
where
assertion = saddlePoints matrix `shouldBe` expected
rows = length xss
columns = length $ head xss
matrix = listArray ((0, 0), (rows - 1, columns - 1)) (concat xss)

-- As of 2016-09-12, there was no reference file
-- for the test cases in `exercism/x-common`.

cases = [ ("Example from README", [ [9, 8, 7]
, [5, 3, 2]
, [6, 6, 7] ], [(1, 0)] )

, ( "no saddle point", [ [2, 1]
, [1, 2] ], [] )

, ( "a saddle point", [ [1, 2]
, [3, 4] ], [(0, 1)] )

, ( "another saddle point", [ [18, 3, 39, 19, 91]
, [38, 10, 8, 77, 320]
, [ 3, 4, 8, 6, 7] ], [(2, 2)] )

, ("multiple saddle points", [ [4, 5, 4]
, [3, 5, 5]
, [1, 5, 4] ], [ (0, 1)
, (1, 1)
, (2, 1) ] )
]