Filter/Rule.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 91.6% 11 / 12 Conditioncond 86.6% 13 / 15 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 94.2% 98 / 104 CC 14 SCAR 11.8
CC
14
Cov
95%
new
5.0
BEGIN
4.1
source
4.0
CRAP 3.3
« Filter/RulePool.pm ↑ Summary Filter/Hooker.pm »
1 #################################################################
2 #
3 # Hook::Filter::Rule - A filter rule
4 #
5 # $Id: Rule.pm,v 1.7 2008/06/09 21:04:08 erwan_lemonnier Exp $
6 #
7 # 060301 erwan Created
8 # 070516 erwan Small POD and layout fixes
9 # 070524 erwan Used BEGIN instead of INIT
10 # 080609 erwan Updated POD
11 #
12  
13 package Hook::Filter::Rule;
14  
15 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use Module::Pluggable search_path => ['Hook::Filter::Plugins'], require => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 13 our $VERSION='0.04';
24  
25 #----------------------------------------------------------------
26 #
27 # load test functions from plugins
28 #
29  
30 13 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=14.0
31  
32 13 my %TESTS;
33  
34 13 foreach my $plugin (Hook::Filter::Rule->plugins()) {
35 13 my @tests = $plugin->register();
36 # TODO: test that @tests is an array of strings. die with BUG:
37  
38 13 foreach my $test ($plugin->register()) {
39 39 ▶ if (exists $TESTS{$test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $TESTS{$test}) 0 39 39
40 0 croak "invalid plugin function: test function [$test] exported by plugin [$plugin] is already exported by an other plugin.";
41 }
42 39 *{ qualify_to_ref($test,"Hook::Filter::Rule") } = *{ qualify_to_ref($test,$plugin) };
43 39 $TESTS{$test} = 1;
44 }
45 }
46 }
47  
48 #----------------------------------------------------------------
49 #
50 # new - build a new filter rule
51 #
52  
53 38 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
54 38 my($pkg,$rule) = @_;
55 38 ▶ $pkg = ref $pkg || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
0380
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
56 38 my $self = bless({},$pkg);
57  
58 38 ▶ if (!defined $rule || ref \$rule ne "SCALAR" || scalar @_ != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rule or ref \$rule ne "SCALAR" or scalar @_ != 2) 4 34 38
Condition100%6/6
not defined $rule or ref \$rule ne "SCALAR" or scalar @_ != 2
l!l&&r!l&&!r
3134
not defined $rule or ref \$rule ne "SCALAR"
l!l&&r!l&&!r
1235
MC/DC100%3/3
not defined $rule or ref \$rule ne "SCALAR" or scalar @_ != 2
not defined $rule ref \$rule ne "SCALAR" scalar @_ != 2
Truth table100%4/4
not(A) or B or C
A= not defined $ruleB= ref \$rule ne "SCALAR"C= scalar @_ != 2
ABCresult
1XX1
01X1
0011
0000
59 4 shift @_;
60 4 croak "invalid parameter: Hook::Filter::Rule->new expects one string describing a filter rule, but got [".Dumper(@_)."].";
61 }
62  
63 34 $self->{RULE} = $rule;
64  
65 34 return $self;
66 }
67  
68 #----------------------------------------------------------------
69 #
70 # rule - accessor for the rule
71 #
72  
73 6 ▶sub rule {
Subroutine rule: called CC=1 SCAR=0.0
Pod documented
74 6 return $_[0]->{RULE};
75 }
76  
77 #----------------------------------------------------------------
78 #
79 # source - where the rule came from (used in error messages only)
80 #
81  
82 30 ▶sub source {
Subroutine source: called CC=4 SCAR=13.9
Pod documented
83 30 my($self,$orig) = @_;
84  
85 30 ▶ if (!defined $orig || ref \$orig ne "SCALAR" || scalar @_ != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $orig or ref \$orig ne "SCALAR" or scalar @_ != 2) 4 26 30
Condition100%6/6
not defined $orig or ref \$orig ne "SCALAR" or scalar @_ != 2
l!l&&r!l&&!r
3126
not defined $orig or ref \$orig ne "SCALAR"
l!l&&r!l&&!r
1227
MC/DC100%3/3
not defined $orig or ref \$orig ne "SCALAR" or scalar @_ != 2
not defined $orig ref \$orig ne "SCALAR" scalar @_ != 2
Truth table100%4/4
not(A) or B or C
A= not defined $origB= ref \$orig ne "SCALAR"C= scalar @_ != 2
ABCresult
1XX1
01X1
0011
0000
86 4 shift @_;
87 4 croak "invalid parameter: Hook::Filter::Rule->source expects one string, but got [".Dumper(@_)."].";
88 }
89  
90 26 $self->{SOURCE} = $orig;
91 }
92  
93 #----------------------------------------------------------------
94 #
95 # eval - evaluate a rule. return either true or false
96 #
97  
98 88 ▶sub eval {
Subroutine eval: called CC=4 SCAR=13.9
Pod documented
99 88 my $self = shift;
100 88 my $rule = $self->{RULE};
101  
102 88 my $res = eval $rule;
103 88 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 5 83 88
104 # in doubt, let's assume we are not filtering anything, ie allow function calls as if we were not here
105 warn "WARNING: invalid Hook::Filter rule [$rule] ".
106 5 ▶ ( (defined $self->{SOURCE})?"from file [".$self->{SOURCE}."] ":"")."caused error:\n".
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'SOURCE'} ? : 4 1 5
107 "[".$@."]. Assuming this rule returned true.\n";
108 5 return 1;
109 }
110  
111 83 ▶ return ($res)?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 29 54 83
112 }
113  
114 13 1;
115  
116 __END__
« Filter/RulePool.pm ↑ Summary Filter/Hooker.pm »