Doctest.pm

filter: Statementstmt 99.2% 139 / 140 Branchbran 80.5% 29 / 36 Conditioncond 70.8% 17 / 24 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 96.4% 27 / 28 Podpod 83.3% 5 / 6 totaltotal 90.2% 223 / 247 CC 33 SCAR 19.6
CC
33
Cov
92%
verbatim
16.5
test
10.0
runtests
4.0
CRAP 7.1
↑ Summary Doctest/Example.pm »
1 package Test::Doctest;
2  
3 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper 'Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent 'Pod::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.02';
12 1 our @EXPORT = qw(run runtests);
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec::Functions 'devnull';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Module::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Test::Doctest - extract and evaluate tests from pod fragments
22  
23 =head1 SYNOPSIS
24  
25 perl -Ilib -MTest::Doctest -e run Some/Module.pm
26  
27 - or -
28  
29 perl -Ilib -MTest::Doctest -e run Some::Module
30  
31 - or -
32  
33 use Test::Doctest;
34 runtests($filepath);
35  
36 - or -
37  
38 use Test::Doctest;
39 my $p = Test::Doctest->new;
40 $p->parse_from_filehandle(\*STDIN);
41 $p->test;
42  
43 =head1 DESCRIPTION
44  
45 B<run> and B<runtests> uses B<Pod::Parser> to extract pod text from the files
46 specified, evaluates each line begining with a prompt (>>>),
47 and finally compares the results with the expected output using
48 B<is_eq> from B<Test::Builder>.
49  
50 =head1 EXAMPLES
51  
52 >>> 1 + 1
53 2
54  
55 >>> my @a = qw(2 3 4)
56 3
57  
58 >>> use Pod::Parser;
59 >>> my $p = Pod::Parser->new;
60 >>> ref $p;
61 'Pod::Parser'
62  
63 >>> $a = 10
64 10
65  
66 >>> $a *= 2
67 20
68  
69 See more examples in L<Test::Doctest::Example>.
70  
71 =head1 EXPORTS
72  
73 =head2 B<run()>
74  
75 Extract and run tests from pod for each file in @ARGV.
76  
77 =head2 B<runtests()>
78  
79 Extract and run tests from pod for each file argument.
80  
81 =begin runtests
82  
83 >>> use Test::Doctest
84 >>> runtests
85 0
86  
87 =end runtests
88  
89 =cut
90  
91 2 ▶sub runtests {
Subroutine runtests: called CC=4 SCAR=13.9
92 2 my ($total, $success, @tests) = (0, 0);
93 2 my $test = Test::Doctest::Builder->new;
94  
95 2 foreach (@_) {
96 2 my $t = Test::Doctest->new;
97 2 $t->parse_from_file($_);
98 2 $total += @{$t->{tests}};
99 2 push @tests, $t;
100 }
101  
102 2 ▶ $test->plan(tests => $total) unless $test->has_plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $test->has_plan 1 1 2
103  
104 2 foreach (@tests) {
105 2 $success += $_->test == @{$_->{tests}};
106 }
107  
108 2 return $success;
109 }
110  
111 0 ▶sub run { runtests @ARGV }
Subroutine run: not called CC=1 SCAR=6.9
112  
113 2 ▶sub parse_from_file {
Subroutine parse_from_file: called CC=3 SCAR=11.1
Pod undocumented
114 2 my $self = shift;
115 2 my $module = shift;
116 2 ▶ $module = ($module =~ s{::}{/}gr) . '.pm' unless $module =~ /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\.pm$/u 0 2 2
117 2 require $module;
118 2 my $info = Module::Metadata->new_from_module($module);
119 2 ▶ $self->{package} = $info->{packages}->[0] if @{$info->{packages}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$info->{"packages"};} 2 0 2
120 2 return $self->SUPER::parse_from_file($info->{filename}, devnull);
121 }
122  
123 =head1 METHODS
124  
125 =head2 B<initialize()>
126  
127 Initialize this B<Test::Doctest> pod parser. This method is
128 not typically called directly, but rather, is called by
129 B<Pod::Parser::new> when creating a new parser.
130  
131 =begin initialize
132  
133 >>> my $t = Test::Doctest->new
134 >>> @{$t->{tests}}
135 0
136  
137 =end initialize
138  
139 =cut
140  
141 8 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
142 8 my ($self) = @_;
143 8 $self->SUPER::initialize;
144 8 $self->{tests} = [];
145 }
146  
147 =head2 B<command()>
148  
149 Override B<Pod::Parser::command> to save the name of the
150 current section which is used to name the tests.
151  
152 =begin command
153  
154 >>> my $t = Test::Doctest->new
155 >>> $t->command('head1', "EXAMPLES\nthese are examples", 1)
156 >>> $t->{name}
157 'EXAMPLES'
158  
159 =end command
160  
161 =cut
162  
163 45 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
164 45 my ($self, $cmd, $par, $line) = @_;
165 45 $self->{name} = (split /(?:\r|\n|\r\n)/, $par, 2)[0];
166 }
167  
168 =head2 B<textblock()>
169  
170 Override B<Pod::Parser::textblock> to ignore normal blocks of pod text.
171  
172 =begin textblock
173  
174 >>> my $t = Test::Doctest->new
175 >>> not defined $t->textblock
176 1
177  
178 =end textblock
179  
180 =cut
181  
182 23 ▶sub textblock { }
Subroutine textblock: called CC=1 SCAR=0.0
Pod documented
183  
184 =head2 B<verbatim()>
185  
186 Override B<Pod::Parser::verbatim> to search verbatim paragraphs for
187 doctest code blocks. Each block found, along with information about
188 its location in the file and its expected output is appended to the
189 list of tests to be executed.
190  
191 =begin verbatim
192  
193 >>> my $t = Test::Doctest->new
194 >>> $t->verbatim(" >>> 1+1\n 2", 1)
195 1
196  
197 =end verbatim
198  
199 =begin verbatim no prompt
200  
201 >>> my $t = Test::Doctest->new
202 >>> $t->verbatim("abc", 1)
203 0
204  
205 =end verbatim
206  
207 =cut
208  
209  
210 1 package Test::Doctest::Expect {
211 1 ▶ use Test::Deep::NoTest ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
212 }
213  
214  
215 38 ▶sub verbatim {
Subroutine verbatim: called CC=16 SCAR=28.1
Pod documented
216 38 my ($self, $par, $line) = @_;
217 38 ▶ my $name = $self->{name} ? $self->{name} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'name'} ? : 36 2 38
218 38 ▶ my $file = $self->input_file ? $self->input_file : 'stdin';
Branch: 1 branch
BranchTrueFalseTotal
$self->input_file ? : 35 3 38
219  
220 38 my @lines = split /(?:\r|\n|\r\n)/, $par;
221  
222 38 my (@tests, $code, $expect);
223 38 foreach (@lines) {
224 94 ▶ if (/^\s+(>{3}|\.{3}|\$)\s+(.+)/ && ($1 ne '...' || $code && @$code)) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s+(>{3}|\.{3}|\$)\s+(.+)/u and $1 ne '...' || $code && @$code) { } 48 46 94
elsif (/^\s*(.+)/u and $code and @$code) { } 34 24 58
Condition55%5/9
/^\s+(>{3}|\.{3}|\$)\s+(.+)/u and $1 ne "..." || $code && @$code
!ll&&!rl&&r
46048
$1 ne "..." or $code and @$code
l!l&&r!l&&!r
4620
$code and @$code
!ll&&!rl&&r
002
MC/DC25%1/4
/^\s+(>{3}|\.{3}|\$)\s+(.+)/u and $1 ne "..." || $code && @$code
/^\s+(>{3}|\.{3}|\$)\s+(.+)/u $1 ne "..." $code @$code
Truth table60%3/5
A and B or C and D
A= /^\s+(>{3}|\.{3}|\$)\s+(.+)/uB= $1 ne "..."C= $codeD= @$code
ABCDresult
0XXX0
100X0
10100
11XX1
10111
225 48 ▶ if (!defined $expect || @$expect) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $expect or @$expect) 31 17 48
Condition100%3/3
not defined $expect or @$expect
l!l&&r!l&&!r
30117
MC/DC100%2/2
not defined $expect or @$expect
not defined $expect @$expect
Truth table100%3/3
not(A) or B
A= not defined $expectB= @$expect
ABresult
1X1
011
000
226 31 push(
227 @tests,
228 {
229 code => $code = [],
230 expect => $expect = []
231 }
232 );
233 }
234 # capture code
235 48 ▶ if ($1 eq '...') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '...') { } 2 46 48
236 # capture multiline code
237 2 $code->[$#$code] .= $2;
238 }
239 else {
240 46 push @$code, $2;
241 }
242 }
243 ▶ elsif (/^\s*(.+)/ && $code && @$code) {
Condition66%4/6
/^\s*(.+)/u and $code and @$code
!ll&&!rl&&r
12034
/^\s*(.+)/u and $code
!ll&&!rl&&r
01234
MC/DC33%1/3
/^\s*(.+)/u and $code and @$code
/^\s*(.+)/u $code @$code
Truth table50%2/4
A and B and C
A= /^\s*(.+)/uB= $codeC= @$code
ABCresult
0XX0
10X0
1100
1111
244 # on first non-code line, with valid code accumlated
245 34 push(@$expect, $1);
246 }
247 }
248  
249 38 foreach (@tests) {
250 31 my $expect = do {
251 package Test::Doctest::Expect;
252 31 eval(join('', @{$_->{expect}}));
253 };
254  
255 31 ▶ die "$@\t$file, line $line" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 31 31
256 31 push @{$self->{tests}}, [$name, $file, $line, $expect, @{$_->{code}}];
257 }
258 38 return @{$self->{tests}};
259 }
260  
261 =head2 B<test()>
262  
263 Evaluates each test discovered via parsing and compares the results
264 with the expected output using B<Test::Builder::is_eq>.
265  
266 =begin test
267  
268 >>> my $t = Test::Doctest->new
269 >>> $t->test
270 0
271  
272 >>> $t->command('begin', 'test', 1)
273 >>> $t->verbatim(" >>> 1+1\n 2", 2)
274 >>> @{$t->{tests}}
275 1
276  
277 =end test
278  
279 =cut
280  
281 1 package Test::Doctest::Builder {
282 1 ▶ use Test::Deep::NoTest qw(cmp_details deep_diag);
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 1 ▶ use base 'Test::Builder';
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 1 our $Test;
287 5 ▶ sub new { $Test ||= shift->create }
Subroutine new: called CC=2 SCAR=7.2
Condition66%2/3
$Test::Doctest::Builder::Test ||= (shift())->create
l!l&&r!l&&!r
410
MC/DC0%0/2
$Test::Doctest::Builder::Test ||= (shift())->create
$Test::Doctest::Builder::Test (shift())->create
Truth table66%2/3
A ||= B
A= $Test::Doctest::Builder::TestB= (shift())->create
ABresult
1X1
011
000
288  
289 29 ▶ sub is_eq {
Subroutine is_eq: called CC=3 SCAR=11.0
290 29 my $self = shift;
291 29 my ($got, $expected, $test_name) = @_;
292 29 ▶ if (ref $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expected) 6 23 29
293 6 my ($ok, $stack) = cmp_details($got, $expected);
294 6 ▶ $self->diag(deep_diag($stack)) unless $self->ok($ok, $test_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ok($ok, $test_name) 0 6 6
295 6 return;
296 }
297 23 $self->SUPER::is_eq(@_);
298 }
299 }
300  
301 1 our ($Result, $Check);
302  
303 3 ▶sub test {
Subroutine test: called CC=10 SCAR=23.0
Pod documented
304 3 my ($self) = @_;
305 3 my $tests = $self->{tests};
306  
307 3 my $test = Test::Doctest::Builder->new;
308 3 ▶ $test->plan(tests => scalar @$tests) unless $test->has_plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $test->has_plan 0 3 3
309  
310 3 my (@grouped, $current_group);
311 3 foreach (@$tests) {
312 29 ▶ if (!defined($current_group) || $_->[0] ne $current_group->[0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $current_group or $_->[0] ne $current_group->[0][0]) 19 10 29
Condition100%3/3
not defined $current_group or $_->[0] ne $current_group->[0][0]
l!l&&r!l&&!r
21710
MC/DC100%2/2
not defined $current_group or $_->[0] ne $current_group->[0][0]
not defined $current_group $_->[0] ne $current_group->[0][0]
Truth table100%3/3
not(A) or B
A= not defined $current_groupB= $_->[0] ne $current_group->[0][0]
ABresult
1X1
011
000
313 19 push(@grouped, $current_group = []);
314 }
315 29 push(@$current_group, $_);
316 }
317  
318 3 my $run = 0;
319 3 foreach my $group (@grouped) {
320 19 my (@group_code, @checks);
321 19 ▶ push(@group_code, "package $self->{package}") if $self->{package};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"package"} 19 0 19
322 19 ▶ for (my $i = 0 ; $i <= $#$group ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $group->$#*) 29 19 48
323 29 my ($name, $file, $line, $expect, @code) = @{$group->[$i]};
324 29 ▶ my $outof =
Branch: 1 branch
BranchTrueFalseTotal
$group->$#* > 0 ? : 16 13 29
325 $#$group > 0 ? sprintf("%d/%d", $i + 1, $#$group + 1) : q();
326 29 my $test_name = "$name $outof ($file, $line)";
327 29 ▶ push(@checks, sub { $test->is_eq($Result, $expect, $test_name) });
Subroutine __ANON__: called CC=1 SCAR=0.0
328 29 my $result_line = pop(@code);
329 29 push(
330 @group_code,
331 @code,
332 "\$Test::Doctest::Result = $result_line",
333 "\$Test::Doctest::Check->()"
334 );
335 }
336 19 ▶ local $Check = sub { shift(@checks)->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
337 1 ▶ eval join(";", @group_code);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
338 19 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 19 19
339 19 $run += @$group;
340 }
341  
342 3 return $run;
343 }
344  
345 1 1;
346  
347 __END__
↑ Summary Doctest/Example.pm »