Debian Codegraph @

t/04-h2x-lx.t

raw
1
2 use strict;
3 use warnings;
4
5 use Test::More tests => 15;
6
7 use XML::Hash::XS 'hash2xml';
8
9 $XML::Hash::XS::method = 'LX';
10 $XML::Hash::XS::trim = 1;
11
12 our $xml_decl = qq{<?xml version="1.0" encoding="utf-8"?>\n};
13
14 {
15 is
16 hash2xml( { root => { '#text' => ' zzzz ' } } ),
17 qq{$xml_decl<root>zzzz</root>},
18 'text',
19 ;
20 }
21 {
22 is
23 hash2xml( { root => { sub => 'test' } } ),
24 qq{$xml_decl<root><sub>test</sub></root>},
25 'node',
26 ;
27 }
28 {
29 is
30 hash2xml( { root => { -attr => "test < > & \" \t \n \r end" } } ),
31 qq{$xml_decl<root attr="test &lt; &gt; &amp; &quot; &#9; &#10; &#13; end"></root>},
32 'attr',
33 ;
34 }
35 {
36 is
37 hash2xml( { root => { _attr => "test" } }, attr => '_' ),
38 qq{$xml_decl<root attr="test"></root>},
39 'attr _',
40 ;
41 }
42 {
43 is
44 hash2xml( { root => { '~' => "zzzz < > & \r end" } }, text => '~' ),
45 qq{$xml_decl<root>zzzz &lt; &gt; &amp; &#13; end</root>},
46 'text ~',
47 ;
48 }
49 {
50 is
51 hash2xml( { root => " \t\ntest" }, trim => 1 ),
52 qq{$xml_decl<root>test</root>},
53 'trim 1',
54 ;
55 is
56 hash2xml( { root => " \t\ntest" }, trim => 0 ),
57 qq{$xml_decl<root> \t\ntest</root>},
58 'trim 0',
59 ;
60 }
61 {
62 is
63 hash2xml( { root => { sub => { '@' => "cdata < > & \" \t \n \r end" } } }, cdata => '@' ),
64 qq{$xml_decl<root><sub><![CDATA[cdata < > & \" \t \n \r end]]></sub></root>},
65 'cdata @',
66 ;
67 }
68 {
69 is
70 hash2xml( { root => { sub => { '/' => "comment < > & \" \t \n \r end" } } },comm => '/' ),
71 qq{$xml_decl<root><sub><!--comment < > & \" \t \n \r end--></sub></root>},
72 'comm /',
73 ;
74 }
75 {
76 is
77 hash2xml( { root => { -attr => undef } } ),
78 qq{$xml_decl<root attr=""></root>},
79 'empty attr',
80 ;
81 }
82 {
83 is
84 hash2xml( { root => { '#cdata' => undef } }, cdata => '#cdata' ),
85 qq{$xml_decl<root></root>},
86 'empty cdata',
87 ;
88 }
89 {
90 is
91 hash2xml( { root => { '/' => undef } }, comm => '/' ),
92 qq{$xml_decl<root><!----></root>},
93 'empty comment',
94 ;
95 }
96 {
97 is
98 hash2xml( { root => { x=>undef } } ),
99 qq{$xml_decl<root><x/></root>},
100 'empty tag',
101 ;
102 }
103 {
104 is
105 hash2xml( { root => { item => [1, 2, 3, { -attr => 4, node => 5 }, [6, 7] ] } } ),
106 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>},
107 'array',
108 ;
109 }
110 SKIP: {
111 my $data;
112 eval { $data = hash2xml( { root => { test => "Тест" } }, encoding => 'cp1251' ) };
113 my $err = $@;
114 chomp $err;
115 skip $err, 1 if $err;
116 is
117 $data,
118 qq{<?xml version="1.0" encoding="cp1251"?>\n<root><test>\322\345\361\362</test></root>},
119 'encoding support',
120 ;
121 }