package main; use strict; use warnings; use Test::More; use File::Temp qw(tempfile); use XML::Hash::XS; our $c; eval { $c = XML::Hash::XS->new(doc => 1) }; if ($@) { plan skip_all => "Option 'doc' is not supported"; } else { plan tests => 13; require XML::LibXML; } ## no critic (Subroutines::ProhibitSubroutinePrototypes) sub fix_xml($) { my $xml = shift; chomp $xml; $xml =~ s|(<\w[^]*[^/])>|$1/>|g; $xml } ## use critic our $xml_decl = qq{}; { is fix_xml $c->hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] } )->toString(), qq{$xml_decl\nvalue1value2}, 'default', ; } { is fix_xml $c->hash2xml( { node3 => 'value3', node1 => 'value1', node2 => 'value2' }, canonical => 1 )->toString(), qq{$xml_decl\nvalue1value2value3}, 'canonical', ; } { is fix_xml $c->hash2xml( { node1 => [ 1, '2', '2' + 1 ] } )->toString(), qq{$xml_decl\n123}, 'integer, string, integer + string', ; } { my $x = 1.1; my $y = '2.2'; is fix_xml $c->hash2xml( { node1 => [ $x, $y, $y + $x ] } )->toString(), qq{$xml_decl\n1.12.23.3}, 'double, string, double + string', ; } { is fix_xml $c->hash2xml( { 1 => 'value1' } )->toString(), qq{$xml_decl\n<_1>value1}, 'quote tag name', ; } SKIP: { my $data; eval { $data = $c->hash2xml( { node1 => 'Тест' }, encoding => 'cp1251' )->toString(); }; my $err = $@; chomp $err; skip $err, 1 if $err; # a libxml2 built without iconv/ICU only knows its built-in encodings and # reports "unknown encoding" on stderr, returning undef instead of dying skip 'libxml2 without cp1251 encoding handler', 1 unless defined $data; is fix_xml($data), qq{\n\322\345\361\362}, 'encoding support', ; } { is fix_xml $c->hash2xml( { node1 => "< > & \r" } )->toString(), qq{$xml_decl\n< > & }, 'escaping', ; } { is fix_xml $c->hash2xml( { node => " \t\ntest " }, trim => 0 )->toString(), qq{$xml_decl\n \t\ntest }, 'trim 0', ; is fix_xml $c->hash2xml( { node => " \t\ntest " }, trim => 1 )->toString(), qq{$xml_decl\ntest}, 'trim 1', ; } { is fix_xml $c->hash2xml( { node1 => 'value1"', node2 => 'value2&', node3 => { node31 => 'value31', t => [ 'text' ] }, node4 => [ { node41 => 'value41', t => [ 'text' ] }, { node42 => 'value42', t => [ 'text' ] } ], node5 => [ 51, 52, { node53 => 'value53', t => [ 'text' ] } ], node6 => [], }, use_attr => 1, canonical => 1, indent => 2, )->toString(), qq{$xml_decl\ntexttexttext5152text}, 'use attributes', ; } { is fix_xml $c->hash2xml( { content => 'content&1', node2 => [ 21, { node22 => "value22 < > & \" \t \n \r", content => "content < > & \r", } ], }, use_attr => 1, canonical => 1, indent => 2, content => 'content', )->toString(), qq{$xml_decl\ncontent&121content < > & }, 'content', ; } { my @arr = (1, 2, 3, { att1 => 1, att2 => 2}); my $obj = new Iterator sub { shift @arr }; is fix_xml $c->hash2xml( { iterator => $obj, }, use_attr => 1, canonical => 1, indent => 2, content => 'content', )->toString(), qq{$xml_decl\n123}, 'content & attr', ; } { my @arr = (1, 2, 3, { att1 => 1, att2 => 2}); my $obj = new Iterator sub { shift @arr }; is fix_xml $c->hash2xml( { iterator => $obj, }, use_attr => 0, canonical => 1, indent => 2, content => 'content', )->toString(), qq{$xml_decl\n12312}, 'iterator', ; } package Iterator; sub new { my ($class, $cb) = @_; return bless { cb => $cb }, $class; } sub iternext { return shift->{cb}->(); }