File Coverage

blib/lib/Devel/Command/Tdump.pm
Criterion Covered Total %
statement 44 46 95.6
branch 16 18 88.8
condition 5 5 100.0
subroutine 6 7 85.7
pod 1 5 20.0
total 72 81 88.8


line stmt bran cond sub pod time code
1             package Devel::Command::Tdump;
2 5     5   119112 use strict;
  5         12  
  5         195  
3 5     5   29 use base qw(Devel::Command);
  5         9  
  5         4466  
4              
5             $Devel::Command::Tdump::VERSION = "1.1";
6             my %test_names = map { $_ => 1 } get_test_names();
7              
8             # Preload Test::More
9             sub afterinit {
10 0     0 1 0 push @DB::typeahead, "use Test::More qw(no_plan)";
11             }
12              
13             # Dump just the tests
14             sub command {
15 10     10 0 24374 my ($cmd) = @_;
16 10         15 my @tests;
17             my $tfh;
18              
19 10         30 my(undef, $outfile) = split(/\s+/,$cmd);
20 10 100       28 $outfile = "unnamed_test.t" unless defined $outfile;;
21              
22 10   100     29 my $first_test = $DB::_first_test || 0;
23              
24 10         46 print DB::OUT "Recording tests for this session in $outfile ...";
25 10 50       929 unless (open $tfh, ">$outfile") {
26 0         0 print DB::OUT " can't write history: $!\n";
27             }
28             else {
29 10         13 my @output;
30 10         14 my $test_count = 0;
31 10         25 my @lines = @DB::hist;
32              
33 10         29 while (@lines) {
34 45         59 my $line = shift @lines;
35 45         57 my $forced_capture = 0;
36             # Following lines are comments?
37 45 100       82 if (@lines) {
38 36         105 while ($lines[0] =~ /^\s*#/) {
39             # Yes. Print and discard.
40 11         18 push @output, $lines[0],"\n";
41 11         13 $forced_capture = 1;
42 11         34 shift @lines;
43             }
44             }
45             # skip this one unless we are supposed to keep it
46             # or it's a test
47 45         47 my $is_test;
48 45 100 100     131 next unless $forced_capture or
49             ($is_test = is_a_test($line, \%test_names));
50 15 100       29 $test_count++ if $is_test;
51 15 50       36 $line = "$line;" unless $line =~ /;$/;
52 15         45 push @output, "$line\n";
53             }
54 10         32 unshift @output, "use Test::More tests=>$test_count;\n";
55 10         82 print $tfh @output;
56 10         400 close $tfh;
57 10 100       28 my $s = ($test_count == 1 ? "" : "s");
58 10         44 print DB::OUT " done ($test_count test$s).\n";
59 10         88 $DB::_first_test = $#DB::hist;
60             }
61             }
62              
63             # Get the names defined in Test::More that are the names of tests
64             # and save them in a debugger global.
65             sub get_test_names {
66 6     6 0 173 my @names = keys %Test::More::;
67 6         29 grep { is_a_sub($_) } @names;
  317         487  
68             }
69              
70              
71             # Returns true if this is a sub in Test::More, false otherwise
72             sub is_a_sub {
73 321     321 0 463 local $_ = shift;
74 321 100       11592 (!/^_/) and eval "defined &Test::More::$_";
75             }
76              
77             # Returns true if this line of history is a Test::More test.
78             sub is_a_test {
79 41     41 0 69 local $_ = shift;
80 41         45 my $map = shift;
81 41 100       273 if (my($possible, $paren) = /^\s*(\w+)\(/) {
82 12         61 return $map->{$possible};
83             }
84             }
85              
86             1;
87              
88             __END__