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 Main
(main
) where
14 import Network
.MPD
.Commands
15 import Network
.MPD
.Core
(Response
)
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 ,("currentSong(_)", testCurrentSongStopped
)
42 ,("currentSong(>)", testCurrentSongPlaying
)
43 ,("delete0", testDelete0
)
44 ,("delete1", testDelete1
)
45 ,("delete2", testDelete2
)
51 ,("rename", testRename
)
55 ,("shuffle", testShuffle
)
56 ,("crossfade", testCrossfade
)
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
)
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
) =
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
91 testEnableOutput
= test_
[("enableoutput 1", Right
"OK")] (enableOutput
1)
93 testDisableOutput
= test_
[("disableoutput 1", Right
"OK")] (disableOutput
1)
96 test
[("outputs", Right
$ unlines ["outputid: 0"
97 ,"outputname: SoundCard0"
100 ,"outputname: SoundCard1"
103 (Right
[Device
{ dOutputID
= 0
104 , dOutputName
= "SoundCard0"
105 , dOutputEnabled
= True }
106 ,Device
{ dOutputID
= 1
107 , dOutputName
= "SoundCard1"
108 , dOutputEnabled
= False }])
111 testUpdate0
= test_
[("update", Right
"updating_db: 1\nOK")] (update
[])
114 test_
[("update \"foo\"", Right
"updating_db: 1\nOK")]
118 test_
[("command_list_begin\nupdate \"foo\"\nupdate \"bar\"\n\
119 \command_list_end", Right
"updating_db: 1\nOK")]
120 (update
["foo","bar"])
127 test
[("find Artist \"Foo\"", Right
"file: dir/Foo-Bar.ogg\n\
132 (Right
[Song
{ sgArtist
= "Foo"
135 , sgFilePath
= "dir/Foo-Bar.ogg"
146 (find (Query Artist
"Foo"))
149 test
[("list Title", Right
"Title: Foo\nTitle: Bar\nOK")]
150 (Right
["Foo", "Bar"])
154 test
[("list Title Artist \"Muzz\"", Right
"Title: Foo\nOK")]
156 (list Title
(Just
$ Query Artist
"Muzz"))
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"])
168 test
[("lsinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
169 (Right
[Left
"Bar", Left
"Foo"])
173 test
[("listallinfo \"\"", Right
"directory: Foo\ndirectory: Bar\nOK")]
174 (Right
[Left
"Bar", Left
"Foo"])
178 test
[("search Artist \"oo\"", Right
"file: dir/Foo-Bar.ogg\n\
183 (Right
[Song
{ sgArtist
= "Foo"
186 , sgFilePath
= "dir/Foo-Bar.ogg"
197 (search
(Query Artist
"oo"))
200 test
[("count Title \"Foo\"", Right
"songs: 1\nplaytime: 60\nOK")]
202 (count
(Query Title
"Foo"))
209 test
[("add \"foo\"", Right
"OK"),
210 ("listall \"foo\"", Right
"file: Foo\nfile: Bar\nOK")]
211 (Right
["Foo", "Bar"])
214 testAdd_
= test_
[("add \"foo\"", Right
"OK")] (add_
"" "foo")
217 test
[("addid \"dir/Foo-Bar.ogg\"", Right
"Id: 20\nOK")]
219 (addId
"dir/Foo-Bar.ogg")
221 testClear
= test_
[("playlistclear \"foo\"", Right
"OK")] (clear
"foo")
223 testCurrentSongStopped
=
224 test
[("status", Right
"repeat: 0\n\
227 \playlistlength: 0\n\
233 testCurrentSongPlaying
=
234 test
[("status", Right
"volume: 80\n\
238 \playlistlength: 21\n\
245 \audio: 44100:16:2\n\
247 ,("currentsong", Right
"file: dir/Foo-Bar.ogg\n\
252 (Right
. Just
$ Song
{ sgArtist
= "Foo"
255 , sgFilePath
= "dir/Foo-Bar.ogg"
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
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
325 test
[("commands", Right
"command: foo\ncommand: bar")]
326 (Right
["foo", "bar"])
330 test
[("notcommands", Right
"command: foo\ncommand: bar")]
331 (Right
["foo", "bar"])
335 test
[("tagtypes", Right
"tagtype: foo\ntagtype: bar")]
336 (Right
["foo", "bar"])
340 test
[("urlhandlers", Right
"urlhandler: foo\nurlhandler: bar")]
341 (Right
["foo", "bar"])
344 testPing
= test_
[("ping", Right
"OK")] ping
347 -- Extensions\/shortcuts