License | BSD-3 |
---|---|
Maintainer | autotaker@gmail.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- mockup :: Method 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)), TupleLike (Args method)) => Matcher (Args method) -> Mock method
- throwNoStub :: Method method => (Args method -> String) -> (Args method -> Bool) -> Mock method
- 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: no stub found for argument: -1 CallStack (from HasCallStack): error, called at src/Test/Method/Mock.hs:98:9 in method-0.2.0.0-inplace:Test.Method.Mock"
References
mockup :: Method 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)), TupleLike (Args method)) => Matcher (Args method) -> Mock method Source #
means the method raises a runtime exception
if the arguments matches throwNoStubShow
matchermatcher
. The argument tuple is converted to String
by
using show
function.
throwNoStub :: Method method => (Args method -> String) -> (Args method -> Bool) -> Mock method Source #
means the method raises runtime exception
if the arguments matches throwNoStubShow
fshow matchermatcher
. The argument tuple is converted to String
by
using fshow
function.
Monitor
Usage
Production code
type ExampleMethod env = Int -> String -> RIO env ()
class HasExampleMethod env where
exampleL :: Lens' env (ExampleMethod env)
doit :: HasExampleMethod env => RIO env ()
doit = (do
invoke exampleL 2 "foo"
invoke exampleL 3 "foo"
invoke exampleL (-1) "bar"
invoke exampleL 3 "bar") catchAny
(const $ pure ())
Test code
data Env = Env { _example :: ExampleMethod env } makeLenses Env'' instance HasExampleMethod Env where exampleL = example exampleMock :: ExampleMethod exampleMock =mockup
$ dowhen
(args
((<0),anything
)) `thenAction
` throwString "negative n"when
anything
`thenReturn
` () env = Env exampleMock spec :: Spec spec = describe "doit" $ do before $withMonitor_
$ \monitor -> runRIO env $ local (exampleL %~watch
monitor) doit 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 # |