| Test-XML-Deep documentation | Contained in the Test-XML-Deep distribution. |
Test::XML::Deep = XML::Simple + Test::Deep
Version 0.07
This module can be used to easily test that some XML has the structure and values you desire.
It is particularly useful when some values in your XML document may differ from one test run to another (for example, a timestamp).
An Example:
use Test::XML::Deep;
my $xml = <<EOXML;
<?xml version="1.0" encoding="UTF-8"?>
<example>
<sometag attribute="value">some data</sometag>
<sometag attribute="other">more data</sometag>
</example>
EOXML
my $expected = { sometag => [ { attribute => 'value',
content => 'some data'
},
]
};
cmp_xml_deeply($xml, $expected);
The real power comes from using Test::Deep and making use of the functions it exports (I.E. array_each(), re()):
use Test::Deep;
my $expected = { sometag => array_each( { attribute => re('^\w+$'),
content => re('data$'),
}
)
};
cmp_xml_deeply($xml, $expected);
You can also pass in a filename:
cmp_xml_deeply('somefile.xml', { my_expected => 'data_structure' });
cmp_xml_deeply
$xml is a filename or string of XML that you'd like to test.
$hashref_expected should be the expected value of the XML as it would be parsed by XML::Simple.
An optional test name is accepted as the third parameter.
Fergal Daly, for exporting cmp_details from Test::Deep lickety-split!
Michael G Schwern, for Test::Builder, which makes modules like this possible.
Jeff Lavallee, <jeff at zeroclue.com>
Please report any bugs or feature requests to bug-test-xml-deep at rt.cpan.org, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-XML-Deep. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Test::XML::Deep
You can also look for information at:
Copyright 2009 Jeff Lavallee, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Test-XML-Deep documentation | Contained in the Test-XML-Deep distribution. |
package Test::XML::Deep; use warnings; use strict; use base 'Test::Builder::Module'; use XML::Parser; use XML::Simple; use Test::Deep qw/deep_diag cmp_details/; my $Builder = __PACKAGE__->builder; our @EXPORT = qw/ cmp_xml_deeply /;
our $VERSION = '0.07';
sub cmp_xml_deeply { my ( $xml, $expected, $name ) = @_; my $parse_method = 'parse'; # allow filenames to work if( ( ref $xml && ref $xml ne 'SCALAR' ) || $xml !~ m{<.*?>}s ) { $parse_method = 'parsefile'; } my $parser = new XML::Parser(Style => 'Tree'); eval { $parser->$parse_method($xml); }; my $not_ok = $@; if( $not_ok ){ ( my $message = $not_ok ) =~ s/ at (?!line).*//g; # ick! $message =~ s/^\n//g; #chomp $message; $Builder->ok(0, $name); $Builder->diag("Failed to parse \n$xml\nXML::Parser error was: $message\n"); }else{ my $test = XMLin( $xml ); my ($ok, $stack) = cmp_details($test, $expected); if( not $Builder->ok($ok, $name) ){ my $diag = deep_diag($stack); $Builder->diag($diag); } } }
1; # End of Test::XML::Deep