File Coverage

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


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