|
Packit |
6375e3 |
{-# LANGUAGE ConstraintKinds #-}
|
|
Packit |
6375e3 |
{-# LANGUAGE FlexibleContexts #-}
|
|
Packit |
6375e3 |
module Test.Hspec.ExpectationsSpec (spec) where
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
import Control.Exception
|
|
Packit |
6375e3 |
import Test.HUnit.Lang
|
|
Packit |
6375e3 |
import Test.Hspec (Spec, describe, it)
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
import Test.Hspec.Expectations hiding (HasCallStack)
|
|
Packit |
6375e3 |
import Data.CallStack
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
expectationFailed :: HasCallStack => FailureReason -> HUnitFailure -> Bool
|
|
Packit |
6375e3 |
expectationFailed msg (HUnitFailure l m) = m == msg && (fmap setColumn l) == (fmap setColumn location)
|
|
Packit |
6375e3 |
where
|
|
Packit |
6375e3 |
location = case reverse callStack of
|
|
Packit |
6375e3 |
[] -> Nothing
|
|
Packit |
6375e3 |
(_, loc) : _ -> Just loc
|
|
Packit |
6375e3 |
location :: Maybe SrcLoc
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
setColumn loc_ = loc_{srcLocStartCol = 0, srcLocEndCol = 0}
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
spec :: Spec
|
|
Packit |
6375e3 |
spec = do
|
|
Packit |
6375e3 |
describe "shouldBe" $ do
|
|
Packit |
6375e3 |
it "succeeds if arguments are equal" $ do
|
|
Packit |
6375e3 |
"foo" `shouldBe` "foo"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if arguments are not equal" $ do
|
|
Packit |
6375e3 |
("foo" `shouldBe` "bar") `shouldThrow` expectationFailed (ExpectedButGot Nothing "\"bar\"" "\"foo\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldSatisfy" $ do
|
|
Packit |
6375e3 |
it "succeeds if value satisfies predicate" $ do
|
|
Packit |
6375e3 |
"" `shouldSatisfy` null
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if value does not satisfy predicate" $ do
|
|
Packit |
6375e3 |
("foo" `shouldSatisfy` null) `shouldThrow` expectationFailed (Reason "predicate failed on: \"foo\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldReturn" $ do
|
|
Packit |
6375e3 |
it "succeeds if arguments represent equal values" $ do
|
|
Packit |
6375e3 |
return "foo" `shouldReturn` "foo"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if arguments do not represent equal values" $ do
|
|
Packit |
6375e3 |
(return "foo" `shouldReturn` "bar") `shouldThrow` expectationFailed (ExpectedButGot Nothing "\"bar\"" "\"foo\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldStartWith" $ do
|
|
Packit |
6375e3 |
it "succeeds if second is prefix of first" $ do
|
|
Packit |
6375e3 |
"hello world" `shouldStartWith` "hello"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if second is not prefix of first" $ do
|
|
Packit |
6375e3 |
("hello world" `shouldStartWith` "world") `shouldThrow` expectationFailed (Reason "\"hello world\" does not start with \"world\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldEndWith" $ do
|
|
Packit |
6375e3 |
it "succeeds if second is suffix of first" $ do
|
|
Packit |
6375e3 |
"hello world" `shouldEndWith` "world"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if second is not suffix of first" $ do
|
|
Packit |
6375e3 |
("hello world" `shouldEndWith` "hello") `shouldThrow` expectationFailed (Reason "\"hello world\" does not end with \"hello\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldContain" $ do
|
|
Packit |
6375e3 |
it "succeeds if second argument is contained in the first" $ do
|
|
Packit |
6375e3 |
"I'm an hello world message" `shouldContain` "an hello"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if first argument does not contain the second" $ do
|
|
Packit |
6375e3 |
("foo" `shouldContain` "bar") `shouldThrow` expectationFailed (Reason "\"foo\" does not contain \"bar\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldNotBe" $ do
|
|
Packit |
6375e3 |
it "succeeds if arguments are not equal" $ do
|
|
Packit |
6375e3 |
"foo" `shouldNotBe` "bar"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if arguments are equal" $ do
|
|
Packit |
6375e3 |
("foo" `shouldNotBe` "foo") `shouldThrow` expectationFailed (Reason "not expected: \"foo\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldNotSatisfy" $ do
|
|
Packit |
6375e3 |
it "succeeds if value does not satisfy predicate" $ do
|
|
Packit |
6375e3 |
"bar" `shouldNotSatisfy` null
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if the value does satisfy predicate" $ do
|
|
Packit |
6375e3 |
("" `shouldNotSatisfy` null) `shouldThrow` expectationFailed (Reason "predicate succeeded on: \"\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldNotReturn" $ do
|
|
Packit |
6375e3 |
it "succeeds if arguments does not represent equal values" $ do
|
|
Packit |
6375e3 |
return "foo" `shouldNotReturn` "bar"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if arguments do represent equal values" $ do
|
|
Packit |
6375e3 |
(return "foo" `shouldNotReturn` "foo") `shouldThrow` expectationFailed (Reason "not expected: \"foo\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldNotContain" $ do
|
|
Packit |
6375e3 |
it "succeeds if second argument is not contained in the first" $ do
|
|
Packit |
6375e3 |
"I'm an hello world message" `shouldNotContain` "test"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails if first argument does contain the second" $ do
|
|
Packit |
6375e3 |
("foo abc def" `shouldNotContain` "def") `shouldThrow` expectationFailed (Reason "\"foo abc def\" does contain \"def\"")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
describe "shouldThrow" $ do
|
|
Packit |
6375e3 |
it "can be used to require a specific exception" $ do
|
|
Packit |
6375e3 |
throwIO DivideByZero `shouldThrow` (== DivideByZero)
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "can be used to require any exception" $ do
|
|
Packit |
6375e3 |
error "foobar" `shouldThrow` anyException
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "can be used to require an exception of a specific type" $ do
|
|
Packit |
6375e3 |
error "foobar" `shouldThrow` anyErrorCall
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "can be used to require a specific exception" $ do
|
|
Packit |
6375e3 |
error "foobar" `shouldThrow` errorCall "foobar"
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails, if a required specific exception is not thrown" $ do
|
|
Packit |
6375e3 |
(throwIO Overflow `shouldThrow` (== DivideByZero)) `shouldThrow` expectationFailed (Reason "predicate failed on expected exception: ArithException (arithmetic overflow)")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails, if any exception is required, but no exception is thrown" $ do
|
|
Packit |
6375e3 |
(return () `shouldThrow` anyException) `shouldThrow` expectationFailed (Reason "did not get expected exception: SomeException")
|
|
Packit |
6375e3 |
|
|
Packit |
6375e3 |
it "fails, if an exception of a specific type is required, but no exception is thrown" $ do
|
|
Packit |
6375e3 |
(return () `shouldThrow` anyErrorCall) `shouldThrow` expectationFailed (Reason "did not get expected exception: ErrorCall")
|