blib/lib/Markup/MatchNode.pm

filter: Statementstmt 62.6% 62 / 99 Branchbran 38.0% 19 / 50 Conditioncond 40.0% 8 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 90.0% 9 / 10 Podpod 33.3% 1 / 3 totaltotal 51.2% 100 / 195 CC 38 SCAR 36.6
CC
38
Cov
53%
compare_to
82.2
__ANON__
11.2
init
9.9
CRAP 38.8
↑ Summary
1 package Markup::MatchNode;
2 1 $VERSION = '1.0.0';
3
4 ####################################################
5 # This module is protected under the terms of the
6 # GNU GPL. Please see
7 # http://www.opensource.org/licenses/gpl-license.php
8 # for more information.
9 ####################################################
10
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 require Markup::TreeNode;
15
16 1 our @ISA = qw(Markup::TreeNode);
17 1 our $empty = '(empty)';
18
19 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
20 1 my $invocant = shift();
21 1 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
22 1 $class = bless {
23 element_type => 'tag',
24 tagname => '',
25 attr => { },
26 level => 0,
27 parent => $empty,
28 child_num => 0,
29 children => [ ],
30 text => '',
31 options => { }
32 }, $class;
33 1 $class->init (@_);
34 1 return $class;
35 }
36
37 1 ▶sub init {
Subroutine init: called CC=6 SCAR=22.9
Pod undocumented
38 1 my $self = shift();
39 1 my %args = @_;
40
41 1 foreach (keys %args) {
42 # enforce integrity
43 2 ▶ if ($_ eq 'parent' && $args{$_} ne $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "parent" and $args{$_} ne $Markup::MatchNode::empty) 0 2 2
Condition33%1/3
$_ eq "parent" and $args{$_} ne $Markup::MatchNode::empty
!ll&&!rl&&r
200
MC/DC0%0/2
$_ eq "parent" and $args{$_} ne $Markup::MatchNode::empty
$_ eq "parent" $args{$_} ne $Markup::MatchNode::empty
Truth table33%1/3
A and B
A= $_ eq "parent"B= $args{$_} ne $Markup::MatchNode::empty
ABresult
0X0
100
111
44 0 $self->attach_parent($args{$_});
45 0 next;
46 }
47
48 # enforce integrity
49 2 ▶ if ($_ eq 'children') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "children") 0 2 2
50 0 $self->attach_children($args{$_});
51 0 next;
52 }
53
54 2 ▶ if (exists $self->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$_}) { } 2 0 2
55 2 $self->{$_} = $args{$_};
56 }
57 else {
58 0 croak ("unrecognized node option $_");
59 }
60 }
61 }
62
63 1 ▶sub _ignore_me {
Subroutine _ignore_me: called CC=3 SCAR=14.2
64 1 my $self = shift();
65
66 1 ▶ while (($self = $self->{'parent'}) ne $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self = $self->{"parent"}) ne $Markup::MatchNode::empty) 0 1 1
67 0 ▶ return 1 if ($self->{'options'}->{'ignore_children'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"ignore_children"} 0 0 0
68 }
69
70 1 return 0;
71 }
72
73 1 ▶sub compare_to {
Subroutine compare_to: called CC=20 SCAR=44.1
Pod documented
74 1 my ($self, $treenode) = @_;
75
76 1 ▶ return undef if ($self->_ignore_me());
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ignore_me 0 1 1
77
78 1 ▶ if (scalar(@{ $self->{'options'}->{'call_filter'} || [] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"options"}{"call_filter"} or [];}) 0 1 1
Condition50%1/2
$self->{'options'}{'call_filter'} || []
l!l
01
MC/DC0%0/1
$self->{'options'}{'call_filter'} || []
$self->{'options'}{'call_filter'}
Truth table50%1/2
A
A= $self->{'options'}{'call_filter'}
Aresult
11
00
79 0 foreach (@{$self->{'options'}->{'call_filter'}}) {
80 0 $_->($self);
81 }
82
83 0 return undef;
84 }
85
86 1 my (@minor, @major);
87 1 @minor = (0, 0, 0, '');
88 1 @major = (0, 0, 0, '');
89 # array positions: [0] == possible errors : [1] == correct points : [2] percent correct : [3] errors
90 1 ▶ my $similar = sub {
Subroutine __ANON__: called CC=6 SCAR=24.2
91 1 my ($nodeA, $nodeB) = @_;
92
93 1 ▶ if ($nodeA eq $empty && $nodeB eq $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodeA eq $Markup::MatchNode::empty and $nodeB eq $Markup::MatchNode::empty) 0 1 1
Condition33%1/3
$nodeA eq $Markup::MatchNode::empty and $nodeB eq $Markup::MatchNode::empty
!ll&&!rl&&r
100
MC/DC0%0/2
$nodeA eq $Markup::MatchNode::empty and $nodeB eq $Markup::MatchNode::empty
$nodeA eq $Markup::MatchNode::empty $nodeB eq $Markup::MatchNode::empty
Truth table33%1/3
A and B
A= $nodeA eq $Markup::MatchNode::emptyB= $nodeB eq $Markup::MatchNode::empty
ABresult
0X0
100
111
94 0 return (1);
95 }
96
97 1 ▶ if ($nodeA eq $empty || $nodeB eq $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodeA eq $Markup::MatchNode::empty or $nodeB eq $Markup::MatchNode::empty) 0 1 1
Condition33%1/3
$nodeA eq $Markup::MatchNode::empty or $nodeB eq $Markup::MatchNode::empty
l!l&&r!l&&!r
001
MC/DC0%0/2
$nodeA eq $Markup::MatchNode::empty or $nodeB eq $Markup::MatchNode::empty
$nodeA eq $Markup::MatchNode::empty $nodeB eq $Markup::MatchNode::empty
Truth table33%1/3
A or B
A= $nodeA eq $Markup::MatchNode::emptyB= $nodeB eq $Markup::MatchNode::empty
ABresult
1X1
011
000
98 0 return (0);
99 }
100
101 return ((($nodeA->{'element_type'} eq $nodeB->{'element_type'}) &&
102 1 ▶ ($nodeA->{'tagname'} eq $nodeB->{'tagname'})));
Condition33%1/3
$nodeA->{'element_type'} eq $nodeB->{'element_type'} && $nodeA->{'tagname'} eq $nodeB->{'tagname'}
!ll&&!rl&&r
001
MC/DC0%0/2
$nodeA->{'element_type'} eq $nodeB->{'element_type'} && $nodeA->{'tagname'} eq $nodeB->{'tagname'}
$nodeA->{'element_type'} eq $nodeB->{'element_type'} $nodeA->{'tagname'} eq $nodeB->{'tagname'}
Truth table33%1/3
A && B
A= $nodeA->{'element_type'} eq $nodeB->{'element_type'}B= $nodeA->{'tagname'} eq $nodeB->{'tagname'}
ABresult
0X0
100
111
103 1 };
104 1 ▶ my $calc = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
105 1 my ($minor, $major, $list) = @_;
106
107 1 $minor->[3] =~ s/^://;
108 1 $major->[3] =~ s/^://;
109
110 1 $minor->[2] = _percent($minor->[1], $minor->[0]);
111 1 $major->[2] = _percent($major->[1], $major->[0]);
112 1 ▶ return $list ? ($minor, $major) : int((($minor->[2] + $major->[2]) / 2));
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 0 1 1
113 1 };
114
115 1 ▶ if ($self->{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"text"}) 1 0 1
116 1 $minor[0]++;
117 1 ▶ if ($self->{'options'}->{'text_not_null'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'options'}{'text_not_null'}) { } 0 1 1
elsif ($self->{'text'} =~ /^{!(.+)!}/) { } 0 1 1
elsif ($self->{'text'} eq $treenode->{'text'}) { } 1 0 1
118 0 ▶ if ($treenode->{'text'} =~ m/(?:\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($treenode->{'text'} =~ /(?:\S+)/) { } 0 0 0
119 0 $minor[1]++;
120 }
121 else {
122 0 $minor[3] .= ':text is null where option text_not_null was specified';
123 }
124 }
125 elsif ($self->{'text'} =~ m/^{!(.+)!}/) {
126 0 ▶ if ($treenode->{'text'} =~ m/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($treenode->{'text'} =~ /$1/) { } 0 0 0
127 0 $minor[1]++;
128 }
129 else {
130 0 $minor[3] .= ":your regular expression /$1/ did not match the text";
131 }
132 }
133 elsif ($self->{'text'} eq $treenode->{'text'}) {
134 1 $minor[1]++;
135 }
136 else {
137 0 $minor[3] .= ':unmatched text';
138 }
139 }
140
141 1 $_ = \@minor;
142
143 # these are tags which, if out of place, I find to be major errors
144 1 foreach my $j (qw(table td tr tbody map object img body head title html)) {
145 11 ▶ if ($self->{'tagname'} eq $j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tagname"} eq $j) 0 11 11
146 0 $_ = \@major; last;
147 }
148 }
149
150 1 $_->[0]++;
151 1 ▶ if ($similar->($self, $treenode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$similar($self, $treenode)) { } 1 0 1
152 1 $_->[1]++;
153 }
154 else {
155 # optional nodes get special treatment :)
156 0 ▶ if ($self->{'options'}->{'optional'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"optional"}) 0 0 0
157 0 return 'optional';
158 }
159
160 0 $_->[3] .= ':nodes are not simliar';
161
162 # no point in going on - this node is out of place!
163 0 return $calc->(\@minor, \@major, wantarray);
164 }
165
166 1 ▶ unless ($self->{'options'}->{'ignore_attrs'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"options"}{"ignore_attrs"}) 1 0 1
167 1 foreach (keys %{ $self->{'attr'} }) {
168 0 $minor[0]++;
169 0 ▶ if ($self->{'attr'}->{$_} =~ m/^{!(.+)!}/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'attr'}{$_} =~ /^{!(.+)!}/) { } 0 0 0
elsif ($self->{'attr'}{$_} eq $treenode->{'attr'}{$_}) { } 0 0 0
170 0 ▶ if ($treenode->{'attr'}->{$_} =~ m/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($treenode->{'attr'}{$_} =~ /$1/) { } 0 0 0
171 0 $minor[1]++;
172 }
173 else {
174 0 $minor[3] .= ":attribute '$_' could not be matched with /$1/";
175 }
176 }
177 elsif ($self->{'attr'}->{$_} eq $treenode->{'attr'}->{$_}) {
178 0 $minor[1]++;
179 }
180 else {
181 0 $minor[3] .= ":attribute '$_' could not be matched";
182 }
183 }
184 }
185
186 1 return $calc->(\@minor, \@major, wantarray);
187 }
188
189 2 ▶sub _percent {
Subroutine _percent: called CC=4 SCAR=14.2
190 2 my ($div, $by) = @_;
191 2 ▶ return 100 if (!$div && !$by);
Branch: 1 branch
BranchTrueFalseTotal
unless $div or $by 1 1 2
Condition66%2/3
$div or $by
l!l&&r!l&&!r
101
MC/DC50%1/2
$div or $by
$div $by
Truth table66%2/3
A or B
A= $divB= $by
ABresult
1X1
011
000
192 1 ▶ return (int(($div /((!$by) ? 1 : $by)) * 100));
Branch: 1 branch
BranchTrueFalseTotal
!$by ? : 0 1 1
193 }
194
195 0 ▶sub _avg {
Subroutine _avg: not called CC=2 SCAR=17.9
196 0 my ($res, $cnt);
197 0 foreach (@_) {
198 0 $res += $_;
199 0 $cnt++;
200 }
201 0 return (int($res / $cnt));
202 }
203
204 1 1;
205
206 __END__
↑ Summary