Rename Prim.hs to Core.hs.
[libmpd_haskell.git] / Network / MPD / StringConn.hs
blobba65b68856cac9270ef160b3cbfe6cab4a8bcd83
1 {-
2 libmpd for Haskell, an MPD client library.
3 Copyright (C) 2005-2007 Ben Sinclair <bsinclai@turing.une.edu.au>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 -- | Module : Network.MPD.StringConn
21 -- Copyright : (c) Ben Sinclair 2005-2007
22 -- License : LGPL
23 -- Maintainer : bsinclai@turing.une.edu.au
24 -- Stability : alpha
25 -- Portability : Haskell 98
27 -- Connection over a network socket.
29 module Network.MPD.StringConn (Expect, Result(..), testMPD) where
31 import Control.Monad (liftM)
32 import Prelude hiding (exp)
33 import Network.MPD.Core
34 import Data.IORef
36 -- | An expected request.
37 type Expect = String
39 data Result a = Ok | Failure (Response a) [(Expect,String)]
40 deriving Show
42 -- | Run an action against a set of expected requests and responses,
43 -- and an expected result. The result is Nothing if everything matched
44 -- what was expected. If anything differed the result of the
45 -- computation is returned along with pairs of expected and received
46 -- requests.
47 testMPD :: (Eq a)
48 => [(Expect, Response String)] -- ^ The expected requests and their
49 -- ^ corresponding responses.
50 -> Response a -- ^ The expected result.
51 -> IO (Maybe String) -- ^ An action that supplies passwords.
52 -> MPD a -- ^ The MPD action to run.
53 -> IO (Result a)
54 testMPD pairs expected getpw m = do
55 expectsRef <- newIORef pairs
56 mismatchesRef <- newIORef ([] :: [(Expect, String)])
57 let open' = return ()
58 close' = return ()
59 send' = send expectsRef mismatchesRef
60 result <- runMPD m $ Conn open' close' send' getpw
61 mismatches <- liftM reverse $ readIORef mismatchesRef
62 return $ if null mismatches && result == expected
63 then Ok
64 else Failure result mismatches
66 send :: IORef [(Expect, Response String)] -- Expected requests and their
67 -- responses.
68 -> IORef [(Expect, String)] -- An initially empty list of
69 -- mismatches between expected and
70 -- actual requests.
71 -> String
72 -> IO (Response String)
73 send expsR mmsR str = do
74 xs <- readIORef expsR
75 case xs of
76 ((exp,resp):_) | exp == str -> modifyIORef expsR (drop 1) >> return resp
77 | otherwise -> addMismatch exp
78 [] -> addMismatch ""
79 where
80 addMismatch exp = modifyIORef mmsR ((exp,str):) >> return (Left NoMPD)