module Test.Hspec (
Spec
, Arg
, SpecWith
, ActionWith
, Example
, module Test.Hspec.Expectations
, describe
, context
, it
, specify
, example
, pending
, pendingWith
, before
, beforeWith
, beforeAll
, beforeAllWith
, after
, after_
, afterAll
, afterAll_
, around
, around_
, aroundWith
, parallel
, runIO
, hspec
) where
import Control.Exception (finally)
import Control.Concurrent.MVar
import Test.Hspec.Core.Type hiding (describe, it)
import Test.Hspec.Runner
import Test.Hspec.HUnit ()
import Test.Hspec.Expectations
import qualified Test.Hspec.Core as Core
describe :: String -> SpecWith a -> SpecWith a
describe label spec = runIO (runSpecM spec) >>= fromSpecList . return . Core.describe label
context :: String -> SpecWith a -> SpecWith a
context = describe
it :: Example e => String -> e -> SpecWith (Arg e)
it label action = fromSpecList [Core.it label action]
specify :: Example e => String -> e -> SpecWith (Arg e)
specify = it
example :: Expectation -> Expectation
example = id
parallel :: SpecWith a -> SpecWith a
parallel = mapSpecItem_ $ \item -> item {itemIsParallelizable = True}
before :: IO a -> SpecWith a -> Spec
before action = around (action >>=)
beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b
beforeWith action = aroundWith $ \e x -> action x >>= e
beforeAll :: IO a -> SpecWith a -> Spec
beforeAll action spec = do
mvar <- runIO (newMVar Nothing)
let action_ = memoize mvar action
before action_ spec
memoize :: MVar (Maybe a) -> IO a -> IO a
memoize mvar action = modifyMVar mvar $ \ma -> case ma of
Just a -> return (ma, a)
Nothing -> do
a <- action
return (Just a, a)
beforeAllWith :: (b -> IO a) -> SpecWith a -> SpecWith b
beforeAllWith action spec = do
mvar <- runIO (newMVar Nothing)
let action_ = memoize mvar . action
aroundWith (\e x -> action_ x >>= e) spec
after :: ActionWith a -> SpecWith a -> SpecWith a
after action = aroundWith $ \e x -> e x `finally` action x
after_ :: IO () -> Spec -> Spec
after_ action = after $ \() -> action
around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
around action = aroundWith $ \e () -> action e
afterAll :: ActionWith a -> SpecWith a -> SpecWith a
afterAll action spec = runIO (runSpecM spec) >>= fromSpecList . return . SpecWithCleanup action
afterAll_ :: IO () -> Spec -> Spec
afterAll_ action = afterAll (\() -> action)
around_ :: (IO () -> IO ()) -> Spec -> Spec
around_ action = around $ action . ($ ())
aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
aroundWith action = mapAround (. action)
mapAround :: ((ActionWith b -> IO ()) -> ActionWith a -> IO ()) -> SpecWith a -> SpecWith b
mapAround f = mapSpecItem (untangle f) $ \i@Item{itemExample = e} -> i{itemExample = (. f) . e}
untangle :: ((ActionWith b -> IO ()) -> ActionWith a -> IO ()) -> ActionWith a -> ActionWith b
untangle f g = \b -> f ($ b) g