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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
|
#!/usr/bin/perl -w
########################################################################
# test.pl - test script for XML::Writer module.
# Copyright (c) 1999 by Megginson Technologies.
# Copyright (c) 2004 - 2006 by Joseph Walton <joe@kafsemo.org>.
# No warranty. Commercial and non-commercial use freely permitted.
#
# $Id: 01_main.t 175 2006-11-11 16:54:22Z josephw $
########################################################################
# Before 'make install' is performed this script should be runnable with
# 'make test'. After 'make install' it should work as 'perl 01_main.t'
use strict;
use Test::More(tests => 213);
# Catch warnings
my $warning;
$SIG{__WARN__} = sub {
($warning) = @_ unless ($warning);
};
sub wasNoWarning($)
{
my ($reason) = @_;
if (!ok(!$warning, $reason)) {
diag($warning);
}
}
# Constants for Unicode support
my $unicodeSkipMessage = 'Unicode only supported with Perl >= 5.8.1';
sub isUnicodeSupported()
{
return $] >= 5.008001;
}
require XML::Writer;
SKIP: {
skip "Perls before 5.6 always warn when loading XML::Writer", 1 if $] <=
5.006;
wasNoWarning('Loading XML::Writer should not result in warnings');
}
use IO::File;
# The XML::Writer that will be used
my $w;
my $outputFile = IO::File->new_tmpfile or die "Unable to create temporary file: $!";
# Fetch the current contents of the scratch file as a scalar
sub getBufStr()
{
local($/);
binmode($outputFile, ':bytes') if isUnicodeSupported();
$outputFile->seek(0, 0);
return <$outputFile>;
}
# Set up the environment to run a test.
sub initEnv(@)
{
my (%args) = @_;
# Reset the scratch file
$outputFile->seek(0, 0);
$outputFile->truncate(0);
binmode($outputFile, ':raw') if $] >= 5.006;
# Overwrite OUTPUT so it goes to the scratch file
$args{'OUTPUT'} = $outputFile;
# Set NAMESPACES, unless it's present
$args{'NAMESPACES'} = 1 unless(defined($args{'NAMESPACES'}));
undef($warning);
$w = new XML::Writer(%args) || die "Cannot create XML writer";
}
#
# Check the results in the temporary output file.
#
# $expected - the exact output expected
#
sub checkResult($$)
{
my ($expected, $explanation) = (@_);
my $actual = getBufStr();
if ($expected eq $actual) {
ok(1, $explanation);
} else {
my @e = split(/\n/, $expected);
my @a = split(/\n/, $actual);
if (@e + @a == 2) {
is(getBufStr(), $expected, $explanation);
} else {
if (eval {require Algorithm::Diff;}) {
fail($explanation);
Algorithm::Diff::traverse_sequences( \@e, \@a, {
MATCH => sub { diag(" $e[$_[0]]\n"); },
DISCARD_A => sub { diag("-$e[$_[0]]\n"); },
DISCARD_B => sub { diag("+$a[$_[1]]\n"); }
});
} else {
fail($explanation);
diag(" got: '$actual'\n");
diag(" expected: '$expected'\n");
}
}
}
wasNoWarning('(no warnings)');
}
#
# Expect an error of some sort, and check that the message matches.
#
# $pattern - a regular expression that must match the error message
# $value - the return value from an eval{} block
#
sub expectError($$) {
my ($pattern, $value) = (@_);
if (!ok((!defined($value) and ($@ =~ $pattern)), "Error expected: $pattern"))
{
diag('Actual error:');
if ($@) {
diag($@);
} else {
diag('(no error)');
diag(getBufStr());
}
}
}
# Empty element tag.
TEST: {
initEnv();
$w->emptyTag("foo");
$w->end();
checkResult("<foo />\n", 'An empty element tag');
};
# Empty element tag with XML decl.
TEST: {
initEnv();
$w->xmlDecl();
$w->emptyTag("foo");
$w->end();
checkResult(<<"EOS", 'Empty element tag with XML declaration');
<?xml version="1.0"?>
<foo />
EOS
};
# A document with a public and system identifier set
TEST: {
initEnv();
$w->doctype('html', "-//W3C//DTD XHTML 1.1//EN",
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
$w->emptyTag('html');
$w->end();
checkResult(<<"EOS", 'A document with a public and system identifier');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html />
EOS
};
# A document with a public and system identifier set, using startTag
TEST: {
initEnv();
$w->doctype('html', "-//W3C//DTD XHTML 1.1//EN",
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
$w->startTag('html');
$w->endTag('html');
$w->end();
checkResult(<<"EOS", 'A document with a public and system identifier');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html></html>
EOS
};
# A document with a only a public identifier
TEST: {
initEnv();
expectError("A DOCTYPE declaration with a public ID must also have a system ID", eval {
$w->doctype('html', "-//W3C//DTD XHTML 1.1//EN");
});
};
# A document with only a system identifier set
TEST: {
initEnv();
$w->doctype('html', undef, "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
$w->emptyTag('html');
$w->end();
checkResult(<<"EOS", 'A document with just a system identifier');
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html />
EOS
};
# Empty element tag with standalone set
TEST: {
initEnv();
$w->xmlDecl(undef, 'yes');
$w->emptyTag("foo");
$w->end();
checkResult(<<"EOS", 'A document with "standalone" declared');
<?xml version="1.0" standalone="yes"?>
<foo />
EOS
};
# Empty element tag with standalone explicitly set to 'no'
TEST: {
initEnv();
$w->xmlDecl(undef, 'no');
$w->emptyTag("foo");
$w->end();
checkResult(<<"EOS", "A document with 'standalone' declared as 'no'");
<?xml version="1.0" standalone="no"?>
<foo />
EOS
};
# xmlDecl with encoding set
TEST: {
initEnv();
$w->xmlDecl('ISO-8859-1');
$w->emptyTag("foo");
$w->end();
checkResult(<<"EOS", 'A document with a declared encoding');
<?xml version="1.0" encoding="ISO-8859-1"?>
<foo />
EOS
};
# Start/end tag.
TEST: {
initEnv();
$w->startTag("foo");
$w->endTag("foo");
$w->end();
checkResult("<foo></foo>\n", 'A separate start and end tag');
};
# Attributes
TEST: {
initEnv();
$w->emptyTag("foo", "x" => "1>2");
$w->end();
checkResult("<foo x=\"1>2\" />\n", 'Simple attributes');
};
# Character data
TEST: {
initEnv();
$w->startTag("foo");
$w->characters("<tag>&</tag>");
$w->endTag("foo");
$w->end();
checkResult("<foo><tag>&amp;</tag></foo>\n", 'Escaped character data');
};
# Comment outside document element
TEST: {
initEnv();
$w->comment("comment");
$w->emptyTag("foo");
$w->end();
checkResult("<!-- comment -->\n<foo />\n", 'A comment outside the document element');
};
# Processing instruction without data (outside document element)
TEST: {
initEnv();
$w->pi("pi");
$w->emptyTag("foo");
$w->end();
checkResult("<?pi?>\n<foo />\n", 'A data-less processing instruction');
};
# Processing instruction with data (outside document element)
TEST: {
initEnv();
$w->pi("pi", "data");
$w->emptyTag("foo");
$w->end();
checkResult("<?pi data?>\n<foo />\n", 'A processing instruction with data');
};
# Comment inside document element
TEST: {
initEnv();
$w->startTag("foo");
$w->comment("comment");
$w->endTag("foo");
$w->end();
checkResult("<foo><!-- comment --></foo>\n", 'A comment inside an element');
};
# Processing instruction inside document element
TEST: {
initEnv();
$w->startTag("foo");
$w->pi("pi");
$w->endTag("foo");
$w->end();
checkResult("<foo><?pi?></foo>\n", 'A processing instruction inside an element');
};
# WFE for mismatched tags
TEST: {
initEnv();
$w->startTag("foo");
expectError("Attempt to end element \"foo\" with \"bar\" tag", eval {
$w->endTag("bar");
});
};
# WFE for unclosed elements
TEST: {
initEnv();
$w->startTag("foo");
$w->startTag("foo");
$w->endTag("foo");
expectError("Document ended with unmatched start tag\\(s\\)", eval {
$w->end();
});
};
# WFE for no document element
TEST: {
initEnv();
$w->xmlDecl();
expectError("Document cannot end without a document element", eval {
$w->end();
});
};
# WFE for multiple document elements (non-empty)
TEST: {
initEnv();
$w->startTag('foo');
$w->endTag('foo');
expectError("Attempt to insert start tag after close of", eval {
$w->startTag('foo');
});
};
# WFE for multiple document elements (empty)
TEST: {
initEnv();
$w->emptyTag('foo');
expectError("Attempt to insert empty tag after close of", eval {
$w->emptyTag('foo');
});
};
# DOCTYPE mismatch with empty tag
TEST: {
initEnv();
$w->doctype('foo');
expectError("Document element is \"bar\", but DOCTYPE is \"foo\"", eval {
$w->emptyTag('bar');
});
};
# DOCTYPE mismatch with start tag
TEST: {
initEnv();
$w->doctype('foo');
expectError("Document element is \"bar\", but DOCTYPE is \"foo\"", eval {
$w->startTag('bar');
});
};
# DOCTYPE declarations
TEST: {
initEnv();
$w->doctype('foo');
expectError("Attempt to insert second DOCTYPE", eval {
$w->doctype('bar');
});
};
# Misplaced DOCTYPE declaration
TEST: {
initEnv();
$w->startTag('foo');
expectError("The DOCTYPE declaration must come before", eval {
$w->doctype('foo');
});
};
# Multiple XML declarations
TEST: {
initEnv();
$w->xmlDecl();
expectError("The XML declaration is not the first thing", eval {
$w->xmlDecl();
});
};
# Misplaced XML declaration
TEST: {
initEnv();
$w->comment();
expectError("The XML declaration is not the first thing", eval {
$w->xmlDecl();
});
};
# Implied end-tag name.
TEST: {
initEnv();
$w->startTag('foo');
$w->endTag();
$w->end();
checkResult("<foo></foo>\n", 'A tag ended using an implied tag name');
};
# in_element query
TEST: {
initEnv();
$w->startTag('foo');
$w->startTag('bar');
ok($w->in_element('bar'), 'in_element should identify the current element');
};
# within_element query
TEST: {
initEnv();
$w->startTag('foo');
$w->startTag('bar');
ok($w->within_element('foo') && $w->within_element('bar'),
'within_element should know about all elements above us');
};
# current_element query
TEST: {
initEnv();
$w->startTag('foo');
$w->startTag('bar');
is($w->current_element(), 'bar', 'current_element should identify the element we are in');
};
# ancestor query
TEST: {
initEnv();
$w->startTag('foo');
$w->startTag('bar');
ok($w->ancestor(0) eq 'bar' && $w->ancestor(1) eq 'foo',
'ancestor() should match the startTag calls that have been made');
};
# Basic namespace processing with empty element
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, 'foo');
$w->emptyTag([$ns, 'doc']);
$w->end();
checkResult("<foo:doc xmlns:foo=\"$ns\" />\n", 'Basic namespace processing');
};
# Basic namespace processing with start/end tags
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, 'foo');
$w->startTag([$ns, 'doc']);
$w->endTag([$ns, 'doc']);
$w->end();
checkResult("<foo:doc xmlns:foo=\"$ns\"></foo:doc>\n", 'Basic namespace processing');
};
# Basic namespace processing with generated prefix
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->startTag([$ns, 'doc']);
$w->endTag([$ns, 'doc']);
$w->end();
checkResult("<__NS1:doc xmlns:__NS1=\"$ns\"></__NS1:doc>\n",
'Basic namespace processing with a generated prefix');
};
# Basic namespace processing with attributes and empty tag.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, 'foo');
$w->emptyTag([$ns, 'doc'], [$ns, 'id'] => 'x');
$w->end();
checkResult("<foo:doc foo:id=\"x\" xmlns:foo=\"$ns\" />\n",
'A namespaced element with a namespaced attribute');
};
# Same as above, but with default namespace.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, '');
$w->emptyTag([$ns, 'doc'], [$ns, 'id'] => 'x');
$w->end();
checkResult("<doc __NS1:id=\"x\" xmlns=\"$ns\" xmlns:__NS1=\"$ns\" />\n",
'Same as above, but with a default namespace');
};
# Same as above, but passing namespace prefixes through constructor
TEST: {
my $ns = 'http://www.foo.com/';
initEnv(PREFIX_MAP => {$ns => ''});
$w->emptyTag([$ns, 'doc'], [$ns, 'id'] => 'x');
$w->end();
checkResult("<doc __NS1:id=\"x\" xmlns=\"$ns\" xmlns:__NS1=\"$ns\" />\n",
'Same as above, but passing the prefixes through the constructor');
};
# Same as above, but passing namespace prefixes through constructor and
# then removing them programatically
TEST: {
my $ns = 'http://www.foo.com/';
initEnv(PREFIX_MAP => {$ns => ''});
$w->removePrefix($ns);
$w->emptyTag([$ns, 'doc'], [$ns, 'id'] => 'x');
$w->end();
checkResult("<__NS1:doc __NS1:id=\"x\" xmlns:__NS1=\"$ns\" />\n",
'Same as above, but removing the prefix before the document starts');
};
# Verify that removePrefix works when there is no default prefix
TEST: {
my $ns = 'http://www.foo.com/';
initEnv(PREFIX_MAP => {$ns => 'pfx'});
$w->removePrefix($ns);
wasNoWarning('removePrefix should not warn when there is no default prefix');
}
# Verify that a removed namespace prefix behaves as if it were never added
TEST: {
my $ns = 'http://www.foo.com/';
initEnv(PREFIX_MAP => {$ns => 'pfx', 'http://www.example.com/' => ''});
$w->removePrefix($ns);
$w->startTag([$ns, 'x']);
$w->emptyTag([$ns, 'y']);
$w->endTag([$ns, 'x']);
$w->end();
checkResult("<__NS1:x xmlns:__NS1=\"$ns\"><__NS1:y /></__NS1:x>\n",
'Same as above, but with a non-default namespace');
};
# Test that autogenerated prefixes avoid collision.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix('http://www.bar.com/', '__NS1');
$w->emptyTag([$ns, 'doc']);
$w->end();
checkResult("<__NS2:doc xmlns:__NS2=\"$ns\" />\n",
"Make sure that an autogenerated prefix doesn't clash");
};
# Check for proper declaration nesting with subtrees.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, 'foo');
$w->startTag('doc');
$w->characters("\n");
$w->emptyTag([$ns, 'ptr1']);
$w->characters("\n");
$w->emptyTag([$ns, 'ptr2']);
$w->characters("\n");
$w->endTag('doc');
$w->end();
checkResult(<<"EOS", 'Check for proper declaration nesting with subtrees.');
<doc>
<foo:ptr1 xmlns:foo="$ns" />
<foo:ptr2 xmlns:foo="$ns" />
</doc>
EOS
};
# Check for proper declaration nesting with top level.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, 'foo');
$w->startTag([$ns, 'doc']);
$w->characters("\n");
$w->emptyTag([$ns, 'ptr1']);
$w->characters("\n");
$w->emptyTag([$ns, 'ptr2']);
$w->characters("\n");
$w->endTag([$ns, 'doc']);
$w->end();
checkResult(<<"EOS", 'Check for proper declaration nesting with top level.');
<foo:doc xmlns:foo="$ns">
<foo:ptr1 />
<foo:ptr2 />
</foo:doc>
EOS
};
# Check for proper default declaration nesting with subtrees.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, '');
$w->startTag('doc');
$w->characters("\n");
$w->emptyTag([$ns, 'ptr1']);
$w->characters("\n");
$w->emptyTag([$ns, 'ptr2']);
$w->characters("\n");
$w->endTag('doc');
$w->end();
checkResult(<<"EOS", 'Check for proper default declaration nesting with subtrees.');
<doc>
<ptr1 xmlns="$ns" />
<ptr2 xmlns="$ns" />
</doc>
EOS
};
# Check for proper default declaration nesting with top level.
TEST: {
initEnv();
my $ns = 'http://www.foo.com/';
$w->addPrefix($ns, '');
$w->startTag([$ns, 'doc']);
$w->characters("\n");
$w->emptyTag([$ns, 'ptr1']);
$w->characters("\n");
$w->emptyTag([$ns, 'ptr2']);
$w->characters("\n");
$w->endTag([$ns, 'doc']);
$w->end();
checkResult(<<"EOS", 'Check for proper default declaration nesting with top level.');
<doc xmlns="$ns">
<ptr1 />
<ptr2 />
</doc>
EOS
};
# Namespace error: attribute name beginning 'xmlns'
TEST: {
initEnv();
expectError("Attribute name.*begins with 'xmlns'", eval {
$w->emptyTag('foo', 'xmlnsxxx' => 'x');
});
};
# Namespace error: Detect an illegal colon in a PI target.
TEST: {
initEnv();
expectError("PI target.*contains a colon", eval {
$w->pi('foo:foo');
});
};
# Namespace error: Detect an illegal colon in an element name.
TEST: {
initEnv();
expectError("Element name.*contains a colon", eval {
$w->emptyTag('foo:foo');
});
};
# Namespace error: Detect an illegal colon in local part of an element name.
TEST: {
initEnv();
expectError("Local part of element name.*contains a colon", eval {
my $ns = 'http://www.foo.com/';
$w->emptyTag([$ns, 'foo:foo']);
});
};
# Namespace error: attribute name containing ':'.
TEST: {
initEnv();
expectError("Attribute name.*contains ':'", eval {
$w->emptyTag('foo', 'foo:bar' => 'x');
});
};
# Namespace error: Detect a colon in the local part of an att name.
TEST: {
initEnv();
expectError("Local part of attribute name.*contains a colon.", eval {
my $ns = "http://www.foo.com/";
$w->emptyTag('foo', [$ns, 'foo:bar']);
});
};
# Verify that no warning is generated when namespace prefixes are passed
# in on construction.
TEST: {
initEnv();
$w->emptyTag(['uri:null', 'element']);
$w->end();
wasNoWarning('No warnings should be generated during writing');
};
# Verify that the 'xml:' prefix is known, and that the declaration is not
# passed through.
#
TEST: {
initEnv();
$w->emptyTag('elem', ['http://www.w3.org/XML/1998/namespace', 'space'] => 'preserve');
$w->end();
if (!unlike(getBufStr(), '/1998/', "No declaration should be generated for the 'xml:' prefix"))
{
diag(getBufStr());
}
};
# This is an API-driving test; to pass, it needs an added method to force XML
# namespace declarations on outer elements that aren't necessarily
# in the namespace themselves.
TEST: {
initEnv(PREFIX_MAP => {'uri:test', 'test'},
FORCED_NS_DECLS => ['uri:test']
);
$w->startTag('doc');
$w->emptyTag(['uri:test', 'elem']);
$w->emptyTag(['uri:test', 'elem']);
$w->emptyTag(['uri:test', 'elem']);
$w->endTag('doc');
$w->end();
if (!unlike(getBufStr(), '/uri:test.*uri:test/', 'An API should allow forced namespace declarations'))
{
diag(getBufStr());
}
};
# Verify that a processing instruction of 'xml-stylesheet' can be added
# without causing a warning, as well as a PI that contains 'xml'
# other than at the beginning, and a PI with no data
TEST: {
initEnv();
$w->pi('xml-stylesheet', "type='text/xsl' href='style.xsl'");
$w->pi('not-reserved-by-xml-spec', '');
$w->pi('pi-with-no-data');
$w->emptyTag('x');
$w->end();
wasNoWarning('The test processing instructions should not cause warnings');
};
# Verify that a still-reserved processing instruction generates
# a warning.
TEST: {
initEnv();
$w->pi('xml-reserves-this-name');
$w->emptyTag('x');
$w->end();
ok($warning =~ "^Processing instruction target begins with 'xml'",
"Reserved processing instruction names should cause warnings");
};
# Processing instruction data may not contain '?>'
TEST: {
initEnv();
expectError("Processing instruction may not contain", eval {
$w->pi('test', 'This string is bad?>');
});
};
# A processing instruction name may not contain '?>'
TEST: {
initEnv();
expectError("Processing instruction may not contain", eval {
$w->pi('bad-processing-instruction-bad?>');
});
};
# A processing instruction name can't contain spaces
TEST: {
initEnv();
expectError("", eval {
$w->pi('processing instruction');
});
};
# Verify that dataMode can be turned on and off for specific elements
TEST: {
initEnv(
DATA_MODE => 1,
DATA_INDENT => 1
);
ok($w->getDataMode(), 'Should be in data mode');
$w->startTag('doc');
$w->dataElement('data', 'This is data');
$w->dataElement('empty', '');
$w->emptyTag('empty');
$w->startTag('mixed');
$w->setDataMode(0);
$w->characters('This is ');
$w->emptyTag('mixed');
ok(!$w->getDataMode(), 'Should be in mixed mode');
$w->characters(' ');
$w->startTag('x');
$w->characters('content');
$w->endTag('x');
$w->characters('.');
$w->setDataMode(1);
$w->setDataIndent(5);
$w->endTag('mixed');
is($w->getDataIndent(), 5, 'Data indent should be changeable');
$w->dataElement('data', 'This is data');
$w->endTag('doc');
$w->end();
checkResult(<<"EOS", 'Turning dataMode on and off whilst writing');
<doc>
<data>This is data</data>
<empty></empty>
<empty />
<mixed>This is <mixed /> <x>content</x>.</mixed>
<data>This is data</data>
</doc>
EOS
};
# Verify that DATA_MODE on its own doesn't cause warnings
TEST: {
initEnv(
DATA_MODE => 1
);
$w->startTag('doc');
$w->endTag('doc');
wasNoWarning('DATA_MODE should not cause warnings');
};
# Test DATA_MODE and initial spacing
TEST: {
initEnv(
DATA_MODE => 1
);
$w->emptyTag('doc');
$w->end();
checkResult("<doc />\n", "An empty element with DATA_MODE");
};
# Test DATA_MODE and initial spacing
TEST: {
initEnv(
DATA_MODE => 1
);
$w->xmlDecl();
$w->emptyTag('doc');
$w->end();
checkResult(<<"EOS", "An empty element with DATA_MODE");
<?xml version="1.0"?>
<doc />
EOS
};
# Test DATA_MODE and initial spacing
TEST: {
initEnv(
DATA_MODE => 1,
DATA_INDENT => 1
);
$w->xmlDecl();
$w->startTag('doc');
$w->emptyTag('item');
$w->endTag('doc');
$w->end();
checkResult(<<"EOS", "A nested element with DATA_MODE and a declaration");
<?xml version="1.0"?>
<doc>
<item />
</doc>
EOS
};
# Writing without namespaces should allow colons
TEST: {
initEnv(NAMESPACES => 0);
$w->startTag('test:doc', 'x:attr' => 'value');
$w->endTag('test:doc');
checkResult('<test:doc x:attr="value"></test:doc>', 'A namespace-less document that uses colons in names');
};
# Test with NEWLINES
TEST: {
initEnv(NEWLINES => 1);
$w->startTag('test');
$w->endTag('test');
$w->end();
checkResult("<test\n></test\n>\n", 'Use of the NEWLINES parameter');
};
# Test bad comments
TEST: {
initEnv();
expectError("Comment may not contain '-->'", eval {
$w->comment('A bad comment -->');
});
};
# Test invadvisible comments
TEST: {
initEnv();
$w->comment("Comments shouldn't contain double dashes i.e., --");
$w->emptyTag('x');
$w->end();
ok($warning =~ "Interoperability problem: ", 'Comments with doubled dashes should cause warnings');
};
# Expect to break on mixed content in data mode
TEST: {
initEnv();
$w->setDataMode(1);
$w->startTag('x');
$w->characters('Text');
expectError("Mixed content not allowed in data mode: element x", eval {
$w->startTag('x');
});
};
# Break with mixed content with emptyTag as well
TEST: {
initEnv();
$w->setDataMode(1);
$w->startTag('x');
$w->characters('Text');
expectError("Mixed content not allowed in data mode: element empty", eval {
$w->emptyTag('empty');
});
};
# Break with mixed content when the element is written before the characters
TEST: {
initEnv();
$w->setDataMode(1);
$w->startTag('x');
$w->emptyTag('empty');
expectError("Mixed content not allowed in data mode: characters", eval {
$w->characters('Text');
});
};
# Break if there are two attributes with the same name
TEST: {
initEnv(NAMESPACES => 0);
expectError("Two attributes named", eval {
$w->emptyTag('x', 'a' => 'First', 'a' => 'Second');
});
};
# Break if there are two attributes with the same namespace-qualified name
TEST: {
initEnv();
expectError("Two attributes named", eval {
$w->emptyTag('x', ['x', 'a'] => 'First', ['x', 'a'] => 'Second');
});
};
# Succeed if there are two attributes with the same local name, but
# in different namespaces
TEST: {
initEnv();
$w->emptyTag('x', ['x', 'a'] => 'First', ['y', 'a'] => 'Second');
checkResult('<x __NS1:a="First" __NS2:a="Second" xmlns:__NS1="x" xmlns:__NS2="y" />', 'Two attributes with the same local name, but in different namespaces');
};
# Check failure when characters are written outside the document
TEST: {
initEnv();
expectError('Attempt to insert characters outside of document element',
eval {
$w->characters('This should fail.');
});
};
# Make sure that closing a tag straight off fails
TEST: {
initEnv();
expectError('End tag .* does not close any open element', eval {
$w->endTag('x');
});
};
# Use UNSAFE to allow attributes with emptyTag
TEST: {
initEnv(UNSAFE => 1);
$w->emptyTag('x', 'xml:space' => 'preserve', ['x', 'y'] => 'z');
$w->end();
checkResult("<x xml:space=\"preserve\" __NS1:y=\"z\" xmlns:__NS1=\"x\" />\n", 'Using UNSAFE to bypass the namespace system for emptyTag');
};
# Use UNSAFE to allow attributes with startTag
TEST: {
initEnv(UNSAFE => 1);
$w->startTag('sys:element', 'xml:space' => 'preserve', ['x', 'y'] => 'z');
$w->endTag('sys:element');
$w->end();
checkResult("<sys:element xml:space=\"preserve\" __NS1:y=\"z\" xmlns:__NS1=\"x\"></sys:element>\n", 'Using UNSAFE to bypass the namespace system for startTag');
};
# Exercise nesting and namespaces
TEST: {
initEnv(DATA_MODE => 1, DATA_INDENT => 1);
$w->startTag(['a', 'element']);
$w->startTag(['a', 'element']);
$w->startTag(['b', 'element']);
$w->startTag(['b', 'element']);
$w->startTag(['c', 'element']);
$w->startTag(['d', 'element']);
$w->endTag(['d', 'element']);
$w->startTag(['d', 'element']);
$w->endTag(['d', 'element']);
$w->endTag(['c', 'element']);
$w->endTag(['b', 'element']);
$w->endTag(['b', 'element']);
$w->endTag(['a', 'element']);
$w->endTag(['a', 'element']);
$w->end();
checkResult(<<"EOS", "Deep-nesting, to exercise prefix management");
<__NS1:element xmlns:__NS1="a">
<__NS1:element>
<__NS2:element xmlns:__NS2="b">
<__NS2:element>
<__NS3:element xmlns:__NS3="c">
<__NS4:element xmlns:__NS4="d"></__NS4:element>
<__NS4:element xmlns:__NS4="d"></__NS4:element>
</__NS3:element>
</__NS2:element>
</__NS2:element>
</__NS1:element>
</__NS1:element>
EOS
};
# Raw output.
TEST: {
initEnv(UNSAFE => 1);
$w->startTag("foo");
$w->raw("<bar/>");
$w->endTag("foo");
$w->end();
checkResult("<foo><bar/></foo>\n", 'raw() should pass text through without escaping it');
};
# Attempting raw output in safe mode
TEST: {
initEnv();
$w->startTag("foo");
expectError('raw\(\) is only available when UNSAFE is set', eval {
$w->raw("<bar/>");
});
}
# Inserting a CDATA section.
TEST: {
initEnv();
$w->startTag("foo");
$w->cdata("cdata testing - test");
$w->endTag("foo");
$w->end();
checkResult("<foo><![CDATA[cdata testing - test]]></foo>\n",
'cdata() should create CDATA sections');
};
# Inserting CDATA containing CDATA delimeters ']]>'.
TEST: {
initEnv();
$w->startTag("foo");
$w->cdata("This is a CDATA section <![CDATA[text]]>");
$w->endTag("foo");
$w->end();
checkResult("<foo><![CDATA[This is a CDATA section <![CDATA[text]]]]><![CDATA[>]]></foo>\n", 'If a CDATA section would be invalid, it should be split up');
};
# cdataElement().
TEST: {
initEnv();
$w->cdataElement("foo", "hello", a => 'b');
$w->end();
checkResult(qq'<foo a="b"><![CDATA[hello]]></foo>\n',
'cdataElement should produce a valid element containing a CDATA section');
};
# Verify that writing characters using CDATA outside of an element fails
TEST: {
initEnv();
expectError('Attempt to insert characters outside of document element',
eval {
$w->cdata('Test');
});
};
# Expect to break on mixed content in data mode
TEST: {
initEnv();
$w->setDataMode(1);
$w->startTag('x');
$w->cdata('Text');
expectError("Mixed content not allowed in data mode: element x", eval {
$w->startTag('x');
});
};
# Break with mixed content when the element is written before the characters
TEST: {
initEnv();
$w->setDataMode(1);
$w->startTag('x');
$w->emptyTag('empty');
expectError("Mixed content not allowed in data mode: characters", eval {
$w->cdata('Text');
});
};
# Make sure addPrefix-caused clashes are resolved
TEST: {
initEnv();
$w->addPrefix('a', '');
$w->addPrefix('b', '');
$w->startTag(['a', 'doc']);
$w->emptyTag(['b', 'elem']);
$w->endTag(['a', 'doc']);
$w->end();
checkResult(<<"EOS", 'Later addPrefix()s should override earlier ones');
<__NS1:doc xmlns:__NS1="a"><elem xmlns="b" /></__NS1:doc>
EOS
};
# addPrefix should work in the middle of a document
TEST: {
initEnv();
$w->addPrefix('a', '');
$w->startTag(['a', 'doc']);
$w->addPrefix('b', '');
$w->emptyTag(['b', 'elem']);
$w->endTag(['a', 'doc']);
$w->end();
checkResult(<<"EOS", 'addPrefix should work in the middle of a document');
<doc xmlns="a"><elem xmlns="b" /></doc>
EOS
};
# Verify changing the default namespace
TEST: {
initEnv(
DATA_MODE => 1,
DATA_INDENT => 1
);
$w->addPrefix('a', '');
$w->startTag(['a', 'doc']);
$w->startTag(['b', 'elem1']);
$w->emptyTag(['b', 'elem1']);
$w->emptyTag(['a', 'elem2']);
$w->endTag(['b', 'elem1']);
$w->addPrefix('b', '');
$w->startTag(['b', 'elem1']);
$w->emptyTag(['b', 'elem1']);
$w->emptyTag(['a', 'elem2']);
$w->endTag(['b', 'elem1']);
$w->addPrefix('a', '');
$w->startTag(['b', 'elem1']);
$w->emptyTag(['b', 'elem1']);
$w->emptyTag(['a', 'elem2']);
$w->endTag(['b', 'elem1']);
$w->endTag(['a', 'doc']);
$w->end();
checkResult(<<"EOS", 'The default namespace should be modifiable during a document');
<doc xmlns="a">
<__NS1:elem1 xmlns:__NS1="b">
<__NS1:elem1 />
<elem2 />
</__NS1:elem1>
<elem1 xmlns="b">
<elem1 />
<__NS1:elem2 xmlns:__NS1="a" />
</elem1>
<__NS1:elem1 xmlns:__NS1="b">
<__NS1:elem1 />
<elem2 />
</__NS1:elem1>
</doc>
EOS
};
# Verify forcing namespace declarations mid-document
TEST: {
initEnv(
DATA_MODE => 1,
DATA_INDENT => 1
);
$w->addPrefix('a', '');
$w->startTag(['a', 'doc']);
$w->forceNSDecl('c');
$w->startTag(['b', 'elem1']);
$w->emptyTag(['c', 'elem3']);
$w->emptyTag(['c', 'elem3']);
$w->emptyTag(['c', 'elem3']);
$w->endTag(['b', 'elem1']);
$w->endTag(['a', 'doc']);
$w->end();
checkResult(<<"EOS", 'Namespace declarations should be forceable mid-document');
<doc xmlns="a">
<__NS1:elem1 xmlns:__NS1="b" xmlns:__NS2="c">
<__NS2:elem3 />
<__NS2:elem3 />
<__NS2:elem3 />
</__NS1:elem1>
</doc>
EOS
};
# Verify that PREFIX_MAP's default prefix is not ignored when
# a document element is from a different namespace
TEST: {
initEnv(PREFIX_MAP => {'uri:test', ''},
FORCED_NS_DECLS => ['uri:test']
);
$w->emptyTag(['uri:test2', 'document']);
$w->end();
checkResult(<<"EOS", 'The default namespace declaration should be present and correct when the document element belongs to a different namespace');
<__NS1:document xmlns:__NS1="uri:test2" xmlns="uri:test" />
EOS
};
# Without namespaces, addPrefix and removePrefix should be safe NOPs
TEST: {
initEnv(NAMESPACES => 0);
$w->addPrefix('these', 'arguments', 'are', 'ignored');
$w->removePrefix('as', 'are', 'these');
wasNoWarning('Prefix manipulation on a namespace-unaware instance should not warn');
};
# Make sure that getting and setting the output stream behaves as expected
TEST: {
initEnv();
my $out = $w->getOutput();
isnt($out, undef, 'Output for this fixture must be defined');
$w->setOutput(\*STDERR);
is($w->getOutput(), \*STDERR, 'Changing output should be reflected in a subsequent get');
$w->setOutput($out);
is ($w->getOutput(), $out, 'Changing output back should succeed');
$w->emptyTag('x');
$w->end();
checkResult("<x />\n", 'After changing the output a document should still be generated');
};
# Make sure that undef implies STDOUT for setOutput
TEST: {
initEnv();
$w->setOutput();
is($w->getOutput(), \*STDOUT, 'If no output is given, STDOUT should be used');
};
# Create an ill-formed document using unsafe mode
TEST: {
initEnv(UNSAFE => 1);
$w->xmlDecl('us-ascii');
$w->comment("--");
$w->characters("Test\n");
$w->cdata("Test\n");
$w->doctype('y', undef, '/');
$w->emptyTag('x');
$w->end();
checkResult(<<EOR, 'Unsafe mode should not enforce validity tests.');
<?xml version="1.0" encoding="us-ascii"?>
<!-- -- -->
Test
<![CDATA[Test
]]><!DOCTYPE y SYSTEM "/">
<x />
EOR
};
# Ensure that newlines in attributes are escaped
TEST: {
initEnv();
$w->emptyTag('x', 'a' => "A\nB");
$w->end();
checkResult("<x a=\"A B\" />\n", 'Newlines in attribute values should be escaped');
};
# Make sure UTF-8 is written properly
SKIP: {
skip $unicodeSkipMessage, 2 unless isUnicodeSupported();
initEnv(ENCODING => 'utf-8', DATA_MODE => 1);
$w->xmlDecl();
$w->comment("\$ \x{A3} \x{20AC}");
$w->startTag('a');
$w->dataElement('b', '$');
# I need U+00A3 as an is_utf8 string; I want to keep the source ASCII.
# There must be a better way to do this.
require Encode;
my $text = Encode::decode('iso-8859-1', "\x{A3}");
$w->dataElement('b', $text);
$w->dataElement('b', "\x{20AC}");
$w->startTag('c');
$w->cdata(" \$ \x{A3} \x{20AC} ");
$w->endTag('c');
$w->endTag('a');
$w->end();
checkResult(<<EOR, 'When requested, output should be UTF-8 encoded');
<?xml version="1.0" encoding="utf-8"?>
<!-- \$ \x{C2}\x{A3} \x{E2}\x{82}\x{AC} -->
<a>
<b>\x{24}</b>
<b>\x{C2}\x{A3}</b>
<b>\x{E2}\x{82}\x{AC}</b>
<c><![CDATA[ \$ \x{C2}\x{A3} \x{E2}\x{82}\x{AC} ]]></c>
</a>
EOR
};
# Capture generated XML in a scalar
TEST: {
initEnv();
my $s;
$w = new XML::Writer(OUTPUT => \$s);
$w->emptyTag('x');
$w->end();
wasNoWarning('Capturing in a scalar should not cause warnings');
is($s, "<x />\n", "Output should be stored in a scalar, if one is passed");
};
# Modify the scalar during capture
TEST: {
initEnv();
my $s;
$w = new XML::Writer(OUTPUT => \$s);
$w->startTag('foo', bar => 'baz');
is($s, "<foo bar=\"baz\">", 'Scalars should be up-to-date during writing');
$s = '';
$w->dataElement('txt', 'blah');
$w->endTag('foo');
$w->end();
is($s, "<txt>blah</txt></foo>\n", 'Resetting the scalar should work properly');
};
# Ensure that ENCODING and SCALAR don't cause failure when used together
TEST: {
initEnv();
my $s;
ok(eval {$w = new XML::Writer(OUTPUT => \$s,
ENCODING => 'utf-8'
);}, 'OUTPUT and ENCODING should not cause failure');
}
# Verify that unknown encodings cause failure
TEST: {
expectError('encoding', eval {
initEnv(ENCODING => 'x-unsupported-encoding');
});
}
# Make sure scalars are built up as UTF-8 (if UTF-8 is passed in)
SKIP: {
skip $unicodeSkipMessage, 2 unless isUnicodeSupported();
initEnv();
my $s;
$w = new XML::Writer(OUTPUT => \$s);
my $x = 'x';
utf8::upgrade($x);
$w->emptyTag($x);
$w->end();
ok(utf8::is_utf8($s), 'A storage scalar should preserve utf8-ness');
undef($s);
$w = new XML::Writer(OUTPUT => \$s);
$w->startTag('a');
$w->dataElement('x', "\$");
$w->dataElement('x', "\x{A3}");
$w->dataElement('x', "\x{20AC}");
$w->endTag('a');
$w->end();
is($s, "<a><x>\$</x><x>\x{A3}</x><x>\x{20AC}</x></a>\n",
'A storage scalar should work with utf8 strings');
}
# Test US-ASCII encoding
SKIP: {
skip $unicodeSkipMessage, 7 unless isUnicodeSupported();
initEnv(ENCODING => 'us-ascii', DATA_MODE => 1);
$w->xmlDecl();
$w->startTag('a');
$w->dataElement('x', "\$", 'a' => "\$");
$w->dataElement('x', "\x{A3}", 'a' => "\x{A3}");
$w->dataElement('x', "\x{20AC}", 'a' => "\x{20AC}");
$w->endTag('a');
$w->end();
checkResult(<<'EOR', 'US-ASCII support should cover text and attributes');
<?xml version="1.0" encoding="us-ascii"?>
<a>
<x a="$">$</x>
<x a="£">£</x>
<x a="€">€</x>
</a>
EOR
# Make sure non-ASCII characters that can't be represented
# as references cause failure
# I need U+00A3 as an is_utf8 string; I want to keep the source ASCII.
# There must be a better way to do this.
require Encode;
my $text = Encode::decode('iso-8859-1', "\x{A3}");
initEnv(ENCODING => 'us-ascii', DATA_MODE => 1);
$w->startTag('a');
$w->cdata('Text');
expectError('ASCII', eval {
$w->cdata($text);
});
initEnv(ENCODING => 'us-ascii', DATA_MODE => 1);
$w->startTag('a');
$w->comment('Text');
expectError('ASCII', eval {
$w->comment($text);
});
initEnv(ENCODING => 'us-ascii', DATA_MODE => 1);
expectError('ASCII', eval {
$w->emptyTag("\x{DC}berpr\x{FC}fung");
});
# Make sure Unicode generates warnings when it makes it through
# to a US-ASCII-encoded stream
initEnv(ENCODING => 'us-ascii', DATA_MODE => 1, UNSAFE => 1);
$w->startTag('a');
$w->cdata($text);
$w->endTag('a');
$w->end();
$outputFile->flush();
ok($warning && $warning =~ /does not map to ascii/,
'Perl IO should warn about non-ASCII characters in output');
initEnv(ENCODING => 'us-ascii', DATA_MODE => 1, UNSAFE => 1);
$w->startTag('a');
$w->comment($text);
$w->endTag('a');
$w->end();
$outputFile->flush();
ok($warning && $warning =~ /does not map to ascii/,
'Perl IO should warn about non-ASCII characters in output');
}
# Make sure comments are formatted in data mode
TEST: {
initEnv(DATA_MODE => 1, DATA_INDENT => 1);
$w->xmlDecl();
$w->comment("Test");
$w->comment("Test");
$w->startTag("x");
$w->comment("Test 2");
$w->startTag("y");
$w->comment("Test 3");
$w->endTag("y");
$w->comment("Test 4");
$w->startTag("y");
$w->endTag("y");
$w->endTag("x");
$w->end();
$w->comment("Test 5");
checkResult(<<'EOR', 'Comments should be formatted like elements when in data mode');
<?xml version="1.0"?>
<!-- Test -->
<!-- Test -->
<x>
<!-- Test 2 -->
<y>
<!-- Test 3 -->
</y>
<!-- Test 4 -->
<y></y>
</x>
<!-- Test 5 -->
EOR
}
# Test characters outside the BMP
SKIP: {
skip $unicodeSkipMessage, 4 unless isUnicodeSupported();
my $s = "\x{10480}"; # U+10480 OSMANYA LETTER ALEF
initEnv(ENCODING => 'utf-8');
$w->dataElement('x', $s);
$w->end();
checkResult(<<"EOR", 'Characters outside the BMP should be encoded correctly in UTF-8');
<x>\xF0\x90\x92\x80</x>
EOR
initEnv(ENCODING => 'us-ascii');
$w->dataElement('x', $s);
$w->end();
checkResult(<<'EOR', 'Characters outside the BMP should be encoded correctly in US-ASCII');
<x>𐒀</x>
EOR
}
# Ensure 'ancestor' returns undef beyond the document
TEST: {
initEnv();
is($w->ancestor(0), undef, 'With no document, ancestors should be undef');
$w->startTag('x');
is($w->ancestor(0), 'x', 'ancestor(0) should return the current element');
is($w->ancestor(1), undef, 'ancestor should return undef beyond the document');
}
# Don't allow undefined Unicode characters, but do allow whitespace
TEST: {
# Test characters
initEnv();
$w->startTag('x');
expectError('\u0000', eval {
$w->characters("\x00");
});
initEnv();
$w->dataElement('x', "\x09\x0A\x0D ");
$w->end();
checkResult(<<"EOR", 'Whitespace below \u0020 is valid.');
<x>\x09\x0A\x0D </x>
EOR
# CDATA
initEnv();
$w->startTag('x');
expectError('\u0000', eval {
$w->cdata("\x00");
});
initEnv();
$w->startTag('x');
$w->cdata("\x09\x0A\x0D ");
$w->endTag('x');
$w->end();
checkResult(<<"EOR", 'Whitespace below \u0020 is valid.');
<x><![CDATA[\x09\x0A\x0D ]]></x>
EOR
# Attribute values
initEnv();
expectError('\u0000', eval {
$w->emptyTag('x', 'a' => "\x00");
});
initEnv();
$w->emptyTag('x', 'a' => "\x09\x0A\x0D ");
$w->end();
# Currently, \u000A is escaped. This test is for lack of errors,
# not exact serialisation, so change it if necessary.
checkResult(<<"EOR", 'Whitespace below \u0020 is valid.');
<x a="\x09 \x0D " />
EOR
}
# Unsafe mode should not enforce character validity tests
TEST: {
initEnv(UNSAFE => 1);
$w->dataElement('x', "\x00");
$w->end();
checkResult(<<"EOR", 'Unsafe mode should not enforce character validity tests');
<x>\x00</x>
EOR
initEnv(UNSAFE => 1);
$w->startTag('x');
$w->cdata("\x00");
$w->endTag('x');
$w->end();
checkResult(<<"EOR", 'Unsafe mode should not enforce character validity tests');
<x><![CDATA[\x00]]></x>
EOR
initEnv(UNSAFE => 1);
$w->emptyTag('x', 'a' => "\x00");
$w->end();
checkResult(<<"EOR", 'Unsafe mode should not enforce character validity tests');
<x a="\x00" />
EOR
}
# Cover XML declaration encoding cases
SKIP: {
skip $unicodeSkipMessage, 8 unless isUnicodeSupported();
# No declaration unless specified
initEnv();
$w->xmlDecl();
$w->emptyTag('x');
$w->end();
checkResult(<<"EOR", 'When no encoding is specified, the declaration should not include one');
<?xml version="1.0"?>
<x />
EOR
# An encoding specified in the constructor carries across to the declaration
initEnv(ENCODING => 'us-ascii');
$w->xmlDecl();
$w->emptyTag('x');
$w->end();
checkResult(<<"EOR", 'If an encoding is specified for the document, it should appear in the declaration');
<?xml version="1.0" encoding="us-ascii"?>
<x />
EOR
# Anything passed in the xmlDecl call should override
initEnv(ENCODING => 'us-ascii');
$w->xmlDecl('utf-8');
$w->emptyTag('x');
$w->end();
checkResult(<<"EOR", 'An encoding passed to xmlDecl should override any other encoding');
<?xml version="1.0" encoding="utf-8"?>
<x />
EOR
# The empty string should force the omission of the decl
initEnv(ENCODING => 'us-ascii');
$w->xmlDecl('');
$w->emptyTag('x');
$w->end();
checkResult(<<"EOR", 'xmlDecl should treat the empty string as instruction to omit the encoding from the declaration');
<?xml version="1.0"?>
<x />
EOR
}
# Bug report: [cpan #14854] Broken namespace report
# Passing a list reference as an argument should work more than once
TEST: {
my $t = ['uri:test', 'elem'];
initEnv(PREFIX_MAP => {'uri:test' => 'prefix'});
$w->startTag($t);
ok(eval {$w->emptyTag($t);}, 'Passing an array twice should not cause failure');
$w->endTag($t);
$w->end();
checkResult(<<"EOR", 'An array passed by reference should not be modified');
<prefix:elem xmlns:prefix="uri:test"><prefix:elem /></prefix:elem>
EOR
}
# As per #14854, list references should also work for attribute names
TEST: {
my $t = ['uri:test', 'elem'];
initEnv(PREFIX_MAP => {'uri:test' => 'prefix'});
$w->startTag('x', $t => '');
ok(eval {$w->emptyTag('y', $t => '');}, 'Passing an array twice should not cause failure');
$w->endTag('x');
$w->end();
checkResult(<<"EOR", 'An array passed by reference should not be modified');
<x prefix:elem="" xmlns:prefix="uri:test"><y prefix:elem="" /></x>
EOR
}
# Free test resources
$outputFile->close() or die "Unable to close temporary file: $!";
1;
__END__
|