Last active
December 16, 2015 21:00
-
-
Save mxey/5496860 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v5.16; | |
use warnings; | |
package Tester { | |
use Moose::Role; | |
requires 'statistics'; | |
} | |
package Channel { | |
use Moose::Role; | |
requires 'send_message'; | |
} | |
package Mock::Module::Tester { | |
use Moose; | |
has statistics => ( | |
is => 'rw', | |
isa => 'HashRef[Str]', | |
default => sub { {} } | |
); | |
with 'Tester'; | |
} | |
package Mock::IRC::Channel { | |
use Moose; | |
has messages => ( | |
is => 'ro', | |
isa => 'ArrayRef[Str]', | |
traits => ['Array'], | |
default => sub { [] }, | |
handles => { | |
send_message => 'push', | |
clear_messages => 'clear', | |
} | |
); | |
with 'Channel'; | |
} | |
package TestBot { | |
use Moose; | |
has tester => ( | |
is => 'ro', | |
isa => 'Tester', | |
); | |
has channel => ( | |
is => 'ro', | |
isa => 'Channel', | |
); | |
has cached_statistics => ( | |
is => 'rw', | |
isa => 'HashRef[Str]', | |
default => sub { {} }, | |
traits => ['Hash'], | |
handles => { cached_state_for_module => 'get' }, | |
); | |
sub receive_statistics { | |
my ($self) = @_; | |
my $statistics = $self->tester->statistics; | |
while ( my ( $module, $state ) = each $statistics ) { | |
my $cached_state = $self->cached_state_for_module($module) // ''; | |
$module =~ s/\b(\w)/\u$1/g; | |
if ( ( $state eq 'FAIL' ) && ( $cached_state ne 'FAIL' ) ) { | |
$self->channel->send_message( | |
"$module just failed its test suite."); | |
} | |
elsif ( ( $state eq 'PASS' ) && ( $cached_state eq 'FAIL' ) ) { | |
$self->channel->send_message( | |
"$module just started passing its test suite."); | |
} | |
} | |
$self->cached_statistics($statistics); | |
} | |
} | |
use Test::More tests => 3; | |
sub set_things_up_so_everything_passes { | |
my ($tester) = @_; | |
$tester->statistics( | |
{ | |
"module C" => "PASS", | |
"module W" => "PASS", | |
} | |
); | |
return; | |
} | |
sub set_things_up_so_something_fails { | |
my ($tester) = @_; | |
$tester->statistics( | |
{ | |
"module W" => "FAIL", | |
"module C" => "PASS", | |
} | |
); | |
return; | |
} | |
{ | |
my $tester = Mock::Module::Tester->new(); | |
my $channel = Mock::IRC::Channel->new(); | |
my $bot = TestBot->new( | |
tester => $tester, | |
channel => $channel, | |
); | |
set_things_up_so_everything_passes($tester); | |
$bot->receive_statistics(); | |
set_things_up_so_something_fails($tester); | |
$bot->receive_statistics(); | |
is_deeply $channel->messages(), ["Module W just failed its test suite."], | |
"Bot tells channel when module starts failing"; | |
} | |
{ | |
my $tester = Mock::Module::Tester->new(); | |
my $channel = Mock::IRC::Channel->new(); | |
my $bot = TestBot->new( | |
tester => $tester, | |
channel => $channel, | |
); | |
set_things_up_so_something_fails($tester); | |
$bot->receive_statistics(); | |
$channel->clear_messages(); | |
set_things_up_so_something_fails($tester); | |
$bot->receive_statistics(); | |
is_deeply $channel->messages(), [], | |
"Bot keeps quiet when module keeps failing"; | |
} | |
{ | |
my $tester = Mock::Module::Tester->new(); | |
my $channel = Mock::IRC::Channel->new(); | |
my $bot = TestBot->new( | |
tester => $tester, | |
channel => $channel, | |
); | |
set_things_up_so_something_fails($tester); | |
$bot->receive_statistics(); | |
$channel->clear_messages(); | |
set_things_up_so_everything_passes($tester); | |
$bot->receive_statistics(); | |
is_deeply $channel->messages(), | |
["Module W just started passing its test suite."], | |
"Bot reports when a module starts passing its test suite again"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment