[project @ 6098]
[poe-component-client-mpd.git] / t / 23-conn-dialog.t
blob7a8fb824d4476ab8bf067b557b6ff2da5d9dd654
1 #!perl
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
22 use strict;
23 use warnings;
25 use POE;
26 use POE::Component::Client::MPD::Connection;
27 use POE::Component::Client::MPD::Message;
28 use Readonly;
29 use Test::More;
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;
35 plan tests => 25;
38 # tests to be run
39 my @tests = (
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(
49     inline_states => {
50         # private events
51         _start     => \&_onpriv_start,
52         _next_test => \&_onpriv_next_test,
53         # protected events
54         _mpd_data    => \&_onprot_mpd_result,
55         _mpd_error   => \&_onprot_mpd_result,
56         _mpd_version => \&_onprot_mpd_version,
57     }
59 my $conn = POE::Component::Client::MPD::Connection->spawn( {
60     host => 'localhost',
61     port => 6600,
62     id   => $id,
63 } );
64 my $msg  = POE::Component::Client::MPD::Message->new({});
65 POE::Kernel->run;
66 exit;
69 #--
70 # private subs
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.
84 sub _check_data_raw {
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.
93 #--
94 # protected events
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' );
121 # private events
124 # event: _start()
126 # Called when the poe session has started.
128 sub _onpriv_start {
129     $_[KERNEL]->alias_set($ALIAS); # increment refcount
134 # event: _next_test()
136 # Called to schedule the next test.
138 sub _onpriv_next_test {
139     my $k = $_[KERNEL];
141     if ( scalar @tests == 0 ) { # no more tests.
142         $k->alias_remove($ALIAS);
143         $k->post( $conn, 'disconnect' );
144         return;
145     }
147     # post next event.
148     $msg->_commands( [ $tests[0][0] ] );
149     $msg->_cooking (   $tests[0][1]   );
150     $k->post( $conn, 'send', $msg );