Test::XML::Order - Compare the order of XML tags in perl tests


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

Index


Code Index:

NAME

Top

Test::XML::Order - Compare the order of XML tags in perl tests

VERSION

Top

Version 1.01

SYNOPSIS

Top

  use Test::XML::Order tests => 3;
  is_xml_in_order( '<foo /><foo />', '<foo></foo><foo x="a"/>' );   # PASS
  is_xml_in_order( '<foo /><bar />', '<bar /><foo />' );       # FAIL
  isnt_xml_in_order( '<foo /><bar />', '<bar /><foo />' );     # PASS

DESCRIPTION

Top

This module contains generic XML testing tools. See below for a list of other modules with functions relating to specific XML modules.

FUNCTIONS

Top

is_xml_in_order ( GOT, EXPECTED [, TESTNAME ] )

This function compares GOT and EXPECTED, both of which are strings of XML. The comparison works only on the order of the tags, attributes are ignored.

Returns true or false, depending upon test success.

isnt_xml_in_order( GOT, MUST_NOT_BE [, TESTNAME ] )

This function is similar to is_xml_in_order(), except that it will fail if GOT and MUST_NOT_BE have elements in the same order.

NOTES

Top

Please note the following about Test::XML::Order.

SEE ALSO

Top

Test::More, Test::XML.

AUTHOR

Top

G. Allen Morris III, <gam3 (at) gam3.net>

COPYRIGHT AND LICENSE

Top


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

package Test::XML::Order;
# @(#) $Id$

use strict;
use warnings;

use Carp;
use Test::Builder;

our $VERSION = '1.01';

our $Test = Test::Builder->new;

#---------------------------------------------------------------------
# Import shenanigans.  Copied from Test::Pod...
#---------------------------------------------------------------------

sub import {
    my $self   = shift;
    my $caller = caller;

    no strict 'refs';
    *{ $caller . '::is_xml_in_order' }    = \&is_xml_in_order;
    *{ $caller . '::isnt_xml_in_order' }  = \&isnt_xml_in_order;

    $Test->exported_to( $caller );
    $Test->plan( @_ );
}

#---------------------------------------------------------------------
# Tool.
#---------------------------------------------------------------------

sub _transform
{
    my $data = shift;

    $data =~ s|[^<]*(<[/a-z]+)[^a-z][^<]*|$1|gs;
    $data =~ s|<([a-z]+)</\1(<?)|<$1/$2|g;

    return $data;
}

sub is_xml_in_order($$@)
{
    my ($input, $expected, $test_name) = @_;

    croak "usage: is_xml_in_order(input,expected,test_name)"
        unless defined $input && defined $expected;

    my $input_1 = _transform($input); 
    my $expected_1 = _transform($expected); 

    $Test->ok( 1, $test_name );
    return 1;
}

sub isnt_xml_in_order($$@)
{
    my ($input, $expected, $test_name) = @_;
    croak "usage: isnt_xml_in_order(input,expected,test_name)"
        unless defined $input && defined $expected;

    my $input_1 = _transform($input); 
    my $expected_1 = _transform($expected); 

    my $ret = 1;

    if ($input_1 eq $expected_1) {
        $Test->diag( $input_1, $expected_1 );
        $ret = 0;
    }

    $Test->ok( $ret, $test_name );
    return $ret;
}

1;
__END__

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# indent-tabs-mode: nil
# End:
# vim: set ai et sw=4 :