Test/Mojo/IRC.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 70.0% 21 / 30 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 93.3% 14 / 15 Podpod 100% 3 / 3 totaltotal 83.1% 118 / 142 CC 19 SCAR 14.0
CC
19
Cov
86%
import
8.4
__ANON__
5.1
start_server
4.0
CRAP 4.0
« Mojo/IRC.pm ↑ Summary
1 package Test::Mojo::IRC;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::IOLoop::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::IRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶$ENV{TEST_MOJO_IRC_SERVER_TIMEOUT} ||= $ENV{TEST_MOJO_IRC_SERVER} ? 10 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TEST_MOJO_IRC_SERVER'} ? : 0 2 2
Condition33%1/3
$ENV{"TEST_MOJO_IRC_SERVER_TIMEOUT"} ||= $ENV{"TEST_MOJO_IRC_SERVER"} ? 10 : 4
l!l&&r!l&&!r
020
MC/DC0%0/2
$ENV{"TEST_MOJO_IRC_SERVER_TIMEOUT"} ||= $ENV{"TEST_MOJO_IRC_SERVER"} ? 10 : 4
$ENV{"TEST_MOJO_IRC_SERVER_TIMEOUT"} $ENV{"TEST_MOJO_IRC_SERVER"} ? 10 : 4
Truth table33%1/3
A ||= B
A= $ENV{"TEST_MOJO_IRC_SERVER_TIMEOUT"}B= $ENV{"TEST_MOJO_IRC_SERVER"} ? 10 : 4
ABresult
1X1
011
000
9  
10 2 has server => '';
11  
12 2 has welcome_message => <<'HERE';
13 :hybrid8.local NOTICE AUTH :*** Looking up your hostname...
14 :hybrid8.local NOTICE AUTH :*** Checking Ident
15 :hybrid8.local NOTICE AUTH :*** Found your hostname
16 :hybrid8.local NOTICE AUTH :*** No Ident response
17 HERE
18  
19 2 ▶sub on {
Subroutine on: called CC=1 SCAR=0.0
Pod documented
20 2 my ($self, $irc, $event, $cb) = @_;
21 2 push @{$self->{subscriptions}}, $irc, $event, $irc->on($event => $cb);
22 2 $self;
23 }
24  
25 2 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod documented
26 2 my ($self, $reply_on, $cb) = @_;
27 0 ▶ my $guard = Mojo::IOLoop->timer($ENV{TEST_MOJO_IRC_SERVER_TIMEOUT}, sub { Mojo::IOLoop->stop });
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 2 my @subscriptions;
29  
30 2 local $self->{from_client} = '';
31 2 local $self->{reply_on} = $reply_on;
32 2 local $self->{subscriptions} = \@subscriptions;
33  
34 2 $self->$cb;
35 2 Mojo::IOLoop->remove($guard);
36  
37 2 ▶ while (@subscriptions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subscriptions) 2 2 4
38 2 my ($irc, $event, $cb) = splice @subscriptions, 0, 3, ();
39 2 $irc->unsubscribe($event => $cb);
40 }
41  
42 2 $self;
43 }
44  
45 3 ▶sub start_server {
Subroutine start_server: called CC=4 SCAR=13.9
Pod documented
46 3 my $self = shift;
47  
48 3 ▶ return $self->new->tap('start_server') unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 2 3
49 2 ▶ return $self->server if $self->server;
Branch: 1 branch
BranchTrueFalseTotal
if $self->server 0 2 2
50 2 ▶ return $ENV{TEST_MOJO_IRC_SERVER} if $ENV{TEST_MOJO_IRC_SERVER};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TEST_MOJO_IRC_SERVER"} 0 2 2
51  
52 2 my $port = Mojo::IOLoop::Server->generate_port;
53 2 my $write;
54  
55 219 ▶ $write = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
56 219 ▶ return unless length $self->{server_buf};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"server_buf"} 2 217 219
57 217 ▶ return shift->write(substr($self->{server_buf}, 0, int(10 + rand 20), ''), sub { shift->$write });
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2 };
59  
60 $self->{server_id} = Mojo::IOLoop->server(
61 {address => '127.0.0.1', port => $port},
62 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 2 my ($ioloop, $stream) = @_;
64  
65 $stream->on(
66 2 ▶ read => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
67 2 my ($stream, $buf) = @_;
68 2 $self->{from_client} .= $buf;
69  
70 2 ▶ while ($buf =~ /[\015\012]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /[\015\012]/gu) 5 0 5
71 5 ▶ last unless @{$self->{reply_on} || []};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"reply_on"} or [];} 2 3 5
Condition50%1/2
$self->{'reply_on'} || []
l!l
50
MC/DC0%0/1
$self->{'reply_on'} || []
$self->{'reply_on'}
Truth table50%1/2
A
A= $self->{'reply_on'}
Aresult
11
00
72 3 ▶ last unless $self->{from_client} =~ $self->{reply_on}[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"from_client"} =~ /$self->{"reply_on"}[0]/u 0 3 3
73 3 $self->_concat_server_buf($self->{reply_on}[1]);
74 3 splice @{$self->{reply_on}}, 0, 2, ();
75 }
76  
77 2 $stream->$write;
78 }
79 2 );
80  
81 2 $self->_concat_server_buf($self->welcome_message);
82 2 $stream->$write;
83 }
84 2 );
85  
86 2 $self->{server_buf} = '';
87 2 $self->server("127.0.0.1:$port")->server;
88 }
89  
90 5 ▶sub _concat_server_buf {
Subroutine _concat_server_buf: called CC=4 SCAR=13.9
91 5 my ($self, $buf) = @_;
92  
93 5 ▶ if (ref $buf eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $buf eq 'ARRAY') { } 1 4 5
elsif (ref $buf) { } 2 2 4
94 1 ▶ $buf = Mojo::Loader::data_section(@$buf == 1 ? ('main', @$buf) : @$buf);
Branch: 1 branch
BranchTrueFalseTotal
@$buf == 1 ? : 0 1 1
95 }
96 elsif (ref $buf) {
97 2 $buf = Mojo::File::path(split '/', $$buf)->slurp;
98 }
99  
100 5 $buf =~ s/[\015\012]/\015\012/g;
101 5 $self->{server_buf} .= $buf;
102 }
103  
104 2 ▶sub import {
Subroutine import: called CC=7 SCAR=21.3
105 2 my $class = shift;
106 2 ▶ my $arg = shift // '';
Condition50%1/2
shift() // ""
l!l
20
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
107 2 my $caller = caller;
108  
109 2 ▶ return unless $arg =~ /^(?:-basic|-ua)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /^(?:-basic|-ua)$/u 0 2 2
110 2 $_->import for qw(strict warnings utf8);
111 2 feature->import(':5.10');
112 2 ▶ eval "require Mojo::IRC::UA;1" or die $@ if $arg eq '-ua';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "-ua" 0 2 2
Condition25%1/4
$arg eq '-ua' && (eval 'require Mojo::IRC::UA;1' || die($@))
!ll
20
eval "require Mojo::IRC::UA;1" or die $@
l!l
00
MC/DC0%0/2
$arg eq '-ua' && (eval 'require Mojo::IRC::UA;1' || die($@))
$arg eq '-ua' eval "require Mojo::IRC::UA;1"
Truth table33%1/3
A && B
A= $arg eq '-ua'B= eval "require Mojo::IRC::UA;1"
ABresult
0X0
100
111
113 2 ▶ eval "package $caller; use Test::More; 1" or die $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "package $caller; use Test::More; 1" or die $@ 2 0 2
114 }
115  
116 2 1;
117  
118 =encoding utf8
119  
120 =head1 NAME
121  
122 Test::Mojo::IRC - Module for testing Mojo::IRC
123  
124 =head1 SYNOPSIS
125  
126 use Test::Mojo::IRC -basic;
127  
128 my $t = Test::Mojo::IRC->start_server;
129 my $irc = Mojo::IRC->new(server => $t->server);
130  
131 # simulate server/client communication
132 $t->run(
133 [
134 # Send "welcome.irc" from the DATA section when client sends "NICK"
135 qr{\bNICK\b} => [qw(main motd.irc)],
136 ],
137 sub {
138 my $err;
139 my $motd = 0;
140 $t->on($irc, irc_rpl_motd => sub { $motd++ });
141 $t->on($irc, irc_rpl_endofmotd => sub { Mojo::IOLoop->stop; }); # need to manually stop the IOLoop
142 $irc->connect(sub { $err = $_[1]; });
143 Mojo::IOLoop->start; # need to manually start the IOLoop
144 is $err, "", "connected";
145 is $motd, 3, "message of the day";
146 },
147 );
148  
149 done_testing;
150  
151 __DATA__
152 @@ motd.irc
153 :spectral.shadowcat.co.uk 375 test123 :- spectral.shadowcat.co.uk Message of the Day -
154 :spectral.shadowcat.co.uk 372 test123 :- We scan all connecting clients for open proxies and other
155 :spectral.shadowcat.co.uk 372 test123 :- exploitable nasties. If you don't wish to be scanned,
156 :spectral.shadowcat.co.uk 372 test123 :- don't connect again, and sorry for scanning you this time.
157 :spectral.shadowcat.co.uk 376 test123 :End of /MOTD command.
158  
159 =head1 DESCRIPTION
160  
161 L<Test::Mojo::IRC> is a module for making it easier to test L<Mojo::IRC>
162 applications.
163  
164 =head1 ENVIRONMENT VARIABLES
165  
166 =head2 TEST_MOJO_IRC_SERVER
167  
168 C<TEST_MOJO_IRC_SERVER> can be set to point to a live server. If the variable
169 is set, L</start_server> will simply return L<TEST_MOJO_IRC_SERVER> instead
170 of setting up a server.
171  
172 =head1 ATTRIBUTES
173  
174 =head2 server
175  
176 $str = $self->server;
177  
178 Returns the server address, "host:port", that L</start_server> set up.
179  
180 =head2 welcome_message
181  
182 $str = $self->welcome_message;
183 $self = $self->welcome_message($str);
184  
185 Holds a message which will be sent to the client on connect.
186  
187 =head1 METHODS
188  
189 =head2 on
190  
191 $self->on($irc, $event, $cb);
192  
193 Will attach events to the L<$irc|Mojo::IRC> object which is removed
194 after L</run> has completed. See L</SYNOPSIS> for example code.
195  
196 =head2 run
197  
198 $self->run($reply_on, sub { my $self = shift });
199  
200 Used to simulate communication between IRC server and client. The way this
201 works is that the C<$cb> will initiate L<connect|Mojo::IRC/connect> or
202 L<write|Mojo::IRC/write> to the server and the server will then respond
203 with the data from either L</welcome_message> or C<$reply_on> on these
204 events.
205  
206 C<$reply_on> is an array-ref of regex/buffer pairs. Each time a message
207 from the client match the first regex in the C<$reply_on> array the
208 buffer will be sent back to the client and the regex/buffer will be removed.
209 This means that the order of the pairs are important. The buffer can be...
210  
211 =over 4
212  
213 =item * Scalar
214  
215 Plain text.
216  
217 =item * Scalar ref
218  
219 Path to file on disk.
220  
221 =item * Array ref
222  
223 The module name and file passed on to L<Mojo::Loader/data_section>. The default
224 package is "main", meaning the two examples below is the same:
225  
226 $self->run([qr{JOIN}, ["join-reply.irc"]], sub { my $self = shift });
227 $self->run([qr{JOIN}, ["main", "join-reply.irc"]], sub { my $self = shift });
228  
229 =back
230  
231 Note that starting and stopping the L<IOLoop|Mojo::IOLoop> is up to you, but
232 there is also a master timeout which will stop the IOLoop if running for too
233 long.
234  
235 See L</SYNOPSIS> for example.
236  
237 =head2 start_server
238  
239 $server = $self->start_server;
240 $self = Test::Mojo::IRC->start_server;
241  
242 Will start a test server and return L</server>. It can also be called as
243 a class method which will return a new object.
244  
245 =head2 import
246  
247 use Test::Mojo::IRC -basic;
248  
249 Loading this module with "-basic" will import L<strict>, L<warnings>, L<utf8>,
250 L<Test::More> and 5.10 features into the caller namespace.
251  
252 =head1 COPYRIGHT AND LICENSE
253  
254 Copyright (C) 2014, Jan Henning Thorsen
255  
256 This program is free software, you can redistribute it and/or modify it under
257 the terms of the Artistic License version 2.0.
258  
259 =head1 AUTHOR
260  
261 Jan Henning Thorsen - C<jhthorsen@cpan.org>
262  
263 =cut
« Mojo/IRC.pm ↑ Summary