File Coverage

blib/lib/Sub/Spy.pm
Criterion Covered Total %
statement 40 41 97.5
branch 8 10 80.0
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 60 63 95.2


line stmt bran cond sub pod time code
1             package Sub::Spy;
2 5     5   117600 use 5.12.0;
  5         17  
  5         201  
3 5     5   26 use strict;
  5         12  
  5         155  
4 5     5   22 use warnings;
  5         13  
  5         227  
5              
6             our $VERSION = '0.04';
7              
8 5     5   4098 use parent qw/Exporter/;
  5         1491  
  5         25  
9             our @EXPORT_OK = qw/spy inspect/;
10              
11 5     5   4923 use Hash::FieldHash qw/fieldhash/;
  5         4060  
  5         407  
12              
13 5     5   2761 use Sub::Spy::Result;
  5         12  
  5         201  
14 5     5   2707 use Sub::Spy::Call;
  5         20  
  5         49  
15              
16              
17             fieldhash our %f_store;
18              
19             sub spy {
20 5     5 1 7924 my $subref = shift;
21              
22 5         26 my $store = +{};
23              
24             my $spy = sub {
25 9     9   437 my @args = @_;
26 9         14 my ($result, @array_result, $e);
27              
28 9 100       27 if ( wantarray ) {
29 2         4 @array_result = eval { $subref->(@args); };
  2         5  
30             }
31             else {
32 7         14 $result = eval { $subref->(@args); };
  7         21  
33             }
34 9 50       49 if ( $@ ) {
35 0         0 $e = $@;
36             }
37              
38 9 100       13 push @{$store->{calls}}, Sub::Spy::Call->new({
  9         176  
39             args => \@args,
40             exception => $e,
41             return_value => wantarray ? \@array_result : $result,
42             });
43              
44 9 100       146 return wantarray ? @array_result : $result;
45 5         38 };
46              
47 5         50 $f_store{$spy} = $store;
48              
49 5         18 return $spy;
50             }
51              
52             sub inspect {
53 22     22 1 3141 my $spy = shift;
54 22 50       77 my $param = $f_store{$spy} or die "given subroutine reference is not a spy!";
55 22         83 return Sub::Spy::Result->new($param);
56             }
57              
58              
59             1;
60             __END__