From 7744defa1f8a5f1b54d8eb78657bd2c8d73df29f Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 4 Apr 2020 00:21:47 +1300 Subject: WIP: Start writing some plugin tests --- test/TODO.md | 39 +++++++++++++++++++++++++++++ test/test_autojoin.t | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_echo.t | 34 ++++++++++++++++++++++++++ test/test_ping.t | 34 ++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 test/TODO.md create mode 100755 test/test_autojoin.t create mode 100755 test/test_echo.t create mode 100755 test/test_ping.t (limited to 'test') 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"); -- cgit v1.1