| 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|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 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 20 | ▶ | use Test::Mockify::TypeTests qw ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | IsFloat | ||||||||||||||||||||||||||||
| 19 | IsString | ||||||||||||||||||||||||||||
| 20 | IsArrayReference | ||||||||||||||||||||||||||||
| 21 | IsHashReference | ||||||||||||||||||||||||||||
| 22 | IsCodeReference | ||||||||||||||||||||||||||||
| 23 | 20 | ); | |||||||||||||||||||||||||||
| 24 | 20 | ▶ | use Test::Mockify::Tools qw (Error); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 20 | ▶ | use base qw( Exporter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 20 | 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<SupportedTypes> will return all supported matcher types as an array ref. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | SupportedTypes(); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | 144 | ▶ | sub SupportedTypes{ | ||||||||||||||||||||||||||
|
Subroutine
SupportedTypes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | return [ | ||||||||||||||||||||||||||||
| 48 | 144 | '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<String> 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 | 67 | ▶ | sub String(;$) { | ||||||||||||||||||||||||||
|
Subroutine
String: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 67 | my ($Value) = @_; | |||||||||||||||||||||||||||
| 72 | 67 | ▶ | Error('NotAString') if $Value && !IsString($Value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Value and not IsString($Value)
MC/DC100%2/2
$Value and not IsString($Value)
$Value
not IsString($Value)
Truth table100%3/3
A and not(B)
A= $ValueB= not IsString($Value)
| |||||||||||||||||||||||||||||
| 73 | 66 | ▶ | 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); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $Value and IsFloat($Value)
MC/DC100%2/2
defined $Value and IsFloat($Value)
defined $Value
IsFloat($Value)
Truth table100%3/3
A and B
A= defined $ValueB= IsFloat($Value)
| |||||||||||||||||||||||||||||
| 74 | 64 | return _Type('string',$Value); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | =pod | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 Number | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | The C<Number> 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 | 56 | ▶ | sub Number(;$) { | ||||||||||||||||||||||||||
|
Subroutine
Number: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 56 | my ($Value) = @_; | |||||||||||||||||||||||||||
| 90 | 56 | ▶ | Error('NotANumber') if defined $Value && !IsFloat($Value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $Value and not IsFloat($Value)
MC/DC100%2/2
defined $Value and not IsFloat($Value)
defined $Value
not IsFloat($Value)
Truth table100%3/3
A and not(B)
A= defined $ValueB= not IsFloat($Value)
| |||||||||||||||||||||||||||||
| 91 | 55 | return _Type('number',$Value); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | =pod | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 HashRef | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | The C<HashRef> 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 | 13 | ▶ | sub HashRef(;$) { | ||||||||||||||||||||||||||
|
Subroutine
HashRef: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 13 | my ($Value) = @_; | |||||||||||||||||||||||||||
| 106 | 13 | ▶ | Error('NotAHashReference') if $Value && !IsHashReference($Value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Value and not IsHashReference($Value)
MC/DC100%2/2
$Value and not IsHashReference($Value)
$Value
not IsHashReference($Value)
Truth table100%3/3
A and not(B)
A= $ValueB= not IsHashReference($Value)
| |||||||||||||||||||||||||||||
| 107 | 12 | return _Type('hashref',$Value); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | =pod | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 ArrayRef | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | The C<ArrayRef> 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 | 12 | ▶ | sub ArrayRef(;$) { | ||||||||||||||||||||||||||
|
Subroutine
ArrayRef: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 12 | my ($Value) = @_; | |||||||||||||||||||||||||||
| 122 | 12 | ▶ | Error('NotAnArrayReference') if $Value && !IsArrayReference($Value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Value and not IsArrayReference($Value)
MC/DC100%2/2
$Value and not IsArrayReference($Value)
$Value
not IsArrayReference($Value)
Truth table100%3/3
A and not(B)
A= $ValueB= not IsArrayReference($Value)
| |||||||||||||||||||||||||||||
| 123 | 11 | return _Type('arrayref',$Value); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 Object | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | The C<Object> 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 | 18 | ▶ | sub Object(;$) { | ||||||||||||||||||||||||||
|
Subroutine
Object: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 18 | my ($Value) = @_; | |||||||||||||||||||||||||||
| 138 | 18 | ▶ | Error('NotAnModulPath') if $Value && !($Value =~ /^\w+(::\w+)*$/sm); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Value and not $Value =~ /^\w+(::\w+)*$/ms
MC/DC100%2/2
$Value and not $Value =~ /^\w+(::\w+)*$/ms
$Value
not $Value =~ /^\w+(::\w+)*$/ms
Truth table100%3/3
A and not(B)
A= $ValueB= not $Value =~ /^\w+(::\w+)*$/ms
| |||||||||||||||||||||||||||||
| 139 | 16 | return _Type('object',$Value); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | =pod | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 Function | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | The C<Function> method will create the matcher in the needed structure to match a function pointer. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Function(); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | 6 | ▶ | sub Function(;$) { | ||||||||||||||||||||||||||
|
Subroutine
Function: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 6 | return _Type('sub',undef); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | =pod | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 Undef | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | The C<Undef> method will create the matcher in the needed structure to match an undefined value. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Undef(); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||
| 162 | 9 | ▶ | sub Undef() { | ||||||||||||||||||||||||||
|
Subroutine
Undef: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 9 | return _Type('undef', undef); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | #####=pod | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 Any | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | The C<Any> method will create the matcher in the needed structure to match any type of parameter. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Any(); | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||
| 174 | 15 | ▶ | sub Any() { | ||||||||||||||||||||||||||
|
Subroutine
Any: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 15 | return _Type('any', undef); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 188 | ▶ | sub _Type($;$){ | ||||||||||||||||||||||||||
|
Subroutine
_Type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | 188 | my ($Type, $Value) = @_; | |||||||||||||||||||||||||||
| 180 | return { | ||||||||||||||||||||||||||||
| 181 | 188 | 'Type' => $Type, | |||||||||||||||||||||||||||
| 182 | 'Value' => $Value, | ||||||||||||||||||||||||||||
| 183 | }; | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 20 | 1; | |||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||