aboutsummaryrefslogtreecommitdiff
path: root/Mock/CommandRegistry.pm
diff options
context:
space:
mode:
authorDavid Phillips <david@yeah.nah.nz>2020-04-04 14:25:27 +1300
committerDavid Phillips <david@yeah.nah.nz>2020-04-04 14:29:59 +1300
commitcdae7275d29c9c370ec7e714dd71af2a1076c0d0 (patch)
tree99ee7ddfe501644a8e595045400c184a32624b8f /Mock/CommandRegistry.pm
parent7744defa1f8a5f1b54d8eb78657bd2c8d73df29f (diff)
downloadidalius-cdae7275d29c9c370ec7e714dd71af2a1076c0d0.tar.xz
WIP: Add Mock::CommandRegistry for testing commands
Diffstat (limited to 'Mock/CommandRegistry.pm')
-rw-r--r--Mock/CommandRegistry.pm53
1 files changed, 49 insertions, 4 deletions
diff --git a/Mock/CommandRegistry.pm b/Mock/CommandRegistry.pm
index 166879d..72c84cf 100644
--- a/Mock/CommandRegistry.pm
+++ b/Mock/CommandRegistry.pm
@@ -3,13 +3,58 @@ package Mock::CommandRegistry;
use strict;
use warnings;
-use fields qw/commands/;
+use fields qw/_commands/;
sub new {
- my ($class) = @_;
- bless {}, $class;
+ my Mock::CommandRegistry $self = shift;
+ unless ($self) {
+ $self = fields::new($self);
+ $self->{_commands} = {};
+ }
+ bless {}, $self;
}
-sub register {
+sub run {
+ my ($self, $command, %ctx) = @_;
+ # XXX should run all commands matching? Only first?
...
}
+
+sub run_owned {
+ my ($self, $owner, $command, %ctx) = @_;
+ die "$owner::$command not registered\n"
+ unless $self->is_registered_to_owner($owner, $command);
+
+ # XXX plugins need to eventually use the ctx hash directly. Much more
+ # readable than remembering the long chain of magic positional args each
+ # time you write a new cmd plugin
+ my @args = (
+ $ctx{irc},
+ $ctx{logger},
+ $ctx{who},
+ $ctx{where},
+ $ctx{ided},
+ $ctx{rest},
+ $ctx{no_reenter},
+ $ctx{args}
+ );
+
+ $self->{_commands}->{$owner}->{$command}->(@args);
+}
+
+sub is_registered {
+ my ($self, $command) = @_;
+ # XXX should tell if any owner registers a given command
+ ...
+}
+
+sub is_registered_to_owner {
+ my ($self, $owner, $command) = @_;
+ defined $self->{_commands}->{$owner}->{$command};
+}
+
+sub register {
+ my ($self, $owner, $command, $coderef) = @_;
+ $self->{_commands}->{$owner}->{$command} = $coderef;
+}
+1;