File Coverage

blib/lib/SimpleMock/Model/PATH_TINY.pm
Criterion Covered Total %
statement 23 23 100.0
branch 6 6 100.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 34 35 97.1


line stmt bran cond sub pod time code
1             package SimpleMock::Model::PATH_TINY;
2 3     3   16 use strict;
  3         3  
  3         137  
3 3     3   11 use warnings;
  3         3  
  3         146  
4 3     3   14 use Path::Tiny;
  3         4  
  3         178  
5              
6 3     3   10 use Data::Dumper;
  3         4  
  3         721  
7              
8             our $VERSION = '0.05';
9              
10              
11             # list attributes that can be 0 (false) or 1 (true)
12             our @t_f_keys = qw(assert exists has_same_bytes);
13             my %t_f = (0 =>1, 1=>1);
14              
15             sub validate_mocks {
16 11     11 0 14 my $mocks_data = shift;
17              
18 11         16 my $new_mocks = {};
19              
20 11         21 PATH: foreach my $path (keys %$mocks_data) {
21 19         37 my $normalized_path = SimpleMock::Mocks::Path::Tiny::_normalize_path($path);
22             # implicit directory if has children
23 19 100       593 $mocks_data->{$path}->{is_dir} =1 if $mocks_data->{$path}->{children};
24              
25 19         27 T_F_KEY: foreach my $key (@t_f_keys) {
26 56         64 my $val = $mocks_data->{$path}->{$key};
27 56 100       80 next T_F_KEY unless defined $val;
28 11 100       47 $t_f{$val} or die "Invalid value for key '$key' in Path::Tiny mock for path '$path' - must be 0|1";
29             }
30              
31 18         38 $new_mocks->{PATH_TINY}->{$normalized_path} = $mocks_data->{$path};
32             }
33 10         18 return $new_mocks;
34             }
35              
36             1;
37              
38             =head1 NAME
39              
40             SimpleMock::Model::PATH_TINY - Mock model for Path::Tiny operations
41              
42             =head1 DESCRIPTION
43              
44             This module allows you to register mocks for Path::Tiny
45              
46             =head1 USAGE
47              
48             You probably won't use this module directly. Instead, you will use the `SimpleMock` module to register your mocks. Here's an example of how to do that:
49              
50             use SimpleMock qw(register_mocks);
51              
52             register_mocks(
53             PATH_TINY => {
54             '/path/to/dir/pr/file' => {
55              
56             # if data is set, it's implicitly a file, otherwise it's a directory
57             data => $file_content,
58              
59             # these are all true by default, but you can set to false for them to throw
60             # or return false as noted
61             assert => 0, # throws
62             exists => 0, # return 0
63             has_same_bytes => 0, # return 0 - value is hard coded for ALL comparisons on a mock
64              
65             # returns this hard coded value for the stat - set as appropriate (obviously fake below)
66             stat => [1,2,3,4],
67              
68             # digest hash for the mock. Set as appropriate if calling digest()
69             digest => '1a2b3c4d536f',
70             },
71             }
72             );
73              
74             For basic usage, you just need this:
75              
76             register_mocks(
77             PATH_TINY => {
78              
79             # file MUST have a data attribute
80             '/path/to/file.txt' => { data => 'file content' },
81              
82             # directory must NOT have a data attribute
83             '/path/to/dir' => {},
84             }
85             );
86              
87             =cut