Test::XML::Deep - Test::XML::Deep documentation


Test-XML-Deep documentation Contained in the Test-XML-Deep distribution.

Index


Code Index:

NAME

Top

Test::XML::Deep = XML::Simple + Test::Deep

VERSION

Top

Version 0.07

SYNOPSIS

Top

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' });




EXPORT

Top

cmp_xml_deeply

FUNCTIONS

Top

cmp_xml_deeply( $xml, $hashref_expected, [ 'test name' ] );

$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.

ACKNOWLEDGEMENTS

Top

Fergal Daly, for exporting cmp_details from Test::Deep lickety-split!

Michael G Schwern, for Test::Builder, which makes modules like this possible.

SOURCE

Top

see: http://github.com/jlavallee/test-xml-deep

AUTHOR

Top

Jeff Lavallee, <jeff at zeroclue.com>

BUGS

Top

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.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Test::XML::Deep




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-XML-Deep

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Test-XML-Deep

* CPAN Ratings

http://cpanratings.perl.org/d/Test-XML-Deep

* Search CPAN

http://search.cpan.org/dist/Test-XML-Deep

COPYRIGHT & LICENSE

Top

SEE ALSO

Top

Test::XML::Simple, Test::Deep


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