Debian Codegraph @

t/04_plugins/14_logger/05_log_sub.t

raw
1 use strict;
2 use warnings FATAL => 'all';
3 use lib 't/inc';
4 use File::Spec::Functions qw(catfile);
5 use POE;
6 use POE::Component::IRC::State;
7 use POE::Component::IRC::Plugin::Logger;
8 use POE::Component::Server::IRC;
9 use Test::More tests => 12;
10
11 my $bot1 = POE::Component::IRC::State->spawn(
12 Flood => 1,
13 plugin_debug => 1,
14 );
15 my $ircd = POE::Component::Server::IRC->spawn(
16 Auth => 0,
17 AntiFlood => 0,
18 );
19
20 my $got = 0;
21 $bot1->plugin_add(Logger => POE::Component::IRC::Plugin::Logger->new(
22 Log_sub => sub {
23 $got++;
24 if ($got == 1) {
25 is($_[0], '#testchannel', 'Got context');
26 is($_[1], 'join', 'Got type');
27 is($_[2], 'TestBot1', 'Got arguments');
28 }
29 elsif ($got == 2) {
30 is($_[0], '#testchannel', 'Got context');
31 is($_[1], '+n', 'Got type');
32 is($_[2], 'poco.server.irc', 'Got arguments');
33 }
34 elsif ($got == 3) {
35 is($_[0], '#testchannel', 'Got context');
36 is($_[1], '+t', 'Got type');
37 is($_[2], 'poco.server.irc', 'Got arguments');
38 }
39 elsif ($got == 4) {
40 is($_[0], '#testchannel', 'Got context');
41 is($_[1], 'quit', 'Got type');
42 is($_[2], 'TestBot1', 'Got arguments');
43 }
44 }
45 ));
46
47 POE::Session->create(
48 package_states => [
49 main => [qw(
50 _start
51 ircd_listener_add
52 ircd_listener_failure
53 _shutdown
54 irc_001
55 irc_join
56 irc_disconnected
57 )],
58 ],
59 );
60
61 $poe_kernel->run();
62
63 sub _start {
64 my ($kernel) = $_[KERNEL];
65
66 $ircd->yield('register', 'all');
67 $ircd->yield('add_listener');
68 $kernel->delay(_shutdown => 60, 'Timed out');
69 }
70
71 sub ircd_listener_failure {
72 my ($kernel, $op, $reason) = @_[KERNEL, ARG1, ARG3];
73 $kernel->yield('_shutdown', "$op: $reason");
74 }
75
76 sub ircd_listener_add {
77 my ($kernel, $port) = @_[KERNEL, ARG0];
78
79 $bot1->yield(register => 'all');
80 $bot1->yield(connect => {
81 nick => 'TestBot1',
82 server => '127.0.0.1',
83 port => $port,
84 });
85 }
86
87 sub _shutdown {
88 my ($kernel, $error) = @_[KERNEL, ARG0];
89 fail($error) if defined $error;
90
91 $kernel->alarm_remove_all();
92 $ircd->yield('shutdown');
93 $bot1->yield('shutdown');
94 }
95
96
97 sub irc_001 {
98 my ($heap, $server) = @_[HEAP, ARG0];
99 my $irc = $_[SENDER]->get_heap();
100 pass($irc->nick_name() . ' logged in');
101 $irc->yield(join => '#testchannel');
102 }
103
104 sub irc_join {
105 my ($sender, $heap, $who, $where) = @_[SENDER, HEAP, ARG0, ARG1];
106 my $nick = (split /!/, $who)[0];
107 my $irc = $sender->get_heap();
108
109 return if $nick ne $irc->nick_name();
110 pass("$nick joined channel");
111 $bot1->yield('quit');
112 }
113
114 sub irc_disconnected {
115 my ($kernel, $sender) = @_[KERNEL, SENDER];
116 my $irc = $sender->get_heap();
117 pass('irc_disconnected');
118 $kernel->yield('_shutdown');
119 }