File: 06-x2h.t

package info (click to toggle)
libxml-hash-xs-perl 0.64-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: ansic: 5,177; perl: 322; xml: 15; makefile: 9
file content (150 lines) | stat: -rw-r--r-- 2,927 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main;
use strict;
use warnings;

use Test::More tests => 10;
use Data::Dumper;
$Data::Dumper::Indent = 0;
$Data::Dumper::Sortkeys = 1;

use XML::Hash::XS 'xml2hash';
$XML::Hash::XS::keep_root = 0;

our $xml_decl_utf8 = qq{<?xml version="1.0" encoding="utf-8"?>};

{
    is
        Dumper(xml2hash(<<"XML", keep_root => 1, content => 'text', trim => 1)),
<root attr1="1" attr2="2">
    <node1>value1</node1>
    <node2 attr1="1">value2</node2>
    <node3>
        content1
        <!-- comment -->
        content2
    </node3>
    <node4>
        content1
        <empty_node4/>
        content2
    </node4>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <cdata><![CDATA[
        abcde!@#$%^&*<>
    ]]></cdata>
    <cdata2><![CDATA[ abc ]]]></cdata2>
    <cdata3><![CDATA[ [ abc ] ]> ]]]]]]></cdata3>
</root>
XML
        Dumper({
root => {
    attr1  => '1',
    attr2  => '2',
    cdata  => 'abcde!@#0^&*<>',
    cdata2 => 'abc ]',
    cdata3 => '[ abc ] ]> ]]]]',
    item   => ['1', '2', '3'],
    node1  => 'value1',
    node2  => {
        attr1 => '1',
        text  => 'value2',
    },
    node3  => ['content1', 'content2'],
    node4  => {
        text        => ['content1', 'content2'],
        empty_node4 => '',
    },
}
}),
        'complex',
    ;
}

{
    use utf8;
    my $xml = <<'XML';
<?xml version="1.0" encoding="UTF-8"?>
<note>Test</note>
XML
    no warnings qw(void);
    substr $xml, 0, 0; # this will cause error in XS param type definition
    is
        xml2hash(\$xml, trim => 1),
        'Test',
        'check validation parameters',
    ;
}

{
    my $xml=qq[<?xml version="1.0" encoding="utf-8"?>\x0D\x0A<aaaa>\x0D\x0Aasdasdsa\x0D\x0A</aaaa>];
    is
        xml2hash(\$xml, trim => 1),
        'asdasdsa',
        'bug RT#103002',
    ;
}

{
    my $xml=qq[<a>\x0D\x0Aasd\x0D\x0Aasd\x0D\x0D\x0Aasd\x0D\x0A</a>];
    is
        xml2hash(\$xml, trim => 1),
        "asd\x0Aasd\x0A\x0Aasd",
        'normalize line feeds',
    ;
}

{
    is
        Dumper(xml2hash(<<"XML")),
<root>
    <aaa>bbb<!-- ccc -->ddd<eee>fff</eee>ggg</aaa>
</root>
XML
        Dumper({aaa => { content => ['bbb', 'ddd', 'ggg'], eee => 'fff' }}),
        'bug with many contents in the one node',
    ;
}

{
    eval { xml2hash("<root></root><root2></root2>") };
    ok($@, 'invalid xml');
}

{
    eval { xml2hash("<root></root><root2>") };
    ok($@, 'invalid xml2');
}

{
    eval { xml2hash("</root>") };
    ok($@, 'invalid xml3');
}

{
    eval { xml2hash("<root></root>text") };
    ok($@, 'invalid xml4');
}

{
    is
        Dumper(xml2hash(<<"XML")),
<root>
    <row>
        <cell text="test&apos;s"/>
    </row>
    <row>
        <cell text=" test&apos;s"/>
    </row>
</root>
XML
        Dumper({
            row => [
                {cell => {'text' => "test's"}},
                {cell => {'text' => " test's"}},
            ],
        }),
        'memory allocation bug',
    ;
}