/usr/share/perl5/Test/XML/Simple.pm is in libtest-xml-simple-perl 1.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | package Test::XML::Simple;
use strict;
use warnings;
our $VERSION = '1.04';
use Test::Builder;
use Test::More;
use Test::LongString;
use XML::LibXML;
my $Test = Test::Builder->new();
my $Xml;
sub import {
my $self = shift;
my $caller = caller;
no strict 'refs';
*{$caller.'::xml_valid'} = \&xml_valid;
*{$caller.'::xml_node'} = \&xml_node;
*{$caller.'::xml_is'} = \&xml_is;
*{$caller.'::xml_is_long'} = \&xml_is_long;
*{$caller.'::xml_is_deeply'} = \&xml_is_deeply;
*{$caller.'::xml_is_deeply_long'} = \&xml_is_deeply_long;
*{$caller.'::xml_like'} = \&xml_like;
*{$caller.'::xml_like_long'} = \&xml_like_long;
$Test->exported_to($caller);
$Test->plan(@_);
}
sub xml_valid($;$) {
my ($xml, $comment) = @_;
my $parsed_xml = _valid_xml($xml);
return 0 unless $parsed_xml;
ok $parsed_xml, $comment;
}
sub _valid_xml {
my $xml = shift;
local $Test::Builder::Level = $Test::Builder::Level + 2;
return fail("XML is not defined") unless defined $xml;
return fail("XML is missing") unless $xml;
if ( ref $xml ) {
return fail("accept only 'XML::LibXML::Document' as object") unless ref $xml eq 'XML::LibXML::Document';
$Xml = $xml;
}
else {
return fail("string can't contain XML: no tags")
unless ($xml =~ /</ and $xml =~/>/);
eval { $Xml = XML::LibXML->new->parse_string($xml); };
do { chomp $@; return fail($@) } if $@;
}
return $Xml;
}
sub _find {
my ($xml_xpath, $xpath) = @_;
my @nodeset = $xml_xpath->findnodes($xpath);
local $Test::Builder::Level = $Test::Builder::Level + 2;
return fail("Couldn't find $xpath") unless @nodeset;
wantarray ? @nodeset : \@nodeset;
}
sub xml_node($$;$) {
my ($xml, $xpath, $comment) = @_;
my $parsed_xml = _valid_xml($xml);
return 0 unless $parsed_xml;
my $nodeset = _find($parsed_xml, $xpath);
return 0 if !$nodeset;
ok(scalar @$nodeset, $comment);
}
sub xml_is($$$;$) {
_xml_is(\&is_string, @_);
}
sub xml_is_long($$$;$) {
_xml_is(\&is, @_);
}
sub _xml_is {
my ($comp_sub, $xml, $xpath, $value, $comment) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 2;
my $parsed_xml = _valid_xml($xml);
return 0 unless $parsed_xml;
my $nodeset = _find($parsed_xml, $xpath);
return 0 if !$nodeset;
foreach my $node (@$nodeset) {
my @kids = $node->getChildNodes;
if (@kids) {
$comp_sub->($kids[0]->toString, $value, $comment);
}
else {
my $got = $node->toString;
$got =~ s/^.*="(.*)"/$1/;
is $got, $value, $comment;
}
}
}
sub xml_is_deeply($$$;$) {
_xml_is_deeply(\&is_string, @_);
}
sub xml_is_deeply_long($$$;$) {
_xml_is_deeply(\&is, @_);
}
sub _xml_is_deeply {
my ($is_sub, $xml, $xpath, $candidate, $comment) = @_;
my $parsed_xml = _valid_xml($xml);
return 0 unless $parsed_xml;
my $candidate_xp;
eval {$candidate_xp = XML::LibXML->new->parse_string($candidate) };
return 0 unless $candidate_xp;
my $parsed_thing = $parsed_xml->findnodes($xpath)->[0];
my $candidate_thing = $candidate_xp->findnodes('/')->[0];
$candidate_thing = $candidate_thing->documentElement
if $parsed_thing->isa('XML::LibXML::Element');
$is_sub->($parsed_thing->toString,
$candidate_thing->toString,
$comment);
}
sub xml_like($$$;$) {
_xml_like(\&like_string, @_);
}
sub xml_like_long($$$;$) {
_xml_like(\&like, @_);
}
sub _xml_like {
my ($like_sub, $xml, $xpath, $regex, $comment) = @_;
my $parsed_xml = _valid_xml($xml);
return 0 unless $parsed_xml;
my $nodeset = _find($parsed_xml, $xpath);
return 0 if !$nodeset;
foreach my $node (@$nodeset) {
my @kids = $node->getChildNodes;
my $found;
if (@kids) {
foreach my $kid (@kids) {
if ($kid->toString =~ /$regex/) {
$found = 1;
return $like_sub->($kid->toString, $regex, $comment);
}
}
if (! $found) {
$comment = "(no comment)" unless defined $comment;
local $Test::Builder::Level = $Test::Builder::Level + 2;
return ok(0, "$comment - no match in tag contents (including CDATA)");
}
}
else {
my $got = $node->toString;
$got =~ s/^.*="(.*)"/$1/;
local $Test::Builder::Level = $Test::Builder::Level + 2;
return $like_sub->(like $got, $regex, $comment);
}
}
}
1;
__END__
=head1 NAME
Test::XML::Simple - easy testing for XML
=head1 SYNOPSIS
use Test::XML::Simple tests => 8;
# pass string with XML as argument
xml_valid $xml, "Is valid XML";
xml_node $xml, "/xpath/expression", "specified xpath node is present";
xml_is, $xml, '/xpath/expr', "expected value", "specified text present";
xml_like, $xml, '/xpath/expr', qr/expected/, "regex text present";
xml_is_deeply, $xml, '/xpath/expr', $xml2, "structure and contents match";
# XML::LibXML::Document can be passed as argument too
# that allow you to test a big documents with several tests
my $xml_doc = XML::LibXML->createDocument( '1.0' );
xml_valid $xml_doc, 'Is valid XML';
xml_node $xml_doc, '/xpath/expression', 'specified xpath node is present';
xml_like, $xml_doc, '/xpath/expression', qr/expected result/, 'regex present';
# Not yet implemented:
# xml_like_deeply would be nice too...
=head1 DESCRIPTION
C<Test::XML::Simple> is a very basic class for testing XML. It uses the XPath
syntax to locate nodes within the XML. You can also check all or part of the
structure vs. an XML fragment.
All routines accept as first argument string with XML or XML::LibXML::Document object.
=head1 TEST ROUTINES
=head2 xml_valid $xml, 'test description'
Pass an XML file or fragment to this test; it succeeds if the XML (fragment)
is valid.
=head2 xml_node $xml, $xpath, 'test description'
Checks the supplied XML to see if the node described by the supplied XPath
expression is present. Test fails if it is not present.
=head2 xml_is_long $xml, $xpath, $value, 'test description'
Finds the node corresponding to the supplied XPath expression and
compares it to the supplied value. Succeeds if the two values match.
Uses Test::More's C<is> function to do the comparison.
=head2 xml_is $xml, $xpath, $value, 'test description'
Finds the node corresponding to the supplied XPath expression and
compares it to the supplied value. Succeeds if the two values match.
Uses Test::LongString's C<is_string> function to do the test.
=head2 xml_like_long $xml, $xpath, $regex, 'test description'
Find the XML corresponding to the the XPath expression and check it
against the supplied regular expression. Succeeds if they match.
Uses Test::More's C<like> function to do the comparison.
=head2 xml_like $xml, $xpath, $regex, 'test description'
Find the XML corresponding to the the XPath expression and check it
against the supplied regular expression. Succeeds if they match.
Uses Test::LongString's C<like_string> function to do the test.
=head2 xml_is_deeply_long $xml, $xpath, $xml2, 'test description'
Find the piece of XML corresponding to the XPath expression,
and compare its structure and contents to the second XML
(fragment) supplied. Succeeds if they match in structure and
content. Uses Test::More's C<is> function to do the comparison.
=head2 xml_is_deeply $xml, $xpath, $xml2, 'test description'
Find the piece of XML corresponding to the XPath expression,
and compare its structure and contents to the second XML
(fragment) supplied. Succeeds if they match in structure and
content. Uses Test::LongString's C<is_string> function to do the test.
=head1 AUTHOR
Joe McMahon, E<lt>mcmahon@cpan.orgE<gt>
=head1 LICENSE
Copyright (c) 2005-2013 by Yahoo! and Joe McMahon
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself, either Perl version 5.6.1 or, at
your option, any later version of Perl 5 you may have available.
=head1 SEE ALSO
L<XML::LibXML>, L<Test::More>, L<Test::Builder>.
=cut
|