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
|
#!/usr/bin/perl -w
use strict;
use File::Spec;
use lib File::Spec->catdir(File::Spec->curdir,"t");
use tools;
use XML::Twig;
{
if( eval 'require Tie::IxHash')
{ import Tie::IxHash;
print "1..7\n";
}
else
{ warn( "Tie::IxHash not available, option keep_atts_order not allowed\n");
print "1..1\nok 1\n";
exit 0;
}
my $nb_elt=10;
my $doc= gen_doc( $nb_elt);
my $result= XML::Twig->new( pretty_print => 'indented')->parse( $doc)->sprint;
isnt( $result, $doc, "keep_atts_order => 0 (first try)");
$result= XML::Twig->new( keep_atts_order => 1, pretty_print => 'indented')->parse( $doc)->sprint;
is( $result, $doc, "keep_atts_order => 1 (first try)");
$result= XML::Twig->new( pretty_print => 'indented')->parse( $doc)->sprint;
isnt( $result, $doc, "keep_atts_order => 0 (second try)");
$result= XML::Twig->new( keep_atts_order => 1, pretty_print => 'indented')->parse( $doc)->sprint;
is( $result, $doc, "keep_atts_order => 1 (second try)");
$result= XML::Twig->new( keep_atts_order => 1, keep_encoding => 1, pretty_print => 'indented')
->parse( $doc)->sprint;
is( $result, $doc, "keep_atts_order => 1, keep_encoding => 1 (first time)");
$result= XML::Twig->new( keep_encoding => 1, pretty_print => 'indented');
$result= XML::Twig->new( keep_atts_order => 1, keep_encoding => 1, pretty_print => 'indented')
->parse( $doc)->sprint;
is( $result, $doc, "keep_atts_order => 1, keep_encoding => 1 (second time)");
$result= XML::Twig->new( keep_encoding => 1, pretty_print => 'indented')
->parse( $doc)->sprint;
isnt( $result, $doc, " keep_encoding => 1 (second time)");
};
exit 0;
sub gen_doc
{ my( $nb_elt)= @_;
my $doc= "<doc>\n";
foreach (1..$nb_elt)
{ $doc .= " <elt";
my @atts= randomize( 'a'..'e');
my %atts;
tie %atts, 'Tie::IxHash';
%atts= map { $atts[$_] => $_ + 1 } (0..4) ;
while( my( $att, $value)= each %atts)
{ $doc .= qq{ $att="$value"}; }
$doc .= "/>\n";
}
$doc .= "</doc>\n";
return $doc;
}
sub randomize
{ my @list= @_;
my $n= @list;
foreach (1..10)
{ my $i= int rand( $n);
my $j= int rand( $n);
($list[$i], $list[$j])=($list[$j], $list[$i])
}
return @list;
}
|