File Coverage

blib/lib/Test/MockPackages/Package.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 6 100.0
condition 6 6 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 55 55 100.0


line stmt bran cond sub pod time code
1             package Test::MockPackages::Package;
2 5     5   26431 use strict;
  5         7  
  5         140  
3 5     5   20 use warnings;
  5         6  
  5         119  
4 5     5   15 use utf8;
  5         7  
  5         71  
5              
6             our $VERSION = '1.00';
7              
8 5     5   197 use Carp qw(croak);
  5         5  
  5         341  
9 5     5   518 use English qw(-no_match_vars);
  5         4116  
  5         30  
10 5     5   2367 use Test::MockPackages::Mock();
  5         10  
  5         1095  
11              
12             sub new {
13 10     10 1 1343 my ( $pkg, $package_name ) = @ARG;
14              
15 10 100 100     56 if ( !$package_name || ref( $package_name ) ) {
16 2         30 croak( '$package_name is required and must be a SCALAR' );
17             }
18              
19 8         57 return bless {
20             _package_name => $package_name,
21             _mocks => {},
22             }, $pkg;
23             }
24              
25             sub mock {
26 12     12 1 719 my ( $self, $name ) = @ARG;
27              
28 12 100 100     74 if ( !$name || ref( $name ) ) {
29 2         19 croak( '$name is required and must be a SCALAR' );
30             }
31              
32 10 100       45 if ( my $mock = $self->{_mocks}{$name} ) {
33 1         6 return $mock;
34             }
35              
36 9         39 return $self->{_mocks}{$name} = Test::MockPackages::Mock->new( $self->{_package_name}, $name );
37             }
38              
39             sub DESTROY {
40 8     8   113 my ( $self ) = @ARG;
41              
42             # ensures that objects are destroyed in a consistent order.
43 8         17 for my $key ( sort keys %{ $self->{_mocks} } ) {
  8         56  
44 9         38 delete $self->{_mocks}{$key};
45             }
46             }
47              
48             1;
49              
50             __END__