package main; use strict; use warnings; use Test::More tests => 25; use File::Temp qw(tempfile); use XML::Hash::XS 'hash2xml'; our $xml_decl = qq{}; { is hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] } ), qq{$xml_decl\nvalue1value2}, 'default', ; } { is hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] }, keep_root => 0, xml_decl => 0 ), qq{value1value2}, 'rootless', ; } { is hash2xml( { node3 => 'value3', node1 => 'value1', node2 => 'value2' }, canonical => 1 ), qq{$xml_decl\nvalue1value2value3}, 'canonical', ; } { is hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] }, indent => 2 ), <<"EOT", $xml_decl value1 value2 EOT 'indent', ; } { is hash2xml( { node1 => [ 1, '2', '2' + 1 ] } ), qq{$xml_decl\n123}, 'integer, string, integer + string', ; } { my $x = 1.1; my $y = '2.2'; is hash2xml( { node1 => [ $x, $y, $y + $x ] } ), qq{$xml_decl\n1.12.23.3}, 'double, string, double + string', ; } { is hash2xml( { 1 => 'value1' } ), qq{$xml_decl\n<_1>value1}, 'quote tag name', ; } { is hash2xml( { node1 => \'value1' } ), qq{$xml_decl\nvalue1}, 'scalar reference', ; } { is hash2xml( { node1 => sub { 'value1' } } ), qq{$xml_decl\nvalue1}, 'code reference', ; } { is hash2xml( { node1 => sub { undef } } ), qq{$xml_decl\n}, 'code reference with undef', ; } { is hash2xml( { node1 => sub { [ 'value1' ] } } ), qq{$xml_decl\nvalue1}, 'code reference with array', ; } SKIP: { my $data; eval { $data = hash2xml( { node1 => 'Тест' }, encoding => 'cp1251' ) }; my $err = $@; chomp $err; skip $err, 1 if $err; is $data, qq{\n\322\345\361\362}, 'encoding support', ; } { is hash2xml( { node1 => "< > & \r" } ), qq{$xml_decl\n< > & }, 'escaping', ; } { is hash2xml( { node => " \t\ntest " }, trim => 0 ), qq{$xml_decl\n \t\ntest }, 'trim 0', ; is hash2xml( { node => " \t\ntest " }, trim => 1 ), qq{$xml_decl\ntest}, 'trim 1', ; } { my $data; my $fh = tempfile(); hash2xml( { node1 => 'value1' }, output => $fh ); seek($fh, 0, 0); { local $/; $data = <$fh> } is $data, qq{$xml_decl\nvalue1}, 'filehandle output', ; } { my $data = ''; tie *STDOUT, "Trapper", \$data; hash2xml( { node1 => 'value1' }, output => \*STDOUT ); untie *STDOUT; is $data, qq{$xml_decl\nvalue1}, 'tied filehandle output', ; } { is hash2xml( { node1 => 'value1"', node2 => 'value2&', node3 => { node31 => 'value31' }, node4 => [ { node41 => 'value41' }, { node42 => 'value42' } ], node5 => [ 51, 52, { node53 => 'value53' } ], node6 => {}, node7 => [], }, use_attr => 1, canonical => 1, indent => 2, ), <<"EOT", $xml_decl 51 52 EOT 'use attributes', ; } { is hash2xml( { content => 'content&1', node2 => [ 21, { node22 => "value22 < > & \" \t \n \r", content => "content < > & \r", } ], }, use_attr => 1, canonical => 1, indent => 2, content => 'content', ), <<"EOT", $xml_decl content&1 21 content < > & EOT 'content', ; } { my $o = TestObject->new(); is hash2xml( { object => $o }, ), qq{$xml_decl\nvalue1}, 'object', ; } { my $o = TestObjectWithOverloadedStringification->new('test'); is hash2xml({ object => $o }), qq{$xml_decl\ntest}, 'object with overloaded stringification', ; } { $XML::Hash::XS::indent = 2; $XML::Hash::XS::use_attr = 1; $XML::Hash::XS::canonical = 1; $XML::Hash::XS::content = 'content'; is hash2xml( { content => 'content&1', node2 => [ 21, { node22 => 'value23', 'content' => 'content2' } ], }, ), <<"EOT", $xml_decl content&1 21 content2 EOT 'global options', ; } { $XML::Hash::XS::indent = 2; $XML::Hash::XS::use_attr = 1; $XML::Hash::XS::canonical = 1; $XML::Hash::XS::content = 'content'; $XML::Hash::XS::xml_decl = 0; is hash2xml( { node1 => 'value1', }, ), <<"EOT", EOT 'xml declaration', ; } { $XML::Hash::XS::indent = 2; $XML::Hash::XS::use_attr = 1; $XML::Hash::XS::canonical = 1; $XML::Hash::XS::xml_decl = 0; my @arr = (1, 2, 3, { att1 => 1, att2 => 2 }); my $obj = new Iterator sub { shift @arr }; is hash2xml( { iterator => $obj, }, ), <<"EOT", 1 2 3 EOT 'iterator & attr', ; } { $XML::Hash::XS::indent = 2; $XML::Hash::XS::use_attr = 0; $XML::Hash::XS::canonical = 1; $XML::Hash::XS::xml_decl = 0; my @arr = (1, 2, 3, { att1 => 1, att2 => 2}); my $obj = new Iterator sub { shift @arr }; is hash2xml( { iterator => $obj, }, ), <<"EOT", 1 2 3 1 2 EOT 'iterator', ; } package Iterator; sub new { my ($class, $cb) = @_; return bless { cb => $cb }, $class; } sub iternext { return shift->{cb}->(); } package TestObject; sub new { return bless [], shift; } sub toString { return 'value1'; } package TestObjectWithOverloadedStringification; use overload '""' => sub { shift->stringify }, fallback => 1; sub new { my ($class, $value) = @_; return bless { value => $value }, $class; } sub stringify { return shift->{value}; } package Trapper; sub TIEHANDLE { my ($class, $str) = @_; return bless [$str], $class; } sub WRITE { my ($self, $buf, $len, $offset) = @_; $len ||= length($buf); $offset ||= 0; ${$self->[0]} .= substr($buf, $offset, $len); return $len; } sub PRINT { ${shift->[0]} .= join('', @_); }