blib/lib/Test/XPath.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 85.2% 29 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 11 / 11 totaltotal 92.3% 144 / 156 CC 21 SCAR 15.2
CC
21
Cov
93%
new
8.2
_doc
8.0
ok
5.0
CRAP 4.6
↑ Summary
1 package Test::XPath;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use 5.6.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use XML::LibXML '1.69';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '0.20';
9  
10 18 ▶sub new {
Subroutine new: called CC=8 SCAR=21.0
Pod documented
11 18 my ($class, %p) = @_;
12 18 ▶ my $doc = delete $p{doc} || _doc(\%p);
Condition66%2/3
delete $p{'doc'} || _doc(\%p)
l!l&&r!l&&!r
1170
MC/DC0%0/2
delete $p{'doc'} || _doc(\%p)
delete $p{'doc'} _doc(\%p)
Truth table66%2/3
A || B
A= delete $p{'doc'}B= _doc(\%p)
ABresult
1X1
011
000
13 17 my $xpc = XML::LibXML::XPathContext->new( $doc->documentElement );
14 17 ▶ if (my $ns = $p{xmlns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $p{"xmlns"}) 3 14 17
15 3 ▶ while (my ($k, $v) = each %{ $ns }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ns;}) 4 3 7
16 4 $xpc->registerNs( $k => $v );
17 }
18 }
19 return bless {
20 xpc => $xpc,
21 node => $doc->documentElement,
22 17 filter => do {
23 17 ▶ if (my $f = $p{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $p{'filter'}) { } 1 16 17
24 1 ▶ if (ref $f eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $f eq 'CODE') { } 0 1 1
elsif ($f eq 'css_selector') { } 1 0 1
25 0 $f;
26 } elsif ($f eq 'css_selector') {
27 1 ▶ eval 'use HTML::Selector::XPath 0.06';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶ die 'Please install HTML::Selector::XPath to use CSS selectors'
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
29 if $@;
30 22 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
31 22 my $xpath = do {
32 22 my $xp = HTML::Selector::XPath->new(shift)->to_xpath(root => '//');
33 22 ▶ if (eval { $_->isa(__PACKAGE__) } && $_->node ne $doc->documentElement) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $_->isa(__PACKAGE__) } } and $_->node ne $doc->documentElement) { } 4 18 22
Condition66%2/3
eval { do { $_->isa(__PACKAGE__) } } and $_->node ne $doc->documentElement
!ll&&!rl&&r
1804
MC/DC50%1/2
eval { do { $_->isa(__PACKAGE__) } } and $_->node ne $doc->documentElement
eval { do { $_->isa(__PACKAGE__) } } $_->node ne $doc->documentElement
Truth table66%2/3
A and B
A= eval { do { $_->isa(__PACKAGE__) } }B= $_->node ne $doc->documentElement
ABresult
0X0
100
111
34 # Make it relative to the current node.
35 4 $xp =~ s{^///[*]}{.};
36 } else {
37 # Start from the top.
38 18 $xp =~ s{^///[*]}{};
39 }
40 22 $xp;
41 };
42 22 return $xpath;
43 }
44 1 } else {
45 0 die "Unknown filter: $f\n";
46 }
47 } else {
48 101 ▶ sub { shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 16 }
50 },
51 }, $class;
52 }
53  
54 72 ▶sub ok {
Subroutine ok: called CC=5 SCAR=16.1
Pod documented
55 72 my ($self, $xpath, $code, $desc) = @_;
56 72 my $xpc = $self->{xpc};
57 72 my $Test = Test::Builder->new;
58 72 $xpath = $self->{filter}->($xpath, $self);
59  
60 # Code and desc can be reversed, to support PerlX::MethodCallWithBlock.
61 72 ▶ ($code, $desc) = ($desc, $code) if ref $desc eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $desc eq "CODE" 0 72 72
62  
63 72 ▶ if (ref $code eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code eq 'CODE') { } 15 57 72
64 # Gonna do some recursive testing.
65 my @nodes = $xpc->findnodes($xpath, $self->{node})
66 15 ▶ or return $Test->ok(0, $desc);
Branch: 1 branch
BranchTrueFalseTotal
my(@nodes) = $xpc->findnodes($xpath, $self->{"node"}) or return $Test->ok(0, $desc) 15 0 15
67  
68 # Record the current test result.
69 15 my $ret = $Test->ok(1, $desc);
70  
71 # Call the code ref on each found node.
72 15 local $_ = $self;
73 15 for my $node (@nodes) {
74 29 local $self->{node} = $node;
75 29 $code->($self);
76 }
77 15 return $ret;
78 } else {
79 # We're just testing for existence ($code is description).
80 57 $Test->ok( $xpc->exists($xpath, $self->{node}), $code);
81 }
82  
83 }
84  
85 4 ▶sub not_ok {
Subroutine not_ok: called CC=1 SCAR=0.0
Pod documented
86 4 my ($self, $xpath, $desc) = @_;
87 4 $xpath = $self->{filter}->($xpath);
88 4 my $Test = Test::Builder->new;
89 4 $Test->ok( !$self->{xpc}->exists($xpath, $self->{node}), $desc);
90 }
91  
92 26 ▶sub is { Test::Builder::new->is_eq( shift->find_value(shift), @_) }
Subroutine is: called CC=1 SCAR=0.0
Pod documented
93 5 ▶sub isnt { Test::Builder::new->isnt_eq( shift->find_value(shift), @_) }
Subroutine isnt: called CC=1 SCAR=0.0
Pod documented
94 5 ▶sub like { Test::Builder::new->like( shift->find_value(shift), @_) }
Subroutine like: called CC=1 SCAR=0.0
Pod documented
95 5 ▶sub unlike { Test::Builder::new->unlike( shift->find_value(shift), @_) }
Subroutine unlike: called CC=1 SCAR=0.0
Pod documented
96 5 ▶sub cmp_ok { Test::Builder::new->cmp_ok( shift->find_value(shift), @_) }
Subroutine cmp_ok: called CC=1 SCAR=0.0
Pod documented
97  
98 4 ▶sub node { shift->{node} }
Subroutine node: called CC=1 SCAR=0.0
Pod documented
99 1 ▶sub xpc { shift->{xpc} }
Subroutine xpc: called CC=1 SCAR=0.0
Pod documented
100  
101 47 ▶sub find_value {
Subroutine find_value: called CC=1 SCAR=0.0
Pod documented
102 47 my $self = shift;
103 47 $self->{xpc}->findvalue( $self->{filter}->(shift), $self->{node} );
104 }
105  
106 17 ▶sub _doc {
Subroutine _doc: called CC=8 SCAR=20.8
107 17 my $p = shift;
108  
109 # Create and configure the parser.
110 17 my $parser = XML::LibXML->new;
111  
112 # Apply any parser options.
113 17 ▶ if (my $opts = $p->{options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = $p->{"options"}) 3 14 17
114 3 ▶ while (my ($k, $v) = each %{ $opts }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$opts;}) 7 3 10
115 7 ▶ if (my $meth = $parser->can($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meth = $parser->can($k)) { } 6 1 7
116 6 $parser->$meth($v)
117 } else {
118 1 $parser->set_option($k => $v);
119 }
120 }
121 }
122  
123 # Parse and return the document.
124 17 ▶ if ($p->{xml}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"xml"}) 11 6 17
125 return $p->{is_html}
126 ? $parser->parse_html_string($p->{xml})
127 11 ▶ : $parser->parse_string($p->{xml});
Branch: 1 branch
BranchTrueFalseTotal
$p->{'is_html'} ? : 3 8 11
128 }
129  
130 6 ▶ if ($p->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"file"}) 5 1 6
131 return $p->{is_html}
132 ? $parser->parse_html_file($p->{file})
133 5 ▶ : $parser->parse_file($p->{file});
Branch: 1 branch
BranchTrueFalseTotal
$p->{'is_html'} ? : 1 4 5
134 }
135  
136 1 require Carp;
137 1 Carp::croak(
138 'Test::XPath->new requires the "xml", "file", or "doc" parameter'
139 );
140 }
141  
142 # Add Test::XML::XPath compatibility?
143 # sub like_xpath($$;$) { __PACKAGE__->new( xml => shift )->ok( @_ ) }
144 # sub unlike_xpath($$;$) { __PACKAGE__->new( xml => shift )->not_ok( @_ ) }
145 # sub is_xpath($$$;$) { __PACKAGE__->new( xml => shift )->is( @_ ) }
146  
147 7 1;
148 __END__
↑ Summary