Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aroundAll hook #103

Open
srghma opened this issue Dec 15, 2019 · 1 comment
Open

aroundAll hook #103

srghma opened this issue Dec 15, 2019 · 1 comment

Comments

@srghma
Copy link

srghma commented Dec 15, 2019

related hspec/hspec#255

@srghma
Copy link
Author

srghma commented Dec 21, 2019

module Lib.AroundAll where

import Prelude (Unit, bind, discard, pure, void, ($), (<<<))

import Effect.Aff (Aff, Error, Fiber, forkAff, killFiber)
import Effect.Aff.Class (class MonadAff, liftAff)
import Effect.Class (class MonadEffect)
import Effect.Exception as Effect.Exception
import Effect.Aff.AVar as Effect.Aff.AVar
import Control.Monad.Error.Class (class MonadError)
import Data.Tuple (Tuple(..), fst)
import Test.Spec (SpecT, afterAll, beforeAll, beforeWith)

-- | Run a custom action before first and after last spec item.

-- | Example usage - to make a database provider for a group of tests

-- it's possible to make `withFunc` argument to have type
--
-- ((config -> g Unit) -> g Unit)
--
-- instead of
--
-- ((config -> g Unit) -> Aff Unit)
--
-- but it would require g to implement `MonadUnliftAff` class (there should be function `g Unit -> Aff Unit`)

aroundAll :: forall config m g . MonadEffect m => MonadAff g => MonadError Error g => ((config -> g Unit) -> Aff Unit) -> SpecT g config m Unit -> SpecT g Unit m Unit
aroundAll withFunc specWith = beforeAll start $ afterAll stop $ beforeWith (pure <<< fst) specWith
  where
        start :: g (Tuple config (Fiber Unit))
        start = do
           (avar :: Effect.Aff.AVar.AVar config) <- liftAff $ Effect.Aff.AVar.empty -- 1

           fiber <- liftAff $ forkAff $ withFunc $ \value -> do
              liftAff $ Effect.Aff.AVar.put value avar -- 2
              liftAff $ void $ Effect.Aff.AVar.take avar -- 3

           theConfig <- liftAff $ Effect.Aff.AVar.take avar -- 2

           pure (Tuple theConfig fiber)

        stop :: Tuple config (Fiber Unit) -> g Unit
        stop (Tuple _config fiber) = liftAff $ killFiber (Effect.Exception.error "Kill fiber") fiber

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant