Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Testing IO based monads and functions (David Hinkes)
   2. Re:  Testing IO based monads and functions (Felipe Almeida Lessa)


----------------------------------------------------------------------

Message: 1
Date: Thu, 26 Jan 2012 17:01:57 -0800
From: David Hinkes <[email protected]>
Subject: [Haskell-beginners] Testing IO based monads and functions
To: [email protected]
Message-ID:
        <ca+_cxfnhs+tdna7fqqgx9aoq-nxw9beg7l8ag6koz35zwja...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hey Haskellers, I had a design question:

I'm using Haskell for the first time for something other than a simple toy
application.  I notice I'm coming up with a lot of IO based APIs that looks
something like this:

type UserInfo = (String, String)  -- (name, password)

getMagicNumberFromHTTPServer :: UserInfo -> IO (Maybe Int)
getMagicNumberFromHTTPServer user = do
  let curlHTTPRequest = ... --Setup curl HTTP request
  httpRepsponse <- ... -- Make curl HTTP request
  return $ tryGetMagicNumberFromResponse httpResponse

I like this API, because it's easy to understand.  However, I find it hard
to test.  In OO land, I'd create a mock CurlHTTPRequest object and pass
that into the magic number logic and test for the correct usage of the curl
library.  Is there a FP way of achieving the same goals?

I suppose given the example above I could break down the above into several
smaller pure functions and test them.  I don't know if this strategy will
hold-up when dealing with more complex monads.

Thanks for your time,
Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120126/35d0eea0/attachment-0001.htm>

------------------------------

Message: 2
Date: Thu, 26 Jan 2012 23:09:29 -0200
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Testing IO based monads and functions
To: David Hinkes <[email protected]>
Cc: [email protected]
Message-ID:
        <CANd=oggngd4np3nt041ytekp6jhypipfdv5capo+ghoux6n...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Thu, Jan 26, 2012 at 11:01 PM, David Hinkes <[email protected]> wrote:
> type UserInfo = (String, String) ?-- (name, password)
>
> getMagicNumberFromHTTPServer :: UserInfo -> IO (Maybe Int)
> getMagicNumberFromHTTPServer user = do
> ? let curlHTTPRequest = ... --Setup curl HTTP request
> ? httpRepsponse <- ... -- Make curl HTTP request
> ? return $ tryGetMagicNumberFromResponse httpResponse

One possible way would be having

  class (Functor m, Monad m) => CurlMonad m where
    curlRequest :: ... -> m ...

  instance CurlMonad IO where
    ...

  getMagicNumberFromHttpServer :: CurlMonad m => UserInfo -> m (Maybe Info)
  getMagicNumberFromHttpServer user = do
    httpResponse <- curlRequest ...
    return $ tryGetMagicNumberFromResponse httpResponse

Now on your tests you could have

  data Mock a = ...

  instance CurlMonad Mock where
    ...

where your Mock instance doesn't really use Curl.

HTH, =)


-- 
Felipe.



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 43, Issue 31
*****************************************

Reply via email to