extended copyright info to 2009
[audio-mpd.git] / t / 40-playlist.t
blob561993e88a9050c5d2cf328556b24cc8627a9305
1 #!perl
3 # This file is part of Audio::MPD
4 # Copyright (c) 2007-2009 Jerome Quelin, all rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself.
11 use strict;
12 use warnings;
14 use Audio::MPD;
15 use FindBin qw[ $Bin ];
16 use Test::More;
18 # are we able to test module?
19 eval 'use Audio::MPD::Test';
20 plan skip_all => $@ if $@ =~ s/\n+Compilation failed.*//s;
23 plan tests => 24;
24 my $mpd = Audio::MPD->new;
25 my ($nb, @items);
29 # testing collection accessor.
30 my $pl = $mpd->playlist;
31 isa_ok( $pl, 'Audio::MPD::Playlist',
32         'playlist return an Audio::MPD::Playlist object' );
36 # testing playlist retrieval.
37 $pl->add(
38     'title.ogg',
39     'dir1/title-artist-album.ogg',
40     'dir1/title-artist.ogg' );
41 @items = $pl->as_items;
42 isa_ok( $_, 'Audio::MPD::Common::Item::Song',
43         'as_items() returns AMC::Item::Song objects' ) for @items;
44 is( $items[0]->title, 'ok-title', 'first song reported first' );
48 # testing playlist changes retrieval.
49 @items = $pl->items_changed_since(0);
50 isa_ok( $_, 'Audio::MPD::Common::Item::Song',
51         'items_changed_since() returns AMC::Item::Song objects' )
52     for @items;
53 is( $items[0]->title, 'ok-title', 'first song reported first' );
57 # testing song insertion.
58 $pl->clear;
59 $nb = $mpd->status->playlistlength;
60 $pl->add( 'title.ogg' );
61 $pl->add( 'dir1/title-artist-album.ogg' );
62 $pl->add( 'dir1/title-artist.ogg' );
63 is( $mpd->status->playlistlength, $nb+3, 'add() songs' );
67 # testing song removal.
68 $pl->clear;
69 $pl->add( 'title.ogg' );
70 $pl->add( 'dir1/title-artist-album.ogg' );
71 $pl->add( 'dir1/title-artist.ogg' );
72 $mpd->play(0); # to set songid
73 $mpd->stop;
74 $nb = $mpd->status->playlistlength;
75 $pl->delete( reverse 1..2 ); # reverse otherwise mpd will get it wrong
76 is( $mpd->status->playlistlength, $nb-2, 'delete() songs' );
78 $nb = $mpd->status->playlistlength;
79 $pl->deleteid( $mpd->status->songid );
80 is( $mpd->status->playlistlength, $nb-1, 'deleteid() songs' );
85 # testing playlist clearing
86 $pl->add( 'title.ogg' );
87 $pl->add( 'dir1/title-artist-album.ogg' );
88 $pl->add( 'dir1/title-artist.ogg' );
89 $nb = $mpd->status->playlistlength;
90 $pl->clear;
91 is(   $mpd->status->playlistlength, 0,   'clear() leaves 0 songs' );
92 isnt( $mpd->status->playlistlength, $nb, 'clear() changes playlist length' );
96 # testing cropping.
97 $pl->add( 'title.ogg' );
98 $pl->add( 'dir1/title-artist-album.ogg' );
99 $pl->add( 'dir1/title-artist.ogg' );
100 $mpd->play(1); # to set song
101 $mpd->stop;
102 $pl->crop;
103 is( $mpd->status->playlistlength, 1, 'crop() leaves only one song' );
107 # testing shuffle.
108 $pl->clear;
109 $pl->add( 'title.ogg' );
110 $pl->add( 'dir1/title-artist-album.ogg' );
111 $pl->add( 'dir1/title-artist.ogg' );
112 my $vers = $mpd->status->playlist;
113 $pl->shuffle;
114 is( $mpd->status->playlist, $vers+1, 'shuffle() changes playlist version' );
118 # testing swap.
119 $pl->clear;
120 $pl->add( 'title.ogg' );
121 $pl->add( 'dir1/title-artist-album.ogg' );
122 $pl->add( 'dir1/title-artist.ogg' );
123 $pl->swap(0,2);
124 is( ($pl->as_items)[2]->title, 'ok-title', 'swap() changes songs' );
128 # testing swapid.
129 $pl->clear;
130 $pl->add( 'title.ogg' );
131 $pl->add( 'dir1/title-artist-album.ogg' );
132 $pl->add( 'dir1/title-artist.ogg' );
133 @items = $pl->as_items;
134 $pl->swapid($items[0]->id,$items[2]->id);
135 is( ($pl->as_items)[2]->title, 'ok-title', 'swapid() changes songs' );
139 # testing move.
140 $pl->clear;
141 $pl->add( 'title.ogg' );
142 $pl->add( 'dir1/title-artist-album.ogg' );
143 $pl->add( 'dir1/title-artist.ogg' );
144 $pl->move(0,2);
145 is( ($pl->as_items)[2]->title, 'ok-title', 'move() changes song' );
149 # testing moveid.
150 $pl->clear;
151 $pl->add( 'title.ogg' );
152 $pl->add( 'dir1/title-artist-album.ogg' );
153 $pl->add( 'dir1/title-artist.ogg' );
154 @items = $pl->as_items;
155 $pl->moveid($items[0]->id,2);
156 is( ($pl->as_items)[2]->title, 'ok-title', 'moveid() changes song' );
160 # testing load.
161 $pl->clear;
162 $pl->load( 'test' );
163 @items = $pl->as_items;
164 is( scalar @items, 1, 'load() adds songs' );
165 is( $items[0]->title, 'ok-title', 'load() adds the correct songs' );
169 # testing save.
170 $pl->clear;
171 $pl->save( 'test-jq' );
172 ok( -f "$Bin/mpd-test/playlists/test-jq.m3u", 'save() creates a playlist' );
176 # testing rm.
177 $pl->rm( 'test-jq' );
178 ok( ! -f "$Bin/mpd-test/playlists/test-jq.m3u", 'rm() removes a playlist' );
182 exit;