XPath/Result.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 92.8% 65 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 10 totaltotal 90.7% 157 / 173 CC 36 SCAR 18.5
CC
36
Cov
96%
get_string
10.0
get_boolean
8.0
get_type
7.0
CRAP 6.4
« XPath/Tree.pm ↑ Summary XPath/Axis.pm »
1 package XML::Parser::Lite::Tree::XPath::Result;
2
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 #
7 # types:
8 #
9 # Error - value is error message string
10 # number - value is numeric scalar
11 # boolean - value is boolean scalar
12 # string - value is string scalar
13 # nodeset - value is arrayref of nodes and/or attributes
14 # node - value is node
15 # attribute - value is attribute
16 #
17
18 1691 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19 1691 my $class = shift;
20 1691 my $self = bless {}, $class;
21
22 1691 $self->{type} = shift;
23 1691 $self->{value} = shift;
24
25 1691 return $self;
26 }
27
28 3823 ▶sub is_error {
Subroutine is_error: called CC=2 SCAR=6.9
Pod undocumented
29 3823 my ($self) = @_;
30 3823 ▶ return ($self->{type} eq 'Error') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'type'} eq 'Error' ? : 7 3816 3823
31 }
32
33 288 ▶sub normalize {
Subroutine normalize: called CC=2 SCAR=7.0
Pod undocumented
34 288 my ($self) = @_;
35
36 288 ▶ if ($self->{type} eq 'nodeset'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "nodeset") 288 0 288
37
38 # uniquify and sort
39 288 my %seen = ();
40 my @tags = sort {
41 $a->{order} <=> $b->{order}
42 1703 } grep {
43 1107 ! $seen{$_->{order}} ++
44 288 } @{$self->{value}};
45
46 288 $self->{value} = \@tags;
47 }
48 }
49
50 145 ▶sub ret {
Subroutine ret: called CC=1 SCAR=0.0
Pod undocumented
51 145 my ($self, $a, $b) = @_;
52 145 return XML::Parser::Lite::Tree::XPath::Result->new($a, $b);
53 }
54
55 179 ▶sub get_type {
Subroutine get_type: called CC=7 SCAR=19.5
Pod undocumented
56 179 my ($self, $type) = @_;
57
58 179 ▶ return $self if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 0 179 179
59
60 179 ▶ return $self->get_number if $type eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "number" 133 46 179
61 46 ▶ return $self->get_boolean if $type eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" 23 23 46
62 23 ▶ return $self->get_string if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 1 22 23
63 22 ▶ return $self->get_nodeset if $type eq 'nodeset';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "nodeset" 19 3 22
64 3 ▶ return $self->get_node if $type eq 'node';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "node" 1 2 3
65
66 2 return $self->ret('Error', "Can't get type '$type'");
67 }
68
69 36 ▶sub get_boolean {
Subroutine get_boolean: called CC=8 SCAR=20.8
Pod undocumented
70 36 my ($self) = @_;
71
72 36 ▶ return $self if $self->{type} eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "boolean" 24 12 36
73 12 ▶ return $self if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 1 11 12
74
75 11 ▶ if ($self->{type} eq 'number'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "number") 3 8 11
76 3 ▶ return $self->ret('boolean', 0) if $self->{value} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"value"} eq "NaN" 1 2 3
77 2 return $self->ret('boolean', $self->{value} != 0);
78 }
79
80 8 ▶ if ($self->{type} eq 'string'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "string") 2 6 8
81 2 return $self->ret('boolean', length $self->{value} > 0);
82 }
83
84 6 ▶ if ($self->{type} eq 'nodeset'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "nodeset") 4 2 6
85 4 return $self->ret('boolean', scalar(@{$self->{value}}) > 0);
86 }
87
88 2 ▶ if ($self->{type} eq 'node'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "node") 0 2 2
89 # todo
90 }
91
92 2 return $self->ret('Error', "can't convert type $self->{type} to boolean");
93 }
94
95 128 ▶sub get_string {
Subroutine get_string: called CC=10 SCAR=23.0
Pod undocumented
96 128 my ($self) = @_;
97
98 128 ▶ return $self if $self->{type} eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "string" 108 20 128
99 20 ▶ return $self if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 1 19 20
100
101
102 19 ▶ if ($self->{type} eq 'nodeset'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "nodeset") 3 16 19
103 3 ▶ return $self->ret('string', '') unless scalar @{$self->{value}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"value"};} 1 2 3
104
105 2 my $node = $self->ret('node', $self->{value}->[0]);
106
107 2 return $node->get_string;
108 }
109
110 16 ▶ if ($self->{type} eq 'node'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "node") 7 9 16
111
112 7 ▶ return $self->ret('string', $self->{value}->{value}) if $self->{value}->{type} eq 'attribute';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"value"}{"type"} eq "attribute" 7 0 7
113
114 0 die "can't convert a node of type $self->{value}->{type} to a string";
115 }
116
117 9 ▶ if ($self->{type} eq 'number'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "number") 5 4 9
118 5 return $self->ret('string', "$self->{value}");
119 }
120
121 4 ▶ if ($self->{type} eq 'boolean'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "boolean") 2 2 4
122 2 ▶ return $self->ret('string', $self->{value} ? 'true' : 'false');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'value'} ? : 1 1 2
123 }
124
125 2 return $self->ret('Error', "can't convert type $self->{type} to string");
126 }
127
128 166 ▶sub get_nodeset {
Subroutine get_nodeset: called CC=4 SCAR=13.9
Pod undocumented
129 166 my ($self) = @_;
130
131 166 ▶ return $self if $self->{type} eq 'nodeset';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "nodeset" 66 100 166
132 100 ▶ return $self if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 1 99 100
133
134 99 ▶ if ($self->{type} eq 'node'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "node") 94 5 99
135 94 return $self->ret('nodeset', [$self->{value}]);
136 }
137
138 5 return $self->ret('Error', "can't convert type $self->{type} to nodeset");
139 }
140
141 6 ▶sub get_node {
Subroutine get_node: called CC=3 SCAR=11.0
Pod undocumented
142 6 my ($self) = @_;
143
144 6 ▶ return $self if $self->{type} eq 'node';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "node" 0 6 6
145 6 ▶ return $self if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 1 5 6
146
147 5 return $self->ret('Error', "can't convert type $self->{type} to node");
148 }
149
150 167 ▶sub get_number {
Subroutine get_number: called CC=7 SCAR=19.5
Pod undocumented
151 167 my ($self) = @_;
152
153 167 ▶ return $self if $self->{type} eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "number" 157 10 167
154 10 ▶ return $self if $self->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_error 1 9 10
155
156 9 ▶ if ($self->{type} eq 'string'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "string") 5 4 9
157 5 ▶ if ($self->{value} =~ m!^[\x20\x09\x0D\x0A]*(-?([0-9]+(\.([0-9]+)?)?)|(\.[0-9]+))[\x20\x09\x0D\x0A]*$!){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'value'} =~ /^[\x20\x09\x0D\x0A]*(-?([0-9]+(\.([0-9]+)?)?)|(\.[0-9]+))[\x20\x09\x0D\x0A]*$/) { } 4 1 5
158
159 4 return $self->ret('number', $1);
160 }else{
161 1 return $self->ret('number', 'NaN');
162 }
163 }
164
165 4 ▶ if ($self->{type} eq 'boolean'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "boolean") 2 2 4
166 2 ▶ return $self->ret('number', $self->{value}?1:0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'value'} ? : 1 1 2
167 }
168
169 2 return $self->ret('Error', "can't convert type $self->{type} to number");
170 }
171
172 34 1;
« XPath/Tree.pm ↑ Summary XPath/Axis.pm »