License | BSD-3 |
---|---|
Maintainer | autotaker@gmail.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- mockup :: (Method method, MonadThrow (Base method)) => Mock method -> method
- thenReturn :: (Method method, Applicative (Base method)) => Matcher (Args method) -> Ret method -> Mock method
- thenAction :: Method method => Matcher (Args method) -> Base method (Ret method) -> Mock method
- thenMethod :: Method method => Matcher (Args method) -> method -> Mock method
- throwNoStubShow :: (Method method, Show (AsTuple (Args method)), MonadThrow (Base method), TupleLike (Args method)) => Matcher (Args method) -> Mock method
- throwNoStub :: (Method method, MonadThrow (Base method)) => (Args method -> String) -> (Args method -> Bool) -> Mock method
- newtype NoStubException = NoStubException String
- data Monitor args ret
- data Event args ret
- watchBy :: (Method method, MonadUnliftIO (Base method)) => (Args method -> args) -> (Ret method -> ret) -> Monitor args ret -> method -> method
- watch :: (Method method, MonadUnliftIO (Base method)) => Monitor (Args method) (Ret method) -> method -> method
- withMonitor :: MonadIO m => (Monitor args ret -> m a) -> m (a, [Event args ret])
- withMonitor_ :: MonadIO m => (Monitor args ret -> m ()) -> m [Event args ret]
- call :: Matcher args -> Matcher (Event args ret)
- times :: Matcher Int -> Matcher (Event args ret) -> Matcher [Event args ret]
- newMonitor :: IO (Monitor args ret)
- listenEventLog :: MonadIO m => Monitor args ret -> m [Event args ret]
- type Matcher a = a -> Bool
- anything :: Matcher a
- when :: Matcher a -> Matcher a
- class TupleLike a where
- class TupleLike a => ArgsMatcher a where
- args :: EachMatcher a -> Matcher a
- args' :: TupleLike a => Matcher (AsTuple a) -> Matcher a
Documentation
This module provides DSLs for mocking methods and for validating method calls
Mock
Usage
fizzbuzz :: Int -> IO String fizzbuzz =mockup
$ dowhen
(args
(x -> mod x 15 == 0)) `thenReturn
` "fizzbuzz"when
(args
(x -> mod x 3 == 0)) `thenReturn
` "fizz"when
(args
(x -> mod x 5 == 0)) `thenReturn
` "buzz"when
(args
(>=0)) `thenMethod
` (x -> pure $ show x)throwNoStubShow
$when
anything
>>>
fizzbuzz 0
"fizzbuzz">>>
fizzbuzz 1
"1">>>
fizzbuzz 3
"fizz">>>
fizzbuzz 5
"buzz">>>
fizzbuzz (-1)
*** Exception: NoStubException "-1"
References
mockup :: (Method method, MonadThrow (Base method)) => Mock method -> method Source #
generate a method from Mock DSL. Mock DSL consists of rules. On a call of generated method, the first rule matched the arguments is applied.
thenReturn :: (Method method, Applicative (Base method)) => Matcher (Args method) -> Ret method -> Mock method Source #
matcher `
means the method return thenReturn
` valuevalue
if the arguments matches matcher
.
thenAction :: Method method => Matcher (Args method) -> Base method (Ret method) -> Mock method Source #
matcher `
means the method executes thenAction
` actionaction
if the arguments matches matcher
.
thenMethod :: Method method => Matcher (Args method) -> method -> Mock method Source #
matcher `
means the method call thenMethod
` actionmethod
with the arguments
if the arguments matches matcher
.
throwNoStubShow :: (Method method, Show (AsTuple (Args method)), MonadThrow (Base method), TupleLike (Args method)) => Matcher (Args method) -> Mock method Source #
means the method throws a throwNoStubShow
matcherNoStubException
if the arguments matches matcher
. The argument tuple is converted to String
by
using show
function.
throwNoStub :: (Method method, MonadThrow (Base method)) => (Args method -> String) -> (Args method -> Bool) -> Mock method Source #
means the method throws a throwNoStubShow
fshow matcherNoStubException
if the arguments matches matcher
. The argument tuple is converted to String
by
using fshow
function.
newtype NoStubException Source #
Instances
Show NoStubException Source # | |
Defined in Test.Method.Mock showsPrec :: Int -> NoStubException -> ShowS # show :: NoStubException -> String # showList :: [NoStubException] -> ShowS # | |
Exception NoStubException Source # | |
Defined in Test.Method.Mock |
Monitor
Usage
type ExampleMethod = Int -> String -> IO String
example :: ExampleMethod
example n s | n < 0 = throwString "negative n"
| otherwise = pure $ concat $ replicate n s
doit :: ExampleMethod -> IO ()
doit example = (do
example 2 "foo" >>= putStrLn
example 3 "foo" >>= putStrLn
example (-1) "bar" >>= putStrLn
example 3 "bar" >>= putStrLn) catchAny
(const $ pure ())
spec :: Spec spec = describe "doit" $ do before (withMonitor_
$ \monitor -> doit (watch
monitor example)) it "calls example _ "foo" twice" $ \logs -> do logs `shouldSatisfy
` ((==2) `times
`call
(args
(anything
, (=="foo")))) it "calls example (-1) "bar" once" $ \logs -> do logs `shouldSatisfy
` ((==1) `times
`call
(args
((==(-1)), (=="bar")))) it "does not call example 3 "bar" " $ \logs -> do logs `shouldSatisfy
` ((==0) `times
`call
(args
((==3), (=="bar"))))
References
data Monitor args ret Source #
Monitor arg ret
is an event monitor of methods,
which logs method calls.
is a function call eventEvent
args ret
Instances
(Eq args, Eq ret) => Eq (Event args ret) Source # | |
(Ord args, Ord ret) => Ord (Event args ret) Source # | |
Defined in Test.Method.Monitor.Internal compare :: Event args ret -> Event args ret -> Ordering # (<) :: Event args ret -> Event args ret -> Bool # (<=) :: Event args ret -> Event args ret -> Bool # (>) :: Event args ret -> Event args ret -> Bool # (>=) :: Event args ret -> Event args ret -> Bool # | |
(Show args, Show ret) => Show (Event args ret) Source # | |
watchBy :: (Method method, MonadUnliftIO (Base method)) => (Args method -> args) -> (Ret method -> ret) -> Monitor args ret -> method -> method Source #
watchBy fArgs fRet monitor method
decorates method
so that monitor
logs the method calls.
This function is suited for monitoring multiple methods.
fArgs
and fRet
is converter for arguments/return values of given method.
foo :: Int -> IO String foo = ... bar :: Int -> String -> IO () bar = ... data MonitorArgs = FooArgs Int | BarArgs (Int,String) deriving(Eq,Show) data MonitorRet = FooRet String | BarRet () deriving(Eq, Show) foo' :: Monitor MonitorArgs MonitorRet -> Int -> IO String foo' monitor = watch monitor (FooArgs . toTuple) FooRet foo bar' :: Monitor MonitorArgs MonitorRet -> Int -> String -> IO () bar' monitor = watch monitor (BarArgs . toTuple) BarRet bar
watch :: (Method method, MonadUnliftIO (Base method)) => Monitor (Args method) (Ret method) -> method -> method Source #
Simplified version of watchBy
. It is suitable to monitor single method.
withMonitor :: MonadIO m => (Monitor args ret -> m a) -> m (a, [Event args ret]) Source #
withMonitor f
calls f
with Monitor
,
and then returns monitored event logs during the function call
in addition to the return value of the function call
withMonitor_ :: MonadIO m => (Monitor args ret -> m ()) -> m [Event args ret] Source #
withMonitor_ f
calls f
with Monitor
, and returns event logs during the call.
Matcher for events
call :: Matcher args -> Matcher (Event args ret) Source #
matches method call whose arguments matches call
matchermatcher
times :: Matcher Int -> Matcher (Event args ret) -> Matcher [Event args ret] Source #
counts events that matches times
countMatcher eventMatchereventMatcher
,
and then the count matches countMatcher
Procedual api for monitor
listenEventLog :: MonadIO m => Monitor args ret -> m [Event args ret] Source #
Get current event logs from monitor
Matcher
References
Basics
when :: Matcher a -> Matcher a Source #
synonym of id
function.
Use this function for improving readability
Matcher for method arguments
class TupleLike a where Source #
Instances
TupleLike Nil Source # | |
TupleLike (a :* (b :* (c :* (d :* (e :* (f :* (g :* Nil))))))) Source # | |
Defined in Control.Method.Internal | |
TupleLike (a :* (b :* (c :* (d :* (e :* (f :* Nil)))))) Source # | |
Defined in Control.Method.Internal | |
TupleLike (a :* (b :* (c :* (d :* (e :* Nil))))) Source # | |
Defined in Control.Method.Internal | |
TupleLike (a :* (b :* (c :* (d :* Nil)))) Source # | |
TupleLike (a :* (b :* (c :* Nil))) Source # | |
TupleLike (a :* (b :* Nil)) Source # | |
TupleLike (a :* Nil) Source # | |
class TupleLike a => ArgsMatcher a where Source #
Matcher for Args
>>>
args ((==2), (>3)) (2 :* 4 :* Nil)
True>>>
args even (1 :* Nil)
False>>>
args () Nil
True
args :: EachMatcher a -> Matcher a Source #
Convert a tuple of matchers to a matcher of tuples
Instances
ArgsMatcher Nil Source # | |
Defined in Test.Method.Matcher type EachMatcher Nil Source # | |
ArgsMatcher (a :* (b :* (c :* (d :* (e :* (f :* (g :* Nil))))))) Source # | |
ArgsMatcher (a :* (b :* (c :* (d :* (e :* (f :* Nil)))))) Source # | |
ArgsMatcher (a :* (b :* (c :* (d :* (e :* Nil))))) Source # | |
ArgsMatcher (a :* (b :* (c :* (d :* Nil)))) Source # | |
ArgsMatcher (a :* (b :* (c :* Nil))) Source # | |
ArgsMatcher (a :* (b :* Nil)) Source # | |
ArgsMatcher (a :* Nil) Source # | |
Defined in Test.Method.Matcher type EachMatcher (a :* Nil) Source # |