Debian Codegraph @

t/01-h2x.t

raw
1 package main;
2 use strict;
3 use warnings;
4
5 use Test::More tests => 25;
6 use File::Temp qw(tempfile);
7
8 use XML::Hash::XS 'hash2xml';
9
10 our $xml_decl = qq{<?xml version="1.0" encoding="utf-8"?>};
11
12 {
13 is
14 hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] } ),
15 qq{$xml_decl\n<root><node1>value1</node1><node1><node2>value2</node2></node1></root>},
16 'default',
17 ;
18 }
19
20 {
21 is
22 hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] }, keep_root => 0, xml_decl => 0 ),
23 qq{<node1>value1</node1><node1><node2>value2</node2></node1>},
24 'rootless',
25 ;
26 }
27
28 {
29 is
30 hash2xml( { node3 => 'value3', node1 => 'value1', node2 => 'value2' }, canonical => 1 ),
31 qq{$xml_decl\n<root><node1>value1</node1><node2>value2</node2><node3>value3</node3></root>},
32 'canonical',
33 ;
34 }
35
36 {
37 is
38 hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] }, indent => 2 ),
39 <<"EOT",
40 $xml_decl
41 <root>
42 <node1>value1</node1>
43 <node1>
44 <node2>value2</node2>
45 </node1>
46 </root>
47 EOT
48 'indent',
49 ;
50 }
51
52 {
53 is
54 hash2xml( { node1 => [ 1, '2', '2' + 1 ] } ),
55 qq{$xml_decl\n<root><node1>1</node1><node1>2</node1><node1>3</node1></root>},
56 'integer, string, integer + string',
57 ;
58 }
59
60 {
61 my $x = 1.1;
62 my $y = '2.2';
63 is
64 hash2xml( { node1 => [ $x, $y, $y + $x ] } ),
65 qq{$xml_decl\n<root><node1>1.1</node1><node1>2.2</node1><node1>3.3</node1></root>},
66 'double, string, double + string',
67 ;
68 }
69
70 {
71 is
72 hash2xml( { 1 => 'value1' } ),
73 qq{$xml_decl\n<root><_1>value1</_1></root>},
74 'quote tag name',
75 ;
76 }
77
78 {
79 is
80 hash2xml( { node1 => \'value1' } ),
81 qq{$xml_decl\n<root><node1>value1</node1></root>},
82 'scalar reference',
83 ;
84 }
85
86 {
87 is
88 hash2xml( { node1 => sub { 'value1' } } ),
89 qq{$xml_decl\n<root><node1>value1</node1></root>},
90 'code reference',
91 ;
92 }
93
94 {
95 is
96 hash2xml( { node1 => sub { undef } } ),
97 qq{$xml_decl\n<root><node1/></root>},
98 'code reference with undef',
99 ;
100 }
101
102 {
103 is
104 hash2xml( { node1 => sub { [ 'value1' ] } } ),
105 qq{$xml_decl\n<root><node1>value1</node1></root>},
106 'code reference with array',
107 ;
108 }
109
110 SKIP: {
111 my $data;
112 eval { $data = hash2xml( { node1 => 'Тест' }, 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><node1>\322\345\361\362</node1></root>},
119 'encoding support',
120 ;
121 }
122
123 {
124 is
125 hash2xml( { node1 => "< > & \r" } ),
126 qq{$xml_decl\n<root><node1>&lt; &gt; &amp; &#13;</node1></root>},
127 'escaping',
128 ;
129 }
130
131 {
132 is
133 hash2xml( { node => " \t\ntest " }, trim => 0 ),
134 qq{$xml_decl\n<root><node> \t\ntest </node></root>},
135 'trim 0',
136 ;
137 is
138 hash2xml( { node => " \t\ntest " }, trim => 1 ),
139 qq{$xml_decl\n<root><node>test</node></root>},
140 'trim 1',
141 ;
142 }
143
144 {
145 my $data;
146 my $fh = tempfile();
147 hash2xml( { node1 => 'value1' }, output => $fh );
148 seek($fh, 0, 0);
149 { local $/; $data = <$fh> }
150 is
151 $data,
152 qq{$xml_decl\n<root><node1>value1</node1></root>},
153 'filehandle output',
154 ;
155 }
156
157 {
158 my $data = '';
159 tie *STDOUT, "Trapper", \$data;
160 hash2xml( { node1 => 'value1' }, output => \*STDOUT );
161 untie *STDOUT;
162 is
163 $data,
164 qq{$xml_decl\n<root><node1>value1</node1></root>},
165 'tied filehandle output',
166 ;
167 }
168
169 {
170 is
171 hash2xml(
172 {
173 node1 => 'value1"',
174 node2 => 'value2&',
175 node3 => { node31 => 'value31' },
176 node4 => [ { node41 => 'value41' }, { node42 => 'value42' } ],
177 node5 => [ 51, 52, { node53 => 'value53' } ],
178 node6 => {},
179 node7 => [],
180 },
181 use_attr => 1,
182 canonical => 1,
183 indent => 2,
184 ),
185 <<"EOT",
186 $xml_decl
187 <root node1="value1&quot;" node2="value2&amp;">
188 <node3 node31="value31"/>
189 <node4 node41="value41"/>
190 <node4 node42="value42"/>
191 <node5>51</node5>
192 <node5>52</node5>
193 <node5 node53="value53"/>
194 <node6/>
195 </root>
196 EOT
197 'use attributes',
198 ;
199 }
200
201 {
202 is
203 hash2xml(
204 {
205 content => 'content&1',
206 node2 => [ 21, {
207 node22 => "value22 < > & \" \t \n \r",
208 content => "content < > & \r",
209 } ],
210 },
211 use_attr => 1,
212 canonical => 1,
213 indent => 2,
214 content => 'content',
215 ),
216 <<"EOT",
217 $xml_decl
218 <root>
219 content&amp;1
220 <node2>21</node2>
221 <node2 node22="value22 &lt; &gt; &amp; &quot; &#9; &#10; &#13;">
222 content &lt; &gt; &amp; &#13;
223 </node2>
224 </root>
225 EOT
226 'content',
227 ;
228 }
229
230 {
231 my $o = TestObject->new();
232 is
233 hash2xml(
234 { object => $o },
235 ),
236 qq{$xml_decl\n<root><object><root attr="1">value1</root></object></root>},
237 'object',
238 ;
239 }
240
241 {
242 my $o = TestObjectWithOverloadedStringification->new('test');
243 is
244 hash2xml({ object => $o }),
245 qq{$xml_decl\n<root><object>test</object></root>},
246 'object with overloaded stringification',
247 ;
248 }
249
250 {
251 $XML::Hash::XS::indent = 2;
252 $XML::Hash::XS::use_attr = 1;
253 $XML::Hash::XS::canonical = 1;
254 $XML::Hash::XS::content = 'content';
255 is
256 hash2xml(
257 {
258 content => 'content&1',
259 node2 => [ 21, { node22 => 'value23', 'content' => 'content2' } ],
260 },
261 ),
262 <<"EOT",
263 $xml_decl
264 <root>
265 content&amp;1
266 <node2>21</node2>
267 <node2 node22="value23">
268 content2
269 </node2>
270 </root>
271 EOT
272 'global options',
273 ;
274 }
275
276 {
277 $XML::Hash::XS::indent = 2;
278 $XML::Hash::XS::use_attr = 1;
279 $XML::Hash::XS::canonical = 1;
280 $XML::Hash::XS::content = 'content';
281 $XML::Hash::XS::xml_decl = 0;
282 is
283 hash2xml(
284 {
285 node1 => 'value1',
286 },
287 ),
288 <<"EOT",
289 <root node1="value1"/>
290 EOT
291 'xml declaration',
292 ;
293 }
294
295 {
296 $XML::Hash::XS::indent = 2;
297 $XML::Hash::XS::use_attr = 1;
298 $XML::Hash::XS::canonical = 1;
299 $XML::Hash::XS::xml_decl = 0;
300 my @arr = (1, 2, 3, { att1 => 1, att2 => 2 });
301 my $obj = new Iterator sub { shift @arr };
302 is
303 hash2xml(
304 {
305 iterator => $obj,
306 },
307 ),
308 <<"EOT",
309 <root>
310 <iterator>1</iterator>
311 <iterator>2</iterator>
312 <iterator>3</iterator>
313 <iterator att1="1" att2="2"/>
314 </root>
315 EOT
316 'iterator & attr',
317 ;
318 }
319
320 {
321 $XML::Hash::XS::indent = 2;
322 $XML::Hash::XS::use_attr = 0;
323 $XML::Hash::XS::canonical = 1;
324 $XML::Hash::XS::xml_decl = 0;
325 my @arr = (1, 2, 3, { att1 => 1, att2 => 2});
326 my $obj = new Iterator sub { shift @arr };
327 is
328 hash2xml(
329 {
330 iterator => $obj,
331 },
332 ),
333 <<"EOT",
334 <root>
335 <iterator>1</iterator>
336 <iterator>2</iterator>
337 <iterator>3</iterator>
338 <iterator>
339 <att1>1</att1>
340 <att2>2</att2>
341 </iterator>
342 </root>
343 EOT
344 'iterator',
345 ;
346 }
347
348 package Iterator;
349
350 sub new {
351 my ($class, $cb) = @_;
352 return bless { cb => $cb }, $class;
353 }
354
355 sub iternext {
356 return shift->{cb}->();
357 }
358
359 package TestObject;
360
361 sub new {
362 return bless [], shift;
363 }
364
365 sub toString {
366 return '<root attr="1">value1</root>';
367 }
368
369 package TestObjectWithOverloadedStringification;
370
371 use overload '""' => sub { shift->stringify }, fallback => 1;
372
373 sub new {
374 my ($class, $value) = @_;
375 return bless { value => $value }, $class;
376 }
377
378 sub stringify {
379 return shift->{value};
380 }
381
382 package Trapper;
383
384 sub TIEHANDLE {
385 my ($class, $str) = @_;
386 return bless [$str], $class;
387 }
388
389 sub WRITE {
390 my ($self, $buf, $len, $offset) = @_;
391
392 $len ||= length($buf);
393 $offset ||= 0;
394
395 ${$self->[0]} .= substr($buf, $offset, $len);
396
397 return $len;
398 }
399
400 sub PRINT {
401 ${shift->[0]} .= join('', @_);
402 }