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   182182 use strict;
  9         15  
  9         254  
3 9     9   25 use warnings;
  9         11  
  9         398  
4 9     9   478 use Data::Dumper qw(Dumper);
  9         5761  
  9         542  
5 9     9   35 use Exporter qw(import);
  9         12  
  9         304  
6 9     9   4068 use Digest::SHA qw(sha256_hex);
  9         21029  
  9         1384  
7              
8             our $VERSION = '0.05';
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 155495 my $file = shift;
19 19 100       65 $INC{$file} or die "File $file not loaded";
20 18         44 my $ns = namespace_from_file($file);
21            
22 18         35 my @subs = ();
23 9     9   75 no strict 'refs'; ## no critic 'ProhibitNoStrict';
  9         12  
  9         3379  
24 18         22 SYM: foreach my $sym (keys %{$ns.'::'}) {
  18         259  
25 1564 100       1290 if (my $code_ref = *{$ns."::$sym"}{CODE}) {
  1564         2705  
26             # ignore constants
27 1371 100       1730 next SYM if (defined(prototype($code_ref)));
28 893         983 push @subs, $sym
29             }
30             }
31 18         201 return @subs;
32             }
33              
34             # create sha for arg lists sent
35             sub generate_args_sha {
36 112     112 1 2147 my $args = shift;
37 112 100       219 @_ 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   96 $_[0] =~ /^Encountered CODE ref/
42             or warn $_[0];
43 111         546 };
44            
45 111         206 local $Data::Dumper::Deepcopy=1;
46 111         136 local $Data::Dumper::Indent=0;
47 111         138 local $Data::Dumper::Purity=1;
48 111         111 local $Data::Dumper::SortKeys=1;
49 111         141 local $Data::Dumper::Terse=1;
50              
51 111 100       457 return defined $args ? sha256_hex(Dumper($args)) : '_default';
52             }
53              
54             sub namespace_from_file {
55 53     53 1 644 my $file = shift;
56 53         152 $file =~ s/\.pm$//;
57 53         115 $file =~ s/\//::/g;
58 53         110 return $file;
59             }
60              
61             sub file_from_namespace {
62 18     18 1 27 my $ns = shift;
63 18         32 $ns =~ s/::/\//g;
64 18         46 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