mockcat-0.5.0.0: Mock library for test in Haskell.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.MockCat.MockT

Synopsis

Documentation

newtype MockT m a Source #

Constructors

MockT 

Fields

Instances

Instances details
MonadTrans MockT Source # 
Instance details

Defined in Test.MockCat.MockT

Methods

lift :: Monad m => m a -> MockT m a #

MonadIO m => MonadIO (MockT m) Source # 
Instance details

Defined in Test.MockCat.MockT

Methods

liftIO :: IO a -> MockT m a #

Monad m => Applicative (MockT m) Source # 
Instance details

Defined in Test.MockCat.MockT

Methods

pure :: a -> MockT m a #

(<*>) :: MockT m (a -> b) -> MockT m a -> MockT m b #

liftA2 :: (a -> b -> c) -> MockT m a -> MockT m b -> MockT m c #

(*>) :: MockT m a -> MockT m b -> MockT m b #

(<*) :: MockT m a -> MockT m b -> MockT m a #

Functor m => Functor (MockT m) Source # 
Instance details

Defined in Test.MockCat.MockT

Methods

fmap :: (a -> b) -> MockT m a -> MockT m b #

(<$) :: a -> MockT m b -> MockT m a #

Monad m => Monad (MockT m) Source # 
Instance details

Defined in Test.MockCat.MockT

Methods

(>>=) :: MockT m a -> (a -> MockT m b) -> MockT m b #

(>>) :: MockT m a -> MockT m b -> MockT m b #

return :: a -> MockT m a #

data Definition Source #

Constructors

forall f p sym.KnownSymbol sym => Definition 

Fields

runMockT :: MonadIO m => MockT m a -> m a Source #

Run MockT monad. After run, verification is performed to see if the stub function has been applied.

 import Test.Hspec
 import Test.MockCat
 ...

 class (Monad m) => FileOperation m where
   writeFile :: FilePath -> Text -> m ()
   readFile :: FilePath -> m Text

 operationProgram ::
   FileOperation m =>
   FilePath ->
   FilePath ->
   m ()
 operationProgram inputPath outputPath = do
   content <- readFile inputPath
   writeFile outputPath content

 makeMock [t|FileOperation|]

 spec :: Spec
 spec = do
   it "test runMockT" do
     result <- runMockT do
       _readFile $ "input.txt" |> pack "content"
       _writeFile $ "output.text" |> pack "content" |> ()
       operationProgram "input.txt" "output.text"

     result shouldBe ()
 

applyTimesIs :: Monad m => MockT m () -> Int -> MockT m () Source #

Specify how many times a stub function should be applied.

 import Test.Hspec
 import Test.MockCat
 ...

 class (Monad m) => FileOperation m where
   writeFile :: FilePath -> Text -> m ()
   readFile :: FilePath -> m Text

 operationProgram ::
   FileOperation m =>
   FilePath ->
   FilePath ->
   m ()
 operationProgram inputPath outputPath = do
   content <- readFile inputPath
   when (content == pack "ng") $ writeFile outputPath content

 makeMock [t|FileOperation|]

 spec :: Spec
 spec = do
   it "test runMockT" do
     result <- runMockT do
       _readFile ("input.txt" |> pack "content")
       _writeFile ("output.text" |> pack "content" |> ()) applyTimesIs 0
       operationProgram "input.txt" "output.text"

     result shouldBe ()

 

neverApply :: Monad m => MockT m () -> MockT m () Source #