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 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
20
20
65696
use strict;
20
80
20
552
16
20
20
108
use warnings;
20
48
20
657
17
20
1163
use Test::Mockify::TypeTests qw (
18
IsFloat
19
IsString
20
IsArrayReference
21
IsHashReference
22
IsCodeReference
23
20
20
871
);
20
45
24
20
20
783
use Test::Mockify::Tools qw (Error);
20
41
20
925
25
20
20
467
use base qw( Exporter );
20
71
20
12441
26
our @EXPORT_OK = qw (
27
SupportedTypes
28
String
29
Number
30
HashRef
31
ArrayRef
32
Object
33
Function
34
Undef
35
Any
36
);
37
=pod
38
39
=head2 SupportedTypes
40
41
The C will return all supported matcher types as an array ref.
42
43
SupportedTypes();
44
45
=cut
46
sub SupportedTypes{
47
return [
48
144
144
1
524
'string',
49
'number',
50
'hashref',
51
'arrayref',
52
'object',
53
'sub',
54
'undef',
55
'any',
56
];
57
}
58
=pod
59
60
=head2 String
61
62
The C method will create the matcher in the needed structure to match a string.
63
If called with parameter, it will be proved that this value is actually a string. If not, it will create an error.
64
Use the Matcher Number() to Check for the string '0' (perl cannot differ that)
65
66
String();
67
String('abc');
68
69
=cut
70
sub String(;$) {
71
67
67
1
3140
my ($Value) = @_;
72
67
100
100
258
Error('NotAString') if $Value && !IsString($Value);
73
66
100
100
198
Error("Please use the Matcher Number($Value) to check for the string '$Value' (perl can not distinguish between numbers and strings)") if defined $Value && IsFloat($Value);
74
64
145
return _Type('string',$Value);
75
}
76
=pod
77
78
=head2 Number
79
80
The C method will create the matcher in the needed structure to match a number.
81
If called with parameter, it will be proved that this value is actually a number. If not, it will create an error.
82
83
Number();
84
Number(123);
85
Number(45.67);
86
87
=cut
88
sub Number(;$) {
89
56
56
1
2451
my ($Value) = @_;
90
56
100
100
222
Error('NotANumber') if defined $Value && !IsFloat($Value);
91
55
159
return _Type('number',$Value);
92
}
93
=pod
94
95
=head2 HashRef
96
97
The C method will create the matcher in the needed structure to match a hash reference.
98
If called with parameter, it will be proved that this value is actually a hash reference. If not, it will create an error.
99
100
HashRef();
101
HashRef({1 => 23});
102
103
=cut
104
sub HashRef(;$) {
105
13
13
1
1449
my ($Value) = @_;
106
13
100
100
77
Error('NotAHashReference') if $Value && !IsHashReference($Value);
107
12
34
return _Type('hashref',$Value);
108
}
109
=pod
110
111
=head2 ArrayRef
112
113
The C method will create the matcher in the needed structure to match an array reference.
114
If called with parameter, it will be proved that this value is actually an array reference. If not, it will create an error.
115
116
ArrayRef();
117
ArrayRef([1,23]);
118
119
=cut
120
sub ArrayRef(;$) {
121
12
12
1
1325
my ($Value) = @_;
122
12
100
100
64
Error('NotAnArrayReference') if $Value && !IsArrayReference($Value);
123
11
36
return _Type('arrayref',$Value);
124
}
125
=pod
126
127
=head2 Object
128
129
The C method will create the matcher in the needed structure to match an object.
130
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.
131
132
Object();
133
Object('Path::To::Object');
134
135
=cut
136
sub Object(;$) {
137
18
18
1
2135
my ($Value) = @_;
138
18
100
100
154
Error('NotAnModulPath') if $Value && !($Value =~ /^\w+(::\w+)*$/sm);
139
16
43
return _Type('object',$Value);
140
}
141
=pod
142
143
=head2 Function
144
145
The C method will create the matcher in the needed structure to match a function pointer.
146
147
Function();
148
149
=cut
150
sub Function(;$) {
151
6
6
1
273
return _Type('sub',undef);
152
}
153
=pod
154
155
=head2 Undef
156
157
The C method will create the matcher in the needed structure to match an undefined value.
158
159
Undef();
160
161
=cut
162
sub Undef() {
163
9
9
1
281
return _Type('undef', undef);
164
}
165
#####=pod
166
167
=head2 Any
168
169
The C method will create the matcher in the needed structure to match any type of parameter.
170
171
Any();
172
173
=cut
174
sub Any() {
175
15
15
1
609
return _Type('any', undef);
176
}
177
178
sub _Type($;$){
179
188
188
348
my ($Type, $Value) = @_;
180
return {
181
188
889
'Type' => $Type,
182
'Value' => $Value,
183
};
184
}
185
1;
186
__END__