line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::XML::Deep; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
237749
|
use warnings; |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
194
|
|
4
|
6
|
|
|
6
|
|
30
|
use strict; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
406
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
34
|
use base 'Test::Builder::Module'; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
1682
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
3890
|
use XML::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use XML::Simple; |
10
|
|
|
|
|
|
|
use Test::Deep qw/deep_diag cmp_details/; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $Builder = __PACKAGE__->builder; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw/ cmp_xml_deeply /; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Test::XML::Deep = XML::Simple + Test::Deep |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 VERSION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Version 0.07 |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This module can be used to easily test that some XML has the structure and values you desire. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
It is particularly useful when some values in your XML document may differ from one test run |
34
|
|
|
|
|
|
|
to another (for example, a timestamp). |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
An Example: |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use Test::XML::Deep; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $xml = <
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
some data |
44
|
|
|
|
|
|
|
more data |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
EOXML |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $expected = { sometag => [ { attribute => 'value', |
49
|
|
|
|
|
|
|
content => 'some data' |
50
|
|
|
|
|
|
|
}, |
51
|
|
|
|
|
|
|
] |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
cmp_xml_deeply($xml, $expected); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
The real power comes from using Test::Deep and making use of the functions it exports (I.E. array_each(), re()): |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
use Test::Deep; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $expected = { sometag => array_each( { attribute => re('^\w+$'), |
62
|
|
|
|
|
|
|
content => re('data$'), |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
) |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
cmp_xml_deeply($xml, $expected); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
You can also pass in a filename: |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
cmp_xml_deeply('somefile.xml', { my_expected => 'data_structure' }); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 EXPORT |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
cmp_xml_deeply |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 FUNCTIONS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 cmp_xml_deeply( $xml, $hashref_expected, [ 'test name' ] ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$xml is a filename or string of XML that you'd like to test. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$hashref_expected should be the expected value of the XML as it would be parsed by XML::Simple. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
An optional test name is accepted as the third parameter. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub cmp_xml_deeply { |
91
|
|
|
|
|
|
|
my ( $xml, $expected, $name ) = @_; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $parse_method = 'parse'; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# allow filenames to work |
96
|
|
|
|
|
|
|
if( ( ref $xml && ref $xml ne 'SCALAR' ) |
97
|
|
|
|
|
|
|
|| $xml !~ m{<.*?>}s ) { |
98
|
|
|
|
|
|
|
$parse_method = 'parsefile'; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my $parser = new XML::Parser(Style => 'Tree'); |
102
|
|
|
|
|
|
|
eval { $parser->$parse_method($xml); }; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $not_ok = $@; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
if( $not_ok ){ |
108
|
|
|
|
|
|
|
( my $message = $not_ok ) =~ s/ at (?!line).*//g; # ick! |
109
|
|
|
|
|
|
|
$message =~ s/^\n//g; |
110
|
|
|
|
|
|
|
#chomp $message; |
111
|
|
|
|
|
|
|
$Builder->ok(0, $name); |
112
|
|
|
|
|
|
|
$Builder->diag("Failed to parse \n$xml\nXML::Parser error was: $message\n"); |
113
|
|
|
|
|
|
|
}else{ |
114
|
|
|
|
|
|
|
my $test = XMLin( $xml ); |
115
|
|
|
|
|
|
|
my ($ok, $stack) = cmp_details($test, $expected); |
116
|
|
|
|
|
|
|
if( not $Builder->ok($ok, $name) ){ |
117
|
|
|
|
|
|
|
my $diag = deep_diag($stack); |
118
|
|
|
|
|
|
|
$Builder->diag($diag); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Fergal Daly, for exporting cmp_details from Test::Deep lickety-split! |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Michael G Schwern, for L, which makes modules like this possible. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SOURCE |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
see: L |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 AUTHOR |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Jeff Lavallee, C<< >> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 BUGS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
140
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
141
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SUPPORT |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
perldoc Test::XML::Deep |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
You can also look for information at: |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over 4 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
L |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
L |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item * CPAN Ratings |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
L |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item * Search CPAN |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
L |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=back |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Copyright 2009 Jeff Lavallee, all rights reserved. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
179
|
|
|
|
|
|
|
under the same terms as Perl itself. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 SEE ALSO |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
L, L |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; # End of Test::XML::Deep |
189
|
|
|
|
|
|
|
|