Debian Codegraph @

t/06-x2h.t

raw
1 package main;
2 use strict;
3 use warnings;
4
5 use Test::More tests => 10;
6 use Data::Dumper;
7 $Data::Dumper::Indent = 0;
8 $Data::Dumper::Sortkeys = 1;
9
10 use XML::Hash::XS 'xml2hash';
11 $XML::Hash::XS::keep_root = 0;
12
13 our $xml_decl_utf8 = qq{<?xml version="1.0" encoding="utf-8"?>};
14
15 {
16 is
17 Dumper(xml2hash(<<"XML", keep_root => 1, content => 'text', trim => 1)),
18 <root attr1="1" attr2="2">
19 <node1>value1</node1>
20 <node2 attr1="1">value2</node2>
21 <node3>
22 content1
23 <!-- comment -->
24 content2
25 </node3>
26 <node4>
27 content1
28 <empty_node4/>
29 content2
30 </node4>
31 <item>1</item>
32 <item>2</item>
33 <item>3</item>
34 <cdata><![CDATA[
35 abcde!@#$%^&*<>
36 ]]></cdata>
37 <cdata2><![CDATA[ abc ]]]></cdata2>
38 <cdata3><![CDATA[ [ abc ] ]> ]]]]]]></cdata3>
39 </root>
40 XML
41 Dumper({
42 root => {
43 attr1 => '1',
44 attr2 => '2',
45 cdata => 'abcde!@#0^&*<>',
46 cdata2 => 'abc ]',
47 cdata3 => '[ abc ] ]> ]]]]',
48 item => ['1', '2', '3'],
49 node1 => 'value1',
50 node2 => {
51 attr1 => '1',
52 text => 'value2',
53 },
54 node3 => ['content1', 'content2'],
55 node4 => {
56 text => ['content1', 'content2'],
57 empty_node4 => '',
58 },
59 }
60 }),
61 'complex',
62 ;
63 }
64
65 {
66 use utf8;
67 my $xml = <<'XML';
68 <?xml version="1.0" encoding="UTF-8"?>
69 <note>Test</note>
70 XML
71 no warnings qw(void);
72 substr $xml, 0, 0; # this will cause error in XS param type definition
73 is
74 xml2hash(\$xml, trim => 1),
75 'Test',
76 'check validation parameters',
77 ;
78 }
79
80 {
81 my $xml=qq[<?xml version="1.0" encoding="utf-8"?>\x0D\x0A<aaaa>\x0D\x0Aasdasdsa\x0D\x0A</aaaa>];
82 is
83 xml2hash(\$xml, trim => 1),
84 'asdasdsa',
85 'bug RT#103002',
86 ;
87 }
88
89 {
90 my $xml=qq[<a>\x0D\x0Aasd\x0D\x0Aasd\x0D\x0D\x0Aasd\x0D\x0A</a>];
91 is
92 xml2hash(\$xml, trim => 1),
93 "asd\x0Aasd\x0A\x0Aasd",
94 'normalize line feeds',
95 ;
96 }
97
98 {
99 is
100 Dumper(xml2hash(<<"XML")),
101 <root>
102 <aaa>bbb<!-- ccc -->ddd<eee>fff</eee>ggg</aaa>
103 </root>
104 XML
105 Dumper({aaa => { content => ['bbb', 'ddd', 'ggg'], eee => 'fff' }}),
106 'bug with many contents in the one node',
107 ;
108 }
109
110 {
111 eval { xml2hash("<root></root><root2></root2>") };
112 ok($@, 'invalid xml');
113 }
114
115 {
116 eval { xml2hash("<root></root><root2>") };
117 ok($@, 'invalid xml2');
118 }
119
120 {
121 eval { xml2hash("</root>") };
122 ok($@, 'invalid xml3');
123 }
124
125 {
126 eval { xml2hash("<root></root>text") };
127 ok($@, 'invalid xml4');
128 }
129
130 {
131 is
132 Dumper(xml2hash(<<"XML")),
133 <root>
134 <row>
135 <cell text="test&apos;s"/>
136 </row>
137 <row>
138 <cell text=" test&apos;s"/>
139 </row>
140 </root>
141 XML
142 Dumper({
143 row => [
144 {cell => {'text' => "test's"}},
145 {cell => {'text' => " test's"}},
146 ],
147 }),
148 'memory allocation bug',
149 ;
150 }