hspec-expectations 0.7.0 → 0.7.1
raw patch · 2 files changed
+59/−28 lines, 2 filesdep ~basenew-uploaderPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
Files
- hspec-expectations.cabal +1/−1
- src/Test/Hspec/Expectations.hs +58/−27
hspec-expectations.cabal view
@@ -1,5 +1,5 @@ name: hspec-expectations-version: 0.7.0+version: 0.7.1 synopsis: Catchy combinators for HUnit description: Catchy combinators for HUnit: <https://github.com/sol/hspec-expectations#readme> license: MIT
src/Test/Hspec/Expectations.hs view
@@ -1,3 +1,8 @@+{-# LANGUAGE CPP #-}+#if MIN_VERSION_base(4,8,1)+#define HAS_SOURCE_LOCATIONS+{-# LANGUAGE ImplicitParams #-}+#endif -- | -- Introductory documentation: <https://github.com/sol/hspec-expectations#readme> module Test.Hspec.Expectations (@@ -44,89 +49,115 @@ , errorCall ) where -import Test.HUnit (Assertion, (@?=), assertBool, assertFailure)+import qualified Test.HUnit import Control.Exception import Data.Typeable import Data.List +import Control.Monad (unless)+ import Test.Hspec.Expectations.Matcher -type Expectation = Assertion+#ifdef HAS_SOURCE_LOCATIONS --- | This is just an alias for HUnit's `assertFailure`.-expectationFailure :: String -> Expectation-expectationFailure = assertFailure+import GHC.SrcLoc+import GHC.Stack +#define with_loc(NAME, TYPE) NAME :: (?loc :: CallStack) => TYPE++#else++#define with_loc(NAME, TYPE) NAME :: TYPE++#endif++type Expectation = Test.HUnit.Assertion++with_loc(expectationFailure, String -> Expectation)+expectationFailure = Test.HUnit.assertFailure . (location ++)+ where+ location :: String+#ifdef HAS_SOURCE_LOCATIONS+ location = case reverse (getCallStack ?loc) of+ (_, loc) : _ -> srcLocFile loc ++ ":" ++ (show $ srcLocStartLine loc) ++ ":\n"+ _ -> ""+#else+ location = ""+#endif++with_loc(expectTrue, String -> Bool -> Expectation)+expectTrue msg b = unless b (expectationFailure msg)+ infix 1 `shouldBe`, `shouldSatisfy`, `shouldStartWith`, `shouldEndWith`, `shouldContain`, `shouldMatchList`, `shouldReturn`, `shouldThrow` infix 1 `shouldNotBe`, `shouldNotSatisfy`, `shouldNotContain`, `shouldNotReturn` -- | -- @actual \`shouldBe\` expected@ sets the expectation that @actual@ is equal--- to @expected@ (this is just an alias for `@?=`).-shouldBe :: (Show a, Eq a) => a -> a -> Expectation-actual `shouldBe` expected = actual @?= expected+-- to @expected@.+with_loc(shouldBe, (Show a, Eq a) => a -> a -> Expectation)+actual `shouldBe` expected = expectTrue ("expected: " ++ show expected ++ "\n but got: " ++ show actual) (actual == expected) -- | -- @v \`shouldSatisfy\` p@ sets the expectation that @p v@ is @True@.-shouldSatisfy :: (Show a) => a -> (a -> Bool) -> Expectation-v `shouldSatisfy` p = assertBool ("predicate failed on: " ++ show v) (p v)+with_loc(shouldSatisfy, (Show a) => a -> (a -> Bool) -> Expectation)+v `shouldSatisfy` p = expectTrue ("predicate failed on: " ++ show v) (p v) -compareWith :: (Show a, Eq a) => (a -> a -> Bool) -> String -> a -> a -> Expectation-compareWith comparator errorDesc result expected = assertBool errorMsg (comparator expected result)+with_loc(compareWith, (Show a, Eq a) => (a -> a -> Bool) -> String -> a -> a -> Expectation)+compareWith comparator errorDesc result expected = expectTrue errorMsg (comparator expected result) where errorMsg = show result ++ " " ++ errorDesc ++ " " ++ show expected -- | -- @list \`shouldStartWith\` prefix@ sets the expectation that @list@ starts with @prefix@,-shouldStartWith :: (Show a, Eq a) => [a] -> [a] -> Expectation+with_loc(shouldStartWith, (Show a, Eq a) => [a] -> [a] -> Expectation) shouldStartWith = compareWith isPrefixOf "does not start with" -- | -- @list \`shouldEndWith\` suffix@ sets the expectation that @list@ ends with @suffix@,-shouldEndWith :: (Show a, Eq a) => [a] -> [a] -> Expectation+with_loc(shouldEndWith, (Show a, Eq a) => [a] -> [a] -> Expectation) shouldEndWith = compareWith isSuffixOf "does not end with" -- | -- @list \`shouldContain\` sublist@ sets the expectation that @sublist@ is contained, -- wholly and intact, anywhere in @list@.-shouldContain :: (Show a, Eq a) => [a] -> [a] -> Expectation+with_loc(shouldContain, (Show a, Eq a) => [a] -> [a] -> Expectation) shouldContain = compareWith isInfixOf "does not contain" -- | -- @xs \`shouldMatchList\` ys@ sets the expectation that @xs@ has the same -- elements that @ys@ has, possibly in another order-shouldMatchList :: (Show a, Eq a) => [a] -> [a] -> Expectation-xs `shouldMatchList` ys = maybe (return ()) assertFailure (matchList xs ys)+with_loc(shouldMatchList, (Show a, Eq a) => [a] -> [a] -> Expectation)+xs `shouldMatchList` ys = maybe (return ()) expectationFailure (matchList xs ys) -- | -- @action \`shouldReturn\` expected@ sets the expectation that @action@ -- returns @expected@.-shouldReturn :: (Show a, Eq a) => IO a -> a -> Expectation+with_loc(shouldReturn, (Show a, Eq a) => IO a -> a -> Expectation) action `shouldReturn` expected = action >>= (`shouldBe` expected) -- | -- @actual \`shouldNotBe\` notExpected@ sets the expectation that @actual@ is not -- equal to @notExpected@-shouldNotBe :: (Show a, Eq a) => a -> a -> Expectation-actual `shouldNotBe` notExpected = assertBool ("not expected: " ++ show actual) (actual /= notExpected)+with_loc(shouldNotBe, (Show a, Eq a) => a -> a -> Expectation)+actual `shouldNotBe` notExpected = expectTrue ("not expected: " ++ show actual) (actual /= notExpected) -- | -- @v \`shouldNotSatisfy\` p@ sets the expectation that @p v@ is @False@.-shouldNotSatisfy :: (Show a) => a -> (a -> Bool) -> Expectation-v `shouldNotSatisfy` p = assertBool ("predicate succeded on: " ++ show v) ((not . p) v)+with_loc(shouldNotSatisfy, (Show a) => a -> (a -> Bool) -> Expectation)+v `shouldNotSatisfy` p = expectTrue ("predicate succeded on: " ++ show v) ((not . p) v) -- | -- @list \`shouldNotContain\` sublist@ sets the expectation that @sublist@ is not -- contained anywhere in @list@.-shouldNotContain :: (Show a, Eq a) => [a] -> [a] -> Expectation-list `shouldNotContain` sublist = assertBool errorMsg ((not . isInfixOf sublist) list)+with_loc(shouldNotContain, (Show a, Eq a) => [a] -> [a] -> Expectation)+list `shouldNotContain` sublist = expectTrue errorMsg ((not . isInfixOf sublist) list) where errorMsg = show list ++ " does contain " ++ show sublist -- | -- @action \`shouldNotReturn\` notExpected@ sets the expectation that @action@ -- does not return @notExpected@.-shouldNotReturn :: (Show a, Eq a) => IO a -> a -> Expectation+with_loc(shouldNotReturn, (Show a, Eq a) => IO a -> a -> Expectation) action `shouldNotReturn` notExpected = action >>= (`shouldNotBe` notExpected) -- |@@ -138,7 +169,7 @@ -- @action \`shouldThrow\` selector@ sets the expectation that @action@ throws -- an exception. The precise nature of the expected exception is described -- with a 'Selector'.-shouldThrow :: Exception e => IO a -> Selector e -> Expectation+with_loc(shouldThrow, Exception e => IO a -> Selector e -> Expectation) action `shouldThrow` p = do r <- try action case r of@@ -146,7 +177,7 @@ expectationFailure $ "did not get expected exception: " ++ exceptionType Left e ->- (`assertBool` p e) $+ (`expectTrue` p e) $ "predicate failed on expected exception: " ++ exceptionType ++ " (" ++ show e ++ ")" where -- a string repsentation of the expected exception's type