File Coverage

blib/lib/Test/Mockify/Tools.pm
Criterion Covered Total %
statement 60 60 100.0
branch 20 20 100.0
condition 5 5 100.0
subroutine 11 11 100.0
pod 0 5 0.0
total 96 101 95.0


line stmt bran cond sub pod time code
1             package Test::Mockify::Tools;
2 9     9   94288 use Module::Load;
  9         7148  
  9         47  
3 9     9   384 use strict;
  9         11  
  9         129  
4 9     9   35 use warnings;
  9         12  
  9         149  
5 9     9   4631 use Data::Dumper;
  9         60870  
  9         614  
6 9     9   59 use Scalar::Util qw( blessed );
  9         12  
  9         604  
7 9     9   39 use base qw( Exporter );
  9         11  
  9         4404  
8             our @EXPORT_OK = qw (
9             Error
10             ExistsMethod
11             IsValid
12             LoadPackage
13             Isa
14             );
15              
16             #------------------------------------------------------------------------
17             sub LoadPackage {
18 61     61 0 2137     my ($Package) = @_;
19              
20 61         307     my $PackageFileName = join( '/', split /::/, $Package ) . '.pm';
21 61         154     load($PackageFileName);
22 61         3186     return;
23             }
24             #------------------------------------------------------------------------
25             sub IsValid {
26 41     41 0 273     my ($Value) = @_;
27              
28 41         44     my $IsValid = 0;
29 41 100 100     201     if( defined($Value) && $Value ne '' ){
30 35         35         $IsValid = 1;
31                 }
32 41         139     return $IsValid;
33             }
34             #------------------------------------------------------------------------
35             sub ExistsMethod {
36 73     73 0 925     my ( $PathOrObject, $MethodName ) = @_;
37              
38 73 100       130     Error('Path or Object is needed') unless defined $PathOrObject;
39 72 100       98     Error('Method name is needed') unless defined $MethodName;
40 71 100       306     if( not $PathOrObject->can( $MethodName ) ){
41 3 100       8         if( IsValid( ref( $PathOrObject ) ) ){
42 1         1             $PathOrObject = ref( $PathOrObject );
43                     }
44 3         15         Error( $PathOrObject." donsn't have a method like: $MethodName", {'Method' => $MethodName});
45                 }
46              
47 68         111     return 1;
48             }
49             #------------------------------------------------------------------------
50             sub Isa {
51 5     5 0 447     my ($Object, $ClassName) = @_;
52 5 100       33     return 0 unless blessed( $Object );
53 3 100       11     return 0 unless $ClassName;
54 2         10     my $ResultIsaCheck = $Object->isa( $ClassName );
55 2 100       6     if($ResultIsaCheck eq ''){
56 1         5         return 0;
57                 }
58 1         6     return $ResultIsaCheck;
59             }
60             #------------------------------------------------------------------------
61             sub Error {
62 16     16 0 1641     my ($Message, $hData) = @_;
63              
64 16 100       40     die('Message is needed')unless(defined $Message);
65             # print hData
66 15         18     local $Data::Dumper::Terse = 1;
67 15         16     local $Data::Dumper::Indent = 0;
68 15         18     local $Data::Dumper::Pair = '=';
69 15         14     local $Data::Dumper::Quotekeys = 0;
70 15 100       36     my $MockedMethod = delete $hData->{'Method'} if defined $hData->{'Method'}; ## no critic (ProhibitConditionalDeclarations)
71 15   100     37     $MockedMethod //= '-not set-';
72 15         38     my $DumpedData = Dumper($hData);
73             # print Callerstack
74 15         886     my $CallerStack = '';
75 15         16     my $CallerStackPosition = 1; # 0 would be this function
76 15         86     while (my @Caller = caller($CallerStackPosition++) ) {
77 129         413         my $FileName = $Caller[1];
78 129         95         my $LineNumber = $Caller[2];
79 129         71         my $FunctionName = $Caller[3];
80 129         647         $CallerStack .= sprintf(
81                         "%s,%s(line %s)\n",
82                         $FunctionName,
83                         $FileName,
84                         $LineNumber,
85                     );
86                    
87                 }
88             # If the last element is a newline, the "at Xxxx.pm line XX" will not be printed
89 15         115     my $ErrorOutput = sprintf(
90                     "%s:\nMockedMethod: %s\nData:%s\n%s\n",
91                     $Message,
92                     $MockedMethod,
93                     $DumpedData,
94                     $CallerStack,
95                 );
96                 
97 15         107     die($ErrorOutput);
98             }   
99              
100              
101             1;
102