diff options
author | David Phillips <david@yeah.nah.nz> | 2020-04-04 00:21:47 +1300 |
---|---|---|
committer | David Phillips <david@yeah.nah.nz> | 2020-04-04 13:19:55 +1300 |
commit | 7744defa1f8a5f1b54d8eb78657bd2c8d73df29f (patch) | |
tree | 17aa70627d43b251a8265891198ef02a30439705 | |
parent | b4eb42636ebe66192d0e531bc691b6f38debfbde (diff) | |
download | idalius-7744defa1f8a5f1b54d8eb78657bd2c8d73df29f.tar.xz |
WIP: Start writing some plugin tests
-rw-r--r-- | Mock/CommandRegistry.pm | 15 | ||||
-rw-r--r-- | Mock/IRC.pm | 62 | ||||
-rwxr-xr-x | test.pl | 11 | ||||
-rw-r--r-- | test/TODO.md | 39 | ||||
-rwxr-xr-x | test/test_autojoin.t | 69 | ||||
-rwxr-xr-x | test/test_echo.t | 34 | ||||
-rwxr-xr-x | test/test_ping.t | 34 |
7 files changed, 264 insertions, 0 deletions
diff --git a/Mock/CommandRegistry.pm b/Mock/CommandRegistry.pm new file mode 100644 index 0000000..166879d --- /dev/null +++ b/Mock/CommandRegistry.pm @@ -0,0 +1,15 @@ +package Mock::CommandRegistry; + +use strict; +use warnings; + +use fields qw/commands/; + +sub new { + my ($class) = @_; + bless {}, $class; +} + +sub register { + ... +} diff --git a/Mock/IRC.pm b/Mock/IRC.pm new file mode 100644 index 0000000..5c62754 --- /dev/null +++ b/Mock/IRC.pm @@ -0,0 +1,62 @@ +package Mock::IRC; + +use strict; +use warnings; +use Switch; + +use fields qw/_joined_channels _nick_name/; + +sub new { + my Mock::IRC $self = shift; + unless ($self) { + $self = fields::new($self); + $self->{_nick_name} = ""; + $self->{_joined_channels} = []; + } + + bless {}, $self; +} + +############################################################################### +# Methods used by code/plugins under test +sub yield { + my ($self, $event, @data) = @_; + switch($event) { + case "join" { + my $channel = $data[0]; + unless (grep {$_ eq $channel} @{$self->{_joined_channels}}) { + push @{$self->{_joined_channels}}, $channel; + } + } + case "part" { + # FIXME supports multi args? + my $channel = $data[0]; + @{$self->{_joined_channels}} = grep {$_ ne $channel} @{$self->{_joined_channels}}; + } + case "nick" { + ($self->{_nick_name}) = @data; + } + else { + die "Unsupported event type: $event\n"; + } + } +} + +sub nick_name { + my ($self) = @_; + return $self->{_nick_name}; +} + + +############################################################################### +# Methods used test-side follow +sub idalius_get_channels { + my ($self) = @_; + return @{$self->{_joined_channels}}; +} + +sub idalius_is_joined_to { + my ($self, $channel) = @_; + return grep {$_ eq $channel} @{$self->{_joined_channels}}; +} +1; @@ -0,0 +1,11 @@ +#!/usr/bin/env perl + +use Test::Harness; + +my @t = ( + "test/test_autojoin.t", + "test/test_echo.t", + "test/test_ping.t", +); + +runtests(@t); diff --git a/test/TODO.md b/test/TODO.md new file mode 100644 index 0000000..e235325 --- /dev/null +++ b/test/TODO.md @@ -0,0 +1,39 @@ +# To Do + +* **Refactor command registration tests.** There should be a Mock::Misc utility + method which forms a dict mapping commands to their sub refs. Checking if + a command was registers then becomes checking keys, and checking return + values under different conditions becomes more feasible, and globals go away. + +### The following need tests written at all: + +* Admin.pm +* Antiflood.pm +* ASL.pm +* Convert.pm +* Dad.pm +* DevNull.pm +* Greet.pm +* Hmm.pm +* Jinx.pm +* Log.pm +* Map.pm +* Markov.pm +* Men.pm +* Natural.pm +* Quote_Grab.pm +* Random.pm +* Source.pm +* Thanks.pm +* Timezone.pm +* Titillate.pm +* Topic.pm +* URL_Title.pm +* Vote.pm + + +### The following have sufficient tests for now: + +* Autojoin.pm +* Echo.pm +* Ping.pm diff --git a/test/test_autojoin.t b/test/test_autojoin.t new file mode 100755 index 0000000..fb0e4b1 --- /dev/null +++ b/test/test_autojoin.t @@ -0,0 +1,69 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::Simple tests => 12; + +use Mock::IRC; + +use Plugin::Autojoin; + + +my $irc = new Mock::IRC; + +sub null_logger{} +my @expected_channels = ( "#channel", "#another", "#and_another" ); +my $channel; +my %config = ( + "channels" => \@expected_channels +); + +my $nick = "someBotNick"; +$irc->yield('nick' => $nick); + +Plugin::Autojoin->configure(undef, undef, \%config, undef); + +### +# Check that all configured channels are joined to on IRC 001 (initial +# connection +Plugin::Autojoin->on_001_welcome(\&null_logger, undef, undef, $irc); +for my $expected (@expected_channels) { + ok($irc->idalius_is_joined_to($expected), "joined $expected"); +} + +# Check that the number of channels joined matches the number of channels +# configured. Along with the above assertions, this ensures we join only those +# channels instructed to +my @joined = $irc->idalius_get_channels(); +ok(@joined == @expected_channels, "No extra channels joined"); + +### +# Check that being "kicked" from a channel (even unconfigured) joins us back +# to it +$channel = "#notconfigured"; +ok(!($irc->idalius_is_joined_to($channel)), "not in $channel"); +Plugin::Autojoin->on_kick(\&null_logger, "someUser", $channel, $nick, "reason", $irc); +ok($irc->idalius_is_joined_to($channel), "joined to $channel after kick"); + +# Check that seeing someone else be kicked wouldn't make us yield any +# unnecessary join +$channel = $expected_channels[0]; +$irc->yield(part => $channel); +ok(!($irc->idalius_is_joined_to($channel)), "not in $channel"); +Plugin::Autojoin->on_kick(\&null_logger, "someUser", $channel, "a$nick", "reason", $irc); +ok(!($irc->idalius_is_joined_to($channel)), "not in $channel after someone else was kicked"); + + +### +# Check that being invited to a configured channel joins us back to it +$channel = $expected_channels[0]; +$irc->yield(part => $channel); +ok(!($irc->idalius_is_joined_to($channel)), "removed from $channel"); +Plugin::Autojoin->on_invite(\&null_logger, "someUser", $channel, $irc); +ok($irc->idalius_is_joined_to($channel), "joined to $channel after invitation"); + +# Check that being invited to a channel not configured doesn't join us to it +$channel = "#fronglegrong"; +ok(!($irc->idalius_is_joined_to($channel)), "not in $channel"); +Plugin::Autojoin->on_invite(\&null_logger, "someUser", $channel, $irc); +ok(!($irc->idalius_is_joined_to($channel)), "not in $channel after invitation"); diff --git a/test/test_echo.t b/test/test_echo.t new file mode 100755 index 0000000..73ff0e8 --- /dev/null +++ b/test/test_echo.t @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::Simple tests => 2; + +use Plugin::Echo; + +my $expected = " Ping pong do the echo thing!"; +our $registered; +our $response; + +sub register_cmd { + my ($module, $name, $run) = @_; + + $registered = 1; + + $response = $run->( + undef, # irc + undef, # logger + undef, # who + undef, # where + undef, # ided + $expected, + undef, # no reenter + undef, # arguments + ); +} + +Plugin::Echo->configure(\®ister_cmd, undef, undef, undef); + +ok($registered, "plugin registered command"); +ok($response eq $expected, "echo expectation met"); diff --git a/test/test_ping.t b/test/test_ping.t new file mode 100755 index 0000000..f7ae60f --- /dev/null +++ b/test/test_ping.t @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::Simple tests => 2; + +use Plugin::Ping; + +our $registered; +our $response; +my $expected = "user: pong"; + +sub register_cmd { + my ($module, $name, $run) = @_; + + $registered = 1; + + $response = $run->( + undef, # irc + undef, # logger + 'user!who@example.com', + undef, # where + undef, # ided + undef, # rest + undef, # no reenter + undef, # arguments + ); +} + +Plugin::Ping->configure(\®ister_cmd, undef, undef, undef); + +ok($registered, "plugin registered command"); +ok($response eq $expected, "echo expectation met"); |