From c03b2d02148e068d73a5aa5602e7d004aa9dfdb6 Mon Sep 17 00:00:00 2001 From: rbasso Date: Tue, 26 Jul 2016 22:37:40 +0900 Subject: [PATCH] robot-name: Rewrite tests to use rspec with fail-fast. --- exercises/robot-name/package.yaml | 2 +- exercises/robot-name/test/Tests.hs | 134 ++++++++++++++--------------- 2 files changed, 68 insertions(+), 68 deletions(-) diff --git a/exercises/robot-name/package.yaml b/exercises/robot-name/package.yaml index e677ca2fe..ee6a59e97 100644 --- a/exercises/robot-name/package.yaml +++ b/exercises/robot-name/package.yaml @@ -17,4 +17,4 @@ tests: source-dirs: test dependencies: - robot-name - - HUnit + - hspec diff --git a/exercises/robot-name/test/Tests.hs b/exercises/robot-name/test/Tests.hs index 4d75954b6..440f0bce7 100644 --- a/exercises/robot-name/test/Tests.hs +++ b/exercises/robot-name/test/Tests.hs @@ -1,21 +1,13 @@ -import Test.HUnit (Assertion, (@?), (@=?), runTestTT, Test(..), Counts(..)) -import System.Exit (ExitCode(..), exitWith) -import Robot (robotName, mkRobot, resetName) -import Control.Applicative -import Data.Ix (inRange) -import Prelude +{-# 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.Ix (inRange) +import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy) +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) -testCase :: String -> Assertion -> Test -testCase label assertion = TestLabel label (TestCase assertion) +import Robot (mkRobot, resetName, robotName) main :: IO () -main = exitProperly $ runTestTT $ TestList - [ TestList robotTests ] +main = hspecWith defaultConfig {configFastFail = True} specs {- These tests of course *can* fail since we are expected to use a random number @@ -23,57 +15,65 @@ generator. The chances of this kind of failure are very small. A real "robot generator" would use a proper serial number system and would likely not be in the business of resetting the name. -} -robotTests :: [Test] -robotTests = - [ testCase "name should match expected pattern" $ - matchesPattern <$> (mkRobot >>= robotName) @? - "name did not match expected pattern" - , testCase "name is persistent" $ do - r <- mkRobot - n1 <- robotName r - n2 <- robotName r - n3 <- robotName r - n1 @=? n2 - n1 @=? n3 - , testCase "different robots have different names" $ do - n1 <- mkRobot >>= robotName - n2 <- mkRobot >>= robotName - n1 /= n2 @? "different robots should have different names" - , testCase "new name should match expected pattern" $ do - r <- mkRobot - resetName r - matchesPattern <$> robotName r @? "name did not match expected pattern" - , testCase "new name is persistent" $ do - r <- mkRobot - resetName r - n1 <- robotName r - n2 <- robotName r - n3 <- robotName r - n1 @=? n2 - n1 @=? n3 - , testCase "new name is different from old name" $ do - r <- mkRobot - n1 <- robotName r - resetName r - n2 <- robotName r - n1 /= n2 @? "name should change when reset" - , testCase "resetting a robot affects only one robot" $ do - r1 <- mkRobot - r2 <- mkRobot - n1 <- robotName r1 - n2 <- robotName r2 - n1 /= n2 @? "different robots should have different names" - resetName r1 - n1' <- robotName r1 - n2' <- robotName r2 - n1' /= n2' @? "names should be different" - n2 @=? n2' - ] +specs :: Spec +specs = describe "robot-name" $ do -matchesPattern :: String -> Bool -matchesPattern s = - length s == 5 && - and (zipWith inRange [a, a, d, d, d] s) - where - a = ('A', 'Z') - d = ('0', '9') + -- As of 2016-07-30, there was no reference file + -- for the test cases in `exercism/x-common`. + + let a = ('A', 'Z') + let d = ('0', '9') + let matchesPattern s = length s == 5 + && and (zipWith inRange [a, a, d, d, d] s) + + -- `shouldNotBe` is not availabe on lts-2.22. + let x `shouldNotBe` y = x `shouldSatisfy` (/= y) + + it "name should match expected pattern" $ + mkRobot >>= robotName >>= (`shouldSatisfy` matchesPattern) + + it "name is persistent" $ do + r <- mkRobot + n1 <- robotName r + n2 <- robotName r + n3 <- robotName r + n1 `shouldBe` n2 + n1 `shouldBe` n3 + + it "different robots have different names" $ do + n1 <- mkRobot >>= robotName + n2 <- mkRobot >>= robotName + n1 `shouldNotBe` n2 + + it "new name should match expected pattern" $ do + r <- mkRobot + resetName r + robotName r >>= (`shouldSatisfy` matchesPattern) + + it "new name is persistent" $ do + r <- mkRobot + resetName r + n1 <- robotName r + n2 <- robotName r + n3 <- robotName r + n1 `shouldBe` n2 + n1 `shouldBe` n3 + + it "new name is different from old name" $ do + r <- mkRobot + n1 <- robotName r + resetName r + n2 <- robotName r + n1 `shouldNotBe` n2 + + it "resetting a robot affects only one robot" $ do + r1 <- mkRobot + r2 <- mkRobot + n1 <- robotName r1 + n2 <- robotName r2 + n1 `shouldNotBe` n2 + resetName r1 + n1' <- robotName r1 + n2' <- robotName r2 + n1' `shouldNotBe` n2' + n2 `shouldBe` n2'