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
23 -- Maintainer : bsinclai@turing.une.edu.au
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
36 -- | An expected request.
39 data Result a
= Ok | Failure
(Response a
) [(Expect
,String)]
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
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.
54 testMPD pairs expected getpw m
= do
55 expectsRef
<- newIORef pairs
56 mismatchesRef
<- newIORef
([] :: [(Expect
, String)])
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
64 else Failure result mismatches
66 send
:: IORef
[(Expect
, Response
String)] -- Expected requests and their
68 -> IORef
[(Expect
, String)] -- An initially empty list of
69 -- mismatches between expected and
72 -> IO (Response
String)
73 send expsR mmsR str
= do
76 ((exp,resp
):_
) |
exp == str
-> modifyIORef expsR
(drop 1) >> return resp
77 |
otherwise -> addMismatch
exp
80 addMismatch
exp = modifyIORef mmsR
((exp,str
):) >> return (Left NoMPD
)