packages feed

hspec-wai 0.12.0 → 0.12.1

raw patch · 2 files changed

+47/−4 lines, 2 filesdep +HUnitPVP ok

version bump matches the API change (PVP)

Dependencies added: HUnit

API changes (from Hackage documentation)

+ Test.Hspec.Wai: annotate :: String -> WaiSession st a -> WaiSession st a

Files

hspec-wai.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack  name:             hspec-wai-version:          0.12.0+version:          0.12.1 homepage:         https://github.com/hspec/hspec-wai#readme bug-reports:      https://github.com/hspec/hspec-wai/issues license:          MIT@@ -32,7 +32,8 @@       src   ghc-options: -Wall   build-depends:-      QuickCheck+      HUnit+    , QuickCheck     , base >=4.9.1.0 && <5     , base-compat     , bytestring >=0.10@@ -64,7 +65,8 @@   build-tool-depends:       hspec-discover:hspec-discover   build-depends:-      QuickCheck+      HUnit+    , QuickCheck     , base >=4.9.1.0 && <5     , base-compat     , bytestring >=0.10
src/Test/Hspec/Wai.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE TupleSections #-} {-# LANGUAGE PackageImports #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE FlexibleContexts #-}@@ -37,6 +38,7 @@ , getState , pending , pendingWith+, annotate , SResponse(..) ) where @@ -46,15 +48,20 @@ import           Data.Foldable import           Data.ByteString (ByteString) import qualified Data.ByteString.Lazy as LB+import           Control.Exception (Exception, handle, throwIO) import           Control.Monad.IO.Class import           Control.Monad.Trans.Class (lift)+import           Control.Monad.Trans.Reader+import           Control.Monad.Trans.State (StateT(..))+ import           Network.Wai (Request(..)) import           Network.HTTP.Types import           Network.Wai.Test hiding (request) import qualified Network.Wai.Test as Wai+import           Test.HUnit.Lang (HUnitFailure(..), FailureReason(..)) import           Test.Hspec.Expectations -import           Test.Hspec.Core.Spec hiding (pending, pendingWith)+import           Test.Hspec.Core.Spec hiding (pending, pendingWith, FailureReason(..)) import qualified Test.Hspec.Core.Spec as Core import           Test.Hspec.Core.Hooks @@ -73,6 +80,40 @@ -- | A lifted version of `Core.pending`. pending :: WaiSession st () pending = liftIO Core.pending++-- | -- If you have a test case that has multiple assertions, you can use the+-- 'annotate' function to provide a string message that will be attached to the+-- 'Expectation'.+--+-- @+-- describe "annotate" $ do+--   it "adds the message" $ do+--     annotate "obvious falsehood" $ do+--       True `shouldBe` False+--+-- ========>+--+-- 1) annotate, adds the message+--       obvious falsehood+--       expected: False+--        but got: True+-- @+annotate :: String -> WaiSession st a -> WaiSession st a+annotate message = handleWai $ \ (HUnitFailure loc reason) -> throwIO . HUnitFailure loc $ case reason of+  Reason err -> Reason $ addMessage err+  ExpectedButGot err expected got -> ExpectedButGot (Just $ maybe message addMessage err) expected got+  where+    addMessage :: String -> String+    addMessage err+      | null err = message+      | otherwise = message ++ "\n" ++ err++    handleWai :: Exception e => (e -> IO a) -> WaiSession st a -> WaiSession st a+    handleWai k (WaiSession (ReaderT f)) = WaiSession $ ReaderT $ \st ->+      ReaderT $ \app -> do+        case flip runReaderT app $ f st of+          StateT runSt -> StateT $ \s ->+            handle (fmap (, s) . k) $ runSt s  -- | A lifted version of `Core.pendingWith`. pendingWith :: String -> WaiSession st ()