Skip to content

robot-name: Rewrite tests to use rspec with fail-fast. #234

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 31, 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/robot-name/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- robot-name
- HUnit
- hspec
134 changes: 67 additions & 67 deletions exercises/robot-name/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
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
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'