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
)
39 ,("add_ / playlist", testAdd_pl
)
42 ,("plChangesPosId 0", testPlChangesPosId_0
)
43 ,("plChangesPosId 1", testPlChangesPosId_1
)
44 ,("plChangesPosId wierd", testPlChangesPosId_Wierd
)
45 ,("currentSong(_)", testCurrentSongStopped
)
46 ,("currentSong(>)", testCurrentSongPlaying
)
47 ,("delete0", testDelete0
)
48 ,("delete1", testDelete1
)
49 ,("delete2", testDelete2
)
55 ,("rename", testRename
)
59 ,("shuffle", testShuffle
)
60 ,("playlistInfo0", testPlaylistInfo0
)
61 ,("playlistInfo / pos", testPlaylistInfoPos
)
62 ,("playlistInfo / id", testPlaylistInfoId
)
63 ,("listPlaylistInfo", testListPlaylistInfo
)
64 ,("listPlaylist", testListPlaylist
)
65 ,("playlist", testPlaylist
)
66 ,("plchanges", testPlChanges
)
67 ,("playlistFind", testPlaylistFind
)
68 ,("playlistSearch", testPlaylistSearch
)
69 ,("crossfade", testCrossfade
)
71 ,("play / pos", testPlayPos
)
72 ,("play / id", testPlayId
)
76 ,("previous", testPrevious
)
77 ,("seek / pos", testSeekPos
)
78 ,("seek / id", testSeekId
)
79 ,("random", testRandom
)
80 ,("repeat", testRepeat
)
81 ,("setVolume", testSetVolume
)
82 ,("volume", testVolume
)
83 ,("clearError", testClearError
)
84 ,("commands", testCommands
)
85 ,("notCommands", testNotCommands
)
86 ,("tagTypes", testTagTypes
)
87 ,("urlHandlers", testUrlHandlers
)
88 ,("password", testPassword
)
91 ,("updateId0", testUpdateId0
)
92 ,("updateId1", testUpdateId1
)
93 ,("toggle / stop", testToggleStop
)
94 ,("toggle / play", testTogglePlay
)
95 ,("toggle / pause", testTogglePause
)
96 ,("addMany0", testAddMany0
)
97 ,("addMany1", testAddMany1
)
98 ,("song parsing / incomplete track",
99 testSongParseIncompleteTrack
)
100 ,("song parsing / complete track",
101 testSongParseCompleteTrack
)
104 test a b c
= liftM (showResult b
) $ testMPD a b
(return Nothing
) c
106 test_ a b
= test a
(Right
()) b
108 showResult
:: (Show a
) => Response a
-> Result a
-> String
109 showResult _ Ok
= "passed"
110 showResult expectedResult
(Failure result mms
) =
112 concatMap (\(x
,y
) -> "\n expected request: " ++ show x
++
113 "\n actual request: " ++ show y
) mms
++
114 "\n expected result: " ++ show expectedResult
++
115 "\n actual result: " ++ show result
117 emptySong
= Song
{ sgArtist
= ""
129 , sgIndex
= Nothing
}
133 -- These tests are meant to expose problems with internal
137 -- Should handle track = 'X'.
138 testSongParseIncompleteTrack
=
139 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
142 (Right
[emptySong
{ sgTrack
= (1,1)
143 , sgFilePath
= "dir/Foo-Bar.ogg"
145 (find $ Query Artist
"Foo")
147 -- Should handle track = 'X/Y'.
148 testSongParseCompleteTrack
=
149 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
152 (Right
[emptySong
{ sgTrack
= (2,12)
153 , sgFilePath
= "dir/Foo-Bar.ogg"
155 (find $ Query Artist
"Foo")
161 testEnableOutput
= test_
[("enableoutput 1", Right
"OK")] (enableOutput
1)
163 testDisableOutput
= test_
[("disableoutput 1", Right
"OK")] (disableOutput
1)
166 test
[("outputs", Right
$ unlines ["outputid: 0"
167 ,"outputname: SoundCard0"
170 ,"outputname: SoundCard1"
173 (Right
[Device
{ dOutputID
= 0
174 , dOutputName
= "SoundCard0"
175 , dOutputEnabled
= True }
176 ,Device
{ dOutputID
= 1
177 , dOutputName
= "SoundCard1"
178 , dOutputEnabled
= False }])
181 testUpdate0
= test_
[("update", Right
"updating_db: 1\nOK")] (update
[])
184 test_
[("update \"foo\"", Right
"updating_db: 1\nOK")]
188 test_
[("command_list_begin\nupdate \"foo\"\nupdate \"bar\"\n\
189 \command_list_end", Right
"updating_db: 1\nOK")]
190 (update
["foo","bar"])
197 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
202 (Right
[Song
{ sgArtist
= "Foo"
205 , sgFilePath
= "dir/Foo-Bar.ogg"
216 (find (Query Artist
"Foo"))
219 test
[("list Title", Right
"Title: Foo\nTitle: Bar\nOK")]
220 (Right
["Foo", "Bar"])
224 test
[("list Title Artist \"Muzz\"", Right
"Title: Foo\nOK")]
226 (list Title
(Just
$ Query Artist
"Muzz"))
229 test
[("listall \"\"", Right
"directory: FooBand\n\
230 \directory: FooBand/album1\n\
231 \file: FooBand/album1/01 - songA.ogg\n\
232 \file: FooBand/album1/02 - songB.ogg\nOK")]
233 (Right
["FooBand/album1/01 - songA.ogg"
234 ,"FooBand/album1/02 - songB.ogg"])
238 test
[("lsinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
239 (Right
[Left
"Bar", Left
"Foo"])
243 test
[("listallinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
244 (Right
[Left
"Bar", Left
"Foo"])
248 test
[("search Artist \"oo\"", Right
"file: dir/Foo-Bar.ogg\n\
253 (Right
[Song
{ sgArtist
= "Foo"
256 , sgFilePath
= "dir/Foo-Bar.ogg"
267 (search
(Query Artist
"oo"))
270 test
[("count Title \"Foo\"", Right
"songs: 1\nplaytime: 60\nOK")]
272 (count
(Query Title
"Foo"))
279 test
[("add \"foo\"", Right
"OK"),
280 ("listall \"foo\"", Right
"file: Foo\nfile: Bar\nOK")]
281 (Right
["Foo", "Bar"])
284 testAdd_
= test_
[("add \"foo\"", Right
"OK")] (add_
"" "foo")
286 testAdd_pl
= test_
[("playlistadd \"foo\" \"bar\"", Right
"OK")]
290 test
[("addid \"dir/Foo-Bar.ogg\"", Right
"Id: 20\nOK")]
292 (addId
"dir/Foo-Bar.ogg")
294 testClear
= test_
[("playlistclear \"foo\"", Right
"OK")] (clear
"foo")
296 testPlChangesPosId_0
=
297 test
[("plchangesposid 10", Right
"OK")]
301 testPlChangesPosId_1
=
302 test
[("plchangesposid 10", Right
"cpos: 0\nId: 20\nOK")]
303 (Right
[(Pos
0, ID
20)])
306 testPlChangesPosId_Wierd
=
307 test
[("plchangesposid 10", Right
"cpos: foo\nId: bar\nOK")]
308 (Left
$ Unexpected
"[(\"cpos\",\"foo\"),(\"Id\",\"bar\")]")
311 testCurrentSongStopped
=
312 test
[("status", Right
"repeat: 0\n\
315 \playlistlength: 0\n\
321 testCurrentSongPlaying
=
322 test
[("status", Right
"volume: 80\n\
326 \playlistlength: 21\n\
333 \audio: 44100:16:2\n\
335 ,("currentsong", Right
"file: dir/Foo-Bar.ogg\n\
340 (Right
. Just
$ Song
{ sgArtist
= "Foo"
343 , sgFilePath
= "dir/Foo-Bar.ogg"
356 testDelete0
= test_
[("delete 1", Right
"OK")] (delete "" (Pos
1))
358 testDelete1
= test_
[("deleteid 1", Right
"OK")] (delete "" (ID
1))
360 testDelete2
= test_
[("playlistdelete \"foo\" 1", Right
"OK")] (delete "foo" (Pos
1))
362 testLoad
= test_
[("load \"foo\"", Right
"OK")] (load
"foo")
364 testMove0
= test_
[("move 1 2", Right
"OK")] (move
"" (Pos
1) 2)
366 testMove1
= test_
[("moveid 1 2", Right
"OK")] (move
"" (ID
1) 2)
368 testMove2
= test_
[("playlistmove \"foo\" 1 2", Right
"OK")] (move
"foo" (Pos
1) 2)
370 testRm
= test_
[("rm \"foo\"", Right
"OK")] (rm
"foo")
372 testRename
= test_
[("rename \"foo\" \"bar\"", Right
"OK")] (rename
"foo" "bar")
374 testSave
= test_
[("save \"foo\"", Right
"OK")] (save
"foo")
376 testSwap0
= test_
[("swap 1 2", Right
"OK")] (swap
(Pos
1) (Pos
2))
378 testSwap1
= test_
[("swapid 1 2", Right
"OK")] (swap
(ID
1) (ID
2))
380 testShuffle
= test_
[("shuffle", Right
"OK")] shuffle
382 testPlaylistInfo0
= test
[("playlistinfo", Right
"file: dir/Foo-Bar.ogg\n\
387 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
390 , sgTitle
= "Bar" }])
391 (playlistInfo Nothing
)
393 testPlaylistInfoPos
= test
[("playlistinfo 1", Right
"file: dir/Foo-Bar.ogg\n\
398 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
401 , sgTitle
= "Bar" }])
402 (playlistInfo
. Just
$ Pos
1)
404 testPlaylistInfoId
= test
[("playlistid 1", Right
"file: dir/Foo-Bar.ogg\n\
409 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
412 , sgTitle
= "Bar" }])
413 (playlistInfo
. Just
$ ID
1)
415 testListPlaylistInfo
= test
[("listplaylistinfo \"foo\""
416 ,Right
"file: dir/Foo-Bar.ogg\n\
421 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
424 , sgTitle
= "Bar" }])
425 (listPlaylistInfo
"foo")
427 testListPlaylist
= test
[("listplaylist \"foo\""
428 ,Right
"file: dir/Foo-bar.ogg\n\
429 \file: dir/Quux-quuz.ogg\n\
431 (Right
["dir/Foo-bar.ogg", "dir/Quux-quuz.ogg"])
434 testPlaylist
= test
[("playlist"
438 (Right
[(Pos
1, "Foo.ogg")
439 ,(Pos
2, "Bar.ogg")])
442 testPlChanges
= test
[("plchanges 0"
443 ,Right
"file: foo/bar.ogg\n\
447 (Right
[emptySong
{ sgArtist
= "Foo"
449 , sgFilePath
= "foo/bar.ogg" }])
452 testPlaylistFind
= test
[("playlistfind Artist \"Foo\""
453 ,Right
"file: dir/Foo/Bar.ogg\n\
456 (Right
[emptySong
{ sgFilePath
= "dir/Foo/Bar.ogg"
457 , sgArtist
= "Foo" }])
458 (playlistFind
$ Query Artist
"Foo")
460 testPlaylistSearch
= test
[("playlistsearch Artist \"Foo\""
461 ,Right
"file: dir/Foo/Bar.ogg\n\
464 (Right
[emptySong
{ sgFilePath
= "dir/Foo/Bar.ogg"
465 , sgArtist
= "Foo" }])
466 (playlistSearch
$ Query Artist
"Foo")
472 testCrossfade
= test_
[("crossfade 0", Right
"OK")] (crossfade
0)
474 testPlay
= test_
[("play", Right
"OK")] (play Nothing
)
476 testPlayPos
= test_
[("play 1", Right
"OK")] (play
. Just
$ Pos
1)
478 testPlayId
= test_
[("playid 1", Right
"OK")] (play
. Just
$ ID
1)
480 testPause
= test_
[("pause 0", Right
"OK")] (pause
False)
482 testStop
= test_
[("stop", Right
"OK")] stop
484 testNext
= test_
[("next", Right
"OK")] next
486 testPrevious
= test_
[("previous", Right
"OK")] previous
488 testSeekPos
= test_
[("seek 1 10", Right
"OK")] (seek
(Just
$ Pos
1) 10)
490 testSeekId
= test_
[("seekid 1 10", Right
"OK")] (seek
(Just
$ ID
1) 10)
492 testRandom
= test_
[("random 0", Right
"OK")] (random False)
494 testRepeat
= test_
[("repeat 0", Right
"OK")] (repeat False)
496 testSetVolume
= test_
[("setvol 10", Right
"OK")] (setVolume
10)
498 testVolume
= test_
[("volume 10", Right
"OK")] (volume
10)
501 -- Miscellaneous commands
504 testClearError
= test_
[("clearerror", Right
"OK")] clearError
507 test
[("commands", Right
"command: foo\ncommand: bar")]
508 (Right
["foo", "bar"])
512 test
[("notcommands", Right
"command: foo\ncommand: bar")]
513 (Right
["foo", "bar"])
517 test
[("tagtypes", Right
"tagtype: foo\ntagtype: bar")]
518 (Right
["foo", "bar"])
522 test
[("urlhandlers", Right
"urlhandler: foo\nurlhandler: bar")]
523 (Right
["foo", "bar"])
526 testPassword
= test_
[("password foo", Right
"OK")] (password
"foo")
528 testPing
= test_
[("ping", Right
"OK")] ping
530 testStats
= test
[("stats", Right
"artists: 1\n\
538 (Right Stats
{ stsArtists
= 1, stsAlbums
= 1, stsSongs
= 1
539 , stsUptime
= 100, stsPlaytime
= 100, stsDbUpdate
= 10
540 , stsDbPlaytime
= 100 })
544 -- Extensions\/shortcuts
547 testUpdateId0
= test
[("update", Right
"updating_db: 1")]
551 testUpdateId1
= test
[("update \"foo\"", Right
"updating_db: 1")]
555 testTogglePlay
= test_
556 [("status", Right
"state: play")
557 ,("pause 1", Right
"OK")]
560 testToggleStop
= test_
561 [("status", Right
"state: stop")
562 ,("play", Right
"OK")]
565 testTogglePause
= test_
566 [("status", Right
"state: pause")
567 ,("play", Right
"OK")]
570 testAddMany0
= test_
[("add \"bar\"", Right
"OK")]
573 testAddMany1
= test_
[("playlistadd \"foo\" \"bar\"", Right
"OK")]
574 (addMany
"foo" ["bar"])