| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 25; |
| 4 |
use XML::LibXML; |
| 5 |
|
| 6 |
my $root = XML::LibXML->load_xml( IO => \*DATA )->documentElement; |
| 7 |
|
| 8 |
# TEST |
| 9 |
ok( |
| 10 |
tied %$root, |
| 11 |
'elements can be hash dereffed to a tied hash', |
| 12 |
); |
| 13 |
|
| 14 |
# TEST |
| 15 |
isa_ok( |
| 16 |
tied %$root, |
| 17 |
'XML::LibXML::AttributeHash', |
| 18 |
'tied %$element', |
| 19 |
); |
| 20 |
|
| 21 |
# TEST |
| 22 |
ok( |
| 23 |
exists $root->{'attr1'}, |
| 24 |
'EXISTS non-namespaced', |
| 25 |
); |
| 26 |
|
| 27 |
# TEST |
| 28 |
is( |
| 29 |
$root->{'attr1'}, |
| 30 |
'foo', |
| 31 |
'FETCH non-namespaced', |
| 32 |
); |
| 33 |
|
| 34 |
$root->{attr1} = 'bar'; |
| 35 |
# TEST |
| 36 |
is( |
| 37 |
$root->getAttribute('attr1'), |
| 38 |
'bar', |
| 39 |
'STORE non-namespaced', |
| 40 |
); |
| 41 |
|
| 42 |
$root->{attr11} = 'baz'; |
| 43 |
# TEST |
| 44 |
is( |
| 45 |
$root->getAttribute('attr11'), |
| 46 |
'baz', |
| 47 |
'STORE (and create) non-namespaced', |
| 48 |
); |
| 49 |
|
| 50 |
delete $root->{attr11}; |
| 51 |
# TEST |
| 52 |
ok( |
| 53 |
!$root->hasAttribute('attr11'), |
| 54 |
'DELETE non-namespaced', |
| 55 |
); |
| 56 |
|
| 57 |
my $fail = 1; |
| 58 |
while (my ($k, $v) = each %$root) |
| 59 |
{ |
| 60 |
if ($k eq 'attr1') |
| 61 |
{ |
| 62 |
$fail = 0; |
| 63 |
# TEST |
| 64 |
pass('FIRSTKEY/NEXTKEY non-namespaced'); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ($fail) |
| 69 |
{ |
| 70 |
fail('FIRSTKEY/NEXTKEY non-namespaced'); |
| 71 |
} |
| 72 |
|
| 73 |
# TEST |
| 74 |
ok( |
| 75 |
exists $root->{'{http://localhost/}attr2'}, |
| 76 |
'EXISTS namespaced', |
| 77 |
); |
| 78 |
|
| 79 |
# TEST |
| 80 |
is( |
| 81 |
$root->{'{http://localhost/}attr2'}, |
| 82 |
'bar', |
| 83 |
'FETCH namespaced', |
| 84 |
); |
| 85 |
|
| 86 |
$root->{'{http://localhost/}attr2'} = 'quux'; |
| 87 |
# TEST |
| 88 |
is( |
| 89 |
$root->getAttributeNS('http://localhost/', 'attr2'), |
| 90 |
'quux', |
| 91 |
'STORE namespaced', |
| 92 |
); |
| 93 |
|
| 94 |
$root->{'{http://localhost/}attr22'} = 'quuux'; |
| 95 |
# TEST |
| 96 |
is( |
| 97 |
$root->getAttributeNS('http://localhost/', 'attr22'), |
| 98 |
'quuux', |
| 99 |
'STORE (and create) namespaced', |
| 100 |
); |
| 101 |
|
| 102 |
$root->{'{http://localhost/another}attr22'} = 'xyzzy'; |
| 103 |
# TEST |
| 104 |
is( |
| 105 |
$root->getAttributeNS('http://localhost/another', 'attr22'), |
| 106 |
'xyzzy', |
| 107 |
'STORE (and create) namespaced, in new namespace', |
| 108 |
); |
| 109 |
|
| 110 |
delete $root->{'{http://localhost/another}attr22'}; |
| 111 |
# TEST |
| 112 |
ok( |
| 113 |
!$root->hasAttributeNS('http://localhost/another', 'attr22'), |
| 114 |
'DELETE namespaced', |
| 115 |
); |
| 116 |
|
| 117 |
my $fail2 = 1; |
| 118 |
while (my ($k, $v) = each %$root) |
| 119 |
{ |
| 120 |
if ($k eq '{http://localhost/}attr22') |
| 121 |
{ |
| 122 |
$fail2 = 0; |
| 123 |
# TEST |
| 124 |
pass('FIRSTKEY/NEXTKEY namespaced'); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if ($fail2) |
| 129 |
{ |
| 130 |
fail('FIRSTKEY/NEXTKEY namespaced'); |
| 131 |
} |
| 132 |
|
| 133 |
# TEST |
| 134 |
like( |
| 135 |
$root->toStringEC14N, |
| 136 |
qr{<root xmlns:x="http://localhost/" attr1="bar" x:attr2="quux" x:attr22="quuux"></root>}, |
| 137 |
'!!! toStringEC14N', |
| 138 |
); |
| 139 |
|
| 140 |
# These are tests for: |
| 141 |
# https://rt.cpan.org/Ticket/Display.html?id=75257 |
| 142 |
# https://rt.cpan.org/Ticket/Display.html?id=75293 |
| 143 |
# https://rt.cpan.org/Ticket/Display.html?id=75259 |
| 144 |
# (Three duplicate reports for the same problem.) |
| 145 |
|
| 146 |
# TEST |
| 147 |
is_deeply( |
| 148 |
[($root == $root)], |
| 149 |
[1], |
| 150 |
'== comparison', |
| 151 |
); |
| 152 |
|
| 153 |
# TEST |
| 154 |
is_deeply( |
| 155 |
[($root eq $root)], |
| 156 |
[1], |
| 157 |
'eq comparison', |
| 158 |
); |
| 159 |
|
| 160 |
# TEST |
| 161 |
is_deeply( |
| 162 |
[($root == 'not-root')], |
| 163 |
[''], |
| 164 |
'== negative comparison', |
| 165 |
); |
| 166 |
|
| 167 |
# TEST |
| 168 |
is_deeply( |
| 169 |
[($root == 'not-root')], |
| 170 |
[''], |
| 171 |
'== negative comparison', |
| 172 |
); |
| 173 |
|
| 174 |
# TEST |
| 175 |
is_deeply( |
| 176 |
[!($root != 'not-root')], |
| 177 |
[''], |
| 178 |
'!== negative comparison', |
| 179 |
); |
| 180 |
|
| 181 |
# TEST |
| 182 |
is_deeply( |
| 183 |
[($root eq 'not-root')], |
| 184 |
[''], |
| 185 |
'eq negative comparison', |
| 186 |
); |
| 187 |
|
| 188 |
# TEST |
| 189 |
is_deeply( |
| 190 |
[!($root ne 'not-root')], |
| 191 |
[''], |
| 192 |
'eq negative comparison', |
| 193 |
); |
| 194 |
|
| 195 |
{ |
| 196 |
my $doc = XML::LibXML->load_xml( string => <<'EOT' )->documentElement; |
| 197 |
<foo> |
| 198 |
<bar /> |
| 199 |
<baz /> |
| 200 |
</foo> |
| 201 |
EOT |
| 202 |
|
| 203 |
my ($bar_elem) = $doc->findnodes('//bar'); |
| 204 |
my ($baz_elem) = $doc->findnodes('//baz'); |
| 205 |
|
| 206 |
# TEST |
| 207 |
is_deeply([$bar_elem == $baz_elem], [''], |
| 208 |
'== comparison between two differenet nodes' |
| 209 |
); |
| 210 |
|
| 211 |
# TEST |
| 212 |
is_deeply([$bar_elem eq $baz_elem], [''], |
| 213 |
'eq comparison between two differenet nodes' |
| 214 |
); |
| 215 |
} |
| 216 |
__DATA__ |
| 217 |
<root attr1="foo" xmlns:x="http://localhost/" x:attr2="bar" /> |