[Haskell-cafe] Need feedback on my EDSL attempt for writing test scripts

C K Kashyap ckkashyap at gmail.com
Mon Apr 23 19:57:30 CEST 2012


Dear cafe,
Recently, I decided to use Haskell to drive the testing of a C++ DLL that
we develop. After getting the FFI etc working, I thought it might be a good
idea to expose an EDSL for the testers to alter the flow of invocations of
the functions in the DLL. I've tried to demonstrate the approach I am
contemplating here - http://hpaste.org/67495

The outcome is that I'll have a set of "Instructions" for the testers can
arrange to create a sequence of calls.

> data Command = Void | Init | Get3Numbers Int Int Int| GetName String |
PrintName | PrintSum | Close | PrintMessage String
>     deriving (Show)

> mainloop :: StateT (ScriptState Command) IO ()
> mainloop = do
>         liftIO $ putStrLn "Hello World"
>         executeCommand Init
>         executeCommand Init
>         executeCommand $ PrintMessage "Enter name"
>         executeCommand $ GetName "abcd"
>         return ()


I'd like to ensure that some level of validation done. For example, if Init
is called twice, the tester should get to know about it. Similarly, GeName
should not be called unless PrintMessage has been called.

> executeCommand :: Command -> StateT (ScriptState Command) IO ()
> executeCommand Init = do
>                (ScriptState c) <- get
>                case c of
>                     Void -> liftIO $ putStrLn (show c)
>                     _    -> liftIO $ putStrLn "Init already called"
>                put (ScriptState Init)
>                return ()
> executeCommand (GetName x) = do
>                (ScriptState c) <- get
>                case c of
>                     PrintMessage _ -> do { str <- liftIO $ getLine; put
(ScriptState (GetName str)); return ()}
>                     _              -> liftIO $ putStrLn "PrintMessage not
called"
> executeCommand (PrintMessage m) = do
>                liftIO $ putStrLn m
>                put (ScriptState (PrintMessage m))


I'd appreciate it very much if you could give me some feedback on my
approach. I get this feeling that I am wrapping up the whole program inside
a State monad - does this mean that I am giving up the functional goodies.

Regards,
Kashyap
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120423/3252ec50/attachment.htm>


More information about the Haskell-Cafe mailing list