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 ,("crossfade", testCrossfade
)
67 ,("play / pos", testPlayPos
)
68 ,("play / id", testPlayId
)
72 ,("previous", testPrevious
)
73 ,("random", testRandom
)
74 ,("repeat", testRepeat
)
75 ,("setVolume", testSetVolume
)
76 ,("volume", testVolume
)
77 ,("clearError", testClearError
)
78 ,("commands", testCommands
)
79 ,("notCommands", testNotCommands
)
80 ,("tagTypes", testTagTypes
)
81 ,("urlHandlers", testUrlHandlers
)
82 ,("password", testPassword
)
84 ,("song parsing / incomplete track",
85 testSongParseIncompleteTrack
)
86 ,("song parsing / complete track",
87 testSongParseCompleteTrack
)
90 test a b c
= liftM (showResult b
) $ testMPD a b
(return Nothing
) c
92 test_ a b
= test a
(Right
()) b
94 showResult
:: (Show a
) => Response a
-> Result a
-> String
95 showResult _ Ok
= "passed"
96 showResult expectedResult
(Failure result mms
) =
98 concatMap (\(x
,y
) -> "\n expected request: " ++ show x
++
99 "\n actual request: " ++ show y
) mms
++
100 "\n expected result: " ++ show expectedResult
++
101 "\n actual result: " ++ show result
103 emptySong
= Song
{ sgArtist
= ""
115 , sgIndex
= Nothing
}
119 -- These tests are meant to expose problems with internal
123 -- Should handle track = 'X'.
124 testSongParseIncompleteTrack
=
125 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
128 (Right
[emptySong
{ sgTrack
= (1,1)
129 , sgFilePath
= "dir/Foo-Bar.ogg"
131 (find $ Query Artist
"Foo")
133 -- Should handle track = 'X/Y'.
134 testSongParseCompleteTrack
=
135 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
138 (Right
[emptySong
{ sgTrack
= (2,12)
139 , sgFilePath
= "dir/Foo-Bar.ogg"
141 (find $ Query Artist
"Foo")
147 testEnableOutput
= test_
[("enableoutput 1", Right
"OK")] (enableOutput
1)
149 testDisableOutput
= test_
[("disableoutput 1", Right
"OK")] (disableOutput
1)
152 test
[("outputs", Right
$ unlines ["outputid: 0"
153 ,"outputname: SoundCard0"
156 ,"outputname: SoundCard1"
159 (Right
[Device
{ dOutputID
= 0
160 , dOutputName
= "SoundCard0"
161 , dOutputEnabled
= True }
162 ,Device
{ dOutputID
= 1
163 , dOutputName
= "SoundCard1"
164 , dOutputEnabled
= False }])
167 testUpdate0
= test_
[("update", Right
"updating_db: 1\nOK")] (update
[])
170 test_
[("update \"foo\"", Right
"updating_db: 1\nOK")]
174 test_
[("command_list_begin\nupdate \"foo\"\nupdate \"bar\"\n\
175 \command_list_end", Right
"updating_db: 1\nOK")]
176 (update
["foo","bar"])
183 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
188 (Right
[Song
{ sgArtist
= "Foo"
191 , sgFilePath
= "dir/Foo-Bar.ogg"
202 (find (Query Artist
"Foo"))
205 test
[("list Title", Right
"Title: Foo\nTitle: Bar\nOK")]
206 (Right
["Foo", "Bar"])
210 test
[("list Title Artist \"Muzz\"", Right
"Title: Foo\nOK")]
212 (list Title
(Just
$ Query Artist
"Muzz"))
215 test
[("listall \"\"", Right
"directory: FooBand\n\
216 \directory: FooBand/album1\n\
217 \file: FooBand/album1/01 - songA.ogg\n\
218 \file: FooBand/album1/02 - songB.ogg\nOK")]
219 (Right
["FooBand/album1/01 - songA.ogg"
220 ,"FooBand/album1/02 - songB.ogg"])
224 test
[("lsinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
225 (Right
[Left
"Bar", Left
"Foo"])
229 test
[("listallinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
230 (Right
[Left
"Bar", Left
"Foo"])
234 test
[("search Artist \"oo\"", Right
"file: dir/Foo-Bar.ogg\n\
239 (Right
[Song
{ sgArtist
= "Foo"
242 , sgFilePath
= "dir/Foo-Bar.ogg"
253 (search
(Query Artist
"oo"))
256 test
[("count Title \"Foo\"", Right
"songs: 1\nplaytime: 60\nOK")]
258 (count
(Query Title
"Foo"))
265 test
[("add \"foo\"", Right
"OK"),
266 ("listall \"foo\"", Right
"file: Foo\nfile: Bar\nOK")]
267 (Right
["Foo", "Bar"])
270 testAdd_
= test_
[("add \"foo\"", Right
"OK")] (add_
"" "foo")
272 testAdd_pl
= test_
[("playlistadd \"foo\" \"bar\"", Right
"OK")]
276 test
[("addid \"dir/Foo-Bar.ogg\"", Right
"Id: 20\nOK")]
278 (addId
"dir/Foo-Bar.ogg")
280 testClear
= test_
[("playlistclear \"foo\"", Right
"OK")] (clear
"foo")
282 testPlChangesPosId_0
=
283 test
[("plchangesposid 10", Right
"OK")]
287 testPlChangesPosId_1
=
288 test
[("plchangesposid 10", Right
"cpos: 0\nId: 20\nOK")]
289 (Right
[(Pos
0, ID
20)])
292 testPlChangesPosId_Wierd
=
293 test
[("plchangesposid 10", Right
"cpos: foo\nId: bar\nOK")]
294 (Left
$ Unexpected
"[(\"cpos\",\"foo\"),(\"Id\",\"bar\")]")
297 testCurrentSongStopped
=
298 test
[("status", Right
"repeat: 0\n\
301 \playlistlength: 0\n\
307 testCurrentSongPlaying
=
308 test
[("status", Right
"volume: 80\n\
312 \playlistlength: 21\n\
319 \audio: 44100:16:2\n\
321 ,("currentsong", Right
"file: dir/Foo-Bar.ogg\n\
326 (Right
. Just
$ Song
{ sgArtist
= "Foo"
329 , sgFilePath
= "dir/Foo-Bar.ogg"
342 testDelete0
= test_
[("delete 1", Right
"OK")] (delete "" (Pos
1))
344 testDelete1
= test_
[("deleteid 1", Right
"OK")] (delete "" (ID
1))
346 testDelete2
= test_
[("playlistdelete \"foo\" 1", Right
"OK")] (delete "foo" (Pos
1))
348 testLoad
= test_
[("load \"foo\"", Right
"OK")] (load
"foo")
350 testMove0
= test_
[("move 1 2", Right
"OK")] (move
"" (Pos
1) 2)
352 testMove1
= test_
[("moveid 1 2", Right
"OK")] (move
"" (ID
1) 2)
354 testMove2
= test_
[("playlistmove \"foo\" 1 2", Right
"OK")] (move
"foo" (Pos
1) 2)
356 testRm
= test_
[("rm \"foo\"", Right
"OK")] (rm
"foo")
358 testRename
= test_
[("rename \"foo\" \"bar\"", Right
"OK")] (rename
"foo" "bar")
360 testSave
= test_
[("save \"foo\"", Right
"OK")] (save
"foo")
362 testSwap0
= test_
[("swap 1 2", Right
"OK")] (swap
(Pos
1) (Pos
2))
364 testSwap1
= test_
[("swapid 1 2", Right
"OK")] (swap
(ID
1) (ID
2))
366 testShuffle
= test_
[("shuffle", Right
"OK")] shuffle
368 testPlaylistInfo0
= test
[("playlistinfo", Right
"file: dir/Foo-Bar.ogg\n\
373 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
376 , sgTitle
= "Bar" }])
377 (playlistInfo Nothing
)
379 testPlaylistInfoPos
= test
[("playlistinfo 1", Right
"file: dir/Foo-Bar.ogg\n\
384 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
387 , sgTitle
= "Bar" }])
388 (playlistInfo
. Just
$ Pos
1)
390 testPlaylistInfoId
= test
[("playlistid 1", Right
"file: dir/Foo-Bar.ogg\n\
395 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
398 , sgTitle
= "Bar" }])
399 (playlistInfo
. Just
$ ID
1)
401 testListPlaylistInfo
= test
[("listplaylistinfo \"foo\""
402 ,Right
"file: dir/Foo-Bar.ogg\n\
407 (Right
[emptySong
{ sgFilePath
= "dir/Foo-Bar.ogg"
410 , sgTitle
= "Bar" }])
411 (listPlaylistInfo
"foo")
413 testListPlaylist
= test
[("listplaylist \"foo\""
414 ,Right
"file: dir/Foo-bar.ogg\n\
415 \file: dir/Quux-quuz.ogg\n\
417 (Right
["dir/Foo-bar.ogg", "dir/Quux-quuz.ogg"])
424 testCrossfade
= test_
[("crossfade 0", Right
"OK")] (crossfade
0)
426 testPlay
= test_
[("play", Right
"OK")] (play Nothing
)
428 testPlayPos
= test_
[("play 1", Right
"OK")] (play
. Just
$ Pos
1)
430 testPlayId
= test_
[("playid 1", Right
"OK")] (play
. Just
$ ID
1)
432 testPause
= test_
[("pause 0", Right
"OK")] (pause
False)
434 testStop
= test_
[("stop", Right
"OK")] stop
436 testNext
= test_
[("next", Right
"OK")] next
438 testPrevious
= test_
[("previous", Right
"OK")] previous
440 testRandom
= test_
[("random 0", Right
"OK")] (random False)
442 testRepeat
= test_
[("repeat 0", Right
"OK")] (repeat False)
444 testSetVolume
= test_
[("setvol 10", Right
"OK")] (setVolume
10)
446 testVolume
= test_
[("volume 10", Right
"OK")] (volume
10)
449 -- Miscellaneous commands
452 testClearError
= test_
[("clearerror", Right
"OK")] clearError
455 test
[("commands", Right
"command: foo\ncommand: bar")]
456 (Right
["foo", "bar"])
460 test
[("notcommands", Right
"command: foo\ncommand: bar")]
461 (Right
["foo", "bar"])
465 test
[("tagtypes", Right
"tagtype: foo\ntagtype: bar")]
466 (Right
["foo", "bar"])
470 test
[("urlhandlers", Right
"urlhandler: foo\nurlhandler: bar")]
471 (Right
["foo", "bar"])
474 testPassword
= test_
[("password foo", Right
"OK")] (password
"foo")
476 testPing
= test_
[("ping", Right
"OK")] ping
479 -- Extensions\/shortcuts