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   26746 use strict;
  5         7  
  5         131  
3 5     5   19 use warnings;
  5         7  
  5         151  
4 5     5   15 use utf8;
  5         6  
  5         29  
5              
6             our $VERSION = '1.00';
7              
8 5     5   234 use Carp qw(croak);
  5         5  
  5         283  
9 5     5   544 use English qw(-no_match_vars);
  5         3666  
  5         30  
10 5     5   2335 use Test::MockPackages::Mock();
  5         7  
  5         1159  
11              
12             sub new {
13 10     10 1 1303 my ( $pkg, $package_name ) = @ARG;
14              
15 10 100 100     53 if ( !$package_name || ref( $package_name ) ) {
16 2         25 croak( '$package_name is required and must be a SCALAR' );
17             }
18              
19 8         58 return bless {
20             _package_name => $package_name,
21             _mocks => {},
22             }, $pkg;
23             }
24              
25             sub mock {
26 12     12 1 570 my ( $self, $name ) = @ARG;
27              
28 12 100 100     57 if ( !$name || ref( $name ) ) {
29 2         17 croak( '$name is required and must be a SCALAR' );
30             }
31              
32 10 100       37 if ( my $mock = $self->{_mocks}{$name} ) {
33 1         5 return $mock;
34             }
35              
36 9         47 return $self->{_mocks}{$name} = Test::MockPackages::Mock->new( $self->{_package_name}, $name );
37             }
38              
39             sub DESTROY {
40 8     8   93 my ( $self ) = @ARG;
41              
42             # ensures that objects are destroyed in a consistent order.
43 8         12 for my $key ( sort keys %{ $self->{_mocks} } ) {
  8         41  
44 9         35 delete $self->{_mocks}{$key};
45             }
46             }
47              
48             1;
49              
50             __END__