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 9     9   236097 use strict;
  9         18  
  9         263  
3 9     9   28 use warnings;
  9         12  
  9         352  
4 9     9   545 use Data::Dumper qw(Dumper);
  9         6096  
  9         480  
5 9     9   35 use Exporter qw(import);
  9         28  
  9         272  
6 9     9   3988 use Digest::SHA qw(sha256_hex);
  9         19747  
  9         1342  
7              
8             our $VERSION = '0.04';
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 19     19 1 169414 my $file = shift;
19 19 100       79 $INC{$file} or die "File $file not loaded";
20 18         44 my $ns = namespace_from_file($file);
21            
22 18         32 my @subs = ();
23 9     9   52 no strict 'refs'; ## no critic 'ProhibitNoStrict';
  9         36  
  9         3293  
24 18         25 SYM: foreach my $sym (keys %{$ns.'::'}) {
  18         329  
25 1564 100       1357 if (my $code_ref = *{$ns."::$sym"}{CODE}) {
  1564         2964  
26             # ignore constants
27 1371 100       1834 next SYM if (defined(prototype($code_ref)));
28 893         1067 push @subs, $sym
29             }
30             }
31 18         209 return @subs;
32             }
33              
34             # create sha for arg lists sent
35             sub generate_args_sha {
36 111     111 1 3153 my $args = shift;
37 111 100       235 @_ 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   147 $_[0] =~ /^Encountered CODE ref/
42             or warn $_[0];
43 110         599 };
44            
45 110         196 local $Data::Dumper::Deepcopy=1;
46 110         160 local $Data::Dumper::Indent=0;
47 110         128 local $Data::Dumper::Purity=1;
48 110         123 local $Data::Dumper::SortKeys=1;
49 110         134 local $Data::Dumper::Terse=1;
50              
51 110 100       611 return defined $args ? sha256_hex(Dumper($args)) : '_default';
52             }
53              
54             sub namespace_from_file {
55 53     53 1 923 my $file = shift;
56 53         162 $file =~ s/\.pm$//;
57 53         119 $file =~ s/\//::/g;
58 53         97 return $file;
59             }
60              
61             sub file_from_namespace {
62 18     18 1 36 my $ns = shift;
63 18         40 $ns =~ s/::/\//g;
64 18         47 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