1 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
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 Commands
(main
) where
14 import Network
.MPD
.Commands
15 import Network
.MPD
.Core
(Response
, MPDError
(..))
16 import Network
.MPD
.StringConn
19 import Prelude
hiding (repeat)
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
)
30 ,("list(Nothing)", testListNothing
)
31 ,("list(Just)", testListJust
)
32 ,("listAll", testListAll
)
33 ,("lsInfo", testLsInfo
)
34 ,("listAllInfo", testListAllInfo
)
35 ,("search", testSearch
)
41 ,("plChangesPosId 0", testPlChangesPosId_0
)
42 ,("plChangesPosId 1", testPlChangesPosId_1
)
43 ,("plChangesPosId wierd", testPlChangesPosId_Wierd
)
44 ,("currentSong(_)", testCurrentSongStopped
)
45 ,("currentSong(>)", testCurrentSongPlaying
)
46 ,("delete0", testDelete0
)
47 ,("delete1", testDelete1
)
48 ,("delete2", testDelete2
)
54 ,("rename", testRename
)
58 ,("shuffle", testShuffle
)
59 ,("playlistInfo0", testPlaylistInfo0
)
60 ,("playlistInfo / pos", testPlaylistInfoPos
)
61 ,("playlistInfo / id", testPlaylistInfoId
)
62 ,("listPlaylistInfo", testListPlaylistInfo
)
63 ,("listPlaylist", testListPlaylist
)
64 ,("crossfade", testCrossfade
)
66 ,("play / pos", testPlayPos
)
67 ,("play / id", testPlayId
)
71 ,("previous", testPrevious
)
72 ,("random", testRandom
)
73 ,("repeat", testRepeat
)
74 ,("setVolume", testSetVolume
)
75 ,("volume", testVolume
)
76 ,("clearError", testClearError
)
77 ,("commands", testCommands
)
78 ,("notCommands", testNotCommands
)
79 ,("tagTypes", testTagTypes
)
80 ,("urlHandlers", testUrlHandlers
)
81 ,("password", testPassword
)
83 ,("song parsing / incomplete track",
84 testSongParseIncompleteTrack
)
85 ,("song parsing / complete track",
86 testSongParseCompleteTrack
)
89 test a b c
= liftM (showResult b
) $ testMPD a b
(return Nothing
) c
91 test_ a b
= test a
(Right
()) b
93 showResult
:: (Show a
) => Response a
-> Result a
-> String
94 showResult _ Ok
= "passed"
95 showResult expectedResult
(Failure result mms
) =
97 concatMap (\(x
,y
) -> "\n expected request: " ++ show x
++
98 "\n actual request: " ++ show y
) mms
++
99 "\n expected result: " ++ show expectedResult
++
100 "\n actual result: " ++ show result
102 emptySong
= Song
{ sgArtist
= ""
114 , sgIndex
= Nothing
}
118 -- These tests are meant to expose problems with internal
122 -- Should handle track = 'X'.
123 testSongParseIncompleteTrack
=
124 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
127 (Right
[emptySong
{ sgTrack
= (1,1)
128 , sgFilePath
= "dir/Foo-Bar.ogg"
130 (find $ Query Artist
"Foo")
132 -- Should handle track = 'X/Y'.
133 testSongParseCompleteTrack
=
134 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
137 (Right
[emptySong
{ sgTrack
= (2,12)
138 , sgFilePath
= "dir/Foo-Bar.ogg"
140 (find $ Query Artist
"Foo")
146 testEnableOutput
= test_
[("enableoutput 1", Right
"OK")] (enableOutput
1)
148 testDisableOutput
= test_
[("disableoutput 1", Right
"OK")] (disableOutput
1)
151 test
[("outputs", Right
$ unlines ["outputid: 0"
152 ,"outputname: SoundCard0"
155 ,"outputname: SoundCard1"
158 (Right
[Device
{ dOutputID
= 0
159 , dOutputName
= "SoundCard0"
160 , dOutputEnabled
= True }
161 ,Device
{ dOutputID
= 1
162 , dOutputName
= "SoundCard1"
163 , dOutputEnabled
= False }])
166 testUpdate0
= test_
[("update", Right
"updating_db: 1\nOK")] (update
[])
169 test_
[("update \"foo\"", Right
"updating_db: 1\nOK")]
173 test_
[("command_list_begin\nupdate \"foo\"\nupdate \"bar\"\n\
174 \command_list_end", Right
"updating_db: 1\nOK")]
175 (update
["foo","bar"])
182 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
187 (Right
[Song
{ sgArtist
= "Foo"
190 , sgFilePath
= "dir/Foo-Bar.ogg"
201 (find (Query Artist
"Foo"))
204 test
[("list Title", Right
"Title: Foo\nTitle: Bar\nOK")]
205 (Right
["Foo", "Bar"])
209 test
[("list Title Artist \"Muzz\"", Right
"Title: Foo\nOK")]
211 (list Title
(Just
$ Query Artist
"Muzz"))
214 test
[("listall \"\"", Right
"directory: FooBand\n\
215 \directory: FooBand/album1\n\
216 \file: FooBand/album1/01 - songA.ogg\n\
217 \file: FooBand/album1/02 - songB.ogg\nOK")]
218 (Right
["FooBand/album1/01 - songA.ogg"
219 ,"FooBand/album1/02 - songB.ogg"])
223 test
[("lsinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
224 (Right
[Left
"Bar", Left
"Foo"])
228 test
[("listallinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
229 (Right
[Left
"Bar", Left
"Foo"])
233 test
[("search Artist \"oo\"", Right
"file: dir/Foo-Bar.ogg\n\
238 (Right
[Song
{ sgArtist
= "Foo"
241 , sgFilePath
= "dir/Foo-Bar.ogg"
252 (search
(Query Artist
"oo"))
255 test
[("count Title \"Foo\"", Right
"songs: 1\nplaytime: 60\nOK")]
257 (count
(Query Title
"Foo"))
264 test
[("add \"foo\"", Right
"OK"),
265 ("listall \"foo\"", Right
"file: Foo\nfile: Bar\nOK")]
266 (Right
["Foo", "Bar"])
269 testAdd_
= test_
[("add \"foo\"", Right
"OK")] (add_
"" "foo")
272 test
[("addid \"dir/Foo-Bar.ogg\"", Right
"Id: 20\nOK")]
274 (addId
"dir/Foo-Bar.ogg")
276 testClear
= test_
[("playlistclear \"foo\"", Right
"OK")] (clear
"foo")
278 testPlChangesPosId_0
=
279 test
[("plchangesposid 10", Right
"OK")]
283 testPlChangesPosId_1
=
284 test
[("plchangesposid 10", Right
"cpos: 0\nId: 20\nOK")]
285 (Right
[(Pos
0, ID
20)])
288 testPlChangesPosId_Wierd
=
289 test
[("plchangesposid 10", Right
"cpos: foo\nId: bar\nOK")]
290 (Left
$ Unexpected
"[(\"cpos\",\"foo\"),(\"Id\",\"bar\")]")
293 testCurrentSongStopped
=
294 test
[("status", Right
"repeat: 0\n\
297 \playlistlength: 0\n\
303 testCurrentSongPlaying
=
304 test
[("status", Right
"volume: 80\n\
308 \playlistlength: 21\n\
315 \audio: 44100:16:2\n\
317 ,("currentsong", Right
"file: dir/Foo-Bar.ogg\n\
322 (Right
. Just
$ Song
{ sgArtist
= "Foo"
325 , sgFilePath
= "dir/Foo-Bar.ogg"
338 testDelete0
= test_
[("delete 1", Right
"OK")] (delete "" (Pos
1))
340 testDelete1
= test_
[("deleteid 1", Right
"OK")] (delete "" (ID
1))
342 testDelete2
= test_
[("playlistdelete \"foo\" 1", Right
"OK")] (delete "foo" (Pos
1))
344 testLoad
= test_
[("load \"foo\"", Right
"OK")] (load
"foo")
346 testMove0
= test_
[("move 1 2", Right
"OK")] (move
"" (Pos
1) 2)
348 testMove1
= test_
[("moveid 1 2", Right
"OK")] (move
"" (ID
1) 2)
350 testMove2
= test_
[("playlistmove \"foo\" 1 2", Right
"OK")] (move
"foo" (Pos
1) 2)
352 testRm
= test_
[("rm \"foo\"", Right
"OK")] (rm
"foo")
354 testRename
= test_
[("rename \"foo\" \"bar\"", Right
"OK")] (rename
"foo" "bar")
356 testSave
= test_
[("save \"foo\"", Right
"OK")] (save
"foo")
358 testSwap0
= test_
[("swap 1 2", Right
"OK")] (swap
(Pos
1) (Pos
2))
360 testSwap1
= test_
[("swapid 1 2", Right
"OK")] (swap
(ID
1) (ID
2))
362 testShuffle
= test_
[("shuffle", Right
"OK")] shuffle
364 testPlaylistInfo0
= test
[("playlistinfo", Right
"file: dir/Foo-Bar.ogg\n\
369 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
372 , sgTitle
= "Bar" }])
373 (playlistInfo Nothing
)
375 testPlaylistInfoPos
= test
[("playlistinfo 1", Right
"file: dir/Foo-Bar.ogg\n\
380 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
383 , sgTitle
= "Bar" }])
384 (playlistInfo
. Just
$ Pos
1)
386 testPlaylistInfoId
= test
[("playlistid 1", Right
"file: dir/Foo-Bar.ogg\n\
391 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
394 , sgTitle
= "Bar" }])
395 (playlistInfo
. Just
$ ID
1)
397 testListPlaylistInfo
= test
[("listplaylistinfo \"foo\""
398 ,Right
"file: dir/Foo-Bar.ogg\n\
403 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
406 , sgTitle
= "Bar" }])
407 (listPlaylistInfo
"foo")
409 testListPlaylist
= test
[("listplaylist \"foo\""
410 ,Right
"file: dir/Foo-bar.ogg\n\
411 \file: dir/Quux-quuz.ogg\n\
413 (Right
["dir/Foo-bar.ogg", "dir/Quux-quuz.ogg"])
420 testCrossfade
= test_
[("crossfade 0", Right
"OK")] (crossfade
0)
422 testPlay
= test_
[("play", Right
"OK")] (play Nothing
)
424 testPlayPos
= test_
[("play 1", Right
"OK")] (play
. Just
$ Pos
1)
426 testPlayId
= test_
[("playid 1", Right
"OK")] (play
. Just
$ ID
1)
428 testPause
= test_
[("pause 0", Right
"OK")] (pause
False)
430 testStop
= test_
[("stop", Right
"OK")] stop
432 testNext
= test_
[("next", Right
"OK")] next
434 testPrevious
= test_
[("previous", Right
"OK")] previous
436 testRandom
= test_
[("random 0", Right
"OK")] (random False)
438 testRepeat
= test_
[("repeat 0", Right
"OK")] (repeat False)
440 testSetVolume
= test_
[("setvol 10", Right
"OK")] (setVolume
10)
442 testVolume
= test_
[("volume 10", Right
"OK")] (volume
10)
445 -- Miscellaneous commands
448 testClearError
= test_
[("clearerror", Right
"OK")] clearError
451 test
[("commands", Right
"command: foo\ncommand: bar")]
452 (Right
["foo", "bar"])
456 test
[("notcommands", Right
"command: foo\ncommand: bar")]
457 (Right
["foo", "bar"])
461 test
[("tagtypes", Right
"tagtype: foo\ntagtype: bar")]
462 (Right
["foo", "bar"])
466 test
[("urlhandlers", Right
"urlhandler: foo\nurlhandler: bar")]
467 (Right
["foo", "bar"])
470 testPassword
= test_
[("password foo", Right
"OK")] (password
"foo")
472 testPing
= test_
[("ping", Right
"OK")] ping
475 -- Extensions\/shortcuts