| 1 |
|
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use Test::More; |
| 6 |
|
| 7 |
use XML::Hash::XS; |
| 8 |
|
| 9 |
our $c; |
| 10 |
eval { $c = XML::Hash::XS->new(doc => 1, method => 'LX', trim => 1) }; |
| 11 |
if ($@) { |
| 12 |
plan skip_all => "Option 'doc' is not supported"; |
| 13 |
} |
| 14 |
else { |
| 15 |
plan tests => 14; |
| 16 |
require XML::LibXML; |
| 17 |
} |
| 18 |
|
| 19 |
our $xml_decl = qq{<?xml version="1.0" encoding="utf-8"?>\n}; |
| 20 |
|
| 21 |
## no critic (Subroutines::ProhibitSubroutinePrototypes) |
| 22 |
sub fix_xml($) { my $xml = shift; chomp $xml; $xml =~ s|(<\w[^</>]*[^/])></\w+>|$1/>|g; $xml } |
| 23 |
## use critic |
| 24 |
|
| 25 |
{ |
| 26 |
is |
| 27 |
fix_xml $c->hash2xml( { root => { '#text' => ' zzzz ' } } )->toString(), |
| 28 |
qq{$xml_decl<root>zzzz</root>}, |
| 29 |
'text', |
| 30 |
; |
| 31 |
} |
| 32 |
{ |
| 33 |
is |
| 34 |
fix_xml $c->hash2xml( { root => { sub => 'test' } } )->toString(), |
| 35 |
qq{$xml_decl<root><sub>test</sub></root>}, |
| 36 |
'node', |
| 37 |
; |
| 38 |
} |
| 39 |
{ |
| 40 |
is |
| 41 |
fix_xml $c->hash2xml( { root => { -attr => "test < > & \" \t \n \r end" } } )->toString(), |
| 42 |
qq{$xml_decl<root attr="test < > & " 	 end"/>}, |
| 43 |
'attr', |
| 44 |
; |
| 45 |
} |
| 46 |
{ |
| 47 |
is |
| 48 |
fix_xml $c->hash2xml( { root => { _attr => "test" } }, attr => '_' )->toString(), |
| 49 |
qq{$xml_decl<root attr="test"/>}, |
| 50 |
'attr _', |
| 51 |
; |
| 52 |
} |
| 53 |
{ |
| 54 |
is |
| 55 |
fix_xml $c->hash2xml( { root => { '~' => "zzzz < > & \r end" } }, text => '~' )->toString(), |
| 56 |
qq{$xml_decl<root>zzzz < > & end</root>}, |
| 57 |
'text ~', |
| 58 |
; |
| 59 |
} |
| 60 |
{ |
| 61 |
is |
| 62 |
fix_xml $c->hash2xml( { root => " \t\ntest" }, trim => 1 )->toString(), |
| 63 |
qq{$xml_decl<root>test</root>}, |
| 64 |
'trim 1', |
| 65 |
; |
| 66 |
is |
| 67 |
fix_xml $c->hash2xml( { root => " \t\ntest" }, trim => 0 )->toString(), |
| 68 |
qq{$xml_decl<root> \t\ntest</root>}, |
| 69 |
'trim 0', |
| 70 |
; |
| 71 |
} |
| 72 |
{ |
| 73 |
is |
| 74 |
fix_xml $c->hash2xml( { root => { sub => { '@' => "cdata < > & \" \t \n \r end" } } }, cdata => '@' )->toString(), |
| 75 |
qq{$xml_decl<root><sub><![CDATA[cdata < > & \" \t \n \r end]]></sub></root>}, |
| 76 |
'cdata @', |
| 77 |
; |
| 78 |
} |
| 79 |
{ |
| 80 |
is |
| 81 |
fix_xml $c->hash2xml( { root => { sub => { '/' => "comment < > & \" \t \n \r end" } } },comm => '/' )->toString(), |
| 82 |
qq{$xml_decl<root><sub><!--comment < > & \" \t \n \r end--></sub></root>}, |
| 83 |
'comm /', |
| 84 |
; |
| 85 |
} |
| 86 |
{ |
| 87 |
is |
| 88 |
fix_xml $c->hash2xml( { root => { -attr => undef, '#text' => 'text' } } )->toString(), |
| 89 |
qq{$xml_decl<root attr="">text</root>}, |
| 90 |
'empty attr', |
| 91 |
; |
| 92 |
} |
| 93 |
{ |
| 94 |
is |
| 95 |
fix_xml $c->hash2xml( { root => { '#cdata' => undef, '#text' => 'text' } }, cdata => '#cdata' )->toString(), |
| 96 |
qq{$xml_decl<root>text</root>}, |
| 97 |
'empty cdata', |
| 98 |
; |
| 99 |
} |
| 100 |
{ |
| 101 |
is |
| 102 |
fix_xml $c->hash2xml( { root => { '/' => undef } }, comm => '/' )->toString(), |
| 103 |
qq{$xml_decl<root><!----></root>}, |
| 104 |
'empty comment', |
| 105 |
; |
| 106 |
} |
| 107 |
{ |
| 108 |
is |
| 109 |
fix_xml $c->hash2xml( { root => { item => [1, 2, 3, { -attr => 4, node => 5 }, [6, 7] ] } } )->toString(), |
| 110 |
qq{$xml_decl<root><item>1</item><item>2</item><item>3</item><item attr="4"><node>5</node></item><item><item>6</item><item>7</item></item></root>}, |
| 111 |
'array', |
| 112 |
; |
| 113 |
} |
| 114 |
SKIP: { |
| 115 |
my $data; |
| 116 |
eval { $data = $c->hash2xml( { root => { test => "Тест" } }, encoding => 'cp1251' )->toString() }; |
| 117 |
my $err = $@; |
| 118 |
chomp $err; |
| 119 |
skip $err, 1 if $err; |
| 120 |
# a libxml2 built without iconv/ICU only knows its built-in encodings and |
| 121 |
# reports "unknown encoding" on stderr, returning undef instead of dying |
| 122 |
skip 'libxml2 without cp1251 encoding handler', 1 unless defined $data; |
| 123 |
is |
| 124 |
fix_xml($data), |
| 125 |
qq{<?xml version="1.0" encoding="cp1251"?>\n<root><test>\322\345\361\362</test></root>}, |
| 126 |
'encoding support', |
| 127 |
; |
| 128 |
} |