blib/lib/Test/Group/Tester.pm

filter: Statementstmt 84.5% 115 / 136 Branchbran 70.3% 45 / 64 Conditioncond 52.9% 9 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 76.5% 186 / 243 CC 43 SCAR 32.6
CC
43
Cov
78%
testscript_ok
42.1
fail_diag
14.6
want_test
6.0
CRAP 26.2
↑ Summary blib/lib/Test/Group.pm »
1 package Test::Group::Tester;
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Test::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Test::Group::Tester - Test Test::Group extensions
13  
14 =head1 VERSION
15  
16 Test::Group::Tester version 0.01
17  
18 =cut
19  
20 23 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 23 $VERSION = '0.01';
22  
23 =head1 SYNOPSIS
24  
25 =for tests "synopsis" begin
26  
27 use Test::More tests => 1;
28 use Test::Group::Tester;
29  
30 testscript_ok('#line '.(__LINE__+1)."\n".<<'EOSCRIPT', 3);
31  
32 use Test::More;
33 use Test::Group;
34  
35 # Test a passing test group
36 want_test('pass', "this_should_pass");
37 test this_should_pass => sub {
38 ok 1, "1 is true";
39 ok 2, "2 is true";
40 };
41  
42 # Test a failing test group
43 want_test('fail', "this_should_fail",
44 fail_diag("0 is true", 0, __LINE__+5),
45 fail_diag("this_should_fail", 1, __LINE__+5),
46 );
47 test this_should_fail => sub {
48 ok 1, "1 is true";
49 ok 0, "0 is true";
50 };
51  
52 # Test a skipped test group
53 want_test('skip', "just because I can");
54 skip_next_test("just because I can");
55 test this_should_be_skipped => sub {
56 ok 0;
57 };
58  
59 EOSCRIPT
60  
61 =for tests "synopsis" end
62  
63 =head1 DESCRIPTION
64  
65 Test the behavior of a L<Test::Harness> compatible test script, by
66 spawning an external process to run the script and capturing its STDOUT
67 and STDERR. Includes support for matching the failed test diagnostic
68 messages produced by L<Test::Group> and L<Test::Builder>.
69  
70 Useful when writing tests for L<Test::Group> extension modules, see
71 L<Test::Group::Extending>.
72  
73 This module is used within the test suite of L<Test::Group> itself, so
74 several usage examples can be found by searching for C<testscript_ok> in
75 the files in L<Test::Group>'s F<t> subdirectory.
76  
77 =head1 FUNCTIONS EXPORTED BY DEFAULT
78  
79 =cut
80  
81 23 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 23 ▶use vars qw(@ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
83 23 @ISA = qw(Exporter);
84 23 @EXPORT = qw(testscript_ok);
85 23 @EXPORT_OK = qw(want_test fail_diag);
86  
87 =over
88  
89 =item I<testscript_ok($source, $plan, $name)>
90  
91 A test predicate for checking that a test script acts as expected. Runs
92 the script capturing STDOUT and STDERR and fails if anything unexpected
93 happens.
94  
95 The expected behavior of the script is defined by calling want_test()
96 from within the script, just before running each test.
97  
98 I<$source> is the body of the test script, as a single multi-line string.
99  
100 I<$plan> is the number of tests that the test script will run.
101  
102 I<$name> is a name for this test.
103  
104 Some code will be prepended to I<$source>, to make the want_test() and
105 fail_diag() functions available and to set the test plan to I<$plan>.
106  
107 Tip: include a C<#line> directive in your script source as shown in the
108 SYNOPSIS above, so that the reported line numbers for problems will point
109 to the correct line in your source file.
110  
111 =cut
112  
113 15 ▶sub testscript_ok {
Subroutine testscript_ok: called CC=29 SCAR=37.4
Pod documented
114 15 my ($source, $plan, $name) = @_;
115 15 ▶ $plan =~ /^\d+$/ or croak "non-numeric plan [$plan]";
Branch: 1 branch
BranchTrueFalseTotal
$plan =~ /^\d+$/ or croak("non-numeric plan [$plan]") 15 0 15
116 15 ▶ $name ||= 'testscript_ok';
Condition100%2/2
$name ||= "testscript_ok"
l!l
510
MC/DC100%1/1
$name ||= "testscript_ok"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
117  
118 15 local $Test::Builder::Level = $Test::Builder::Level + 1;
119  
120 15 my $script_source = <<EOSCRIPT;
121 use Test::Builder;
122 Test::Builder->new->plan( tests => $plan );
123 use Test::Group::Tester qw(want_test fail_diag);
124  
125 $source
126  
127 print STDERR "\nXXtestscript_under_test_endXX\n";
128 EOSCRIPT
129  
130 my $perl = Test::Cmd->new
131 (prog => join(' ', $Config{perlpath},
132 15 (map { ("-I", $_) } @INC), '-'),
133 workdir => '');
134 15 ▶ $perl or croak "$name Test::Cmd failed";
Branch: 1 branch
BranchTrueFalseTotal
$perl or croak("$name Test::Cmd failed") 15 0 15
135  
136 15 my $status = $perl->run(stdin => $script_source);
137 15 my $stdout = $perl->stdout();
138 15 my $stderr = $perl->stderr();
139 15 $stderr =~ s/\nXXtestscript_under_test_endXX\n.*//s;
140  
141 15 my $ok = 1;
142 15 my $expect_failed_tests = 0;
143 15 my @fail;
144  
145 15 my @errbits = split /XXwant_test_markerXX/, $stderr, -1;
146 15 my $preamble = shift @errbits;
147 15 ▶ if (length $preamble) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $preamble) 0 15 15
148 0 $ok = 0;
149 0 push @fail, "STDERR output before first test:";
150 0 push @fail, " $preamble";
151 }
152 15 my $rantests = @errbits;
153 15 ▶ unless ($rantests == $plan) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rantests == $plan) 0 15 15
154 0 $ok = 0;
155 0 push @fail, "planned $plan tests, script ran $rantests";
156 }
157  
158 15 my $want_out = "1..$plan\n";
159 15 foreach my $i (0 .. $#errbits) {
160 97 my $e = $errbits[$i];
161 97 ▶ unless ($e =~ s/^ want_test:([,\w]+)\n//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($e =~ s/^ want_test:([,\w]+)\n//) 0 97 97
162 0 $ok = 0;
163 0 push @fail, "missing header in section [$e]";
164 0 next;
165 }
166 my ($call_line, $type, $name, @diag) =
167 97 ▶ map { $_ eq 'undef' ? undef : pack 'H*', $_} split /,/, $1, -1;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'undef' ? : 1 651 652
168  
169 97 ▶ my $out = ($type eq 'fail' ? 'not ' : '') . 'ok ' . ($i+1);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'fail' ? : 56 41 97
170 97 ▶ if ($type eq 'skip') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'skip') { } 3 94 97
171 3 $out .= " # skip";
172 3 ▶ defined $name and $out .= " $name";
Branch: 1 branch
BranchTrueFalseTotal
defined $name and $out .= " $name" 3 0 3
173 } else {
174 94 ▶ defined $name and $out .= " - $name";
Branch: 1 branch
BranchTrueFalseTotal
defined $name and $out .= " - $name" 93 1 94
175 }
176 97 $want_out .= "$out\n";
177  
178 97 ▶ ++$expect_failed_tests if $type eq 'fail';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "fail" 56 41 97
179  
180 97 $e =~ s/\n$//;
181 97 my @lines = split /\n/, $e, -1;
182 97 my @mismatch;
183 97 foreach my $i (0 .. $#lines) {
184 176 ▶ last if @mismatch;
Branch: 1 branch
BranchTrueFalseTotal
if @mismatch 45 131 176
185 131 my $line = $lines[$i];
186 131 my $want = $diag[$i];
187 131 ▶ if (!defined $want) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $want) { } 0 131 131
elsif ($want =~ s[^/][]) { } 34 97 131
elsif ($line ne $want) { } 29 68 97
188 0 push @mismatch, "unmatched line '$line'";
189 } elsif ($want =~ s{^/}{}) {
190 34 ▶ unless ($line =~ /$want/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /$want/) 16 18 34
191 16 push @mismatch,
192 "line '$line'",
193 "doesn't match /$want/";
194 }
195 } elsif ($line ne $want) {
196 29 push @mismatch,
197 "line '$line'",
198 "isnt '$want'";
199 }
200 }
201 97 ▶ if (@lines < @diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines < @diag) 0 97 97
202 0 push @mismatch, "too few lines";
203 }
204 97 ▶ if (@mismatch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mismatch) 45 52 97
205 45 $ok = 0;
206 45 my $msg = "STDERR MISMATCH";
207 45 ▶ defined $name and $msg .= " FOR $name";
Branch: 1 branch
BranchTrueFalseTotal
defined $name and $msg .= " FOR $name" 45 0 45
208 45 $msg .= " (line $call_line)";
209 push @fail, "$msg...",
210 " got stderr:",
211 322 map({" [$_]"} @lines),
212 " want stderr:",
213 293 map({" [$_]"} @diag),
214 " mismatch details:",
215 45 map({" $_"} @mismatch),
216 ;
217
218 }
219 }
220  
221 15 ▶ if ($stdout ne $want_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout ne $want_out) 0 15 15
222 0 $ok = 0;
223 0 push @fail, "want stdout: $want_out",
224 "got stdout: $stdout";
225 }
226  
227 15 ▶ if ($expect_failed_tests and not $status) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expect_failed_tests and not $status) { } 0 15 15
elsif ($status and not $expect_failed_tests) { } 0 15 15
Condition33%1/3
$expect_failed_tests and not $status
!ll&&!rl&&r
0150
MC/DC0%0/2
$expect_failed_tests and not $status
$expect_failed_tests not $status
Truth table33%1/3
A and not(B)
A= $expect_failed_testsB= not $status
ABresult
0X0
100
111
228 0 $ok = 0;
229 0 push @fail, "test script failed to fail";
230 ▶ } elsif ($status and not $expect_failed_tests) {
Condition33%1/3
$status and not $expect_failed_tests
!ll&&!rl&&r
0150
MC/DC0%0/2
$status and not $expect_failed_tests
$status not $expect_failed_tests
Truth table33%1/3
A and not(B)
A= $statusB= not $expect_failed_tests
ABresult
0X0
100
111
231 0 $ok = 0;
232 0 push @fail, "test script unexpectedly failed";
233 }
234  
235 15 my $Test = Test::Builder->new;
236 15 $Test->ok($ok, $name);
237 15 foreach my $fail (@fail) {
238 885 $Test->diag("* $fail");
239 }
240 }
241  
242 =back
243  
244 =head1 TEST SCRIPT FUNCTIONS
245  
246 The following functions are for use from within the script under test.
247 They are not exported by default.
248  
249 =over
250  
251 =item I<want_test($type, $name, @diag)>
252  
253 Declares that the next test will pass or fail or be skipped according to
254 I<$type>, will have name I<$name> and will produce the diagnostic output
255 lines listed in I<@diag>.
256  
257 I<$type> must be one of the strings 'pass', 'fail', 'skip'. I<$name>
258 can be undef for a test without a name. The elements of I<@diag> can
259 be strings for an exact match, or regular expressions prefixed with
260 C</> or compiled with C<qr//>.
261  
262 Note that diagnostic lines consist of a hash character followed by a
263 space and then the diagnostic message. The strings and patterns passed
264 to want_test() must include this prefix.
265  
266 =cut
267  
268 97 ▶sub want_test {
Subroutine want_test: called CC=6 SCAR=18.0
Pod documented
269 97 my ($type, $name, @diag) = @_;
270 97 my $call_line = (caller)[2];
271  
272 97 ▶ $type =~ /^(pass|fail|skip)\z/i or croak
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^(pass|fail|skip)\z/i or croak("want_test type=[$type], need pass|fail|skip") 97 0 97
273 "want_test type=[$type], need pass|fail|skip";
274 97 $type = lc $1;
275  
276 # flatten diags to strings
277 97 foreach my $diag (@diag) {
278 361 ▶ ref $diag eq 'Regexp' and $diag = "/$diag";
Branch: 1 branch
BranchTrueFalseTotal
ref $diag eq "Regexp" and $diag = "/$diag" 29 332 361
279 361 ▶ ref $diag and croak "unexpected reference diag [$diag] in want_test";
Branch: 1 branch
BranchTrueFalseTotal
ref $diag and croak("unexpected reference diag [$diag] in want_test") 0 361 361
280 }
281  
282 97 ▶ my @args = map {defined $_ ? unpack('H*', $_) : 'undef'}
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 651 1 652
283 $call_line, $type, $name, @diag;
284 97 print STDERR 'XXwant_test_markerXX want_test:', join(',', @args), "\n";
285 }
286  
287 =item I<fail_diag($test_name [,$from_test_builder] [,$line] [,$file])>
288  
289 Call only in a list context, and pass the results to want_test() as
290 diagnostic line patterns.
291  
292 Returns the diagnostic line pattern(s) to match output from a failed
293 test. I<$test_name> is the name of the test, or undef for a nameless
294 test. I<$line> should be defined only if a file and line diagnostic
295 is expected, and should give the expected line number. I<$file> is
296 the filename for the failed test diagnostic, it defaults to the
297 current file.
298  
299 C<$from_test_builder> should be true if L<Test::Builder> will produce
300 the diagnostic, false if the diagnostic will come from L<Test::Group>.
301 The expected text will be adjusted according to the version of
302 L<Test::Builder> or L<Test::Group> in use.
303  
304 =cut
305  
306 145 ▶sub fail_diag {
Subroutine fail_diag: called CC=10 SCAR=26.8
Pod documented
307 145 ▶ wantarray or croak "fail_diag needs a list context";
Branch: 1 branch
BranchTrueFalseTotal
wantarray or croak("fail_diag needs a list context") 145 0 145
308  
309 145 my ($test_name, $from_test_builder, $line, $file) = @_;
310 145 ▶ $file ||= (caller)[1];
Condition33%1/3
$file ||= (caller)[1]
l!l&&r!l&&!r
01450
MC/DC0%0/2
$file ||= (caller)[1]
$file (caller)[1]
Truth table33%1/3
A ||= B
A= $fileB= (caller)[1]
ABresult
1X1
011
000
311  
312 145 my @diag;
313  
314 145 ▶ if ($from_test_builder and $ENV{HARNESS_ACTIVE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_test_builder and $ENV{"HARNESS_ACTIVE"}) 56 89 145
Condition66%2/3
$from_test_builder and $ENV{"HARNESS_ACTIVE"}
!ll&&!rl&&r
89056
MC/DC50%1/2
$from_test_builder and $ENV{"HARNESS_ACTIVE"}
$from_test_builder $ENV{"HARNESS_ACTIVE"}
Truth table66%2/3
A and B
A= $from_test_builderB= $ENV{"HARNESS_ACTIVE"}
ABresult
0X0
100
111
315 # Test::Builder adds a blank diag line for a failed test
316 # if HARNESS_ACTIVE is set.
317 56 push @diag, '';
318 }
319  
320 145 ▶ if ($from_test_builder and $Test::Builder::VERSION <= 0.30) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_test_builder and $Test::Builder::VERSION <= 0.3) { } 0 145 145
Condition66%2/3
$from_test_builder and $Test::Builder::VERSION <= "0.3"
!ll&&!rl&&r
89560
MC/DC0%0/2
$from_test_builder and $Test::Builder::VERSION <= "0.3"
$from_test_builder $Test::Builder::VERSION <= "0.3"
Truth table66%2/3
A and B
A= $from_test_builderB= $Test::Builder::VERSION <= "0.3"
ABresult
0X0
100
111
321 0 my $diag = "# Failed test";
322 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
323 0 $diag .= " ($file at line $line)";
324 }
325 0 push @diag, $diag;
326 } else {
327 145 ▶ if (defined $test_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $test_name) { } 145 0 145
328 145 push @diag, "# Failed test '$test_name'";
329 } else {
330 0 push @diag, "# Failed test";
331 }
332 145 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 105 40 145
333 105 my $qm = quotemeta $file;
334 105 push @diag, "/^\\#\\s+(at $qm|in $qm at) line $line\\.?\\s*\$";
335 }
336 }
337  
338 145 return @diag;
339 }
340  
341 =back
342  
343 =head1 AUTHORS
344  
345 Nick Cleaton <ncleaton@cpan.org>
346  
347 Dominique Quatravaux <domq@cpan.org>
348  
349 =head1 LICENSE
350  
351 Copyright (c) 2009 by Nick Cleaton and Dominique Quatravaux
352  
353 This library is free software; you can redistribute it and/or modify
354 it under the same terms as Perl itself, either Perl version 5.8.1 or,
355 at your option, any later version of Perl 5 you may have available.
356  
357 =cut
358  
359 23 1;
↑ Summary blib/lib/Test/Group.pm »