[gitconv @ N.M.Core: note that Unexpected is a bug]
[libmpd-haskell.git] / tests / Commands.hs
blobfadad416d6ffd8646cf12a13dc5188b425ccaeb9
1 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
3 -- |
4 -- This module provides a way of verifying that the interface to the MPD
5 -- commands is correct. It does so by capturing the data flow between the
6 -- command and a dummy socket, checking the captured data against a set of
7 -- predefined values that are known to be correct. Of course, this does not
8 -- verify that the external behaviour is correct, it's simply a way of
9 -- catching silly mistakes and subtle bugs in the interface itself, without
10 -- having to actually send any requests to a real server.
12 module Main (main) where
14 import Network.MPD.Commands
15 import Network.MPD.Core (Response)
16 import Network.MPD.StringConn
18 import Control.Monad
19 import Prelude hiding (repeat)
20 import Text.Printf
22 main = mapM_ (\(n, f) -> f >>= \x -> printf "%-14s: %s\n" n x) tests
23 where tests = [("enableOutput", testEnableOutput)
24 ,("disableOutput", testDisableOutput)
25 ,("outputs", testOutputs)
26 ,("update0", testUpdate0)
27 ,("update1", testUpdate1)
28 ,("updateMany", testUpdateMany)
29 ,("find", testFind)
30 ,("list(Nothing)", testListNothing)
31 ,("list(Just)", testListJust)
32 ,("listAll", testListAll)
33 ,("lsInfo", testLsInfo)
34 ,("listAllInfo", testListAllInfo)
35 ,("search", testSearch)
36 ,("count", testCount)
37 ,("add", testAdd)
38 ,("add_", testAdd_)
39 ,("addId", testAddId)
40 ,("clear", testClear)
41 ,("currentSong(_)", testCurrentSongStopped)
42 ,("currentSong(>)", testCurrentSongPlaying)
43 ,("delete0", testDelete0)
44 ,("delete1", testDelete1)
45 ,("delete2", testDelete2)
46 ,("load", testLoad)
47 ,("move0", testMove0)
48 ,("move1", testMove1)
49 ,("move2", testMove2)
50 ,("rm", testRm)
51 ,("rename", testRename)
52 ,("save", testSave)
53 ,("swap0", testSwap0)
54 ,("swap1", testSwap1)
55 ,("shuffle", testShuffle)
56 ,("crossfade", testCrossfade)
57 ,("play", testPlay)
58 ,("pause", testPause)
59 ,("stop", testStop)
60 ,("next", testNext)
61 ,("previous", testPrevious)
62 ,("random", testRandom)
63 ,("repeat", testRepeat)
64 ,("setVolume", testSetVolume)
65 ,("volume", testVolume)
66 ,("clearError", testClearError)
67 ,("commands", testCommands)
68 ,("notCommands", testNotCommands)
69 ,("tagTypes", testTagTypes)
70 ,("urlHandlers", testUrlHandlers)
71 ,("ping", testPing)
74 test a b c = liftM (showResult b) $ testMPD a b (return Nothing) c
76 test_ a b = test a (Right ()) b
78 showResult :: (Show a) => Response a -> Result a -> String
79 showResult _ Ok = "passed"
80 showResult expectedResult (Failure result mms) =
81 "*** FAILURE ***" ++
82 concatMap (\(x,y) -> "\n expected request: " ++ show x ++
83 "\n actual request: " ++ show y) mms ++
84 "\n expected result: " ++ show expectedResult ++
85 "\n actual result: " ++ show result
88 -- Admin commands
91 testEnableOutput = test_ [("enableoutput 1", Right "OK")] (enableOutput 1)
93 testDisableOutput = test_ [("disableoutput 1", Right "OK")] (disableOutput 1)
95 testOutputs =
96 test [("outputs", Right $ unlines ["outputid: 0"
97 ,"outputname: SoundCard0"
98 ,"outputenabled: 1"
99 ,"outputid: 1"
100 ,"outputname: SoundCard1"
101 ,"outputenabled: 0"
102 ,"OK"])]
103 (Right [Device { dOutputID = 0
104 , dOutputName = "SoundCard0"
105 , dOutputEnabled = True }
106 ,Device { dOutputID = 1
107 , dOutputName = "SoundCard1"
108 , dOutputEnabled = False }])
109 outputs
111 testUpdate0 = test_ [("update", Right "updating_db: 1\nOK")] (update [])
113 testUpdate1 =
114 test_ [("update \"foo\"", Right "updating_db: 1\nOK")]
115 (update ["foo"])
117 testUpdateMany =
118 test_ [("command_list_begin\nupdate \"foo\"\nupdate \"bar\"\n\
119 \command_list_end", Right "updating_db: 1\nOK")]
120 (update ["foo","bar"])
123 -- Database commands
126 testFind =
127 test [("find Artist \"Foo\"", Right "file: dir/Foo-Bar.ogg\n\
128 \Time: 60\n\
129 \Artist: Foo\n\
130 \Title: Bar\n\
131 \OK")]
132 (Right [Song { sgArtist = "Foo"
133 , sgAlbum = ""
134 , sgTitle = "Bar"
135 , sgFilePath = "dir/Foo-Bar.ogg"
136 , sgGenre = ""
137 , sgName = ""
138 , sgComposer = ""
139 , sgPerformer = ""
140 , sgLength = 60
141 , sgDate = 0
142 , sgTrack = (0,0)
143 , sgDisc = (0,0)
144 , sgIndex = Nothing
146 (find (Query Artist "Foo"))
148 testListNothing =
149 test [("list Title", Right "Title: Foo\nTitle: Bar\nOK")]
150 (Right ["Foo", "Bar"])
151 (list Title Nothing)
153 testListJust =
154 test [("list Title Artist \"Muzz\"", Right "Title: Foo\nOK")]
155 (Right ["Foo"])
156 (list Title (Just $ Query Artist "Muzz"))
158 testListAll =
159 test [("listall \"\"", Right "directory: FooBand\n\
160 \directory: FooBand/album1\n\
161 \file: FooBand/album1/01 - songA.ogg\n\
162 \file: FooBand/album1/02 - songB.ogg\nOK")]
163 (Right ["FooBand/album1/01 - songA.ogg"
164 ,"FooBand/album1/02 - songB.ogg"])
165 (listAll "")
167 testLsInfo =
168 test [("lsinfo \"\"", Right "directory: Foo\ndirectory: Bar\nOK")]
169 (Right [Left "Bar", Left "Foo"])
170 (lsInfo "")
172 testListAllInfo =
173 test [("listallinfo \"\"", Right "directory: Foo\ndirectory: Bar\nOK")]
174 (Right [Left "Bar", Left "Foo"])
175 (listAllInfo "")
177 testSearch =
178 test [("search Artist \"oo\"", Right "file: dir/Foo-Bar.ogg\n\
179 \Time: 60\n\
180 \Artist: Foo\n\
181 \Title: Bar\n\
182 \OK")]
183 (Right [Song { sgArtist = "Foo"
184 , sgAlbum = ""
185 , sgTitle = "Bar"
186 , sgFilePath = "dir/Foo-Bar.ogg"
187 , sgGenre = ""
188 , sgName = ""
189 , sgComposer = ""
190 , sgPerformer = ""
191 , sgLength = 60
192 , sgDate = 0
193 , sgTrack = (0,0)
194 , sgDisc = (0,0)
195 , sgIndex = Nothing
197 (search (Query Artist "oo"))
199 testCount =
200 test [("count Title \"Foo\"", Right "songs: 1\nplaytime: 60\nOK")]
201 (Right (Count 1 60))
202 (count (Query Title "Foo"))
205 -- Playlist commands
208 testAdd =
209 test [("add \"foo\"", Right "OK"),
210 ("listall \"foo\"", Right "file: Foo\nfile: Bar\nOK")]
211 (Right ["Foo", "Bar"])
212 (add "" "foo")
214 testAdd_ = test_ [("add \"foo\"", Right "OK")] (add_ "" "foo")
216 testAddId =
217 test [("addid \"dir/Foo-Bar.ogg\"", Right "Id: 20\nOK")]
218 (Right 20)
219 (addId "dir/Foo-Bar.ogg")
221 testClear = test_ [("playlistclear \"foo\"", Right "OK")] (clear "foo")
223 testCurrentSongStopped =
224 test [("status", Right "repeat: 0\n\
225 \random: 0\n\
226 \playlist: 253\n\
227 \playlistlength: 0\n\
228 \xfade: 0\n\
229 \state: stop\nOK")]
230 (Right Nothing)
231 (currentSong)
233 testCurrentSongPlaying =
234 test [("status", Right "volume: 80\n\
235 \repeat: 0\n\
236 \random: 0\n\
237 \playlist: 252\n\
238 \playlistlength: 21\n\
239 \xfade: 0\n\
240 \state: play\n\
241 \song: 20\n\
242 \songid: 238\n\
243 \time: 158:376\n\
244 \bitrate: 192\n\
245 \audio: 44100:16:2\n\
246 \OK")
247 ,("currentsong", Right "file: dir/Foo-Bar.ogg\n\
248 \Time: 60\n\
249 \Artist: Foo\n\
250 \Title: Bar\n\
251 \OK")]
252 (Right . Just $ Song { sgArtist = "Foo"
253 , sgAlbum = ""
254 , sgTitle = "Bar"
255 , sgFilePath = "dir/Foo-Bar.ogg"
256 , sgGenre = ""
257 , sgName = ""
258 , sgComposer = ""
259 , sgPerformer = ""
260 , sgLength = 60
261 , sgDate = 0
262 , sgTrack = (0,0)
263 , sgDisc = (0,0)
264 , sgIndex = Nothing
266 (currentSong)
268 testDelete0 = test_ [("delete 1", Right "OK")] (delete "" (Pos 1))
270 testDelete1 = test_ [("deleteid 1", Right "OK")] (delete "" (ID 1))
272 testDelete2 = test_ [("playlistdelete \"foo\" 1", Right "OK")] (delete "foo" (Pos 1))
274 testLoad = test_ [("load \"foo\"", Right "OK")] (load "foo")
276 testMove0 = test_ [("move 1 2", Right "OK")] (move "" (Pos 1) 2)
278 testMove1 = test_ [("moveid 1 2", Right "OK")] (move "" (ID 1) 2)
280 testMove2 = test_ [("playlistmove \"foo\" 1 2", Right "OK")] (move "foo" (Pos 1) 2)
282 testRm = test_ [("rm \"foo\"", Right "OK")] (rm "foo")
284 testRename = test_ [("rename \"foo\" \"bar\"", Right "OK")] (rename "foo" "bar")
286 testSave = test_ [("save \"foo\"", Right "OK")] (save "foo")
288 testSwap0 = test_ [("swap 1 2", Right "OK")] (swap (Pos 1) (Pos 2))
290 testSwap1 = test_ [("swapid 1 2", Right "OK")] (swap (ID 1) (ID 2))
292 testShuffle = test_ [("shuffle", Right "OK")] shuffle
295 -- Playback commands
298 testCrossfade = test_ [("crossfade 0", Right "OK")] (crossfade 0)
300 testPlay = test_ [("play", Right "OK")] (play Nothing)
302 testPause = test_ [("pause 0", Right "OK")] (pause False)
304 testStop = test_ [("stop", Right "OK")] stop
306 testNext = test_ [("next", Right "OK")] next
308 testPrevious = test_ [("previous", Right "OK")] previous
310 testRandom = test_ [("random 0", Right "OK")] (random False)
312 testRepeat = test_ [("repeat 0", Right "OK")] (repeat False)
314 testSetVolume = test_ [("setvol 10", Right "OK")] (setVolume 10)
316 testVolume = test_ [("volume 10", Right "OK")] (volume 10)
319 -- Miscellaneous commands
322 testClearError = test_ [("clearerror", Right "OK")] clearError
324 testCommands =
325 test [("commands", Right "command: foo\ncommand: bar")]
326 (Right ["foo", "bar"])
327 commands
329 testNotCommands =
330 test [("notcommands", Right "command: foo\ncommand: bar")]
331 (Right ["foo", "bar"])
332 notCommands
334 testTagTypes =
335 test [("tagtypes", Right "tagtype: foo\ntagtype: bar")]
336 (Right ["foo", "bar"])
337 tagTypes
339 testUrlHandlers =
340 test [("urlhandlers", Right "urlhandler: foo\nurlhandler: bar")]
341 (Right ["foo", "bar"])
342 urlHandlers
344 testPing = test_ [("ping", Right "OK")] ping
347 -- Extensions\/shortcuts