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

, mapSubject
, ignoreSubject

#ifdef TEST
, decompose
#endif
) where

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

import           Control.Concurrent

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 :: forall a. IO a -> SpecWith a -> Spec
before IO a
action = forall a. (ActionWith a -> IO ()) -> SpecWith a -> Spec
around (IO a
action 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_ :: forall a. IO () -> SpecWith a -> SpecWith a
before_ IO ()
action = forall a. (IO () -> IO ()) -> SpecWith a -> SpecWith a
around_ (IO ()
action 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 :: forall b a. (b -> IO a) -> SpecWith a -> SpecWith b
beforeWith b -> IO a
action = forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith forall a b. (a -> b) -> a -> b
$ \ActionWith a
e b
x -> b -> IO a
action b
x 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 :: HasCallStack => IO a -> SpecWith a -> Spec
beforeAll :: forall a. HasCallStack => IO a -> SpecWith a -> Spec
beforeAll IO a
action SpecWith a
spec = do
  MVar (Memoized a)
mvar <- forall r a. IO r -> SpecM a r
runIO (forall a. a -> IO (MVar a)
newMVar forall a. Memoized a
Empty)
  forall a. IO a -> SpecWith a -> Spec
before (forall a. HasCallStack => 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_ :: HasCallStack => IO () -> SpecWith a -> SpecWith a
beforeAll_ :: forall a. HasCallStack => IO () -> SpecWith a -> SpecWith a
beforeAll_ IO ()
action SpecWith a
spec = do
  MVar (Memoized ())
mvar <- forall r a. IO r -> SpecM a r
runIO (forall a. a -> IO (MVar a)
newMVar forall a. Memoized a
Empty)
  forall a. IO () -> SpecWith a -> SpecWith a
before_ (forall a. HasCallStack => 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 :: HasCallStack => (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith :: forall b a. HasCallStack => (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith b -> IO a
action SpecWith a
spec = do
  MVar (Memoized a)
mvar <- forall r a. IO r -> SpecM a r
runIO (forall a. a -> IO (MVar a)
newMVar forall a. Memoized a
Empty)
  forall b a. (b -> IO a) -> SpecWith a -> SpecWith b
beforeWith (forall a. HasCallStack => MVar (Memoized a) -> IO a -> IO a
memoize MVar (Memoized a)
mvar 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 :: HasCallStack => MVar (Memoized a) -> IO a -> IO a
memoize :: forall a. HasCallStack => MVar (Memoized a) -> IO a -> IO a
memoize MVar (Memoized a)
mvar IO a
action = do
  Either SomeException a
result <- forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar (Memoized a)
mvar forall a b. (a -> b) -> a -> b
$ \Memoized a
ma -> case Memoized a
ma of
    Memoized a
Empty -> do
      Either SomeException a
a <- forall e a. Exception e => IO a -> IO (Either e a)
try IO a
action
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. SomeException -> Memoized a
Failed forall a. a -> Memoized a
Memoized Either SomeException a
a, Either SomeException a
a)
    Memoized a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (Memoized a
ma, forall a b. b -> Either a b
Right a
a)
    Failed SomeException
_ -> forall e a. Exception e => e -> IO a
throwIO (Maybe Location -> Maybe String -> ResultStatus
Pending forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ String
"exception in " forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"beforeAll" forall a b. (a, b) -> a
fst HasCallStack => Maybe (String, Location)
callSite forall a. Semigroup a => a -> a -> a
<> String
"-hook (see previous failure)"))
  forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall e a. Exception e => e -> IO a
throwIO 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 :: forall a. ActionWith a -> SpecWith a -> SpecWith a
after ActionWith a
action = forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith forall a b. (a -> b) -> a -> b
$ \ActionWith a
e a
x -> ActionWith a
e a
x 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_ :: forall a. IO () -> SpecWith a -> SpecWith a
after_ IO ()
action = forall a. ActionWith a -> SpecWith a -> SpecWith a
after 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 :: forall a. (ActionWith a -> IO ()) -> SpecWith a -> Spec
around ActionWith a -> IO ()
action = forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith 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 :: forall a. HasCallStack => ActionWith a -> SpecWith a -> SpecWith a
afterAll ActionWith a
action = forall a b.
HasCallStack =>
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith (\ ActionWith a
hook a
a -> ActionWith a
hook a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ActionWith a
action a
a)

-- | Run a custom action after the last spec item.
afterAll_ :: HasCallStack => IO () -> SpecWith a -> SpecWith a
afterAll_ :: forall a. HasCallStack => IO () -> SpecWith a -> SpecWith a
afterAll_ IO ()
action = forall a b r.
([SpecTree a] -> [SpecTree b]) -> SpecM a r -> SpecM b r
mapSpecForest (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c a. Maybe (String, Location) -> c -> [Tree c a] -> Tree c a
NodeWithCleanup HasCallStack => Maybe (String, Location)
callSite IO ()
action)

-- | Run a custom action before and/or after every spec item.
around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
around_ :: forall a. (IO () -> IO ()) -> SpecWith a -> SpecWith a
around_ IO () -> IO ()
action = forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith 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 :: forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith = forall a b. (Item a -> Item b) -> SpecWith a -> SpecWith b
mapSpecItem_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (ActionWith a -> ActionWith b) -> Item a -> Item b
modifyHook

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

-- | Wrap an action around the given spec.
aroundAll :: HasCallStack => (ActionWith a -> IO ()) -> SpecWith a -> Spec
aroundAll :: forall a.
HasCallStack =>
(ActionWith a -> IO ()) -> SpecWith a -> Spec
aroundAll ActionWith a -> IO ()
action = forall a b.
HasCallStack =>
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith 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_ :: HasCallStack => (IO () -> IO ()) -> SpecWith a -> SpecWith a
aroundAll_ :: forall a.
HasCallStack =>
(IO () -> IO ()) -> SpecWith a -> SpecWith a
aroundAll_ IO () -> IO ()
action SpecWith a
spec = do
  (() -> IO ()
acquire, IO ()
release) <- forall r a. IO r -> SpecM a r
runIO forall a b. (a -> b) -> a -> b
$ forall a b. ((a -> IO ()) -> b -> IO ()) -> IO (b -> IO a, IO ())
decompose (IO () -> IO ()
action forall b c a. (b -> c) -> (a -> b) -> a -> c
.)
  forall a. HasCallStack => IO () -> SpecWith a -> SpecWith a
beforeAll_ (() -> IO ()
acquire ()) forall a b. (a -> b) -> a -> b
$ 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. HasCallStack => (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith :: forall a b.
HasCallStack =>
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundAllWith ActionWith a -> ActionWith b
action SpecWith a
spec = do
  (b -> IO a
acquire, IO ()
release) <- forall r a. IO r -> SpecM a r
runIO forall a b. (a -> b) -> a -> b
$ forall a b. ((a -> IO ()) -> b -> IO ()) -> IO (b -> IO a, IO ())
decompose ActionWith a -> ActionWith b
action
  forall b a. HasCallStack => (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith b -> IO a
acquire forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => IO () -> SpecWith a -> SpecWith a
afterAll_ IO ()
release SpecWith a
spec

data Acquired a = Acquired a | ExceptionDuringAcquire SomeException
data Released   = Released   | ExceptionDuringRelease SomeException

decompose :: forall a b. ((a -> IO ()) -> b -> IO ()) -> IO (b -> IO a, IO ())
decompose :: forall a b. ((a -> IO ()) -> b -> IO ()) -> IO (b -> IO a, IO ())
decompose (a -> IO ()) -> b -> IO ()
action = do
  MVar ()
doCleanupNow <- forall a. IO (MVar a)
newEmptyMVar
  MVar (Acquired a)
acquired <- forall a. IO (MVar a)
newEmptyMVar
  MVar Released
released <- forall a. IO (MVar a)
newEmptyMVar

  let
    notify :: Either SomeException () -> IO ()
    -- `notify` is guaranteed to run without being interrupted by an async
    -- exception for the following reasons:
    --
    -- 1. `forkFinally` runs the final action within `mask`
    -- 2. `tryPutMVar` is guaranteed not to be interruptible
    -- 3. `putMVar` is guaranteed not to be interruptible on an empty `MVar`
    notify :: Either SomeException () -> IO ()
notify Either SomeException ()
r = case Either SomeException ()
r of
      Left SomeException
err -> do
        Bool
exceptionDuringAcquire <- forall a. MVar a -> a -> IO Bool
tryPutMVar MVar (Acquired a)
acquired (forall a. SomeException -> Acquired a
ExceptionDuringAcquire SomeException
err)
        forall a. MVar a -> a -> IO ()
putMVar MVar Released
released forall a b. (a -> b) -> a -> b
$ if Bool
exceptionDuringAcquire then Released
Released else SomeException -> Released
ExceptionDuringRelease SomeException
err
      Right () -> do
        forall a. MVar a -> a -> IO ()
putMVar MVar Released
released Released
Released

    forkWorker :: b -> IO ()
    forkWorker :: b -> IO ()
forkWorker b
b = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
forkFinally Either SomeException () -> IO ()
notify forall a b. (a -> b) -> a -> b
$ do
      forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> IO ()) -> b -> IO ()
action b
b forall a b. (a -> b) -> a -> b
$ \ a
a -> do
        forall a. MVar a -> a -> IO ()
putMVar MVar (Acquired a)
acquired (forall a. a -> Acquired a
Acquired a
a)
        MVar () -> IO ()
waitFor MVar ()
doCleanupNow

    acquire :: b -> IO a
    acquire :: b -> IO a
acquire b
b = do
      b -> IO ()
forkWorker b
b
      Acquired a
r <- forall a. MVar a -> IO a
readMVar MVar (Acquired a)
acquired -- This does not work reliably with base < 4.7
      case Acquired a
r of
        Acquired a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return a
a
        ExceptionDuringAcquire SomeException
err -> forall e a. Exception e => e -> IO a
throwIO SomeException
err

    release :: IO ()
    release :: IO ()
release = do
      Bool
acquireHasNotBeenCalled <- forall a. MVar a -> IO Bool
isEmptyMVar MVar (Acquired a)
acquired -- NOTE: This can happen if an outer beforeAll fails
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
acquireHasNotBeenCalled forall a b. (a -> b) -> a -> b
$ do
        MVar () -> IO ()
signal MVar ()
doCleanupNow
        Released
r <- forall a. MVar a -> IO a
takeMVar MVar Released
released
        case Released
r of
          Released
Released -> forall (m :: * -> *). Applicative m => m ()
pass
          ExceptionDuringRelease SomeException
err -> forall e a. Exception e => e -> IO a
throwIO SomeException
err

  forall (m :: * -> *) a. Monad m => a -> m a
return (b -> IO a
acquire, IO ()
release)

type BinarySemaphore = MVar ()

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

waitFor :: BinarySemaphore -> IO ()
waitFor :: MVar () -> IO ()
waitFor = 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 :: forall b a. (b -> a) -> SpecWith a -> SpecWith b
mapSubject b -> a
f = forall a b.
(ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith (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 :: forall a. Spec -> SpecWith a
ignoreSubject = forall b a. (b -> a) -> SpecWith a -> SpecWith b
mapSubject (forall a b. a -> b -> a
const ())