Debian Codegraph @

t/02-h2d.t

raw
1 package main;
2 use strict;
3 use warnings;
4
5 use Test::More;
6 use File::Temp qw(tempfile);
7
8 use XML::Hash::XS;
9
10 our $c;
11 eval { $c = XML::Hash::XS->new(doc => 1) };
12 if ($@) {
13 plan skip_all => "Option 'doc' is not supported";
14 }
15 else {
16 plan tests => 13;
17 require XML::LibXML;
18 }
19
20 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
21 sub fix_xml($) { my $xml = shift; chomp $xml; $xml =~ s|(<\w[^</>]*[^/])></\w+>|$1/>|g; $xml }
22 ## use critic
23
24 our $xml_decl = qq{<?xml version="1.0" encoding="utf-8"?>};
25
26 {
27 is
28 fix_xml $c->hash2xml( { node1 => [ 'value1', { node2 => 'value2' } ] } )->toString(),
29 qq{$xml_decl\n<root><node1>value1</node1><node1><node2>value2</node2></node1></root>},
30 'default',
31 ;
32 }
33
34 {
35 is
36 fix_xml $c->hash2xml( { node3 => 'value3', node1 => 'value1', node2 => 'value2' }, canonical => 1 )->toString(),
37 qq{$xml_decl\n<root><node1>value1</node1><node2>value2</node2><node3>value3</node3></root>},
38 'canonical',
39 ;
40 }
41
42 {
43 is
44 fix_xml $c->hash2xml( { node1 => [ 1, '2', '2' + 1 ] } )->toString(),
45 qq{$xml_decl\n<root><node1>1</node1><node1>2</node1><node1>3</node1></root>},
46 'integer, string, integer + string',
47 ;
48 }
49
50 {
51 my $x = 1.1;
52 my $y = '2.2';
53 is
54 fix_xml $c->hash2xml( { node1 => [ $x, $y, $y + $x ] } )->toString(),
55 qq{$xml_decl\n<root><node1>1.1</node1><node1>2.2</node1><node1>3.3</node1></root>},
56 'double, string, double + string',
57 ;
58 }
59
60 {
61 is
62 fix_xml $c->hash2xml( { 1 => 'value1' } )->toString(),
63 qq{$xml_decl\n<root><_1>value1</_1></root>},
64 'quote tag name',
65 ;
66 }
67
68 SKIP: {
69 my $data;
70 eval { $data = $c->hash2xml( { node1 => 'Тест' }, encoding => 'cp1251' )->toString(); };
71 my $err = $@;
72 chomp $err;
73 skip $err, 1 if $err;
74 # a libxml2 built without iconv/ICU only knows its built-in encodings and
75 # reports "unknown encoding" on stderr, returning undef instead of dying
76 skip 'libxml2 without cp1251 encoding handler', 1 unless defined $data;
77 is
78 fix_xml($data),
79 qq{<?xml version="1.0" encoding="cp1251"?>\n<root><node1>\322\345\361\362</node1></root>},
80 'encoding support',
81 ;
82 }
83
84 {
85 is
86 fix_xml $c->hash2xml( { node1 => "< > & \r" } )->toString(),
87 qq{$xml_decl\n<root><node1>&lt; &gt; &amp; &#13;</node1></root>},
88 'escaping',
89 ;
90 }
91
92 {
93 is
94 fix_xml $c->hash2xml( { node => " \t\ntest " }, trim => 0 )->toString(),
95 qq{$xml_decl\n<root><node> \t\ntest </node></root>},
96 'trim 0',
97 ;
98 is
99 fix_xml $c->hash2xml( { node => " \t\ntest " }, trim => 1 )->toString(),
100 qq{$xml_decl\n<root><node>test</node></root>},
101 'trim 1',
102 ;
103 }
104
105 {
106 is
107 fix_xml $c->hash2xml(
108 {
109 node1 => 'value1"',
110 node2 => 'value2&',
111 node3 => { node31 => 'value31', t => [ 'text' ] },
112 node4 => [ { node41 => 'value41', t => [ 'text' ] }, { node42 => 'value42', t => [ 'text' ] } ],
113 node5 => [ 51, 52, { node53 => 'value53', t => [ 'text' ] } ],
114 node6 => [],
115 },
116 use_attr => 1,
117 canonical => 1,
118 indent => 2,
119 )->toString(),
120 qq{$xml_decl\n<root node1="value1&quot;" node2="value2&amp;"><node3 node31="value31"><t>text</t></node3><node4 node41="value41"><t>text</t></node4><node4 node42="value42"><t>text</t></node4><node5>51</node5><node5>52</node5><node5 node53="value53"><t>text</t></node5></root>},
121 'use attributes',
122 ;
123 }
124
125 {
126 is
127 fix_xml $c->hash2xml(
128 {
129 content => 'content&1',
130 node2 => [ 21, {
131 node22 => "value22 < > & \" \t \n \r",
132 content => "content < > & \r",
133 } ],
134 },
135 use_attr => 1,
136 canonical => 1,
137 indent => 2,
138 content => 'content',
139 )->toString(),
140 qq{$xml_decl\n<root>content&amp;1<node2>21</node2><node2 node22="value22 &lt; &gt; &amp; &quot; &#9; &#10; &#13;">content &lt; &gt; &amp; &#13;</node2></root>},
141 'content',
142 ;
143 }
144
145 {
146 my @arr = (1, 2, 3, { att1 => 1, att2 => 2});
147 my $obj = new Iterator sub { shift @arr };
148 is
149 fix_xml $c->hash2xml(
150 {
151 iterator => $obj,
152 },
153 use_attr => 1,
154 canonical => 1,
155 indent => 2,
156 content => 'content',
157 )->toString(),
158 qq{$xml_decl\n<root><iterator>1</iterator><iterator>2</iterator><iterator>3</iterator><iterator att1="1" att2="2"/></root>},
159 'content & attr',
160 ;
161 }
162
163 {
164 my @arr = (1, 2, 3, { att1 => 1, att2 => 2});
165 my $obj = new Iterator sub { shift @arr };
166 is
167 fix_xml $c->hash2xml(
168 {
169 iterator => $obj,
170 },
171 use_attr => 0,
172 canonical => 1,
173 indent => 2,
174 content => 'content',
175 )->toString(),
176 qq{$xml_decl\n<root><iterator>1</iterator><iterator>2</iterator><iterator>3</iterator><iterator><att1>1</att1><att2>2</att2></iterator></root>},
177 'iterator',
178 ;
179 }
180
181 package Iterator;
182
183 sub new {
184 my ($class, $cb) = @_;
185 return bless { cb => $cb }, $class;
186 }
187
188 sub iternext {
189 return shift->{cb}->();
190 }