3 # This file is part of POE::Component::Client::MPD.
4 # Copyright (c) 2007 Jerome Quelin, all rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or (at
9 # your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 use POE::Component::Client::MPD::Connection;
27 use POE::Component::Client::MPD::Message;
32 # are we able to test module?
33 eval 'use POE::Component::Client::MPD::Test dont_start_poe => 1';
34 plan skip_all => $@ if $@ =~ s/\n+BEGIN failed--compilation aborted.*//s;
40 [ 'bad command', $RAW, '_mpd_error', \&_check_bad_command ],
41 [ 'status', $RAW, '_mpd_data', \&_check_data_raw ],
42 [ 'lsinfo', $AS_ITEMS, '_mpd_data', \&_check_data_as_items ],
43 [ 'stats', $STRIP_FIRST, '_mpd_data', \&_check_data_strip_first ],
44 [ 'stats', $AS_KV, '_mpd_data', \&_check_data_as_kv ],
47 Readonly my $ALIAS => 'tester';
48 my $id = POE::Session->create(
51 _start => \&_onpriv_start,
52 _next_test => \&_onpriv_next_test,
54 _mpd_data => \&_onprot_mpd_result,
55 _mpd_error => \&_onprot_mpd_result,
56 _mpd_version => \&_onprot_mpd_version,
59 my $conn = POE::Component::Client::MPD::Connection->spawn( {
64 my $msg = POE::Component::Client::MPD::Message->new({});
72 sub _check_bad_command {
73 like($_[0]->error, qr/unknown command "bad"/, 'unknown command');
75 sub _check_data_as_items {
76 isa_ok( $_, 'POE::Component::Client::MPD::Item',
77 '$AS_ITEMS cooks as items' ) for @{ $_[0]->data };
79 sub _check_data_as_kv {
80 my %h = @{ $_[0]->data };
81 unlike( $h{$_}, qr/\D/, '$AS_KV cooks as a hash' ) for keys %h;
82 # stats return numerical data as second field.
85 isnt(scalar @{ $_[0]->data }, 0, 'commands return stuff' );
87 sub _check_data_strip_first {
88 unlike( $_, qr/\D/, '$STRIP_FIRST return only 2nd field' ) for @{ $_[0]->data };
89 # stats return numerical data as second field.
97 # event: _mpd_data ( $msg )
98 # event: _mpd_error( $msg )
100 # Called when mpd talks back, with $msg as a pococm-message param.
102 sub _onprot_mpd_result {
103 is( $_[STATE], $tests[0][2], "got a $tests[0][2] event" );
104 $tests[0][3]->( $_[ARG0] ); # check if everything went fine
105 shift @tests; # remove test being played
106 $_[KERNEL]->yield( '_next_test' ); # call next test
111 # event: _mpd_version( $version )
113 # Called when mpd gives its $version.
115 sub _onprot_mpd_version {
116 $_[KERNEL]->yield( '_next_test' );
126 # Called when the poe session has started.
129 $_[KERNEL]->alias_set($ALIAS); # increment refcount
134 # event: _next_test()
136 # Called to schedule the next test.
138 sub _onpriv_next_test {
141 if ( scalar @tests == 0 ) { # no more tests.
142 $k->alias_remove($ALIAS);
143 $k->post( $conn, 'disconnect' );
148 $msg->_commands( [ $tests[0][0] ] );
149 $msg->_cooking ( $tests[0][1] );
150 $k->post( $conn, 'send', $msg );