line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
################################################### |
2
|
|
|
|
|
|
|
# Text::XML::Count |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# A module to help test element occurance counts |
5
|
|
|
|
|
|
|
# in XML documents. |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# Author: Adam Kaplan |
8
|
|
|
|
|
|
|
################################################### |
9
|
|
|
|
|
|
|
# $Id$ |
10
|
|
|
|
|
|
|
################################################### |
11
|
|
|
|
|
|
|
package Test::XML::Count; |
12
|
|
|
|
|
|
|
|
13
|
3
|
|
|
3
|
|
20979
|
use 5.006000; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
114
|
|
14
|
3
|
|
|
3
|
|
16
|
use base 'Test::Builder::Module'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2669
|
|
15
|
3
|
|
|
3
|
|
12017
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
95
|
|
16
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
107
|
|
17
|
3
|
|
|
3
|
|
3887
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use Test::Builder; |
19
|
|
|
|
|
|
|
use Test::More; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
require Exporter; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our @EXPORT = qw( |
26
|
|
|
|
|
|
|
xml_node_count |
27
|
|
|
|
|
|
|
xml_min_nodes |
28
|
|
|
|
|
|
|
xml_max_nodes |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $TEST = Test::Builder->new(); |
34
|
|
|
|
|
|
|
my $PARSER = XML::LibXML->new(); |
35
|
|
|
|
|
|
|
my $LAST_XML = ''; |
36
|
|
|
|
|
|
|
my $LAST_DOC; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub import { |
39
|
|
|
|
|
|
|
my $class = shift @_; |
40
|
|
|
|
|
|
|
Test::XML::Count->export_to_level(1, $class); |
41
|
|
|
|
|
|
|
$TEST->exported_to(caller); |
42
|
|
|
|
|
|
|
$TEST->plan(@_); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _parse_and_get_nodelist { |
46
|
|
|
|
|
|
|
my $xml = shift or fail("XML string is not defined"); |
47
|
|
|
|
|
|
|
my $xpath = shift or fail("XPath string is not defined"); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Parsing is slow & probably not needed more than once per test file; Avoid. |
50
|
|
|
|
|
|
|
return $LAST_DOC if $xml eq $LAST_XML; |
51
|
|
|
|
|
|
|
# don't die if bad/unbalanced xml |
52
|
|
|
|
|
|
|
eval {$LAST_DOC = $PARSER->parse_string($xml)}; |
53
|
|
|
|
|
|
|
return ($@) ? $TEST->fail($@) : $LAST_DOC->findnodes($xpath); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
## Preloaded methods go here. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# boolean xml_node_count : xml string, xpath, number, test name |
59
|
|
|
|
|
|
|
sub xml_node_count($$$;$) { |
60
|
|
|
|
|
|
|
my ($xml, $xpath, $count, $name) = @_; |
61
|
|
|
|
|
|
|
$TEST->ok (_parse_and_get_nodelist($xml, $xpath)->size() == $count, $name); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# boolean xml_min_nodes : xml string, xpath, number, test name |
65
|
|
|
|
|
|
|
sub xml_min_nodes($$$;$) { |
66
|
|
|
|
|
|
|
my ($xml, $xpath, $count, $name) = @_; |
67
|
|
|
|
|
|
|
$TEST->ok (_parse_and_get_nodelist($xml, $xpath)->size() >= $count, $name); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# boolean xml_max_nodes : xml string, xpath, number, test name |
71
|
|
|
|
|
|
|
sub xml_max_nodes($$$;$) { |
72
|
|
|
|
|
|
|
my ($xml, $xpath, $count, $name) = @_; |
73
|
|
|
|
|
|
|
$TEST->ok (_parse_and_get_nodelist($xml, $xpath)->size() <= $count, $name); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
__END__ |