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