blib/lib/Message/Match.pm

filter: Statementstmt 87.5% 56 / 64 Branchbran 69.4% 25 / 36 Conditioncond 66.6% 22 / 33 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 74.6% 118 / 158 CC 34 SCAR 33.2
CC
34
Cov
77%
_match
41.4
mmatch
7.0
_special
5.4
CRAP 27.5
↑ Summary
1 package Message::Match;
2 {
3 3 $Message::Match::VERSION = '1.132270';
4 }
5  
6 3 ▶use strict;use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 require Exporter;
8 3 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 @ISA = qw(Exporter);
10 3 @EXPORT_OK = qw(mmatch);
11  
12 3 our $named_regex = {};
13  
14 30 ▶sub mmatch {
Subroutine mmatch: called CC=7 SCAR=19.5
Pod documented
15 30 my ($message, $match) = @_;
16 30 ▶ die 'Message::Match::mmatch: two HASH references required'
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 2 or scalar @_ > 2 or not ref $message or not ref $match or ref $message ne "HASH" or ref $match ne "HASH" 6 24 30
Condition93%14/15
scalar @_ < 2 or scalar @_ > 2 or not ref $message or not ref $match or ref $message ne "HASH" or ref $match ne "HASH"
l!l&&r!l&&!r
5124
scalar @_ < 2 or scalar @_ > 2 or not ref $message or not ref $match or ref $message ne "HASH"
l!l&&r!l&&!r
5025
scalar @_ < 2 or scalar @_ > 2 or not ref $message or not ref $match
l!l&&r!l&&!r
4125
scalar @_ < 2 or scalar @_ > 2 or not ref $message
l!l&&r!l&&!r
3126
scalar @_ < 2 or scalar @_ > 2
l!l&&r!l&&!r
2127
MC/DC83%5/6
scalar @_ < 2 or scalar @_ > 2 or not ref $message or not ref $match or ref $message ne "HASH" or ref $match ne "HASH"
scalar @_ < 2 scalar @_ > 2 not ref $message not ref $match ref $message ne "HASH" ref $match ne "HASH"
Truth table85%6/7
A or B or not(C) or not(D) or E or F
A= scalar @_ < 2B= scalar @_ > 2C= not ref $messageD= not ref $matchE= ref $message ne "HASH"F= ref $match ne "HASH"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
17 if scalar @_ < 2 or
18 scalar @_ > 2 or
19 not ref $message or
20 not ref $match or
21 ref $message ne 'HASH' or
22 ref $match ne 'HASH';
23  
24 24 return _match($message, $match);
25 }
26  
27 5 ▶sub _special {
Subroutine _special: called CC=5 SCAR=16.8
28 5 my ($message, $match) = @_;
29 5 substr($match, 0, 8, '');
30 5 ▶ if($match =~ m{^/}) { #regex type
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ m[^/]) 4 1 5
31 4 my $re;
32 4 eval "\$re = qr$match;"; #this is hideously inefficient
33 #but it is highly cacheable, later on
34 4 ▶ if($message =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ /$re/) { } 2 2 4
35 2 ▶ if(%+) {
Branch: 1 branch
BranchTrueFalseTotal
if (%+) 0 2 2
36 0 ▶ while(my($key, $value) = each %+) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %+) 0 0 0
37 0 $named_regex->{$key} = $value;
38 }
39 }
40 2 return 1
41 } else {
42 2 return 0;
43 }
44 }
45 1 die "Message::Match::mmatch: special of unknown type passed: $match";
46 }
47  
48 56 ▶sub _match {
Subroutine _match: called CC=24 SCAR=37.2
49 56 my ($message, $match) = @_;
50 56 my $ref_message = ref $message; my $ref_match = ref $match;
51 56 ▶ if(not $ref_message and not $ref_match) { #scalar on both sides
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref_message or $ref_match) 21 35 56
Condition66%2/3
$ref_message or $ref_match
l!l&&r!l&&!r
35021
MC/DC50%1/2
$ref_message or $ref_match
$ref_message $ref_match
Truth table66%2/3
A or B
A= $ref_messageB= $ref_match
ABresult
1X1
011
000
52 21 ▶ if(substr($match, 0, 8) eq ' special') { #special handling
Branch: 1 branch
BranchTrueFalseTotal
if (substr($match, 0, 8) eq " special") 5 16 21
53 5 return _special($message, $match);
54 }
55 16 return $message eq $match; #otherwise, brain-dead comparison
56 }
57 35 ▶ if($ref_message eq 'JSON::PP::Boolean' and $ref_match eq 'JSON::PP::Boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_message eq "JSON::PP::Boolean" and $ref_match eq "JSON::PP::Boolean") 0 35 35
Condition33%1/3
$ref_message eq "JSON::PP::Boolean" and $ref_match eq "JSON::PP::Boolean"
!ll&&!rl&&r
3500
MC/DC0%0/2
$ref_message eq "JSON::PP::Boolean" and $ref_match eq "JSON::PP::Boolean"
$ref_message eq "JSON::PP::Boolean" $ref_match eq "JSON::PP::Boolean"
Truth table33%1/3
A and B
A= $ref_message eq "JSON::PP::Boolean"B= $ref_match eq "JSON::PP::Boolean"
ABresult
0X0
100
111
58 0 return "$message" eq "$match";
59 }
60 35 ▶ if($ref_message eq 'HASH' and $ref_match eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_message eq "HASH" and $ref_match eq "HASH") 31 4 35
Condition66%2/3
$ref_message eq "HASH" and $ref_match eq "HASH"
!ll&&!rl&&r
4031
MC/DC50%1/2
$ref_message eq "HASH" and $ref_match eq "HASH"
$ref_message eq "HASH" $ref_match eq "HASH"
Truth table66%2/3
A and B
A= $ref_message eq "HASH"B= $ref_match eq "HASH"
ABresult
0X0
100
111
61 31 foreach my $key (keys %$match) {
62 28 my $message = $message->{$key};
63 28 my $match = $match->{$key};
64 28 ▶ return 0 if not defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 2 26 28
65 26 ▶ return 0 if not defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match 0 26 26
66 26 ▶ return 0 unless _match($message, $match);
Branch: 1 branch
BranchTrueFalseTotal
unless _match($message, $match) 4 21 25
67 }
68 24 return 1;
69 }
70 4 ▶ if($ref_message eq 'ARRAY' and not $ref_match) { #check for scalar inside the array
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_message eq "ARRAY" and not $ref_match) 2 2 4
Condition66%2/3
$ref_message eq "ARRAY" and not $ref_match
!ll&&!rl&&r
022
MC/DC50%1/2
$ref_message eq "ARRAY" and not $ref_match
$ref_message eq "ARRAY" not $ref_match
Truth table66%2/3
A and not(B)
A= $ref_message eq "ARRAY"B= not $ref_match
ABresult
0X0
100
111
71 2 foreach my $item (@$message) {
72 5 ▶ return 1 if $item eq $match;
Branch: 1 branch
BranchTrueFalseTotal
if $item eq $match 1 4 5
73 }
74 1 return 0;
75 }
76 2 ▶ if($ref_message eq 'ARRAY' and $ref_match eq 'ARRAY') { #check the entire array
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_message eq "ARRAY" and $ref_match eq "ARRAY") 2 0 2
Condition33%1/3
$ref_message eq "ARRAY" and $ref_match eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
$ref_message eq "ARRAY" and $ref_match eq "ARRAY"
$ref_message eq "ARRAY" $ref_match eq "ARRAY"
Truth table33%1/3
A and B
A= $ref_message eq "ARRAY"B= $ref_match eq "ARRAY"
ABresult
0X0
100
111
77 2 foreach my $item (@$match) {
78 6 my $match = $item;
79 6 my $message = shift @{$message};
80 6 ▶ return 0 unless _match($message, $match);
Branch: 1 branch
BranchTrueFalseTotal
unless _match($message, $match) 0 6 6
81 }
82 2 return 1;
83 }
84 0 ▶ if($ref_message eq 'ARRAY' and $ref_match eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_message eq "ARRAY" and $ref_match eq "HASH") 0 0 0
Condition0%0/3
$ref_message eq "ARRAY" and $ref_match eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$ref_message eq "ARRAY" and $ref_match eq "HASH"
$ref_message eq "ARRAY" $ref_match eq "HASH"
Truth table0%0/3
A and B
A= $ref_message eq "ARRAY"B= $ref_match eq "HASH"
ABresult
0X0
100
111
85 #The idea is that if a message field is an array, and the
86 #match field is a hash, every element in the array must have a key in the
87 #hash in order to pass
88 0 foreach my $item (@$message) {
89 0 ▶ return 0 unless defined $match->{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match->{$item} 0 0 0
90 }
91 0 return 1;
92 }
93 0 return 0; #anything we don't know about fails
94 }
95 3 1;
96 __END__
↑ Summary