Skip to content

Commit

Permalink
tidied
Browse files Browse the repository at this point in the history
  • Loading branch information
aanoaa committed Nov 15, 2012
1 parent f839fb1 commit 9ef6af2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 85 deletions.
7 changes: 3 additions & 4 deletions lib/Hubot/Scripts/shorten.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ sub load {
sub { sub {
my $msg = shift; my $msg = shift;
my $bitly = $msg->match->[0]; my $bitly = $msg->match->[0];
if ( if ( length $bitly > 50
length $bitly > 50
&& $ENV{HUBOT_BITLY_USERNAME} && $ENV{HUBOT_BITLY_USERNAME}
&& $ENV{HUBOT_BITLY_API_KEY} && $ENV{HUBOT_BITLY_API_KEY} )
) { {
my $uri = URI->new("http://api.bitly.com/v3/shorten"); my $uri = URI->new("http://api.bitly.com/v3/shorten");
$uri->query_form_hash( $uri->query_form_hash(
login => $ENV{HUBOT_BITLY_USERNAME}, login => $ENV{HUBOT_BITLY_USERNAME},
Expand Down
16 changes: 9 additions & 7 deletions t/01_help.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use Hubot::Robot;
use lib 't/lib'; use lib 't/lib';
use Test::More tests => 2; use Test::More tests => 2;


my $robot = Hubot::Robot->new({ my $robot = Hubot::Robot->new(
adapter => 'helper', {
name => 'hubot' adapter => 'helper',
}); name => 'hubot'
}
);


$robot->loadHubotScripts(["help","roles"]); $robot->loadHubotScripts( [ "help", "roles" ] );


push @{ $robot->{receive} }, 'hubot help'; push @{ $robot->{receive} }, 'hubot help';
push @{ $robot->{receive} }, 'hubot help roles'; push @{ $robot->{receive} }, 'hubot help roles';
Expand All @@ -18,6 +20,6 @@ $robot->run;


my $got; my $got;
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/help <query>/, 'generall help'); like( "@$got", qr/help <query>/, 'generall help' );
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/see what roles a user has/, 'specific help'); like( "@$got", qr/see what roles a user has/, 'specific help' );
16 changes: 9 additions & 7 deletions t/02_ascii.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use Hubot::Robot;
use lib 't/lib'; use lib 't/lib';
use Test::More tests => 2; use Test::More tests => 2;


my $robot = Hubot::Robot->new({ my $robot = Hubot::Robot->new(
adapter => 'helper', {
name => 'hubot' adapter => 'helper',
}); name => 'hubot'
}
);


$robot->loadHubotScripts(["help","ascii"]); $robot->loadHubotScripts( [ "help", "ascii" ] );
$robot->adapter->interval(3); $robot->adapter->interval(3);


push @{ $robot->{receive} }, 'hubot help ascii'; push @{ $robot->{receive} }, 'hubot help ascii';
Expand All @@ -19,6 +21,6 @@ $robot->run;


my $got; my $got;
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/ascii me/, 'correct help message'); like( "@$got", qr/ascii me/, 'correct help message' );
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/\| \|/, 'ascii art'); like( "@$got", qr/\| \|/, 'ascii art' );
39 changes: 21 additions & 18 deletions t/03_roles.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,53 +5,56 @@ use Hubot::User;
use lib 't/lib'; use lib 't/lib';
use Test::More tests => 9; use Test::More tests => 9;


my $robot = Hubot::Robot->new({ my $robot = Hubot::Robot->new(
adapter => 'helper', {
name => 'hubot' adapter => 'helper',
}); name => 'hubot'
}
);


$robot->loadHubotScripts(["help","roles"]); $robot->loadHubotScripts( [ "help", "roles" ] );
$robot->adapter->interval(0.2); $robot->adapter->interval(0.2);
$robot->userForId('misskim', {}); # for known user test $robot->userForId( 'misskim', {} ); # for known user test


push @{ $robot->{receive} }, ( push @{ $robot->{receive} },
(
'hubot help user', 'hubot help user',
'hubot who is ' . time, 'hubot who is ' . time,
'hubot ' . time . ' is a dayfly', 'hubot ' . time . ' is a dayfly',
'hubot who is misskim' , 'hubot who is misskim',
'hubot misskim is a dayfly', 'hubot misskim is a dayfly',
'hubot who is misskim', 'hubot who is misskim',
'hubot misskim is not a dayfly', 'hubot misskim is not a dayfly',
'hubot who is misskim', 'hubot who is misskim',
'hubot who is ' . $robot->name, 'hubot who is ' . $robot->name,
); );


$robot->run; $robot->run;


my $got; my $got;
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/badass guitarist/, '<robot> help roles'); like( "@$got", qr/badass guitarist/, '<robot> help roles' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/Never heard/, 'who is <unknown>'); like( "@$got", qr/Never heard/, 'who is <unknown>' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/anything about/, 'assign a role to <unknown>'); like( "@$got", qr/anything about/, 'assign a role to <unknown>' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/nothing to me/, 'who is <known>'); like( "@$got", qr/nothing to me/, 'who is <known>' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/is a dayfly/, 'give a role'); like( "@$got", qr/is a dayfly/, 'give a role' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/is a dayfly/, 'got correct role'); like( "@$got", qr/is a dayfly/, 'got correct role' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/is no longer a dayfly/, 'drop a role'); like( "@$got", qr/is no longer a dayfly/, 'drop a role' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/nothing to me/, 'who is <known> again after drop roles'); like( "@$got", qr/nothing to me/, 'who is <known> again after drop roles' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/The best/, 'who is hubot'); like( "@$got", qr/The best/, 'who is hubot' );
33 changes: 19 additions & 14 deletions t/04_shorten.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,34 +4,39 @@ use Hubot::Robot;
use lib 't/lib'; use lib 't/lib';
use Test::More tests => 6; use Test::More tests => 6;


my $robot = Hubot::Robot->new({ my $robot = Hubot::Robot->new(
adapter => 'helper', {
name => 'hubot' adapter => 'helper',
}); name => 'hubot'
}
);


$robot->loadHubotScripts(["help","shorten"]); $robot->loadHubotScripts( [ "help", "shorten" ] );
$robot->adapter->interval(3); $robot->adapter->interval(3);


push @{ $robot->{receive} }, ( push @{ $robot->{receive} },
(
'hubot help shorten', 'hubot help shorten',
'https://www.google.com/', 'https://www.google.com/',
'https://github.com/aanoaa/p5-hubot/blob/master/lib/Hubot/Scripts/shorten.pm', 'https://github.com/aanoaa/p5-hubot/blob/master/lib/Hubot/Scripts/shorten.pm',
); );


$robot->run; $robot->run;


my $got; my $got;
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
ok("@$got", 'containing help messages'); ok( "@$got", 'containing help messages' );


SKIP: { SKIP: {
skip "ENV [HUBOT_BITLY_USERNAME] and [HUBOT_BITLY_API_KEY] are required", 5 if !$ENV{HUBOT_BITLY_USERNAME} or !$ENV{HUBOT_BITLY_API_KEY}; skip "ENV [HUBOT_BITLY_USERNAME] and [HUBOT_BITLY_API_KEY] are required", 5
if !$ENV{HUBOT_BITLY_USERNAME}
or !$ENV{HUBOT_BITLY_API_KEY};
$got = shift @{ $robot->{sent} } || []; $got = shift @{ $robot->{sent} } || [];
ok("@$got", "got response on https"); ok( "@$got", "got response on https" );
like("@$got", qr/google/i, 'pick a title from google'); like( "@$got", qr/google/i, 'pick a title from google' );
like("@$got", qr/google\.com/, 'has link'); like( "@$got", qr/google\.com/, 'has link' );


$got = shift @{ $robot->{sent} } || []; $got = shift @{ $robot->{sent} } || [];
like("@$got", qr/github/i, 'pick a title from github'); like( "@$got", qr/github/i, 'pick a title from github' );
like("@$got", qr/bit\.ly/, 'got shorten link'); like( "@$got", qr/bit\.ly/, 'got shorten link' );
} }
21 changes: 12 additions & 9 deletions t/05_tweet.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ use Hubot::Robot;
use lib 't/lib'; use lib 't/lib';
use Test::More tests => 2; use Test::More tests => 2;


my $robot = Hubot::Robot->new({ my $robot = Hubot::Robot->new(
adapter => 'helper', {
name => 'hubot' adapter => 'helper',
}); name => 'hubot'
}
);


$robot->loadHubotScripts(["help","tweet"]); $robot->loadHubotScripts( [ "help", "tweet" ] );
$robot->adapter->interval(3); $robot->adapter->interval(3);


push @{ $robot->{receive} }, ( push @{ $robot->{receive} },
(
'hubot help tweet', 'hubot help tweet',
'https://twitter.com/saltfactory/status/263821902001369088', 'https://twitter.com/saltfactory/status/263821902001369088',
); );


$robot->run; $robot->run;


my $got; my $got;
$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
ok("@$got", 'containing help messages'); ok( "@$got", 'containing help messages' );


$got = shift @{ $robot->{sent} }; $got = shift @{ $robot->{sent} };
like("@$got", qr/Ruby/, 'has tweet content'); like( "@$got", qr/Ruby/, 'has tweet content' );
53 changes: 27 additions & 26 deletions t/lib/Hubot/Adapter/Helper.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ has 'cv' => (
); );


has 'interval' => ( has 'interval' => (
is => 'rw', is => 'rw',
default => 1, default => 1,
); );


sub BUILD { sub BUILD {
my $self = shift; my $self = shift;
$self->robot->{sent} = []; $self->robot->{sent} = [];
$self->robot->{receive} = []; $self->robot->{receive} = [];
} }


sub _build_cv { AnyEvent->condvar } sub _build_cv { AnyEvent->condvar }
sub close { shift->cv->send } sub close { shift->cv->send }

sub send { sub send {
my ( $self, $user, @strings ) = @_; my ( $self, $user, @strings ) = @_;
push @{ $self->robot->{sent} }, \@strings; push @{ $self->robot->{sent} }, \@strings;
Expand All @@ -48,35 +49,35 @@ sub run {


local $| = 1; local $| = 1;
my $w = AnyEvent->timer( my $w = AnyEvent->timer(
after => 0, after => 0,
interval => $self->interval, interval => $self->interval,
cb => cb => sub {
sub { my $text = shift @{ $self->robot->{receive} };
my $text = shift @{ $self->robot->{receive} }; return $self->cv->end unless $text;
return $self->cv->end unless $text;

$self->cv->begin;
$self->cv->begin; my $user = $self->userForId(
my $user = $self->userForId( 1,
1, {
name => 'helper',
room => 'helper'
}
);

$self->receive(
new Hubot::TextMessage(
{ {
name => 'helper', user => $user,
room => 'helper' text => $text,
} }
); )

);
$self->receive( }
new Hubot::TextMessage( );
{
user => $user,
text => $text,
}
)
);
}
);


$self->cv->begin; $self->cv->begin;
$self->cv->recv; $self->cv->recv;

# callback? # callback?
} }


Expand Down

0 comments on commit 9ef6af2

Please sign in to comment.