{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ConstraintKinds #-}
-- | Stability: provisional
module Test.Hspec.Core.Hooks (
  before
, before_
, beforeWith
, beforeAll
, beforeAll_
, beforeAllWith
, after
, after_
, afterAll
, afterAll_
, around
, around_
, aroundWith
, aroundAll
, aroundAll_
, aroundAllWith

, mapSubject
, ignoreSubject
) where

import           Prelude ()
import           Test.Hspec.Core.Compat
import           Data.CallStack

import           Control.Exception (SomeException, finally, throwIO, try, catch)
import           Control.Concurrent.MVar
import           Control.Concurrent.Async

import           Test.Hspec.Core.Example
import           Test.Hspec.Core.Tree
import           Test.Hspec.Core.Spec.Monad

-- | Run a custom action before every spec item.
before :: IO a -> SpecWith a -> Spec
before :: IO a -> SpecWith a -> Spec
before IO a
action = (ActionWith a -> IO ()) -> SpecWith a -> Spec
forall a. (ActionWith a -> IO ()) -> SpecWith a -> Spec
around (IO a
action IO a -> ActionWith a -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=)

-- | Run a custom action before every spec item.
before_ :: IO () -> SpecWith a -> SpecWith a
before_ :: IO () -> SpecWith a -> SpecWith a
before_ IO ()
action = (IO () -> IO ()) -> SpecWith a -> SpecWith a
forall a. (IO () -> IO ()) -> SpecWith a -> SpecWith a
around_ (IO ()
action IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)

-- | Run a custom action before every spec item.
beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b
beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b
beforeWith b -> IO a
action = (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith ((ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b)
-> (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
forall a b. (a -> b) -> a -> b
$ \ActionWith a
e b
x -> b -> IO a
action b
x IO a -> ActionWith a -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ActionWith a
e

-- | Run a custom action before the first spec item.
beforeAll :: IO a -> SpecWith a -> Spec
beforeAll :: IO a -> SpecWith a -> Spec
beforeAll IO a
action SpecWith a
spec = do
  MVar (Memoized a)
mvar <- IO (MVar (Memoized a)) -> SpecM () (MVar (Memoized a))
forall r a. IO r -> SpecM a r
runIO (Memoized a -> IO (MVar (Memoized a))
forall a. a -> IO (MVar a)
newMVar Memoized a
forall a. Memoized a
Empty)
  IO a -> SpecWith a -> Spec
forall a. IO a -> SpecWith a -> Spec
before (MVar (Memoized a) -> IO a -> IO a
forall a. MVar (Memoized a) -> IO a -> IO a
memoize MVar (Memoized a)
mvar IO a
action) SpecWith a
spec

-- | Run a custom action before the first spec item.
beforeAll_ :: IO () -> SpecWith a -> SpecWith a
beforeAll_ :: IO () -> SpecWith a -> SpecWith a
beforeAll_ IO ()
action SpecWith a
spec = do
  MVar (Memoized ())
mvar <- IO (MVar (Memoized ())) -> SpecM a (MVar (Memoized ()))
forall r a. IO r -> SpecM a r
runIO (Memoized () -> IO (MVar (Memoized ()))
forall a. a -> IO (MVar a)
newMVar Memoized ()
forall a. Memoized a
Empty)
  IO () -> SpecWith a -> SpecWith a
forall a. IO () -> SpecWith a -> SpecWith a
before_ (MVar (Memoized ()) -> IO () -> IO ()
forall a. MVar (Memoized a) -> IO a -> IO a
memoize MVar (Memoized ())
mvar IO ()
action) SpecWith a
spec

-- | Run a custom action with an argument before the first spec item.
beforeAllWith :: (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith :: (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith b -> IO a
action SpecWith a
spec = do
  MVar (Memoized a)
mvar <- IO (MVar (Memoized a)) -> SpecM b (MVar (Memoized a))
forall r a. IO r -> SpecM a r
runIO (Memoized a -> IO (MVar (Memoized a))
forall a. a -> IO (MVar a)
newMVar Memoized a
forall a. Memoized a
Empty)
  (b -> IO a) -> SpecWith a -> SpecWith b
forall b a. (b -> IO a) -> SpecWith a -> SpecWith b
beforeWith (MVar (Memoized a) -> IO a -> IO a
forall a. MVar (Memoized a) -> IO a -> IO a
memoize MVar (Memoized a)
mvar (IO a -> IO a) -> (b -> IO a) -> b -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> IO a
action) SpecWith a
spec

data Memoized a =
    Empty
  | Memoized a
  | Failed SomeException

memoize :: MVar (Memoized a) -> IO a -> IO a
memoize :: MVar (Memoized a) -> IO a -> IO a
memoize MVar (Memoized a)
mvar IO a
action = do
  Either SomeException a
result <- MVar (Memoized a)
-> (Memoized a -> IO (Memoized a, Either SomeException a))
-> IO (Either SomeException a)
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar (Memoized a)
mvar ((Memoized a -> IO (Memoized a, Either SomeException a))
 -> IO (Either SomeException a))
-> (Memoized a -> IO (Memoized a, Either SomeException a))
-> IO (Either SomeException a)
forall a b. (a -> b) -> a -> b
$ \Memoized a
ma -> case Memoized a
ma of
    Memoized a
Empty -> do
      Either SomeException a
a <- IO a -> IO (Either SomeException a)
forall e a. Exception e => IO a -> IO (Either e a)
try IO a
action
      (Memoized a, Either SomeException a)
-> IO (Memoized a, Either SomeException a)
forall (m :: * -> *) a. Monad m => a -> m a
return ((SomeException -> Memoized a)
-> (a -> Memoized a) -> Either SomeException a -> Memoized a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SomeException -> Memoized a
forall a. SomeException -> Memoized a
Failed a -> Memoized a
forall a. a -> Memoized a
Memoized Either SomeException a
a, Either SomeException a
a)
    Memoized a
a -> (Memoized a, Either SomeException a)
-> IO (Memoized a, Either SomeException a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Memoized a
ma, a -> Either SomeException a
forall a b. b -> Either a b
Right a
a)
    Failed SomeException
_ -> ResultStatus -> IO (Memoized a, Either SomeException a)
forall e a. Exception e => e -> IO a
throwIO (Maybe Location -> Maybe String -> ResultStatus
Pending Maybe Location
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just String
"exception in beforeAll-hook (see previous failure)"))
  (SomeException -> IO a)
-> (a -> IO a) -> Either SomeException a -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SomeException -> IO a
forall e a. Exception e => e -> IO a
throwIO a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Either SomeException a
result

-- | Run a custom action after every spec item.
after :: ActionWith a -> SpecWith a -> SpecWith a
after :: ActionWith a -> SpecWith a -> SpecWith a
after ActionWith a
action = (ActionWith a -> ActionWith a) -> SpecWith a -> SpecWith a
forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith ((ActionWith a -> ActionWith a) -> SpecWith a -> SpecWith a)
-> (ActionWith a -> ActionWith a) -> SpecWith a -> SpecWith a
forall a b. (a -> b) -> a -> b
$ \ActionWith a
e a
x -> ActionWith a
e a
x IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
`finally` ActionWith a
action a
x

-- | Run a custom action after every spec item.
after_ :: IO () -> SpecWith a -> SpecWith a
after_ :: IO () -> SpecWith a -> SpecWith a
after_ IO ()
action = ActionWith a -> SpecWith a -> SpecWith a
forall a. ActionWith a -> SpecWith a -> SpecWith a
after (ActionWith a -> SpecWith a -> SpecWith a)
-> ActionWith a -> SpecWith a -> SpecWith a
forall a b. (a -> b) -> a -> b
$ \a
_ -> IO ()
action

-- | Run a custom action before and/or after every spec item.
around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
around ActionWith a -> IO ()
action = (ActionWith a -> ActionWith ()) -> SpecWith a -> Spec
forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith ((ActionWith a -> ActionWith ()) -> SpecWith a -> Spec)
-> (ActionWith a -> ActionWith ()) -> SpecWith a -> Spec
forall a b. (a -> b) -> a -> b
$ \ActionWith a
e () -> ActionWith a -> IO ()
action ActionWith a
e

-- | Run a custom action after the last spec item.
afterAll :: HasCallStack => ActionWith a -> SpecWith a -> SpecWith a
afterAll :: ActionWith a -> SpecWith a -> SpecWith a
afterAll ActionWith a
action SpecWith a
spec = IO [SpecTree a] -> SpecM a [SpecTree a]
forall r a. IO r -> SpecM a r
runIO (SpecWith a -> IO [SpecTree a]
forall a. SpecWith a -> IO [SpecTree a]
runSpecM SpecWith a
spec) SpecM a [SpecTree a] -> ([SpecTree a] -> SpecWith a) -> SpecWith a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [SpecTree a] -> SpecWith a
forall a. [SpecTree a] -> SpecWith a
fromSpecList ([SpecTree a] -> SpecWith a)
-> ([SpecTree a] -> [SpecTree a]) -> [SpecTree a] -> SpecWith a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SpecTree a -> [SpecTree a]
forall (m :: * -> *) a. Monad m => a -> m a
return (SpecTree a -> [SpecTree a])
-> ([SpecTree a] -> SpecTree a) -> [SpecTree a] -> [SpecTree a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Location -> ActionWith a -> [SpecTree a] -> SpecTree a
forall c a. Maybe Location -> c -> [Tree c a] -> Tree c a
NodeWithCleanup Maybe Location
HasCallStack => Maybe Location
location ActionWith a
action

-- | Run a custom action after the last spec item.
afterAll_ :: HasCallStack => IO () -> SpecWith a -> SpecWith a
afterAll_ :: IO () -> SpecWith a -> SpecWith a
afterAll_ IO ()
action = ActionWith a -> SpecWith a -> SpecWith a
forall a. HasCallStack => ActionWith a -> SpecWith a -> SpecWith a
afterAll (\a
_ -> IO ()
action)

-- | Run a custom action before and/or after every spec item.
around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
around_ IO () -> IO ()
action = (ActionWith a -> ActionWith a) -> SpecWith a -> SpecWith a
forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith ((ActionWith a -> ActionWith a) -> SpecWith a -> SpecWith a)
-> (ActionWith a -> ActionWith a) -> SpecWith a -> SpecWith a
forall a b. (a -> b) -> a -> b
$ \ActionWith a
e a
a -> IO () -> IO ()
action (ActionWith a
e a
a)

-- | Run a custom action before and/or after every spec item.
aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith ActionWith a -> ActionWith b
action = (ActionWith a -> ActionWith b)
-> (Item a -> Item b) -> SpecWith a -> SpecWith b
forall a b.
(ActionWith a -> ActionWith b)
-> (Item a -> Item b) -> SpecWith a -> SpecWith b
mapSpecItem ActionWith a -> ActionWith b
action ((ActionWith a -> ActionWith b) -> Item a -> Item b
forall a b. (ActionWith a -> ActionWith b) -> Item a -> Item b
modifyAroundAction ActionWith a -> ActionWith b
action)

modifyAroundAction :: (ActionWith a -> ActionWith b) -> Item a -> Item b
modifyAroundAction :: (ActionWith a -> ActionWith b) -> Item a -> Item b
modifyAroundAction ActionWith a -> ActionWith b
action item :: Item a
item@Item{itemExample :: forall a.
Item a
-> Params
-> (ActionWith a -> IO ())
-> ProgressCallback
-> IO Result
itemExample = Params -> (ActionWith a -> IO ()) -> ProgressCallback -> IO Result
e} =
  Item a
item{ itemExample :: Params -> (ActionWith b -> IO ()) -> ProgressCallback -> IO Result
itemExample = \Params
params ActionWith b -> IO ()
aroundAction -> Params -> (ActionWith a -> IO ()) -> ProgressCallback -> IO Result
e Params
params (ActionWith b -> IO ()
aroundAction (ActionWith b -> IO ())
-> (ActionWith a -> ActionWith b) -> ActionWith a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ActionWith a -> ActionWith b
action) }

-- | Wrap an action around the given spec.
aroundAll :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
aroundAll :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
aroundAll ActionWith a -> IO ()
action = (ActionWith a -> ActionWith ()) -> SpecWith a -> Spec
forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith ((ActionWith a -> ActionWith ()) -> SpecWith a -> Spec)
-> (ActionWith a -> ActionWith ()) -> SpecWith a -> Spec
forall a b. (a -> b) -> a -> b
$ \ ActionWith a
e () -> ActionWith a -> IO ()
action ActionWith a
e

-- | Wrap an action around the given spec.
aroundAll_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
aroundAll_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
aroundAll_ IO () -> IO ()
action SpecWith a
spec = do
  MVar ()
allSpecItemsDone <- IO (MVar ()) -> SpecM a (MVar ())
forall r a. IO r -> SpecM a r
runIO IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
  MVar (Async ())
workerRef <- IO (MVar (Async ())) -> SpecM a (MVar (Async ()))
forall r a. IO r -> SpecM a r
runIO IO (MVar (Async ()))
forall a. IO (MVar a)
newEmptyMVar
  let
    acquire :: IO ()
    acquire :: IO ()
acquire = do
      MVar ()
resource <- IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
      Async ()
worker <- IO () -> IO (Async ())
forall a. IO a -> IO (Async a)
async (IO () -> IO (Async ())) -> IO () -> IO (Async ())
forall a b. (a -> b) -> a -> b
$ do
        IO () -> IO ()
action (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
          MVar () -> IO ()
signal MVar ()
resource
          MVar () -> IO ()
waitFor MVar ()
allSpecItemsDone
      MVar (Async ()) -> Async () -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Async ())
workerRef Async ()
worker
      IO () -> IO ()
forall a. IO a -> IO a
unwrapExceptionsFromLinkedThread (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        Async () -> IO ()
forall a. Async a -> IO ()
link Async ()
worker
        MVar () -> IO ()
waitFor MVar ()
resource
    release :: IO ()
    release :: IO ()
release = MVar () -> IO ()
signal MVar ()
allSpecItemsDone IO () -> IO (Async ()) -> IO (Async ())
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVar (Async ()) -> IO (Async ())
forall a. MVar a -> IO a
takeMVar MVar (Async ())
workerRef IO (Async ()) -> (Async () -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async () -> IO ()
forall a. Async a -> IO a
wait
  IO () -> SpecWith a -> SpecWith a
forall a. IO () -> SpecWith a -> SpecWith a
beforeAll_ IO ()
acquire (SpecWith a -> SpecWith a) -> SpecWith a -> SpecWith a
forall a b. (a -> b) -> a -> b
$ IO () -> SpecWith a -> SpecWith a
forall a. HasCallStack => IO () -> SpecWith a -> SpecWith a
afterAll_ IO ()
release SpecWith a
spec

-- | Wrap an action around the given spec. Changes the arg type inside.
aroundAllWith :: forall a b. (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith ActionWith a -> ActionWith b
action SpecWith a
spec = do
  MVar ()
allSpecItemsDone <- IO (MVar ()) -> SpecM b (MVar ())
forall r a. IO r -> SpecM a r
runIO IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
  MVar (Async ())
workerRef <- IO (MVar (Async ())) -> SpecM b (MVar (Async ()))
forall r a. IO r -> SpecM a r
runIO IO (MVar (Async ()))
forall a. IO (MVar a)
newEmptyMVar
  let
    acquire :: b -> IO a
    acquire :: b -> IO a
acquire b
b = do
      MVar a
resource <- IO (MVar a)
forall a. IO (MVar a)
newEmptyMVar
      Async ()
worker <- IO () -> IO (Async ())
forall a. IO a -> IO (Async a)
async (IO () -> IO (Async ())) -> IO () -> IO (Async ())
forall a b. (a -> b) -> a -> b
$ do
        (ActionWith a -> ActionWith b) -> b -> ActionWith a -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip ActionWith a -> ActionWith b
action b
b (ActionWith a -> IO ()) -> ActionWith a -> IO ()
forall a b. (a -> b) -> a -> b
$ \ a
a -> do
          MVar a -> ActionWith a
forall a. MVar a -> a -> IO ()
putMVar MVar a
resource a
a
          MVar () -> IO ()
waitFor MVar ()
allSpecItemsDone
      MVar (Async ()) -> Async () -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Async ())
workerRef Async ()
worker
      IO a -> IO a
forall a. IO a -> IO a
unwrapExceptionsFromLinkedThread (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
        Async () -> IO ()
forall a. Async a -> IO ()
link Async ()
worker
        MVar a -> IO a
forall a. MVar a -> IO a
takeMVar MVar a
resource
    release :: IO ()
    release :: IO ()
release = MVar () -> IO ()
signal MVar ()
allSpecItemsDone IO () -> IO (Async ()) -> IO (Async ())
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVar (Async ()) -> IO (Async ())
forall a. MVar a -> IO a
takeMVar MVar (Async ())
workerRef IO (Async ()) -> (Async () -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async () -> IO ()
forall a. Async a -> IO a
wait
  (b -> IO a) -> SpecWith a -> SpecWith b
forall b a. (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith b -> IO a
acquire (SpecWith a -> SpecWith b) -> SpecWith a -> SpecWith b
forall a b. (a -> b) -> a -> b
$ IO () -> SpecWith a -> SpecWith a
forall a. HasCallStack => IO () -> SpecWith a -> SpecWith a
afterAll_ IO ()
release SpecWith a
spec

unwrapExceptionsFromLinkedThread :: IO a -> IO a
unwrapExceptionsFromLinkedThread :: IO a -> IO a
unwrapExceptionsFromLinkedThread = (IO a -> (ExceptionInLinkedThread -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \ (ExceptionInLinkedThread Async a
_ SomeException
e) -> SomeException -> IO a
forall e a. Exception e => e -> IO a
throwIO SomeException
e)

type BinarySemaphore = MVar ()

signal :: BinarySemaphore -> IO ()
signal :: MVar () -> IO ()
signal = (MVar () -> ActionWith ()) -> () -> MVar () -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip MVar () -> ActionWith ()
forall a. MVar a -> a -> IO ()
putMVar ()

waitFor :: BinarySemaphore -> IO ()
waitFor :: MVar () -> IO ()
waitFor = MVar () -> IO ()
forall a. MVar a -> IO a
takeMVar

-- | Modify the subject under test.
--
-- Note that this resembles a contravariant functor on the first type parameter
-- of `SpecM`.  This is because the subject is passed inwards, as an argument
-- to the spec item.
mapSubject :: (b -> a) -> SpecWith a -> SpecWith b
mapSubject :: (b -> a) -> SpecWith a -> SpecWith b
mapSubject b -> a
f = (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith (ActionWith a -> (b -> a) -> ActionWith b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
f)

-- | Ignore the subject under test for a given spec.
ignoreSubject :: SpecWith () -> SpecWith a
ignoreSubject :: Spec -> SpecWith a
ignoreSubject = (a -> ()) -> Spec -> SpecWith a
forall b a. (b -> a) -> SpecWith a -> SpecWith b
mapSubject (() -> a -> ()
forall a b. a -> b -> a
const ())