File Coverage

blib/lib/Test/Bot/BasicBot/Pluggable.pm
Criterion Covered Total %
statement 23 25 92.0
branch 4 4 100.0
condition 3 5 60.0
subroutine 9 11 81.8
pod 6 6 100.0
total 45 51 88.2


line stmt bran cond sub pod time code
1             package Test::Bot::BasicBot::Pluggable;
2             $Test::Bot::BasicBot::Pluggable::VERSION = '1.10';
3 5     5   254786 use warnings;
  5         8  
  5         158  
4 5     5   17 use strict;
  5         7  
  5         97  
5 5     5   18 use base qw( Bot::BasicBot::Pluggable );
  5         6  
  5         2039  
6              
7             sub new {
8 5     5 1 649 my ( $class, %args ) = @_;
9 5         92 my $bot = $class->SUPER::new(
10             store => 'Memory',
11             nick => 'test_bot',
12             %args
13             );
14 5         46 return bless $bot, $class;
15             }
16              
17             sub tell_private {
18 22     22 1 51 return shift->tell( shift, 1, 1 );
19             } # tell the module something privately
20              
21 61     61 1 3377 sub tell_direct { return shift->tell( shift, 0, 1 ) }
22              
23             sub tell_indirect {
24 148     148 1 390 return shift->tell( shift, 0, 0 );
25             } # the module has seen something
26              
27             sub tell {
28 231     231 1 286 my ( $bot, $body, $private, $addressed, $who ) = @_;
29 231         168 my @reply;
30             my $message = {
31             body => $body,
32             who => $who || 'test_user',
33             channel => $private ? 'msg' : '#test',
34             address => $addressed,
35 214     214   425 reply_hook => sub { push @reply, $_[1]; }, # $_[1] is the reply text
36 231 100 50     1625 };
37 231 100 66     646 if ( $body =~ /^help/ and $addressed ) {
38 1         13 push @reply, $bot->help($message);
39             }
40             else {
41 230         462 $bot->said($message);
42             }
43 231         1354 return join "\n", @reply;
44             }
45              
46             sub connect {
47 0     0 1   my $self = shift;
48 0           $self->dispatch('connected');
49             }
50              
51             # otherwise AUTOLOAD in Bot::BasicBot will be called
52       0     sub DESTROY { }
53              
54             1;
55              
56             __END__
57              
58             =head1 NAME
59              
60             Test::Bot::BasicBot::Pluggable - utilities to aid in testing of Bot::BasicBot::Pluggable modules
61              
62             =head1 VERSION
63              
64             version 1.10
65              
66             =head1 SYNOPSIS
67              
68             use Test::More;
69             use Test::Bot::BasicBot::Pluggable;
70              
71             my $bot = Test::Bot::BasicBot->new();
72             $bot->load('MyModule');
73              
74             is ( $bot->tell_direct('foo'), 'bar');
75             is ( $bot->tell_indirect('foo'), 'bar');
76             is ( $bot->tell_private('foo'), 'bar');
77              
78             =head1 DESCRIPTION
79              
80             Test::Bot::BasicBot::Pluggable was written to provide a
81             minimalistic testing bot in order to write cleaner unit tests for
82             Bot::BasicBot::Pluggable modules.
83              
84             =head1 SUBROUTINES/METHODS
85              
86             =head2 new
87              
88             Creates a new Test::Bot::BasicBot::Pluggable object, which is
89             basically just a subclass of Bot::BasicBot::Pluggable with a few
90             special methods. The default nickname is 'test_bot' and it contains
91             a in-memory store instead of sqlite. It takes the same arguments as
92             Bot::BasicBot::Pluggable.
93              
94             =head1 INSTANCE METHODS
95              
96             =head2 tell_direct
97              
98             Sends the provided string to the bot like it was send directly to the bot in a public channel. The channel is called '#test' and the sending user 'test_user'.
99              
100             test_user@#test> test_bot: foo
101              
102             =head2 tell_indirect
103              
104             Sends the provided string to the bot like it was send to a public channel without addressing. The channel is called '#test' and the sending user 'test_user'.
105              
106             test_user@#test> foo
107              
108             =head2 tell_private
109              
110             Sends the provided string to the bot like it was send in a private channel. The sending user 'test_user'.
111              
112             test_user@test_bot> foo
113              
114             =head2 tell
115              
116             This is the working horse of Test::Bot::BasicBot::Pluggable. It
117             basically builds a message hash as argument to the bots said()
118             function. You should never have to call it directly.
119              
120             =head2 connect
121              
122             Dispatch the connected event to all loaded modules without actually
123             connecting to anything.
124              
125             =head2 DESTROY
126              
127             The special subroutine is explicitly overridden with an empty
128             subroutine as otherwise AUTOLOAD in Bot::BasicBot will be called
129             for it.
130              
131             =head1 BUGS AND LIMITATIONS
132              
133             There are no methods to test join, part and emote.
134              
135             =head1 AUTHOR
136              
137             Mario Domgoergen <mdom@cpan.org>
138              
139             =head1 LICENSE AND COPYRIGHT
140              
141             Copyright 2009 Mario Domgoergen, all rights reserved.
142              
143             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.