This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Convenient way for manually writing examples #347
Comments
Slightly better version perhaps, checking preconditions: example :: forall model cmd m resp. (Rank2.Foldable resp, Show (cmd Symbolic))
=> StateMachine model cmd m resp
-> Example cmd ()
-> Commands cmd resp
example sm =
Commands . fst . flip runGenSym newCounter . go (initModel sm)
where
go :: model Symbolic -> Example cmd () -> GenSym [Command cmd resp]
go _ (Done ()) = return []
go _ (Fail err) = error $ "example: " ++ err
go m (Run cmd k) = do
case Logic.logic (precondition sm m cmd) of
Logic.VFalse counterexample ->
error $ "Invalid command " ++ show cmd ++ ": " ++ show counterexample
Logic.VTrue -> do
resp <- mock sm m cmd
let m' :: model Symbolic
m' = transition sm m cmd resp
vars :: [Var]
vars = getUsedVars resp
cmd' :: Command cmd resp
cmd' = Command cmd resp vars
(cmd' :) <$> go m' (k vars) |
+1 from me on this. I had also found it inconvenient to write manual commands. |
Neat, sure! |
I think Free Monad may also be used: data ExampleF cmd a =
Done
| Run (cmd Symbolic) ([Var] -> a)
| Fail String
deriving Functor
type Example cmd = Free (ExampleF cmd)
instance MonadFail (Example cmd) where
fail = liftF . Fail But maybe is an overkill? |
It would just change the example right? I don't feel very strongly about that, though personally I generally find code that doesn't use the free monad package easier to understand, and performance for these commands is not going to be critical (if your tests are slow because are generating so many commands that you need a better performing interpreter, then the interpreter is the least of your worries, I think :D ). |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Writing example
Commands
by hand (when developing the tests, or when wanting to save particular generated tests) is a bit inconvenient. Not only doesCommands
capture the result of the mock implementation (and so when the model changes, we have to update our examples, if even if the commands are still the same), we also have to manually deal with references. To make this a bit more convenient, I wrote the following helper, which I find rather neat:For example, I am currently working on some tests to do with threads, killing them, etc. Here are some manually written
Commands
:Quite nice, I think. Might be worth adding to the library?
The text was updated successfully, but these errors were encountered: