From e043acd2441a149a5acc3035d6c6a686fec0c76d Mon Sep 17 00:00:00 2001 From: rbasso Date: Mon, 12 Sep 2016 12:52:00 +0900 Subject: [PATCH] saddle-points: Rewrite tests to use hspec with fail-fast. --- exercises/saddle-points/package.yaml | 2 +- exercises/saddle-points/test/Tests.hs | 87 +++++++++++++-------------- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/exercises/saddle-points/package.yaml b/exercises/saddle-points/package.yaml index 93c13ef68..19c2a377d 100644 --- a/exercises/saddle-points/package.yaml +++ b/exercises/saddle-points/package.yaml @@ -18,4 +18,4 @@ tests: source-dirs: test dependencies: - saddle-points - - HUnit + - hspec diff --git a/exercises/saddle-points/test/Tests.hs b/exercises/saddle-points/test/Tests.hs index f7355a62d..e8a68e89b 100644 --- a/exercises/saddle-points/test/Tests.hs +++ b/exercises/saddle-points/test/Tests.hs @@ -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) ] ) + ]