File Coverage

inc/Test/LeakTrace.pm
Criterion Covered Total %
statement 48 63 76.1
branch 7 16 43.7
condition 2 3 66.6
subroutine 9 13 69.2
pod 6 6 100.0
total 72 101 71.2


line stmt bran cond sub pod time code
1             #line 1
2             package Test::LeakTrace;
3 1     1   691  
  1         5  
  1         41  
4 1     1   7 use 5.008_001;
  1         2  
  1         44  
5 1     1   6 use strict;
  1         2  
  1         55  
6             use warnings;
7              
8             our $VERSION = '0.14';
9 1     1   5  
  1         2  
  1         42  
10             use XSLoader;
11             XSLoader::load(__PACKAGE__, $VERSION);
12 1     1   1286  
  1         570  
  1         8  
13             use Test::Builder::Module;
14             our @ISA = qw(Test::Builder::Module);
15 1     1   44  
  1         1  
  1         905  
16             use Exporter qw(import); # use Exporter::import for backward compatibility
17             our @EXPORT = qw(
18             leaktrace leaked_refs leaked_info leaked_count
19             no_leaks_ok leaks_cmp_ok
20             count_sv
21             );
22              
23             our %EXPORT_TAGS = (
24             all => \@EXPORT,
25             test => [qw(no_leaks_ok leaks_cmp_ok)],
26             util => [qw(leaktrace leaked_refs leaked_info leaked_count count_sv)],
27             );
28              
29              
30 2     2   8 sub _do_leaktrace{
31             my($block, $name, $need_stateinfo, $mode) = @_;
32 2 50 66     19  
33 0         0 if(!defined($mode) && !defined wantarray){
34             warnings::warnif void => "Useless use of $name() in void context";
35             }
36 2 100       16  
37 1         2 if($name eq 'leaked_count') {
38 1         1086 my $start;
39 1         9 $start = count_sv();
40 1         1218 $block->();
41             return count_sv() - $start;
42             }
43 1         12  
44             local $SIG{__DIE__} = 'DEFAULT';
45 1         11964  
46 1         2034 _start($need_stateinfo);
47 1         2494 eval{
48             $block->();
49 1 50       3415 };
50 0         0 if($@){
51 0         0 _finish(-silent);
52             die $@;
53             }
54 1         496680  
55             return _finish($mode);
56             }
57              
58 0     0 1 0 sub leaked_refs(&){
59 0         0 my($block) = @_;
60             return _do_leaktrace($block, 'leaked_refs', 0);
61             }
62              
63 0     0 1 0 sub leaked_info(&){
64 0         0 my($block) = @_;
65             return _do_leaktrace($block, 'leaked_refs', 1);
66             }
67              
68 0     0 1 0 sub leaked_count(&){
69 0         0 my($block) = @_;
70             return scalar _do_leaktrace($block, 'leaked_count', 0);
71             }
72              
73 0     0 1 0 sub leaktrace(&;$){
74 0 0       0 my($block, $mode) = @_;
75 0         0 _do_leaktrace($block, 'leaktrace', 1, defined($mode) ? $mode : -simple);
76             return;
77             }
78              
79              
80 1     1 1 3 sub leaks_cmp_ok(&$$;$){
81             my($block, $cmp_op, $expected, $description) = @_;
82 1         15  
83             my $Test = __PACKAGE__->builder;
84 1 50       13  
85 0 0       0 if(!_runops_installed()){
86 0         0 my $mod = exists $INC{'Devel/Cover.pm'} ? 'Devel::Cover' : 'strange runops routines';
87             return $Test->ok(1, "skipped (under $mod)");
88             }
89              
90 1         5 # calls to prepare cache in $block
91             $block->();
92 1         9  
93             my $got = _do_leaktrace($block, 'leaked_count', 0);
94 1         11  
95 1 50       5 my $desc = sprintf 'leaks %s %-2s %s', $got, $cmp_op, $expected;
96 0         0 if(defined $description){
97             $description .= " ($desc)";
98             }
99 1         14 else{
100             $description = $desc;
101             }
102 1         8  
103             my $result = $Test->cmp_ok($got, $cmp_op, $expected, $description);
104 1 50       1609  
105 1         19 if(!$result){
106 1         6 open local(*STDERR), '>', \(my $content = '');
107             $block->(); # calls it again because opening *STDERR changes the run-time environment
108 1         11  
109 1         26 _do_leaktrace($block, 'leaktrace', 1, -verbose);
110             $Test->diag($content);
111             }
112 1         149016  
113             return $result;
114             }
115              
116             sub no_leaks_ok(&;$){
117 1     1 1 17 # ($block, $description)
118 1         6 splice @_, 1, 0, ('<=', 0); # ($block, '<=', 0, $description);
119             goto &leaks_cmp_ok;
120             }
121              
122              
123             1;
124             __END__
125              
126             #line 339