NAME
Test::Mojo::IRC - Module for testing Mojo::IRC
SYNOPSIS
use Test::Mojo::IRC -basic;
my $t = Test::Mojo::IRC->new;
my $server = $t->start_server;
my $irc = Mojo::IRC->new(server => $server);
# simulate server/client communication
$t->run(
[
# Send t/data/welcome when client sends "NICK"
# The file contains the MOTD text
qr{\bNICK\b} => \ "t/data/welcome",
],
sub {
my $err;
my $motd = 0;
$t->on($irc, irc_rpl_motd => sub { $motd++ });
$t->on($irc, irc_rpl_endofmotd => sub { Mojo::IOLoop->stop; }); # need to manually stop the IOLoop
$irc->connect(sub { $err = $_[1]; });
Mojo::IOLoop->start; # need to manually start the IOLoop
is $err, "", "connected";
is $motd, 19, "message of the day has of 15 lines";
},
);
done_testing;
DESCRIPTION
Test::Mojo::IRC is a module for making it easier to test Mojo::IRC applications.
ENVIRONMENT VARIABLES
TEST_MOJO_IRC_SERVER
TEST_MOJO_IRC_SERVER
can be set to point to a live server. If the variable is set, "start_server" will simply return TEST_MOJO_IRC_SERVER instead of setting up a server.
ATTRIBUTES
welcome_message
$str = $self->welcome_message;
$self = $self->welcome_message($str);
Holds a message which will be sent to the client on connect.
METHODS
on
$self->on($irc, $event, $cb);
Will attach events to the $irc object which is removed after "run" has completed. See "SYNOPSIS" for example code.
run
$self->run($reply_on, $cb);
Used to simulate communication between IRC server and client. The way this works is that the $cb
will initiate connect or write to the server and the server will then respond with the data from either "welcome_message" or $reply_on
on these events.
$reply_on
is an array-ref of regex/buffer pairs. Each time a message from the client match the first regex in the $reply_on
array the buffer will be sent back to the client and the regex/buffer will be removed. This means that the order of the pairs are important. The buffer can be either a scalar ref (path to file) or a plain scalar (simple buffer).
Note that starting and stopping the IOLoop is up to you, but there is also a master timeout which will stop the IOLoop if running for too long.
See "SYNOPSIS" for example.
start_server
$server = $self->start_server;
Will start a test server and return the "host:port" which it listens to.
import
use Test::Mojo::IRC -basic;
Loading this module with "-basic" will import strict, warnings, utf8, Test::More and 5.10 features into the caller namespace.
COPYRIGHT AND LICENSE
Copyright (C) 2014, Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
AUTHOR
Jan Henning Thorsen - jhthorsen@cpan.org