File Coverage

blib/lib/SimpleMock/Util.pm
Criterion Covered Total %
statement 46 46 100.0
branch 11 12 91.6
condition n/a
subroutine 11 11 100.0
pod 4 4 100.0
total 72 73 98.6


line stmt bran cond sub pod time code
1             package SimpleMock::Util;
2 8     8   175241 use strict;
  8         11  
  8         220  
3 8     8   27 use warnings;
  8         10  
  8         346  
4 8     8   473 use Data::Dumper qw(Dumper);
  8         6041  
  8         440  
5 8     8   33 use Exporter qw(import);
  8         9  
  8         235  
6 8     8   3476 use Digest::SHA qw(sha256_hex);
  8         18800  
  8         1247  
7              
8             our $VERSION = '0.01';
9              
10             our @EXPORT_OK = qw(
11             all_file_subs
12             generate_args_sha
13             namespace_from_file
14             file_from_namespace
15             );
16              
17             sub all_file_subs {
18 16     16 1 143332 my $file = shift;
19 16 100       57 $INC{$file} or die "File $file not loaded";
20 15         44 my $ns = namespace_from_file($file);
21            
22 15         31 my @subs = ();
23 8     8   54 no strict 'refs'; ## no critic 'ProhibitNoStrict';
  8         15  
  8         3004  
24 15         20 SYM: foreach my $sym (keys %{$ns.'::'}) {
  15         232  
25 1270 100       1049 if (my $code_ref = *{$ns."::$sym"}{CODE}) {
  1270         2132  
26             # ignore constants
27 1115 100       1425 next SYM if (defined(prototype($code_ref)));
28 730         814 push @subs, $sym
29             }
30             }
31 15         163 return @subs;
32             }
33              
34             # create sha for arg lists sent
35             sub generate_args_sha {
36 109     109 1 2127 my $args = shift;
37 109 100       198 @_ and die "generate_args_sha() does not take a second argument";
38              
39             # coderefs will be replaced with dummy markers safely, so disable warnings for this
40             local $SIG{__WARN__} = sub {
41 2 50   2   97 $_[0] =~ /^Encountered CODE ref/
42             or warn $_[0];
43 108         516 };
44            
45 108         173 local $Data::Dumper::Deepcopy=1;
46 108         183 local $Data::Dumper::Indent=0;
47 108         110 local $Data::Dumper::Purity=1;
48 108         116 local $Data::Dumper::SortKeys=1;
49 108         125 local $Data::Dumper::Terse=1;
50              
51 108 100       443 return defined $args ? sha256_hex(Dumper($args)) : '_default';
52             }
53              
54             sub namespace_from_file {
55 44     44 1 666 my $file = shift;
56 44         127 $file =~ s/\.pm$//;
57 44         95 $file =~ s/\//::/g;
58 44         75 return $file;
59             }
60              
61             sub file_from_namespace {
62 18     18 1 23 my $ns = shift;
63 18         46 $ns =~ s/::/\//g;
64 18         41 return $ns . '.pm';
65             }
66              
67             1;
68              
69             =head1 NAME
70              
71             SimpleMock::Util - Utility functions for SimpleMock
72              
73             =head1 DESCRIPTION
74              
75             This module provides utility functions for the SimpleMock framework.
76              
77             =head1 FUNCTIONS
78              
79             All of these functions are exportable on request.
80              
81             =head2 all_file_subs
82              
83             my @subs = SimpleMock::Util::all_file_subs($file);
84              
85             Returns a list of all subroutine names defined in the given file. The file must already be loaded and in %INC.
86              
87             =head2 generate_args_sha
88              
89             my $sha = SimpleMock::Util::generate_args_sha($args);
90              
91             Generates a SHA-256 hash of the provided arguments. If no arguments are provided, it returns '_default'.
92              
93             This is used to create a unique identifier for the arguments passed to a mock function so that we
94             can retrieve mocks via a lookup hash.
95              
96             =head2 namespace_from_file
97              
98             my $namespace = SimpleMock::Util::namespace_from_file($file);
99              
100             Converts a file path to a namespace. For example, `Foo/Bar.pm` becomes `Foo::Bar`.
101              
102             =head2 file_from_namespace
103              
104             my $file = SimpleMock::Util::file_from_namespace($namespace);
105              
106             Converts a namespace back to a file path. For example, `Foo::Bar` becomes `Foo/Bar.pm`.
107              
108             =cut
109