From 6e23c401b100443891e08cae05b36a1e2b1c43ea Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 25 Oct 2015 00:53:55 +0100 Subject: [PATCH] Make perlcritic happy and add some rudimentary tests --- lib/App/Statsbot.pm | 54 ++++++++++++++++++++++++++------------------- t/App-Statsbot.t | 28 ++++++++++++++++++++++- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/lib/App/Statsbot.pm b/lib/App/Statsbot.pm index 49fb66f..4491170 100644 --- a/lib/App/Statsbot.pm +++ b/lib/App/Statsbot.pm @@ -13,6 +13,7 @@ use POE::Component::IRC::Plugin::Connector; use POE::Component::IRC::Plugin::CTCP; use IRC::Utils qw/parse_user/; +use Carp; use DBI; use DBD::SQLite; use Text::ParseWords qw/shellwords/; @@ -37,6 +38,21 @@ my $irc; my %state; +sub _yield { $irc->yield(@_) } +sub _nick_name { $irc->nick_name } + +sub _uptime { + my ($starttime, $nick) = @_; + my $sth=$dbh->prepare('SELECT start,end FROM presence WHERE end > ? AND nick == ?'); + $sth->execute($starttime, $nick); + + my $uptime=0; + while (my ($start, $end)=$sth->fetchrow_array) { + $uptime+=$end-max($start,$starttime) + } + return $uptime +} + sub run { $irc=POE::Component::IRC::State->spawn; POE::Session->create( @@ -53,10 +69,10 @@ sub run { options => { trace => $DEBUG }, ); - $dbh=DBI->connect("dbi:SQLite:dbname=$DB") or die "Cannot connect to database: $!"; + $dbh=DBI->connect("dbi:SQLite:dbname=$DB") or croak "Cannot connect to database: $!"; $dbh->do('CREATE TABLE presence (start INTEGER, end INTEGER, nick TEXT)'); - $insert=$dbh->prepare('INSERT INTO presence (start, end, nick) VALUES (?,?,?)') or die "Cannot prepare query: $!"; - $update=$dbh->prepare('UPDATE presence SET end = ? WHERE start == ? AND nick == ?') or die "Cannot prepare query: $!"; + $insert=$dbh->prepare('INSERT INTO presence (start, end, nick) VALUES (?,?,?)') or croak "Cannot prepare query: $!"; + $update=$dbh->prepare('UPDATE presence SET end = ? WHERE start == ? AND nick == ?') or croak "Cannot prepare query: $!"; $poe_kernel->run(); }; @@ -75,7 +91,7 @@ sub tick{ $_[KERNEL]->delay(tick => $TICK); } -sub bot_start{ +sub bot_start{ ## no critic (RequireArgUnpacking) $_[KERNEL]->delay(tick => $TICK); $irc->plugin_add(CTCP => POE::Component::IRC::Plugin::CTCP->new( @@ -92,8 +108,8 @@ sub bot_start{ servers => [ $SERVER ], )); - $irc->yield(register => 'all'); - $irc->yield( + _yield(register => 'all'); + _yield( connect => { Nick => $NICKNAME, Username => 'statsbot', @@ -105,20 +121,20 @@ sub bot_start{ ); } -sub on_fatal{ die "Fatal error: $_[ARG0]" } +sub on_fatal{ croak "Fatal error: $_[ARG0]" } sub on_public{ my ($targets,$message)=@_[ARG1,ARG2]; - my $botnick = $irc->nick_name; + my $botnick = _nick_name; if ($message =~ /(?:$botnick[:,])?\s*!?help\s*(.*)/) { - $irc->yield(privmsg => $targets, "Try !presence username interval [truncate]"); - $irc->yield(privmsg => $targets, "For example, !presence mgv '2 days'"); - $irc->yield(privmsg => $targets, "or !presence mgv '1 year' 4"); + _yield(privmsg => $targets, "Try !presence username interval [truncate]"); + _yield(privmsg => $targets, "For example, !presence mgv '2 days'"); + _yield(privmsg => $targets, "or !presence mgv '1 year' 4"); return; } - return unless $message =~ /(?:$botnick[:,])?\s*!?presence\s*(.*)/; + return unless $message =~ /(?:$botnick[:,])?\s*!?presence\s*(.*)/sx; my ($nick, $time, $truncate) = shellwords $1; $truncate//=-1; @@ -131,19 +147,11 @@ sub on_public{ eval { $time = parse_duration $time; } or do { - $irc->yield(privmsg => $targets, "cannot parse timespec: $time"); + _yield(privmsg => $targets, "cannot parse timespec: $time"); return; }; - my $starttime=time-$time; - - my $sth=$dbh->prepare('SELECT start,end FROM presence WHERE end > ? AND nick == ?'); - $sth->execute($starttime, $nick); - - my $uptime=0; - while (my ($start, $end)=$sth->fetchrow_array) { - $uptime+=$end-max($start,$starttime) - } + my $uptime=_uptime time-$time, $nick; my $ret; if ($truncate == -1) { @@ -155,7 +163,7 @@ sub on_public{ $time=duration_exact $time; - $irc->yield(privmsg => $targets, "$nick was here $ret during the last $time"); + _yield(privmsg => $targets, "$nick was here $ret during the last $time"); } diff --git a/t/App-Statsbot.t b/t/App-Statsbot.t index 2f005c6..cf8e1dd 100644 --- a/t/App-Statsbot.t +++ b/t/App-Statsbot.t @@ -2,6 +2,32 @@ use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 6; BEGIN { use_ok('App::Statsbot') }; +my ($time, $reply); + +BEGIN { + no warnings 'redefine'; + *App::Statsbot::_nick_name = sub { 'statsbot' }; + *App::Statsbot::_yield = sub { $reply = $_[2] }; + *App::Statsbot::_uptime = sub { $time }; +} + +sub runtest { + my ($uptime, $msg, $exp_re) = @_; + $time = $uptime; + $reply = ''; + my @args; + @args[App::Statsbot::ARG1, App::Statsbot::ARG2] = ('', $msg); + App::Statsbot::on_public(@args); + like $reply, $exp_re, "$msg with 0 seconds"; +} + +my $magicnr = 13980000; + +runtest 0, '!presence mgv', qr/mgv was here 0 hours during the last 1 day/; +runtest $magicnr, '!presence mgv "1 year"', qr/here 3883 hours during/; +runtest $magicnr, '!presence mgv "1 year" 1', qr/here 162 days during/; +runtest $magicnr, '!presence mgv "1 year" 2', qr/here 161 days and 19 hours during/; +runtest $magicnr, '!presence mgv "1 year" 20', qr/here 161 days, 19 hours, and 20 minutes during/; -- 2.39.2