| 1 | package Test::Pockito::DefaultMatcher; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 21 | ▶ | use Scalar::Util::Reftype; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 21 | ▶ | use Exporter 'import'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | Test::Pockito::DefaultMatcher | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | Default matching for Pockito | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | Default implementation of matching. If none of the any_* subs are used for matching, then it reverts to a ne op for matching. | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | =head1 SUBROUTINES | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | =over 1 | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | =item default_call_match( $package, $method, \@params_found, \@params_expected ) | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | This is the default matching metchanism for Pockito though you are at will to implement your own. Passing an implementation with this signature overrides the matching sub. The default implementation does not use $package nor $method, but they will be of use if you have multiple, different ways to define parameters as equal. | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | =back | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =cut | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 21 | our %lookup = (); | |||||||||||||||||
| 34 | 21 | our @EXPORT_OK = qw(is_defined); | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 93 | ▶ | sub default_call_match { | ||||||||||||||||
|
Subroutine
default_call_match: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||
| 37 | 93 | my $package = shift; | |||||||||||||||||
| 38 | 93 | my $method = shift; | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 93 | my $param_found_ref = shift; | |||||||||||||||||
| 41 | 93 | my $param_expected_ref = shift; | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 93 | my (@left) = @{$param_found_ref}; | |||||||||||||||||
| 44 | 93 | my (@right) = @{$param_expected_ref}; | |||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 93 | ▶ | if ( $#left < $#right ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 47 | 10 | (@left) = @{$param_expected_ref}; | |||||||||||||||||
| 48 | 10 | (@right) = @{$param_found_ref}; | |||||||||||||||||
| 49 | } | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 93 | foreach my $y ( 0 .. $#left ) { | |||||||||||||||||
| 52 | 94 | ▶ | my $l = $left[$y] || 0; | ||||||||||||||||
|
Condition100%2/2
$left[$y] || 0
MC/DC100%1/1
$left[$y] || 0
$left[$y]
Truth table100%2/2
A
A= $left[$y]
| |||||||||||||||||||
| 53 | 94 | ▶ | my $r = $right[$y] || 0; | ||||||||||||||||
|
Condition100%2/2
$right[$y] || 0
MC/DC100%1/1
$right[$y] || 0
$right[$y]
Truth table100%2/2
A
A= $right[$y]
| |||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 94 | ▶ | if ( exists $lookup{$l} ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 56 | 0 | ▶ | return 0 if !&{ $lookup{$l} }($r); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | elsif ( exists $lookup{$r} ) { | ||||||||||||||||||
| 59 | 3 | ▶ | return 0 if !&{ $lookup{$r} }($l); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | elsif ( $l ne $r ) { | ||||||||||||||||||
| 62 | 24 | return 0; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 69 | return 1; | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | =head1 MATCHERS | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | All the following matchers can be exported or refered to by package name. They use Scalar::Util::Reftype under the hood except for is_defined. | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | is_defined | ||||||||||||||||||
| 74 | is_scalar | ||||||||||||||||||
| 75 | is_array | ||||||||||||||||||
| 76 | is_hash | ||||||||||||||||||
| 77 | is_code | ||||||||||||||||||
| 78 | is_global | ||||||||||||||||||
| 79 | is_lvalue | ||||||||||||||||||
| 80 | is_regexp | ||||||||||||||||||
| 81 | is_scalar_object | ||||||||||||||||||
| 82 | is_array_object | ||||||||||||||||||
| 83 | is_hash_object | ||||||||||||||||||
| 84 | is_code_object | ||||||||||||||||||
| 85 | is_glob_object | ||||||||||||||||||
| 86 | is_lvalue_object | ||||||||||||||||||
| 87 | is_ref_object | ||||||||||||||||||
| 88 | is_io_object | ||||||||||||||||||
| 89 | is_regexp_object | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | With these, one can write: | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | $pocket->when( $mock->( is_defined, is_regexp, 1, 2, is_code_object )->... | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | to match | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | any defined value | ||||||||||||||||||
| 98 | any regular expression ref | ||||||||||||||||||
| 99 | the value 1 | ||||||||||||||||||
| 100 | the value 2 | ||||||||||||||||||
| 101 | any blessed code ref | ||||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | =cut | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 21 | our @types = | |||||||||||||||||
| 106 | qw(scalar array hash code global lvalue regexp scalar_object array_object hash_object code_object glob_object lvalue_object ref_object io_object regexp_object); | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 21 | foreach my $type (@types) { | |||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | #Going to hell for this. | ||||||||||||||||||
| 111 | 336 | my $sub_name = __PACKAGE__ . "::is_" . $type; | |||||||||||||||||
| 112 | 1 | ▶ | my $check = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 113 | 1 | return reftype(shift)->$type(); | |||||||||||||||||
| 114 | 336 | }; | |||||||||||||||||
| 115 | 2 | ▶ | my $wrapper = sub { return $check }; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 21 | ▶ | no strict "refs"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 118 | 336 | *$sub_name = $wrapper; | |||||||||||||||||
| 119 | 21 | ▶ | use strict "refs"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | 336 | $lookup{$check} = $check; | |||||||||||||||||
| 122 | 336 | push( @EXPORT_OK, "is_" . $type ); | |||||||||||||||||
| 123 | } | ||||||||||||||||||
| 124 | |||||||||||||||||||
| 125 | 2 | ▶ | sub check_is_defined { | ||||||||||||||||
|
Subroutine
check_is_defined: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 126 | 2 | return defined shift; | |||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 2 | ▶ | sub is_defined { | ||||||||||||||||
|
Subroutine
is_defined: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 130 | 2 | return \&check_is_defined; | |||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 21 | $lookup{ \&check_is_defined } = \&check_is_defined; | |||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | =head1 SUPPORT | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | exussum@gmail.com | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | =head1 AUTHOR | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | Spencer Portee | ||||||||||||||||||
| 142 | CPAN ID: EXUSSUM | ||||||||||||||||||
| 143 | exussum@gmail.com | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | =head1 SOURCE | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | http://bitbucket.org/exussum/pockito/ | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | =head1 COPYRIGHT | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | This program is free software licensed under the... | ||||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | The BSD License | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | The full text of the license can be found in the | ||||||||||||||||||
| 156 | LICENSE file included with this module. | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | =head1 SEE ALSO | ||||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | perl(1). | ||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | =cut | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | 21 | 1; | |||||||||||||||||