blib/lib/Test/Out.pm

filter: Statementstmt 88.6% 109 / 123 Branchbran 50.0% 11 / 22 Conditioncond 36.3% 12 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 75.0% 18 / 24 Podpod 76.9% 10 / 13 totaltotal 68.3% 160 / 234 CC 26 SCAR 14.0
CC
26
Cov
74%
DESTROY
12.0
cmp_output
12.0
redirect
5.2
CRAP 4.1
↑ Summary
1 ## $Id:$ ##
2  
3 package Test::Out;
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 my $tstobj = Test::Builder->new;
15  
16 5 our $VERSION = '0.21';
17  
18 =head1 NAME
19  
20 Test::Out - Test output from FILEHANDLE
21  
22 =head1 SYNOPSIS
23  
24 use Test::Out;
25 my $out = Test::Out->new( output => *STDOUT, tests => 4 );
26  
27 # Or ...
28  
29 my $out = Test::Out->new(tests => 4);
30 $out->redirect( output => *STDOUT );
31  
32 ## This will go to a place that your harness can see
33 $out->diag("Testing is* functions");
34  
35 ## But this will not be displayed but captured for test methods
36 $some->method_that_prints("This is a test\n");
37 $out->is_output("This is a test\n", "test 1");
38 $out->isnt_output("Han shot first", "test 2");
39  
40 $out->diag("Testing regex functions");
41 CORE::print "A random number: @{[int rand 100]}\n";
42 $out->like_output(qr/random number: \d+/, "test 3");
43 $out->unlike_output(qr/i like pickles$/, "test 4");
44  
45 $out->restore;
46  
47 ## This will be printed to STDOUT
48 print "Done.\n";
49  
50 =head1 DESCRIPTION
51  
52 Test out is another Test::Builder application that implements a few of the well known test facilities
53 except the result of output to an IO::Handle is used. This could be used to capture output being
54 printed to a file, but it's ideal for output being sent to STDOUT or STDERR.
55  
56 See the SYNOPSIS for an example use.
57  
58 =begin RCS
59  
60 $Id$
61  
62 =end RCS
63  
64 =head1 AUTHOR
65  
66 Lane Davis <cpan@upt.org>
67  
68 =head1 FUNCTIONS
69  
70 =head2 METHODS
71  
72 =over
73  
74 =item B<$out-E<gt>new(%options)>
75  
76 The F<Test::Out> package constructor has several arguments, some required some optional
77  
78 =over
79  
80 =item B<REQUIRED OPTIONS>
81  
82 The following options must be present in the hash passed to the constructor:
83  
84 =over
85  
86 =item B<THE NUMBER OF PLANNED TESTS>
87  
88 =over
89  
90 =item tests =E<gt> $Tests
91  
92 The number of tests are simply passed to Test::Builder
93  
94 =back
95  
96 =back
97  
98 =item B<NON-REQUIRED OPTIONS>
99  
100 =over
101  
102 =item B<THE OUTPUT YOU WISH TO TEST>
103  
104 Actually the C<output> key is required, but you have the option of passing the key into the constructor or to the B<redirect>
105 method. This is useful if you have several segments of tests wrapped with B<redirect> and B<restore>.
106  
107 =over
108  
109 =item output =E<gt> *FH
110  
111 The output argument is required and may contain either a FILEHANDLE typeglob, or
112  
113 =item output =E<gt> \*FHREF
114  
115 The C<output> key may also point to a typeglob reference
116  
117 =back
118  
119 =back
120  
121 =back
122  
123 =cut
124  
125 4 ▶sub new {
Subroutine new: called CC=4 SCAR=14.4
Pod documented
126 4 my $proto = shift;
127 4 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
128 4 my ($object);
129  
130 ##
131 ## Ensure key/value pairs are passed
132 4 assert(!(@_ & 1));
133  
134 4 $object = { @_ };
135 4 ▶ croak("Missing planned tests key: 'tests'") unless exists $object->{tests};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $object->{"tests"} 0 4 4
136  
137 ##
138 ## Remove it from the object so it can't be reused
139 4 $tstobj->plan(tests => delete $object->{tests});
140  
141 4 my $self = bless($object, $class);
142 4 ▶ $self->redirect(%$object) if exists $object->{output};
Branch: 1 branch
BranchTrueFalseTotal
if exists $object->{"output"} 4 0 4
143  
144 4 return $self;
145 }
146  
147 =item B<$out-E<gt>redirect>
148  
149 =item B<$out-E<gt>redirect(output =E<gt> *FH)>
150  
151 =item B<$out-E<gt>redirect(output =E<gt> \*FH)>
152  
153 This method will be automatically invoked by the constructor if the output key is passed to new.
154  
155 =cut
156  
157 4 ▶sub redirect {
Subroutine redirect: called CC=5 SCAR=16.4
Pod documented
158 4 my $self = shift;
159 4 my $obj = { @_ };
160  
161 4 eval {
162 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 4 ▶ die unless exists $obj->{output} && *{ $obj->{output} }{IO}->isa("IO::Handle");
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{"output"} and *{$obj->{"output"};}{"IO"}->isa("IO::Handle") 0 4 4
Condition33%1/3
exists $obj->{"output"} and *{$obj->{"output"};}{"IO"}->isa("IO::Handle")
!ll&&!rl&&r
004
MC/DC0%0/2
exists $obj->{"output"} and *{$obj->{"output"};}{"IO"}->isa("IO::Handle")
exists $obj->{"output"} *{$obj->{"output"};}{"IO"}->isa("IO::Handle")
Truth table33%1/3
A and B
A= exists $obj->{"output"}B= *{$obj->{"output"};}{"IO"}->isa("IO::Handle")
ABresult
0X0
100
111
164 };
165 4 ▶ croak("Not a valid filehandle or output key missing") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
166  
167 4 $self->_init_redirect($obj);
168  
169 4 ( my $basename = basename $0 ) =~ s/\.[^.]*$//;
170  
171 4 ▶ $self->{_temp_file} = File::Temp->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_temp_file"} = "File::Temp"->new("DIR", "/tmp", "TEMPLATE", join(".", "", $basename, "XXXXXXXX"), "SUFFIX", '.' . int($$ ^ time & 65536), "UNLINK", 0) or croak("File::Temp: $!") 4 0 4
172 DIR => "/tmp",
173 TEMPLATE => join(".", "", $basename, 'X' x 8),
174 SUFFIX => '.' . int($$ ^ time & ( 2 ** 16 )),
175 UNLINK => 0,
176 ) or croak("File::Temp: $!");
177  
178 ##
179 ## Save the prior autoflush value and turn it on
180 4 $self->{_autoflush} = $self->{_io_handle}->autoflush(1);
181  
182 ##
183 ## dup(2) the IO handle to a saved descriptor then redirect it to the temp file
184 4 open($self->{_saved_handle}, "+>&" . fileno($self->{_io_handle}));
185 4 open($self->{_io_handle}, "+>&" . fileno($self->{_temp_file}));
186  
187 ##
188 ## This is where diag() messages will go. Note that I haven't implemented any
189 ## TODO handling, but I put the filehandle here in case it's added later.
190 4 $tstobj->failure_output($self->{_saved_handle});
191 4 $tstobj->todo_output($self->{_saved_handle});
192 4 return $self;
193 }
194  
195 4 ▶sub _init_redirect {
Subroutine _init_redirect: called CC=1 SCAR=0.0
196 4 my $self = shift;
197 4 my $obj = shift;
198  
199 ##
200 ## Internalize the output descriptor and ensure integrity of calling sequence
201 4 $self->{_io_handle} = delete $obj->{output};
202 4 delete $self->{_saved_handle};
203 4 delete $self->{_autoflush};
204 4 $self->_cleanup_temp(1);
205 4 $self->{_read_offset} = $self->{_buffer_size} = 0;
206 4 return $self;
207 }
208  
209 4 ▶sub restore {
Subroutine restore: called CC=3 SCAR=11.2
Pod undocumented
210 4 my $self = shift;
211  
212 ##
213 ## Ensure proper calling sequence
214 croak("Output is not being redirected")
215 4 ▶ unless ( exists $self->{_io_handle} and exists $self->{_saved_handle} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_io_handle"} and exists $self->{"_saved_handle"} 0 4 4
Condition33%1/3
exists $self->{"_io_handle"} and exists $self->{"_saved_handle"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $self->{"_io_handle"} and exists $self->{"_saved_handle"}
exists $self->{"_io_handle"} exists $self->{"_saved_handle"}
Truth table33%1/3
A and B
A= exists $self->{"_io_handle"}B= exists $self->{"_saved_handle"}
ABresult
0X0
100
111
216  
217 ##
218 ## Restore the saved descriptor and restore the autoflush flag
219 4 open($self->{_io_handle}, ">&" . fileno($self->{_saved_handle}));
220 4 $self->{_io_handle}->autoflush($self->{_autoflush});
221  
222 ##
223 ## Grab any data remaining in the buffer and clean up
224 4 my $data = $self->drain_out;
225 4 close($self->{_temp_file});
226 4 $self->_cleanup_temp(1);
227 4 delete $self->{_saved_handle};
228 4 delete $self->{_io_handle};
229 4 return $data;
230 }
231  
232 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
233 0 my $self = shift;
234 0 ▶ $self->restore if exists $self->{_io_handle} and exists $self->{_saved_handle};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_io_handle"} and exists $self->{"_saved_handle"} 0 0 0
Condition0%0/3
exists $self->{"_io_handle"} and exists $self->{"_saved_handle"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_io_handle"} and exists $self->{"_saved_handle"}
exists $self->{"_io_handle"} exists $self->{"_saved_handle"}
Truth table0%0/3
A and B
A= exists $self->{"_io_handle"}B= exists $self->{"_saved_handle"}
ABresult
0X0
100
111
235 0 return $self;
236 }
237  
238 8 ▶sub drain_out {
Subroutine drain_out: called CC=2 SCAR=6.9
Pod undocumented
239 8 my $self = shift;
240 8 my $fh = $self->{_temp_file};
241 8 local ($/);
242  
243 ##
244 ## This does something similar to fstat(2)
245 8 my $cursize = ( stat $fh )[ 7 ];
246 8 ▶ if ($cursize > $self->{_buffer_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursize > $self->{"_buffer_size"}) 4 4 8
247 4 $self->{_read_offset} = $self->{_buffer_size};
248 4 $self->{_buffer_size} = $cursize
249 }
250  
251 ##
252 ## Seek to the end of the last read offset (SEEK_SET)
253 8 seek $fh, $self->{_read_offset}, 0;
254  
255 ##
256 ## Read it all into one scalar
257 8 my $curout = join '', <$fh>;
258  
259 ##
260 ## Reposition to the EOF (SEEK_END)
261 8 seek $fh, 0, 2;
262  
263 8 return $curout;
264 }
265  
266 8 ▶sub _cleanup_temp {
Subroutine _cleanup_temp: called CC=4 SCAR=14.2
267 8 my $self = shift;
268 8 my $unlink = shift;
269 8 ▶ return unless exists $self->{_temp_file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_temp_file"} 4 4 8
270 4 my $file = $self->{_temp_file}->filename;
271 4 close $self->{_temp_file};
272 4 delete $self->{_temp_file};
273 4 ▶ unlink $file if $unlink && -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if $unlink and -f $file 4 0 4
Condition33%1/3
$unlink and -f $file
!ll&&!rl&&r
004
MC/DC0%0/2
$unlink and -f $file
$unlink -f $file
Truth table33%1/3
A and B
A= $unlinkB= -f $file
ABresult
0X0
100
111
274 4 return 1;
275 }
276  
277 =pod
278  
279 =back
280  
281 =head2 TESTS
282  
283 =over
284  
285 =item B<$out-E<gt>is_output(EXPR, NAME)>
286  
287 =item B<$out-E<gt>was_output(EXPR, NAME)>
288  
289 Tests the last output buffer against EXPR. If there isn't a perfect string comparison the test fails. Pay particular
290 attention to possible newlines in the last output. If you're unsure either paste the contents of $\ to your comparitor
291 or use the C<like> method.
292  
293 The was_output method is an alias for is_output.
294  
295 =item B<$out-E<gt>isnt_output(EXPR, NAME)>
296  
297 =item B<$out-E<gt>wasnt_output(EXPR, NAME)>
298  
299 This is the inverse of is_output, the negation of the comparing EXPR to the last printed output is performed.
300  
301 The wasnt_output method is an alias for isnt_output.
302  
303 =item B<$out-E<gt>like_output(qr/STRING/, NAME)>
304  
305 This performs a test with the last output and a compiled regular expression as its first argument.
306  
307 =item B<$out-E<gt>unlike_output(qr/STRING/, NAME)>
308  
309 This performs a negated test with the last output and a compiled regular expression as its first argument.
310  
311 =item B<$out-E<gt>cmp_output(OP, EXPR, NAME)>
312  
313 This performs a comparison allowing you to pass your own perl binary operator as the first arugment (e.g., "==", "eq", etc).
314  
315 =cut
316  
317 1 ▶sub is_output {
Subroutine is_output: called CC=3 SCAR=11.3
Pod documented
318 1 my $self = shift;
319 1 ▶ my $comp = shift || return $self->do_fail(q{$obj->is_output("string")});
Condition50%1/2
shift() || (return $self->do_fail('$obj->is_output("string")'))
l!l
10
MC/DC0%0/1
shift() || (return $self->do_fail('$obj->is_output("string")'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
320 1 ▶ my $name = shift || '';
Condition50%1/2
shift() || ''
l!l
10
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
321 1 my $last = $self->drain_out;
322 1 return $tstobj->is_eq($comp, $last, $name);
323 }
324  
325 0 ▶sub was_output {
Subroutine was_output: not called CC=1 SCAR=6.9
Pod documented
326 0 goto &is_output;
327 }
328  
329 1 ▶sub isnt_output {
Subroutine isnt_output: called CC=3 SCAR=11.3
Pod documented
330 1 my $self = shift;
331 1 ▶ my $comp = shift || return $self->do_fail(q{$obj->isnt_output("string")});
Condition50%1/2
shift() || (return $self->do_fail('$obj->isnt_output("string")'))
l!l
10
MC/DC0%0/1
shift() || (return $self->do_fail('$obj->isnt_output("string")'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
332 1 ▶ my $name = shift || '';
Condition50%1/2
shift() || ''
l!l
10
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
333 1 my $last = $self->drain_out;
334 1 return $tstobj->isnt_eq($comp, $last, $name);
335 }
336  
337 0 ▶sub wasnt_output {
Subroutine wasnt_output: not called CC=1 SCAR=6.9
Pod documented
338 0 goto &isnt_output;
339 }
340  
341 1 ▶sub like_output {
Subroutine like_output: called CC=3 SCAR=11.3
Pod documented
342 1 my $self = shift;
343 1 ▶ my $regex = shift || return $self->do_fail(q{$obj->like_output(qr/regex/)});
Condition50%1/2
shift() || (return $self->do_fail('$obj->like_output(qr/regex/)'))
l!l
10
MC/DC0%0/1
shift() || (return $self->do_fail('$obj->like_output(qr/regex/)'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
344 1 ▶ my $name = shift || '';
Condition50%1/2
shift() || ''
l!l
10
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
345 1 my $last = $self->drain_out;
346 1 return $tstobj->like($last, $regex, $name);
347 }
348  
349 1 ▶sub unlike_output {
Subroutine unlike_output: called CC=3 SCAR=11.3
Pod documented
350 1 my $self = shift;
351 1 ▶ my $regex = shift || return $self->do_fail(q{$obj->unlike_output(qr/regex/)});
Condition50%1/2
shift() || (return $self->do_fail('$obj->unlike_output(qr/regex/)'))
l!l
10
MC/DC0%0/1
shift() || (return $self->do_fail('$obj->unlike_output(qr/regex/)'))
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
352 1 ▶ my $name = shift || '';
Condition50%1/2
shift() || ''
l!l
10
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
353 1 my $last = $self->drain_out;
354 1 return $tstobj->unlike($last, $regex, $name);
355 }
356  
357 0 ▶sub cmp_output {
Subroutine cmp_output: not called CC=3 SCAR=24.8
Pod documented
358 0 my $self = shift;
359 0 ▶ @_ > 1 or return $self->do_fail(q{$obj->cmp_ok(...)});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 or return $self->do_fail("\$obj->cmp_ok(...)") 0 0 0
360 0 my ($type, $that, $name) = @_;
361 0 ▶ $name ||= '';
Condition0%0/2
$name ||= ""
l!l
00
MC/DC0%0/1
$name ||= ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
362 0 my $last = $self->drain_out;
363 0 return $tstobj->cmp_ok($last, $type, $that, $name);
364 }
365  
366 0 ▶sub do_fail {
Subroutine do_fail: not called CC=1 SCAR=6.9
Pod undocumented
367 0 return $tstobj->ok(0, @_);
368 }
369  
370 =item B<$out-E<gt>diag(@messages)>
371  
372 Prints a diagnostic message
373  
374 =cut
375  
376 0 ▶sub diag {
Subroutine diag: not called CC=1 SCAR=6.9
Pod documented
377 0 my $self = shift;
378 0 return $tstobj->diag(@_);
379 }
380  
381 5 1;
382  
383 __END__
↑ Summary