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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
use strict;
use warnings;
use Test::More;
plan tests => 44;
##############################################################################
# T E S T R O U T I N E S
##############################################################################
eval "use XML::Simple qw(:strict);";
ok(!$@, 'XML::Simple loads ok with qw(:strict)');
# Check that the basic functionality still works
my $xml = q(<opt name1="value1" name2="value2"></opt>);
$@ = '';
my $opt = eval {
XMLin($xml, forcearray => 1, keyattr => {});
};
is($@, '', 'XMLin() did not fail');
my $keys = join(' ', sort keys %$opt);
is($keys, 'name1 name2', 'and managed to produce the expected results');
# Confirm that forcearray cannot be omitted
eval {
$opt = XMLin($xml, keyattr => {});
};
isnt($@, '', 'omitting forcearray was a fatal error');
like($@, qr/(?i)No value specified for 'forcearray'/,
'with the correct error message');
# Confirm that keyattr cannot be omitted
eval {
$opt = XMLin($xml, forcearray => []);
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that element names from keyattr cannot be omitted from forcearray
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => 0);
};
isnt($@, '', 'omitting forcearray for elements in keyattr was a fatal error');
like($@, qr/(?i)<part> set in keyattr but not in forcearray/,
'with the correct error message');
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => ['x','y']);
};
isnt($@, '', 'omitting keyattr elements from forcearray was a fatal error');
like($@, qr/(?i)<part> set in keyattr but not in forcearray/,
'with the correct error message');
# Confirm that missing key attributes are detected
$xml = q(
<opt>
<part partnum="12345" desc="Thingy" />
<part partnum="67890" desc="Wotsit" />
<part desc="Fnurgle" />
</opt>
);
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => 1);
};
isnt($@, '', 'key attribute missing from names element was a fatal error');
like($@, qr/(?i)<part> element has no 'partnum' key attribute/,
'with the correct error message');
# Confirm that non-unique values in key attributes are detected
$xml = q(
<opt>
<part partnum="12345" desc="Thingy" />
<part partnum="67890" desc="Wotsit" />
<part partnum="12345" desc="Springy" />
</opt>
);
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => 1);
};
isnt($@, '', 'non-unique key attribute values was a fatal error');
like($@, qr/(?i)<part> element has non-unique value in 'partnum' key attribute: 12345/,
'with the correct error message');
# Confirm that stringification of references is trapped
$xml = q(
<opt>
<item>
<name><firstname>Bob</firstname></name>
<age>21</age>
</item>
</opt>
);
eval {
$opt = XMLin($xml, keyattr => { item => 'name' }, forcearray => ['item']);
};
isnt($@, '', 'key attribute not a scalar was a fatal error');
like($@, qr/(?i)<item> element has non-scalar 'name' key attribute/,
'with the correct error message');
##############################################################################
# Now confirm that XMLout gets checked too
#
# Check that the basic functionality still works under :strict
my $ref = {
person => [
{ name => 'bob' },
{ name => 'kate' },
]
};
$@ = '';
$xml = eval {
XMLout($ref, keyattr => {}, rootname => 'list');
};
is($@, '', 'XMLout() did not fail');
like($xml, qr{
^\s*<list\s*>
\s*<person\s+name="bob"\s*/>
\s*<person\s+name="kate"\s*/>
\s*</list\s*>\s*$
}xs, 'and managed to produce the expected results');
# Confirm that keyattr cannot be omitted
$@ = '';
eval {
XMLout($ref, rootname => 'list');
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that forcearray can be omitted (only rqd on input)
$@ = '';
eval {
XMLout($ref, keyattr => {x => 'y'});
};
is($@, '', 'omitting forcearray was not a fatal error on output');
##############################################################################
# Now repeat all that using the OO syntax
##############################################################################
# Check that the basic functionality still works
$xml = q(<opt name1="value1" name2="value2"></opt>);
my $xs = XML::Simple->new(forcearray => 1, keyattr => {});
$@ = '';
$opt = eval {
$xs->XMLin($xml);
};
is($@, '', '$xs->XMLin() did not fail');
$keys = join(' ', sort keys %$opt);
is($keys, 'name1 name2', 'and managed to produce the expected results');
# Confirm that forcearray cannot be omitted
$xs = XML::Simple->new(keyattr => {});
$@ = '';
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting forcearray was a fatal error');
like($@, qr/(?i)No value specified for 'forcearray'/,
'with the correct error message');
# Confirm that keyattr cannot be omitted
$xs = XML::Simple->new(forcearray => []);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that element names from keyattr cannot be omitted from forcearray
$xs = XML::Simple->new(keyattr => { part => 'partnum' }, forcearray => 0);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting forcearray for elements in keyattr was a fatal error');
like($@, qr/(?i)<part> set in keyattr but not in forcearray/,
'with the correct error message');
$xs = XML::Simple->new(keyattr => { part => 'partnum' }, forcearray => ['x','y']);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting keyattr elements from forcearray was a fatal error');
like($@, qr/(?i)<part> set in keyattr but not in forcearray/,
'with the correct error message');
# Confirm that missing key attributes are detected
$xml = q(
<opt>
<part partnum="12345" desc="Thingy" />
<part partnum="67890" desc="Wotsit" />
<part desc="Fnurgle" />
</opt>
);
$xs = XML::Simple->new(keyattr => { part => 'partnum' }, forcearray => 1);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'key attribute missing from names element was a fatal error');
like($@, qr/(?i)<part> element has no 'partnum' key attribute/,
'with the correct error message');
# Confirm that stringification of references is trapped
$xml = q(
<opt>
<item>
<name><firstname>Bob</firstname></name>
<age>21</age>
</item>
</opt>
);
$xs = XML::Simple->new(keyattr => { item => 'name' }, forcearray => ['item']);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'key attribute not a scalar was a fatal error');
like($@, qr/(?i)<item> element has non-scalar 'name' key attribute/,
'with the correct error message');
##############################################################################
# Now confirm that XMLout gets checked too
#
# Check that the basic functionality still works under :strict
$ref = {
person => [
{ name => 'bob' },
{ name => 'kate' },
]
};
$xs = XML::Simple->new(keyattr => {}, rootname => 'list');
$@ = '';
$xml = eval {
$xs->XMLout($ref);
};
is($@, '', 'XMLout() did not fail');
like($xml, qr{
^\s*<list\s*>
\s*<person\s+name="bob"\s*/>
\s*<person\s+name="kate"\s*/>
\s*</list\s*>\s*$
}xs, 'and managed to produce the expected results');
# Confirm that keyattr cannot be omitted
$xs = XML::Simple->new(rootname => 'list');
eval {
$xs->XMLout($ref);
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that code in other modules can still call XMLin without having
# strict mode forced upon them.
$xml = q(<opt name1="value1" name2="value2"></opt>);
eval {
$opt = SimpleWrapper::XMLin($xml, keyattr => {});
};
is($@, '', 'other namespaces do not have strict mode forced upon them');
# Unless those calls explicitly enable strict mode
eval {
$opt = SimpleWrapper::XMLin($xml, StrictMode => 1, keyattr => {});
};
isnt($@, '', 'other namespaces do not have strict mode forced upon them');
like($@, qr/(?i)No value specified for 'forcearray'/,
'with the correct error message');
# And calls in this namespace can turn strict mode off
eval {
$opt = XMLin($xml, StrictMode => 0, keyattr => {});
};
is($@, '', 'other namespaces do not have strict mode forced upon them');
exit(0);
package SimpleWrapper;
sub XMLin {
XML::Simple::XMLin(@_);
}
|