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
)
69 ,("previous", testPrevious
)
70 ,("random", testRandom
)
71 ,("repeat", testRepeat
)
72 ,("setVolume", testSetVolume
)
73 ,("volume", testVolume
)
74 ,("clearError", testClearError
)
75 ,("commands", testCommands
)
76 ,("notCommands", testNotCommands
)
77 ,("tagTypes", testTagTypes
)
78 ,("urlHandlers", testUrlHandlers
)
80 ,("song parsing / incomplete track",
81 testSongParseIncompleteTrack
)
82 ,("song parsing / complete track",
83 testSongParseCompleteTrack
)
86 test a b c
= liftM (showResult b
) $ testMPD a b
(return Nothing
) c
88 test_ a b
= test a
(Right
()) b
90 showResult
:: (Show a
) => Response a
-> Result a
-> String
91 showResult _ Ok
= "passed"
92 showResult expectedResult
(Failure result mms
) =
94 concatMap (\(x
,y
) -> "\n expected request: " ++ show x
++
95 "\n actual request: " ++ show y
) mms
++
96 "\n expected result: " ++ show expectedResult
++
97 "\n actual result: " ++ show result
99 emptySong
= Song
{ sgArtist
= ""
111 , sgIndex
= Nothing
}
115 -- These tests are meant to expose problems with internal
119 -- Should handle track = 'X'.
120 testSongParseIncompleteTrack
=
121 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
124 (Right
[emptySong
{ sgTrack
= (1,1)
125 , sgFilePath
= "dir/Foo-Bar.ogg"
127 (find $ Query Artist
"Foo")
129 -- Should handle track = 'X/Y'.
130 testSongParseCompleteTrack
=
131 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
134 (Right
[emptySong
{ sgTrack
= (2,12)
135 , sgFilePath
= "dir/Foo-Bar.ogg"
137 (find $ Query Artist
"Foo")
143 testEnableOutput
= test_
[("enableoutput 1", Right
"OK")] (enableOutput
1)
145 testDisableOutput
= test_
[("disableoutput 1", Right
"OK")] (disableOutput
1)
148 test
[("outputs", Right
$ unlines ["outputid: 0"
149 ,"outputname: SoundCard0"
152 ,"outputname: SoundCard1"
155 (Right
[Device
{ dOutputID
= 0
156 , dOutputName
= "SoundCard0"
157 , dOutputEnabled
= True }
158 ,Device
{ dOutputID
= 1
159 , dOutputName
= "SoundCard1"
160 , dOutputEnabled
= False }])
163 testUpdate0
= test_
[("update", Right
"updating_db: 1\nOK")] (update
[])
166 test_
[("update \"foo\"", Right
"updating_db: 1\nOK")]
170 test_
[("command_list_begin\nupdate \"foo\"\nupdate \"bar\"\n\
171 \command_list_end", Right
"updating_db: 1\nOK")]
172 (update
["foo","bar"])
179 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
184 (Right
[Song
{ sgArtist
= "Foo"
187 , sgFilePath
= "dir/Foo-Bar.ogg"
198 (find (Query Artist
"Foo"))
201 test
[("list Title", Right
"Title: Foo\nTitle: Bar\nOK")]
202 (Right
["Foo", "Bar"])
206 test
[("list Title Artist \"Muzz\"", Right
"Title: Foo\nOK")]
208 (list Title
(Just
$ Query Artist
"Muzz"))
211 test
[("listall \"\"", Right
"directory: FooBand\n\
212 \directory: FooBand/album1\n\
213 \file: FooBand/album1/01 - songA.ogg\n\
214 \file: FooBand/album1/02 - songB.ogg\nOK")]
215 (Right
["FooBand/album1/01 - songA.ogg"
216 ,"FooBand/album1/02 - songB.ogg"])
220 test
[("lsinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
221 (Right
[Left
"Bar", Left
"Foo"])
225 test
[("listallinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
226 (Right
[Left
"Bar", Left
"Foo"])
230 test
[("search Artist \"oo\"", Right
"file: dir/Foo-Bar.ogg\n\
235 (Right
[Song
{ sgArtist
= "Foo"
238 , sgFilePath
= "dir/Foo-Bar.ogg"
249 (search
(Query Artist
"oo"))
252 test
[("count Title \"Foo\"", Right
"songs: 1\nplaytime: 60\nOK")]
254 (count
(Query Title
"Foo"))
261 test
[("add \"foo\"", Right
"OK"),
262 ("listall \"foo\"", Right
"file: Foo\nfile: Bar\nOK")]
263 (Right
["Foo", "Bar"])
266 testAdd_
= test_
[("add \"foo\"", Right
"OK")] (add_
"" "foo")
269 test
[("addid \"dir/Foo-Bar.ogg\"", Right
"Id: 20\nOK")]
271 (addId
"dir/Foo-Bar.ogg")
273 testClear
= test_
[("playlistclear \"foo\"", Right
"OK")] (clear
"foo")
275 testPlChangesPosId_0
=
276 test
[("plchangesposid 10", Right
"OK")]
280 testPlChangesPosId_1
=
281 test
[("plchangesposid 10", Right
"cpos: 0\nId: 20\nOK")]
282 (Right
[(Pos
0, ID
20)])
285 testPlChangesPosId_Wierd
=
286 test
[("plchangesposid 10", Right
"cpos: foo\nId: bar\nOK")]
287 (Left
$ Unexpected
"[(\"cpos\",\"foo\"),(\"Id\",\"bar\")]")
290 testCurrentSongStopped
=
291 test
[("status", Right
"repeat: 0\n\
294 \playlistlength: 0\n\
300 testCurrentSongPlaying
=
301 test
[("status", Right
"volume: 80\n\
305 \playlistlength: 21\n\
312 \audio: 44100:16:2\n\
314 ,("currentsong", Right
"file: dir/Foo-Bar.ogg\n\
319 (Right
. Just
$ Song
{ sgArtist
= "Foo"
322 , sgFilePath
= "dir/Foo-Bar.ogg"
335 testDelete0
= test_
[("delete 1", Right
"OK")] (delete "" (Pos
1))
337 testDelete1
= test_
[("deleteid 1", Right
"OK")] (delete "" (ID
1))
339 testDelete2
= test_
[("playlistdelete \"foo\" 1", Right
"OK")] (delete "foo" (Pos
1))
341 testLoad
= test_
[("load \"foo\"", Right
"OK")] (load
"foo")
343 testMove0
= test_
[("move 1 2", Right
"OK")] (move
"" (Pos
1) 2)
345 testMove1
= test_
[("moveid 1 2", Right
"OK")] (move
"" (ID
1) 2)
347 testMove2
= test_
[("playlistmove \"foo\" 1 2", Right
"OK")] (move
"foo" (Pos
1) 2)
349 testRm
= test_
[("rm \"foo\"", Right
"OK")] (rm
"foo")
351 testRename
= test_
[("rename \"foo\" \"bar\"", Right
"OK")] (rename
"foo" "bar")
353 testSave
= test_
[("save \"foo\"", Right
"OK")] (save
"foo")
355 testSwap0
= test_
[("swap 1 2", Right
"OK")] (swap
(Pos
1) (Pos
2))
357 testSwap1
= test_
[("swapid 1 2", Right
"OK")] (swap
(ID
1) (ID
2))
359 testShuffle
= test_
[("shuffle", Right
"OK")] shuffle
361 testPlaylistInfo0
= test
[("playlistinfo", Right
"file: dir/Foo-Bar.ogg\n\
366 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
369 , sgTitle
= "Bar" }])
370 (playlistInfo Nothing
)
372 testPlaylistInfoPos
= test
[("playlistinfo 1", Right
"file: dir/Foo-Bar.ogg\n\
377 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
380 , sgTitle
= "Bar" }])
381 (playlistInfo
. Just
$ Pos
1)
383 testPlaylistInfoId
= test
[("playlistid 1", Right
"file: dir/Foo-Bar.ogg\n\
388 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
391 , sgTitle
= "Bar" }])
392 (playlistInfo
. Just
$ ID
1)
394 testListPlaylistInfo
= test
[("listplaylistinfo \"foo\""
395 ,Right
"file: dir/Foo-Bar.ogg\n\
400 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
403 , sgTitle
= "Bar" }])
404 (listPlaylistInfo
"foo")
406 testListPlaylist
= test
[("listplaylist \"foo\""
407 ,Right
"file: dir/Foo-bar.ogg\n\
408 \file: dir/Quux-quuz.ogg\n\
410 (Right
["dir/Foo-bar.ogg", "dir/Quux-quuz.ogg"])
417 testCrossfade
= test_
[("crossfade 0", Right
"OK")] (crossfade
0)
419 testPlay
= test_
[("play", Right
"OK")] (play Nothing
)
421 testPause
= test_
[("pause 0", Right
"OK")] (pause
False)
423 testStop
= test_
[("stop", Right
"OK")] stop
425 testNext
= test_
[("next", Right
"OK")] next
427 testPrevious
= test_
[("previous", Right
"OK")] previous
429 testRandom
= test_
[("random 0", Right
"OK")] (random False)
431 testRepeat
= test_
[("repeat 0", Right
"OK")] (repeat False)
433 testSetVolume
= test_
[("setvol 10", Right
"OK")] (setVolume
10)
435 testVolume
= test_
[("volume 10", Right
"OK")] (volume
10)
438 -- Miscellaneous commands
441 testClearError
= test_
[("clearerror", Right
"OK")] clearError
444 test
[("commands", Right
"command: foo\ncommand: bar")]
445 (Right
["foo", "bar"])
449 test
[("notcommands", Right
"command: foo\ncommand: bar")]
450 (Right
["foo", "bar"])
454 test
[("tagtypes", Right
"tagtype: foo\ntagtype: bar")]
455 (Right
["foo", "bar"])
459 test
[("urlhandlers", Right
"urlhandler: foo\nurlhandler: bar")]
460 (Right
["foo", "bar"])
463 testPing
= test_
[("ping", Right
"OK")] ping
466 -- Extensions\/shortcuts