File Coverage

blib/lib/Test/Mockify/Matcher.pm
Criterion Covered Total %
statement 33 33 100.0
branch 10 10 100.0
condition 15 15 100.0
subroutine 14 14 100.0
pod 9 9 100.0
total 81 81 100.0


line stmt bran cond sub pod time code
1             =pod
2            
3             =head1 Name
4            
5             Test::Mockify::Matcher - To define parameter matchers
6            
7             =head1 DESCRIPTION
8            
9             Use L<Test::Mockify::Matcher> to define different types of expected parameters. See method description for more details.
10            
11             =head1 METHODS
12            
13             =cut
14             package Test::Mockify::Matcher;
15 9     9   19096 use strict;
  9         12  
  9         223  
16 9     9   27 use warnings;
  9         54  
  9         228  
17 9         542 use Test::Mockify::TypeTests qw (
18             IsFloat
19             IsString
20             IsArrayReference
21             IsHashReference
22             IsCodeReference
23 9     9   1050 );
  9         15  
24 9     9   35 use base qw( Exporter );
  9         10  
  9         3922  
25             our @EXPORT_OK = qw (
26             SupportedTypes
27             String
28             Number
29             HashRef
30             ArrayRef
31             Object
32             Function
33             Undef
34             Any
35             );
36             =pod
37            
38             =head2 SupportedTypes
39            
40             The C<SupportedTypes> will return all supported matcher types as an array ref.
41            
42             SupportedTypes();
43            
44             =cut
45             sub SupportedTypes{
46                 return [
47 144     144 1 485         'string',
48                     'number',
49                     'hashref',
50                     'arrayref',
51                     'object',
52                     'sub',
53                     'undef',
54                     'any',
55                 ];
56             }
57             =pod
58            
59             =head2 String
60            
61             The C<String> method will create the matcher in the needed structure to match a string.
62             If called with parameter, it will be proved that this value is actually a string. If not, it will create an error.
63            
64             String();
65             String('abc');
66            
67             =cut
68             sub String(;$) {
69 51     51 1 2241     my ($Value) = @_;
70 51 100 100     173     die('NotAString') if $Value && !IsString($Value);
71 50         95     return _Type('string',$Value);
72             }
73             =pod
74            
75             =head2 Number
76            
77             The C<Number> method will create the matcher in the needed structure to match a number.
78             If called with parameter, it will be proved that this value is actually a number. If not, it will create an error.
79            
80             Number();
81             Number(123);
82             Number(45.67);
83            
84             =cut
85             sub Number(;$) {
86 19     19 1 1677     my ($Value) = @_;
87 19 100 100     82     die('NotANumber') if $Value && !IsFloat($Value);
88 18         32     return _Type('number',$Value);
89             }
90             =pod
91            
92             =head2 HashRef
93            
94             The C<HashRef> method will create the matcher in the needed structure to match a hash reference.
95             If called with parameter, it will be proved that this value is actually a hash reference. If not, it will create an error.
96            
97             HashRef();
98             HashRef({1 => 23});
99            
100             =cut
101             sub HashRef(;$) {
102 12     12 1 936     my ($Value) = @_;
103 12 100 100     53     die('NotAHashReference') if $Value && !IsHashReference($Value);
104 11         20     return _Type('hashref',$Value);
105             }
106             =pod
107            
108             =head2 ArrayRef
109            
110             The C<ArrayRef> method will create the matcher in the needed structure to match an array reference.
111             If called with parameter, it will be proved that this value is actually an array reference. If not, it will create an error.
112            
113             ArrayRef();
114             ArrayRef([1,23]);
115            
116             =cut
117             sub ArrayRef(;$) {
118 12     12 1 1054     my ($Value) = @_;
119 12 100 100     50     die('NotAnArrayReference') if $Value && !IsArrayReference($Value);
120 11         22     return _Type('arrayref',$Value);
121             }
122             =pod
123            
124             =head2 Object
125            
126             The C<Object> method will create the matcher in the needed structure to match an object.
127             If called with parameter, it will be proved that this value is actually an string of the object path. If not, it will create an error.
128            
129             Object();
130             Object('Path::To::Object');
131            
132             =cut
133             sub Object(;$) {
134 15     15 1 1634     my ($Value) = @_;
135 15 100 100     122     die('NotAnModulPath') if $Value && !($Value =~ /^\w+(::\w+)*$/);
136 13         25     return _Type('object',$Value);
137             }
138             =pod
139            
140             =head2 Function
141            
142             The C<Function> method will create the matcher in the needed structure to match a function pointer.
143            
144             Function();
145            
146             =cut
147             sub Function(;$) {
148 6     6 1 186     return _Type('sub',undef);
149             }
150             =pod
151            
152             =head2 Undef
153            
154             The C<Undef> method will create the matcher in the needed structure to match an undefined value.
155            
156             Undef();
157            
158             =cut
159             sub Undef() {
160 6     6 1 194     return _Type('undef', undef);
161             }
162             #####=pod
163              
164             =head2 Any
165            
166             The C<Any> method will create the matcher in the needed structure to match any type of parameter.
167            
168             Any();
169            
170             =cut
171             sub Any() {
172 7     7 1 381     return _Type('any', undef);
173             }
174              
175             sub _Type($;$){
176 122     122   132     my ($Type, $Value) = @_;
177                     return {
178 122         497             'Type' => $Type,
179                         'Value' => $Value,
180                     };
181             }
182             1;
183             __END__
184            
185             =head1 LICENSE
186            
187             Copyright (C) 2017 ePages GmbH
188            
189             This library is free software; you can redistribute it and/or modify
190             it under the same terms as Perl itself.
191            
192             =head1 AUTHOR
193            
194             Christian Breitkreutz E<lt>christianbreitkreutz@gmx.deE<gt>
195            
196             =cut
197            
198