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 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************
* File TMSGFMT.CPP
*
* Modification History:
*
* Date Name Description
* 03/24/97 helena Converted from Java.
* 07/11/97 helena Updated to work on AIX.
* 08/04/97 jfitz Updated to intltest
*******************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "tmsgfmt.h"
#include "cmemory.h"
#include "loctest.h"
#include "unicode/format.h"
#include "unicode/decimfmt.h"
#include "unicode/localpointer.h"
#include "unicode/locid.h"
#include "unicode/msgfmt.h"
#include "unicode/numfmt.h"
#include "unicode/choicfmt.h"
#include "unicode/messagepattern.h"
#include "unicode/selfmt.h"
#include "unicode/gregocal.h"
#include "unicode/strenum.h"
#include <stdio.h>
void
TestMessageFormat::runIndexedTest(int32_t index, UBool exec,
const char* &name, char* /*par*/) {
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(testBug1);
TESTCASE_AUTO(testBug2);
TESTCASE_AUTO(sample);
TESTCASE_AUTO(PatternTest);
TESTCASE_AUTO(testStaticFormat);
TESTCASE_AUTO(testSimpleFormat);
TESTCASE_AUTO(testMsgFormatChoice);
TESTCASE_AUTO(testCopyConstructor);
TESTCASE_AUTO(testAssignment);
TESTCASE_AUTO(testClone);
TESTCASE_AUTO(testEquals);
TESTCASE_AUTO(testNotEquals);
TESTCASE_AUTO(testSetLocale);
TESTCASE_AUTO(testFormat);
TESTCASE_AUTO(testParse);
TESTCASE_AUTO(testAdopt);
TESTCASE_AUTO(testCopyConstructor2);
TESTCASE_AUTO(TestUnlimitedArgsAndSubformats);
TESTCASE_AUTO(TestRBNF);
TESTCASE_AUTO(TestTurkishCasing);
TESTCASE_AUTO(testAutoQuoteApostrophe);
TESTCASE_AUTO(testMsgFormatPlural);
TESTCASE_AUTO(testMsgFormatSelect);
TESTCASE_AUTO(testApostropheInPluralAndSelect);
TESTCASE_AUTO(TestApostropheMode);
TESTCASE_AUTO(TestCompatibleApostrophe);
TESTCASE_AUTO(testCoverage);
TESTCASE_AUTO(testGetFormatNames);
TESTCASE_AUTO(TestTrimArgumentName);
TESTCASE_AUTO(TestSelectOrdinal);
TESTCASE_AUTO(TestDecimals);
TESTCASE_AUTO(TestArgIsPrefixOfAnother);
TESTCASE_AUTO(TestMessageFormatNumberSkeleton);
TESTCASE_AUTO(TestMessageFormatDateSkeleton);
TESTCASE_AUTO(TestMessageFormatTimeSkeleton);
TESTCASE_AUTO(TestNumberOverflow);
TESTCASE_AUTO_END;
}
void TestMessageFormat::testBug3()
{
double myNumber = -123456;
DecimalFormat* form = nullptr;
Locale locale[] = {
Locale("ar", "", ""),
Locale("be", "", ""),
Locale("bg", "", ""),
Locale("ca", "", ""),
Locale("cs", "", ""),
Locale("da", "", ""),
Locale("de", "", ""),
Locale("de", "AT", ""),
Locale("de", "CH", ""),
Locale("el", "", ""), // 10
Locale("en", "CA", ""),
Locale("en", "GB", ""),
Locale("en", "IE", ""),
Locale("en", "US", ""),
Locale("es", "", ""),
Locale("et", "", ""),
Locale("fi", "", ""),
Locale("fr", "", ""),
Locale("fr", "BE", ""),
Locale("fr", "CA", ""), // 20
Locale("fr", "CH", ""),
Locale("he", "", ""),
Locale("hr", "", ""),
Locale("hu", "", ""),
Locale("is", "", ""),
Locale("it", "", ""),
Locale("it", "CH", ""),
Locale("ja", "", ""),
Locale("ko", "", ""),
Locale("lt", "", ""), // 30
Locale("lv", "", ""),
Locale("mk", "", ""),
Locale("nl", "", ""),
Locale("nl", "BE", ""),
Locale("no", "", ""),
Locale("pl", "", ""),
Locale("pt", "", ""),
Locale("ro", "", ""),
Locale("ru", "", ""),
Locale("sh", "", ""), // 40
Locale("sk", "", ""),
Locale("sl", "", ""),
Locale("sq", "", ""),
Locale("sr", "", ""),
Locale("sv", "", ""),
Locale("tr", "", ""),
Locale("uk", "", ""),
Locale("zh", "", ""),
Locale("zh", "TW", "") // 49
};
int32_t i;
for (i= 0; i < 49; i++) {
UnicodeString buffer;
logln(locale[i].getDisplayName(buffer));
UErrorCode success = U_ZERO_ERROR;
// form = (DecimalFormat*)NumberFormat::createCurrencyInstance(locale[i], success);
form = dynamic_cast<DecimalFormat*>(NumberFormat::createInstance(locale[i], success));
if (U_FAILURE(success)) {
errln("Err: Number Format ");
logln("Number format creation failed.");
continue;
}
Formattable result;
FieldPosition pos(FieldPosition::DONT_CARE);
buffer.remove();
form->format(myNumber, buffer, pos);
success = U_ZERO_ERROR;
ParsePosition parsePos;
form->parse(buffer, result, parsePos);
logln(UnicodeString(" -> ") /* + << dec*/ + toString(result) + UnicodeString("[supposed output for result]"));
if (U_FAILURE(success)) {
errln("Err: Number Format parse");
logln("Number format parse failed.");
}
delete form;
}
}
void TestMessageFormat::testBug1()
{
const double limit[] = {0.0, 1.0, 2.0};
const UnicodeString formats[] = {"0.0<=Arg<1.0",
"1.0<=Arg<2.0",
"2.0<-Arg"};
ChoiceFormat *cf = new ChoiceFormat(limit, formats, 3);
FieldPosition status(FieldPosition::DONT_CARE);
UnicodeString toAppendTo;
cf->format(static_cast<int32_t>(1), toAppendTo, status);
if (toAppendTo != "1.0<=Arg<2.0") {
errln("ChoiceFormat cmp in testBug1");
}
logln(toAppendTo);
delete cf;
}
void TestMessageFormat::testBug2()
{
UErrorCode status = U_ZERO_ERROR;
UnicodeString result;
// {sfb} use double format in pattern, so result will match (not strictly necessary)
const UnicodeString pattern = "There {0,choice,0#are no files|1#is one file|1<are {0, number} files} on disk {1}. ";
logln("The input pattern : " + pattern);
LocalPointer<MessageFormat> fmt(new MessageFormat(pattern, status));
if (U_FAILURE(status)) {
dataerrln("MessageFormat pattern creation failed. - %s", u_errorName(status));
return;
}
logln("The output pattern is : " + fmt->toPattern(result));
if (pattern != result) {
errln("MessageFormat::toPattern() failed.");
}
}
#if 0
#if defined(_DEBUG)
//----------------------------------------------------
// console I/O
//----------------------------------------------------
#include <iostream>
std::ostream& operator<<(std::ostream& stream, const Formattable& obj);
#include "unicode/datefmt.h"
#include <stdlib.h>
#include <string.h>
IntlTest&
operator<<( IntlTest& stream,
const Formattable& obj)
{
static DateFormat *defDateFormat = 0;
UnicodeString buffer;
switch(obj.getType()) {
case Formattable::kDate :
if (defDateFormat == 0) {
defDateFormat = DateFormat::createInstance();
}
defDateFormat->format(obj.getDate(), buffer);
stream << buffer;
break;
case Formattable::kDouble :
char convert[20];
snprintf( convert, sizeof(convert), "%lf", obj.getDouble() );
stream << convert << "D";
break;
case Formattable::kLong :
stream << obj.getLong() << "L";
break;
case Formattable::kString:
stream << "\"" << obj.getString(buffer) << "\"";
break;
case Formattable::kArray:
int32_t i, count;
const Formattable* array;
array = obj.getArray(count);
stream << "[";
for (i=0; i<count; ++i) stream << array[i] << ( (i==(count-1)) ? "" : ", " );
stream << "]";
break;
default:
stream << "INVALID_Formattable";
}
return stream;
}
#endif /* defined(_DEBUG) */
#endif
void TestMessageFormat::PatternTest()
{
Formattable testArgs[] = {
Formattable(static_cast<double>(1)), Formattable(static_cast<double>(3456)),
Formattable("Disk"), Formattable(static_cast<UDate>(static_cast<int32_t>(1000000000L)), Formattable::kIsDate)
};
UnicodeString testCases[] = {
"Quotes '', '{', 'a' {0} '{0}'",
"Quotes '', '{', 'a' {0,number} '{0}'",
"'{'1,number,'#',##} {1,number,'#',##}",
"There are {1} files on {2} at {3}.",
"On {2}, there are {1} files, with {0,number,currency}.",
"'{1,number,percent}', {1,number,percent},",
"'{1,date,full}', {1,date,full},",
"'{3,date,full}', {3,date,full},",
"'{1,number,#,##}' {1,number,#,##}",
};
// ICU 4.8 returns the original pattern (testCases),
// rather than toPattern() reconstituting a new, equivalent pattern string (testResultPatterns).
/*UnicodeString testResultPatterns[] = {
"Quotes '', '{', a {0} '{'0}",
"Quotes '', '{', a {0,number} '{'0}",
"'{'1,number,#,##} {1,number,'#'#,##}",
"There are {1} files on {2} at {3}.",
"On {2}, there are {1} files, with {0,number,currency}.",
"'{'1,number,percent}, {1,number,percent},",
"'{'1,date,full}, {1,date,full},",
"'{'3,date,full}, {3,date,full},",
"'{'1,number,#,##} {1,number,#,##}"
};*/
UnicodeString testResultStrings[] = {
u"Quotes ', {, 'a' 1 {0}",
u"Quotes ', {, 'a' 1 {0}",
u"{1,number,'#',##} #34,56",
u"There are 3,456 files on Disk at 1/12/70, 5:46\u202FAM.",
u"On Disk, there are 3,456 files, with $1.00.",
u"{1,number,percent}, 345,600%,",
u"{1,date,full}, Wednesday, December 31, 1969,",
u"{3,date,full}, Monday, January 12, 1970,",
u"{1,number,#,##} 34,56"
};
for (int32_t i = 0; i < 9; ++i) {
//it_out << "\nPat in: " << testCases[i]);
LocalPointer<MessageFormat> form;
UErrorCode success = U_ZERO_ERROR;
UnicodeString buffer;
form.adoptInstead(new MessageFormat(testCases[i], Locale::getUS(), success));
if (U_FAILURE(success)) {
dataerrln("MessageFormat creation failed.#1 - %s", u_errorName(success));
logln(UnicodeString("MessageFormat for ") + testCases[i] + " creation failed.\n");
continue;
}
// ICU 4.8 returns the original pattern (testCases),
// rather than toPattern() reconstituting a new, equivalent pattern string (testResultPatterns).
if (form->toPattern(buffer) != testCases[i]) {
// Note: An alternative test would be to build MessagePattern objects for
// both the input and output patterns and compare them, taking SKIP_SYNTAX etc.
// into account.
// (Too much trouble...)
errln(UnicodeString("TestMessageFormat::PatternTest failed test #2, i = ") + i);
//form->toPattern(buffer);
errln(UnicodeString(" Orig: ") + testCases[i]);
errln(UnicodeString(" Exp: ") + testCases[i]);
errln(UnicodeString(" Got: ") + buffer);
}
//it_out << "Pat out: " << form->toPattern(buffer));
UnicodeString result;
int32_t count = 4;
FieldPosition fieldpos(FieldPosition::DONT_CARE);
form->format(testArgs, count, result, fieldpos, success);
if (U_FAILURE(success)) {
dataerrln("MessageFormat failed test #3 - %s", u_errorName(success));
logln("TestMessageFormat::PatternTest failed test #3");
continue;
}
if (result != testResultStrings[i]) {
errln(UnicodeString("TestMessageFormat::PatternTest failed test #4.") +
UnicodeString("\n Result: ") + result +
UnicodeString("\n Expected: ") + testResultStrings[i]);
logln(UnicodeString("TestMessageFormat::PatternTest failed test #4.") +
UnicodeString("\n Result: ") + result +
UnicodeString("\n Expected: ") + testResultStrings[i]);
}
//it_out << "Result: " << result);
#if 0
/* TODO: Look at this test and see if this is still a valid test */
logln("---------------- test parse ----------------");
form->toPattern(buffer);
logln("MSG pattern for parse: " + buffer);
int32_t parseCount = 0;
Formattable* values = form->parse(result, parseCount, success);
if (U_FAILURE(success)) {
errln("MessageFormat failed test #5");
logln(UnicodeString("MessageFormat failed test #5 with error code ")+(int32_t)success);
} else if (parseCount != count) {
errln("MSG count not %d as expected. Got %d", count, parseCount);
}
UBool failed = false;
for (int32_t j = 0; j < parseCount; ++j) {
if (values == 0 || testArgs[j] != values[j]) {
errln(((UnicodeString)"MSG testargs[") + j + "]: " + toString(testArgs[j]));
errln(((UnicodeString)"MSG values[") + j + "] : " + toString(values[j]));
failed = true;
}
}
if (failed)
errln("MessageFormat failed test #6");
#endif
}
}
void TestMessageFormat::sample()
{
MessageFormat* form = nullptr;
UnicodeString buffer1, buffer2;
UErrorCode success = U_ZERO_ERROR;
form = new MessageFormat("There are {0} files on {1}", success);
if (U_FAILURE(success)) {
errln("Err: Message format creation failed");
logln("Sample message format creation failed.");
return;
}
UnicodeString abc("abc");
UnicodeString def("def");
Formattable testArgs1[] = { abc, def };
FieldPosition fieldpos(FieldPosition::DONT_CARE);
assertEquals("format",
"There are abc files on def",
form->format(testArgs1, 2, buffer2, fieldpos, success));
assertSuccess("format", success);
delete form;
}
void TestMessageFormat::testStaticFormat()
{
UErrorCode err = U_ZERO_ERROR;
Formattable arguments[] = {
static_cast<int32_t>(7),
Formattable(static_cast<UDate>(8.71068e+011), Formattable::kIsDate),
"a disturbance in the Force"
};
UnicodeString result;
result = MessageFormat::format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
arguments,
3,
result,
err);
if (U_FAILURE(err)) {
dataerrln("TestMessageFormat::testStaticFormat #1 - %s", u_errorName(err));
logln(UnicodeString("TestMessageFormat::testStaticFormat failed test #1 with error code ") + static_cast<int32_t>(err));
return;
}
const UnicodeString expected(
u"At 12:20:00\u202FPM on Aug 8, 1997, there was a disturbance in the Force on planet 7.");
if (result != expected) {
errln(UnicodeString("TestMessageFormat::testStaticFormat failed on test") +
UnicodeString("\n Result: ") + result +
UnicodeString("\n Expected: ") + expected );
}
}
/* When the default locale is tr, make sure that the pattern can still be parsed. */
void TestMessageFormat::TestTurkishCasing()
{
UErrorCode err = U_ZERO_ERROR;
Locale saveDefaultLocale;
Locale::setDefault( Locale("tr"), err );
Formattable arguments[] = {
static_cast<int32_t>(7),
Formattable(static_cast<UDate>(8.71068e+011), Formattable::kIsDate),
"a disturbance in the Force"
};
UnicodeString result;
result = MessageFormat::format(
"At {1,TIME} on {1,DATE,SHORT}, there was {2} on planet {0,NUMBER,INTEGER}.",
arguments,
3,
result,
err);
if (U_FAILURE(err)) {
dataerrln("TestTurkishCasing #1 with error code %s", u_errorName(err));
return;
}
const UnicodeString expected(
"At 12:20:00 on 8.08.1997, there was a disturbance in the Force on planet 7.", "");
if (result != expected) {
errln("TestTurkishCasing failed on test");
errln( UnicodeString(" Result: ") + result );
errln( UnicodeString(" Expected: ") + expected );
}
Locale::setDefault( saveDefaultLocale, err );
}
void TestMessageFormat::testSimpleFormat(/* char* par */)
{
logln("running TestMessageFormat::testSimpleFormat");
UErrorCode err = U_ZERO_ERROR;
Formattable testArgs1[] = {static_cast<int32_t>(0), "MyDisk"};
Formattable testArgs2[] = {static_cast<int32_t>(1), "MyDisk"};
Formattable testArgs3[] = {static_cast<int32_t>(12), "MyDisk"};
MessageFormat* form = new MessageFormat(
"The disk \"{1}\" contains {0} file(s).", err);
UnicodeString string;
FieldPosition ignore(FieldPosition::DONT_CARE);
form->format(testArgs1, 2, string, ignore, err);
if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 0 file(s).") {
dataerrln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #1 - ") + u_errorName(err));
}
ignore.setField(FieldPosition::DONT_CARE);
string.remove();
form->format(testArgs2, 2, string, ignore, err);
if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 1 file(s).") {
logln(string);
dataerrln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #2")+string + " - " + u_errorName(err));
}
ignore.setField(FieldPosition::DONT_CARE);
string.remove();
form->format(testArgs3, 2, string, ignore, err);
if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 12 file(s).") {
dataerrln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #3")+string + " - " + u_errorName(err));
}
delete form;
}
void TestMessageFormat::testMsgFormatChoice(/* char* par */)
{
logln("running TestMessageFormat::testMsgFormatChoice");
UErrorCode err = U_ZERO_ERROR;
MessageFormat* form = new MessageFormat("The disk \"{1}\" contains {0}.", err);
double filelimits[] = {0,1,2};
UnicodeString filepart[] = {"no files","one file","{0,number} files"};
ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3);
form->setFormat(1,*fileform); // NOT zero, see below
//is the format adopted?
FieldPosition ignore(FieldPosition::DONT_CARE);
UnicodeString string;
Formattable testArgs1[] = {static_cast<int32_t>(0), "MyDisk"};
form->format(testArgs1, 2, string, ignore, err);
if (string != "The disk \"MyDisk\" contains no files.") {
errln("TestMessageFormat::testMsgFormatChoice failed on test #1");
}
ignore.setField(FieldPosition::DONT_CARE);
string.remove();
Formattable testArgs2[] = {static_cast<int32_t>(1), "MyDisk"};
form->format(testArgs2, 2, string, ignore, err);
if (string != "The disk \"MyDisk\" contains one file.") {
errln("TestMessageFormat::testMsgFormatChoice failed on test #2");
}
ignore.setField(FieldPosition::DONT_CARE);
string.remove();
Formattable testArgs3[] = {static_cast<int32_t>(1273), "MyDisk"};
form->format(testArgs3, 2, string, ignore, err);
if (string != "The disk \"MyDisk\" contains 1,273 files.") {
dataerrln("TestMessageFormat::testMsgFormatChoice failed on test #3 - %s", u_errorName(err));
}
delete form;
delete fileform;
}
void TestMessageFormat::testMsgFormatPlural(/* char* par */)
{
logln("running TestMessageFormat::testMsgFormatPlural");
UErrorCode err = U_ZERO_ERROR;
UnicodeString t1("{0, plural, one{C''est # fichier} other{Ce sont # fichiers}} dans la liste.");
UnicodeString t2("{argument, plural, one{C''est # fichier} other {Ce sont # fichiers}} dans la liste.");
UnicodeString t3("There {0, plural, one{is # zavod}few{are {0, number,###.0} zavoda} other{are # zavodov}} in the directory.");
UnicodeString t4("There {argument, plural, one{is # zavod}few{are {argument, number,###.0} zavoda} other{are #zavodov}} in the directory.");
UnicodeString t5("{0, plural, one {{0, number,C''est #,##0.0# fichier}} other {Ce sont # fichiers}} dans la liste.");
MessageFormat* mfNum = new MessageFormat(t1, Locale("fr"), err);
if (U_FAILURE(err)) {
dataerrln("TestMessageFormat::testMsgFormatPlural #1 - argumentIndex - %s", u_errorName(err));
logln(UnicodeString("TestMessageFormat::testMsgFormatPlural #1 with error code ") + static_cast<int32_t>(err));
return;
}
Formattable testArgs1(static_cast<int32_t>(0));
FieldPosition ignore(FieldPosition::DONT_CARE);
UnicodeString numResult1;
mfNum->format(&testArgs1, 1, numResult1, ignore, err);
MessageFormat* mfAlpha = new MessageFormat(t2, Locale("fr"), err);
UnicodeString argName[] = {UnicodeString("argument")};
UnicodeString argNameResult;
mfAlpha->format(argName, &testArgs1, 1, argNameResult, err);
if (U_FAILURE(err)) {
dataerrln("TestMessageFormat::testMsgFormatPlural #1 - argumentName - %s", u_errorName(err));
logln(UnicodeString("TestMessageFormat::testMsgFormatPlural #1 with error code ") + static_cast<int32_t>(err));
delete mfNum;
return;
}
if ( numResult1 != argNameResult){
errln("TestMessageFormat::testMsgFormatPlural #1");
logln(UnicodeString("The results of argumentName and argumentIndex are not the same."));
}
if ( numResult1 != UnicodeString("C\'est 0 fichier dans la liste.")) {
errln("TestMessageFormat::testMsgFormatPlural #1");
logln(UnicodeString("The results of argumentName and argumentIndex are not the same."));
}
err = U_ZERO_ERROR;
delete mfNum;
delete mfAlpha;
MessageFormat* mfNum2 = new MessageFormat(t3, Locale("uk"), err);
numResult1.remove();
Formattable testArgs2(static_cast<int32_t>(4));
mfNum2->format(&testArgs2, 1, numResult1, ignore, err);
MessageFormat* mfAlpha2 = new MessageFormat(t4, Locale("uk"), err);
argNameResult.remove();
mfAlpha2->format(argName, &testArgs2, 1, argNameResult, err);
if (U_FAILURE(err)) {
errln("TestMessageFormat::testMsgFormatPlural #2 - argumentName");
logln(UnicodeString("TestMessageFormat::testMsgFormatPlural #2 with error code ") + static_cast<int32_t>(err));
delete mfNum2;
return;
}
if ( numResult1 != argNameResult){
errln("TestMessageFormat::testMsgFormatPlural #2");
logln(UnicodeString("The results of argumentName and argumentIndex are not the same."));
}
if ( numResult1 != UnicodeString("There are 4,0 zavoda in the directory.")) {
errln("TestMessageFormat::testMsgFormatPlural #2");
logln(UnicodeString("The results of argumentName and argumentIndex are not the same."));
}
delete mfNum2;
delete mfAlpha2;
// nested formats
err = U_ZERO_ERROR;
MessageFormat* msgFmt = new MessageFormat(t5, Locale("fr"), err);
if (U_FAILURE(err)) {
errln("TestMessageFormat::test nested PluralFormat with argumentName");
logln(UnicodeString("TestMessageFormat::test nested PluralFormat with error code ") + static_cast<int32_t>(err));
delete msgFmt;
return;
}
Formattable testArgs3(static_cast<int32_t>(0));
argNameResult.remove();
msgFmt->format(&testArgs3, 1, argNameResult, ignore, err);
if (U_FAILURE(err)) {
errln("TestMessageFormat::test nested PluralFormat with argumentName");
}
if ( argNameResult!= UnicodeString("C'est 0,0 fichier dans la liste.")) {
errln(UnicodeString("TestMessageFormat::test nested named PluralFormat: ") + argNameResult);
logln(UnicodeString("The unexpected nested named PluralFormat."));
}
delete msgFmt;
}
void TestMessageFormat::testApostropheInPluralAndSelect() {
UErrorCode errorCode = U_ZERO_ERROR;
MessageFormat msgFmt(UNICODE_STRING_SIMPLE(
"abc_{0,plural,other{#'#'#'{'#''}}_def_{1,select,other{sel'}'ect''}}_xyz"),
Locale::getEnglish(),
errorCode);
if (U_FAILURE(errorCode)) {
errln("MessageFormat constructor failed - %s\n", u_errorName(errorCode));
return;
}
UnicodeString expected = UNICODE_STRING_SIMPLE("abc_3#3{3'_def_sel}ect'_xyz");
Formattable args[] = { static_cast<int32_t>(3), UNICODE_STRING_SIMPLE("x") };
internalFormat(
&msgFmt, args, 2, expected,
"MessageFormat with apostrophes in plural/select arguments failed:\n");
}
void TestMessageFormat::internalFormat(MessageFormat* msgFmt ,
Formattable* args , int32_t numOfArgs ,
UnicodeString expected, const char* errMsg)
{
UnicodeString result;
FieldPosition ignore(FieldPosition::DONT_CARE);
UErrorCode status = U_ZERO_ERROR;
//Format with passed arguments
msgFmt->format( args , numOfArgs , result, ignore, status);
if (U_FAILURE(status)) {
dataerrln( "%s error while formatting with ErrorCode as %s" ,errMsg, u_errorName(status) );
}
//Compare expected with obtained result
if ( result!= expected ) {
UnicodeString err = UnicodeString(errMsg);
err+= UnicodeString(":Unexpected Result \n Expected: " + expected + "\n Obtained: " + result + "\n");
dataerrln(err);
}
}
MessageFormat* TestMessageFormat::internalCreate(
UnicodeString pattern ,Locale locale ,UErrorCode &status , char* errMsg)
{
//Create the MessageFormat with simple SelectFormat
LocalPointer<MessageFormat> msgFmt(new MessageFormat(pattern, locale, status));
if (U_FAILURE(status)) {
dataerrln( "%s error while constructing with ErrorCode as %s" ,errMsg, u_errorName(status) );
logln(UnicodeString("TestMessageFormat::testMsgFormatSelect #1 with error code ") + static_cast<int32_t>(status));
return nullptr;
}
return msgFmt.orphan();
}
void TestMessageFormat::testMsgFormatSelect(/* char* par */)
{
logln("running TestMessageFormat::testMsgFormatSelect");
UErrorCode err = U_ZERO_ERROR;
//French Pattern
UnicodeString t1("{0} est {1, select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
err = U_ZERO_ERROR;
//Create the MessageFormat with simple French pattern
MessageFormat* msgFmt1 = internalCreate(t1.unescape(), Locale("fr"),err,(char*)"From TestMessageFormat::TestSelectFormat create t1");
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"Kirti","female"};
Formattable testArgs11[] = {"Victor","other"};
Formattable testArgs12[] = {"Ash","unknown"};
Formattable* testArgs[] = {testArgs10,testArgs11,testArgs12};
UnicodeString exp[] = {
"Kirti est all\\u00E9e \\u00E0 Paris." ,
"Victor est all\\u00E9 \\u00E0 Paris.",
"Ash est all\\u00E9 \\u00E0 Paris."};
//Format
for( int i=0; i< 3; i++){
internalFormat( msgFmt1 , testArgs[i], 2, exp[i].unescape() ,(char*)"From TestMessageFormat::testSelectFormat format t1");
}
}
delete msgFmt1;
//Quoted French Pattern
UnicodeString t2("{0} est {1, select, female {all\\u00E9e c''est} other {all\\u00E9 c''est}} \\u00E0 Paris.");
err = U_ZERO_ERROR;
//Create the MessageFormat with Quoted French pattern
MessageFormat* msgFmt2 = internalCreate(t2.unescape(), Locale("fr"),err,(char*)"From TestMessageFormat::TestSelectFormat create t2");
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"Kirti","female"};
Formattable testArgs11[] = {"Victor","other"};
Formattable testArgs12[] = {"Ash","male"};
Formattable* testArgs[] = {testArgs10,testArgs11,testArgs12};
UnicodeString exp[] = {
"Kirti est all\\u00E9e c'est \\u00E0 Paris." ,
"Victor est all\\u00E9 c'est \\u00E0 Paris.",
"Ash est all\\u00E9 c'est \\u00E0 Paris."};
//Format
for( int i=0; i< 3; i++){
internalFormat( msgFmt2 , testArgs[i], 2, exp[i].unescape() ,(char*)"From TestMessageFormat::testSelectFormat format t2");
}
}
delete msgFmt2;
//English Pattern
UnicodeString t3("{0, select , male {MALE FR company} female {FEMALE FR company} other {FR otherValue}} published new books.");
err = U_ZERO_ERROR;
//Create the MessageFormat with English pattern
MessageFormat* msgFmt3 = internalCreate(t3, Locale("en"),err,(char*)"From TestMessageFormat::TestSelectFormat create t3");
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"female"};
Formattable testArgs11[] = {"other"};
Formattable testArgs12[] = {"male"};
Formattable* testArgs[] = {testArgs10,testArgs11,testArgs12};
UnicodeString exp[] = {
"FEMALE FR company published new books." ,
"FR otherValue published new books.",
"MALE FR company published new books."};
//Format
for( int i=0; i< 3; i++){
internalFormat( msgFmt3 , testArgs[i], 1, exp[i] ,(char*)"From TestMessageFormat::testSelectFormat format t3");
}
}
delete msgFmt3;
//Nested patterns with plural, number ,choice ,select format etc.
//Select Format with embedded number format
UnicodeString t4("{0} est {1, select, female {{2,number,integer} all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
err = U_ZERO_ERROR;
//Create the MessageFormat with Select Format with embedded number format (nested pattern)
MessageFormat* msgFmt4 = internalCreate(t4.unescape(), Locale("fr"),err,(char*)"From TestMessageFormat::TestSelectFormat create t4");
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"Kirti", "female", static_cast<int32_t>(6)};
Formattable testArgs11[] = {"Kirti","female",100.100};
Formattable testArgs12[] = {"Kirti", "other", static_cast<int32_t>(6)};
Formattable* testArgs[] = {testArgs10,testArgs11,testArgs12};
UnicodeString exp[] = {
"Kirti est 6 all\\u00E9e \\u00E0 Paris." ,
"Kirti est 100 all\\u00E9e \\u00E0 Paris.",
"Kirti est all\\u00E9 \\u00E0 Paris."};
//Format
for( int i=0; i< 3; i++){
internalFormat( msgFmt4 , testArgs[i], 3, exp[i].unescape() ,(char*)"From TestMessageFormat::testSelectFormat format t4");
}
}
delete msgFmt4;
//Plural format with embedded select format
UnicodeString t5("{0} {1, plural, one {est {2, select, female {all\\u00E9e} other {all\\u00E9}}} other {sont {2, select, female {all\\u00E9es} other {all\\u00E9s}}}} \\u00E0 Paris.");
err = U_ZERO_ERROR;
//Create the MessageFormat with Plural format with embedded select format(nested pattern)
MessageFormat* msgFmt5 = internalCreate(t5.unescape(), Locale("fr"),err,(char*)"From TestMessageFormat::TestSelectFormat create t5");
// with no data the above should fail but it seems to construct an invalid MessageFormat with no reported error. See #13079
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"Kirti", static_cast<int32_t>(6), "female"};
Formattable testArgs11[] = {"Kirti", static_cast<int32_t>(1), "female"};
Formattable testArgs12[] = {"Ash", static_cast<int32_t>(1), "other"};
Formattable testArgs13[] = {"Ash", static_cast<int32_t>(5), "other"};
Formattable* testArgs[] = {testArgs10,testArgs11,testArgs12,testArgs13};
UnicodeString exp[] = {
"Kirti sont all\\u00E9es \\u00E0 Paris." ,
"Kirti est all\\u00E9e \\u00E0 Paris.",
"Ash est all\\u00E9 \\u00E0 Paris.",
"Ash sont all\\u00E9s \\u00E0 Paris."};
//Format
for( int i=0; i< 4; i++){
internalFormat( msgFmt5 , testArgs[i], 3, exp[i].unescape() ,(char*)"From TestMessageFormat::testSelectFormat format t5");
}
}
delete msgFmt5;
err = U_ZERO_ERROR;
//Select, plural, and number formats heavily nested
UnicodeString t6("{0} und {1, select, female {{2, plural, one {{3, select, female {ihre Freundin} other {ihr Freund}} } other {ihre {2, number, integer} {3, select, female {Freundinnen} other {Freunde}} } }} other{{2, plural, one {{3, select, female {seine Freundin} other {sein Freund}}} other {seine {2, number, integer} {3, select, female {Freundinnen} other {Freunde}}}}} } gingen nach Paris.");
//Create the MessageFormat with Select, plural, and number formats heavily nested
LocalPointer<MessageFormat> msgFmt6(
internalCreate(t6, Locale("de"),err,(char*)"From TestMessageFormat::TestSelectFormat create t6"));
if (!U_FAILURE(err)) {
//Arguments
Formattable testArgs10[] = {"Kirti", "other", static_cast<int32_t>(1), "other"};
Formattable testArgs11[] = {"Kirti", "other", static_cast<int32_t>(6), "other"};
Formattable testArgs12[] = {"Kirti", "other", static_cast<int32_t>(1), "female"};
Formattable testArgs13[] = {"Kirti", "other", static_cast<int32_t>(3), "female"};
Formattable testArgs14[] = {"Kirti", "female", static_cast<int32_t>(1), "female"};
Formattable testArgs15[] = {"Kirti", "female", static_cast<int32_t>(5), "female"};
Formattable testArgs16[] = {"Kirti", "female", static_cast<int32_t>(1), "other"};
Formattable testArgs17[] = {"Kirti", "female", static_cast<int32_t>(5), "other"};
Formattable testArgs18[] = {"Kirti", "mixed", static_cast<int32_t>(1), "mixed"};
Formattable testArgs19[] = {"Kirti", "mixed", static_cast<int32_t>(1), "other"};
Formattable testArgs20[] = {"Kirti", "female", static_cast<int32_t>(1), "mixed"};
Formattable testArgs21[] = {"Kirti", "mixed", static_cast<int32_t>(5), "mixed"};
Formattable testArgs22[] = {"Kirti", "mixed", static_cast<int32_t>(5), "other"};
Formattable testArgs23[] = {"Kirti", "female", static_cast<int32_t>(5), "mixed"};
Formattable* testArgs[] = {testArgs10,testArgs11,testArgs12,testArgs13,
testArgs14,testArgs15,testArgs16,testArgs17,
testArgs18,testArgs19,testArgs20,testArgs21,
testArgs22,testArgs23 };
UnicodeString exp[] = {
"Kirti und sein Freund gingen nach Paris." ,
"Kirti und seine 6 Freunde gingen nach Paris." ,
"Kirti und seine Freundin gingen nach Paris.",
"Kirti und seine 3 Freundinnen gingen nach Paris.",
"Kirti und ihre Freundin gingen nach Paris.",
"Kirti und ihre 5 Freundinnen gingen nach Paris.",
"Kirti und ihr Freund gingen nach Paris.",
"Kirti und ihre 5 Freunde gingen nach Paris.",
"Kirti und sein Freund gingen nach Paris.",
"Kirti und sein Freund gingen nach Paris.",
"Kirti und ihr Freund gingen nach Paris.",
"Kirti und seine 5 Freunde gingen nach Paris." ,
"Kirti und seine 5 Freunde gingen nach Paris." ,
"Kirti und ihre 5 Freunde gingen nach Paris."
};
//Format
for( int i=0; i< 14; i++){
internalFormat( msgFmt6.getAlias(), testArgs[i], 4, exp[i] ,(char*)"From TestMessageFormat::testSelectFormat format t6");
}
}
}
//---------------------------------
// API Tests
//---------------------------------
void TestMessageFormat::testCopyConstructor()
{
UErrorCode success = U_ZERO_ERROR;
MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
MessageFormat* y = nullptr;
y = new MessageFormat(*x);
if ( (*x == *y) &&
(*x != *z) &&
(*y != *z) )
logln("First test (operator ==): Passed!");
else {
errln("TestMessageFormat::testCopyConstructor failed #1");
logln("First test (operator ==): Failed!");
}
if ( ((*x == *y) && (*y == *x)) &&
((*x != *z) && (*z != *x)) &&
((*y != *z) && (*z != *y)) )
logln("Second test (equals): Passed!");
else {
errln("TestMessageFormat::testCopyConstructor failed #2");
logln("Second test (equals): Failed!");
}
delete x;
delete y;
delete z;
}
void TestMessageFormat::testAssignment()
{
UErrorCode success = U_ZERO_ERROR;
MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
MessageFormat *y = new MessageFormat("There are {0} files on {1} created", success);
*y = *x;
if ( (*x == *y) &&
(*x != *z) &&
(*y != *z) )
logln("First test (operator ==): Passed!");
else {
errln( "TestMessageFormat::testAssignment failed #1");
logln("First test (operator ==): Failed!");
}
if ( ((*x == *y) && (*y == *x)) &&
((*x != *z) && (*z != *x)) &&
((*y != *z) && (*z != *y)) )
logln("Second test (equals): Passed!");
else {
errln("TestMessageFormat::testAssignment failed #2");
logln("Second test (equals): Failed!");
}
delete x;
delete y;
delete z;
}
void TestMessageFormat::testClone()
{
UErrorCode success = U_ZERO_ERROR;
MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
MessageFormat* y = nullptr;
y = x->clone();
if ( (*x == *y) &&
(*x != *z) &&
(*y != *z) )
logln("First test (operator ==): Passed!");
else {
errln("TestMessageFormat::testClone failed #1");
logln("First test (operator ==): Failed!");
}
if ( ((*x == *y) && (*y == *x)) &&
((*x != *z) && (*z != *x)) &&
((*y != *z) && (*z != *y)) )
logln("Second test (equals): Passed!");
else {
errln("TestMessageFormat::testClone failed #2");
logln("Second test (equals): Failed!");
}
delete x;
delete y;
delete z;
}
void TestMessageFormat::testEquals()
{
UErrorCode success = U_ZERO_ERROR;
MessageFormat x("There are {0} files on {1}", success);
MessageFormat y("There are {0} files on {1}", success);
if (!(x == y)) {
errln( "TestMessageFormat::testEquals failed #1");
logln("First test (operator ==): Failed!");
}
}
void TestMessageFormat::testNotEquals()
{
UErrorCode success = U_ZERO_ERROR;
MessageFormat x("There are {0} files on {1}", success);
MessageFormat y(x);
y.setLocale(Locale("fr"));
if (!(x != y)) {
errln( "TestMessageFormat::testEquals failed #1");
logln("First test (operator !=): Failed!");
}
y = x;
y.applyPattern("There are {0} files on {1} the disk", success);
if (!(x != y)) {
errln( "TestMessageFormat::testEquals failed #1");
logln("Second test (operator !=): Failed!");
}
}
void TestMessageFormat::testSetLocale()
{
UErrorCode err = U_ZERO_ERROR;
GregorianCalendar cal(err);
Formattable arguments[] = {
456.83,
Formattable(static_cast<UDate>(8.71068e+011), Formattable::kIsDate),
"deposit"
};
UnicodeString result;
//UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
UnicodeString formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
// {sfb} to get $, would need Locale::US, not Locale::ENGLISH
// Just use unlocalized currency symbol.
//UnicodeString compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
UnicodeString compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of ";
compareStrEng += static_cast<char16_t>(0x00a4);
compareStrEng += "456.83.";
// {sfb} to get DM, would need Locale::GERMANY, not Locale::GERMAN
// Just use unlocalized currency symbol.
//UnicodeString compareStrGer = "At <time> on 08.08.1997, you made a deposit of 456,83 DM.";
UnicodeString compareStrGer = "At <time> on 08.08.1997, you made a deposit of ";
compareStrGer += "456,83";
compareStrGer += static_cast<char16_t>(0x00a0);
compareStrGer += "XXX.";
MessageFormat msg( formatStr, err);
result = "";
FieldPosition pos(FieldPosition::DONT_CARE);
result = msg.format(
arguments,
3,
result,
pos,
err);
logln(result);
if (result != compareStrEng) {
char bbuf[96];
result.extract(0, result.length(), bbuf, sizeof(bbuf));
dataerrln("*** MSG format err. - %s; result was %s", u_errorName(err), bbuf);
}
msg.setLocale(Locale::getEnglish());
UBool getLocale_ok = true;
if (msg.getLocale() != Locale::getEnglish()) {
errln("*** MSG getLocale err.");
getLocale_ok = false;
}
msg.setLocale(Locale::getGerman());
if (msg.getLocale() != Locale::getGerman()) {
errln("*** MSG getLocal err.");
getLocale_ok = false;
}
msg.applyPattern( formatStr, err);
pos.setField(0);
result = "";
result = msg.format(
arguments,
3,
result,
pos,
err);
logln(result);
if (result == compareStrGer) {
logln("MSG setLocale tested.");
}else{
dataerrln( "*** MSG setLocale err. - %s", u_errorName(err));
}
if (getLocale_ok) {
logln("MSG getLocale tested.");
}
}
void TestMessageFormat::testFormat()
{
UErrorCode err = U_ZERO_ERROR;
GregorianCalendar cal(err);
const Formattable ftarray[] =
{
Formattable(static_cast<UDate>(8.71068e+011), Formattable::kIsDate)
};
const int32_t ft_cnt = UPRV_LENGTHOF(ftarray);
Formattable ft_arr( ftarray, ft_cnt );
Formattable* fmt = new Formattable(static_cast<UDate>(8.71068e+011), Formattable::kIsDate);
UnicodeString result;
//UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
UnicodeString formatStr = "On {0,date}, it began.";
UnicodeString compareStr = "On Aug 8, 1997, it began.";
err = U_ZERO_ERROR;
MessageFormat msg( formatStr, err);
FieldPosition fp(FieldPosition::DONT_CARE);
result = "";
fp = 0;
result = msg.format(
*fmt,
result,
//FieldPosition(FieldPosition::DONT_CARE),
fp,
err);
if (err != U_ILLEGAL_ARGUMENT_ERROR) {
dataerrln("*** MSG format without expected error code. - %s", u_errorName(err));
}
err = U_ZERO_ERROR;
result = "";
fp = 0;
result = msg.format(
ft_arr,
result,
//FieldPosition(FieldPosition::DONT_CARE),
fp,
err);
logln("MSG format( Formattable&, ... ) expected:" + compareStr);
logln("MSG format( Formattable&, ... ) result:" + result);
if (result != compareStr) {
dataerrln("*** MSG format( Formattable&, .... ) err. - %s", u_errorName(err));
}else{
logln("MSG format( Formattable&, ... ) tested.");
}
delete fmt;
}
void TestMessageFormat::testParse()
{
UErrorCode err = U_ZERO_ERROR;
int32_t count;
UnicodeString msgFormatString = "{0} =sep= {1}";
MessageFormat msg( msgFormatString, err);
UnicodeString source = "abc =sep= def";
UnicodeString tmp1, tmp2;
Formattable* fmt_arr = msg.parse( source, count, err );
if (U_FAILURE(err) || (!fmt_arr)) {
errln("*** MSG parse (ustring, count, err) error.");
}else{
logln("MSG parse -- count: %d", count);
if (count != 2) {
errln("*** MSG parse (ustring, count, err) count err.");
}else{
if ((fmt_arr[0].getType() == Formattable::kString)
&& (fmt_arr[1].getType() == Formattable::kString)
&& (fmt_arr[0].getString(tmp1) == "abc")
&& (fmt_arr[1].getString(tmp2) == "def")) {
logln("MSG parse (ustring, count, err) tested.");
}else{
errln("*** MSG parse (ustring, count, err) result err.");
}
}
}
delete[] fmt_arr;
ParsePosition pp(0);
fmt_arr = msg.parse( source, pp, count );
if ((pp == 0) || (!fmt_arr)) {
errln("*** MSG parse (ustring, parsepos., count) error.");
}else{
logln("MSG parse -- count: %d", count);
if (count != 2) {
errln("*** MSG parse (ustring, parsepos., count) count err.");
}else{
if ((fmt_arr[0].getType() == Formattable::kString)
&& (fmt_arr[1].getType() == Formattable::kString)
&& (fmt_arr[0].getString(tmp1) == "abc")
&& (fmt_arr[1].getString(tmp2) == "def")) {
logln("MSG parse (ustring, parsepos., count) tested.");
}else{
errln("*** MSG parse (ustring, parsepos., count) result err.");
}
}
}
delete[] fmt_arr;
pp = 0;
Formattable fmta;
msg.parseObject( source, fmta, pp );
if (pp == 0) {
errln("*** MSG parse (ustring, Formattable, parsepos ) error.");
}else{
logln("MSG parse -- count: %d", count);
fmta.getArray(count);
if (count != 2) {
errln("*** MSG parse (ustring, Formattable, parsepos ) count err.");
}else{
if ((fmta[0].getType() == Formattable::kString)
&& (fmta[1].getType() == Formattable::kString)
&& (fmta[0].getString(tmp1) == "abc")
&& (fmta[1].getString(tmp2) == "def")) {
logln("MSG parse (ustring, Formattable, parsepos ) tested.");
}else{
errln("*** MSG parse (ustring, Formattable, parsepos ) result err.");
}
}
}
}
void TestMessageFormat::testAdopt()
{
UErrorCode err = U_ZERO_ERROR;
UnicodeString formatStr("{0,date},{1},{2,number}", "");
UnicodeString formatStrChange("{0,number},{1,number},{2,date}", "");
err = U_ZERO_ERROR;
MessageFormat msg( formatStr, err);
MessageFormat msgCmp( formatStr, err);
if (U_FAILURE(err)) {
dataerrln("Unable to instantiate MessageFormat - %s", u_errorName(err));
return;
}
int32_t count, countCmp;
const Format** formats = msg.getFormats(count);
const Format** formatsCmp = msgCmp.getFormats(countCmp);
const Format** formatsChg = nullptr;
const Format** formatsAct = nullptr;
int32_t countAct;
const Format* a;
const Format* b;
UnicodeString patCmp;
UnicodeString patAct;
Format** formatsToAdopt;
if (!formats || !formatsCmp || (count <= 0) || (count != countCmp)) {
dataerrln("Error getting Formats");
return;
}
int32_t i;
for (i = 0; i < count; i++) {
a = formats[i];
b = formatsCmp[i];
if ((a != nullptr) && (b != nullptr)) {
if (*a != *b) {
errln("a != b");
return;
}
}else if ((a != nullptr) || (b != nullptr)) {
errln("(a != nullptr) || (b != nullptr)");
return;
}
}
msg.applyPattern( formatStrChange, err ); //set msg formats to something different
int32_t countChg;
formatsChg = msg.getFormats(countChg); // tested function
if (!formatsChg || (countChg != count)) {
errln("Error getting Formats");
return;
}
UBool diff;
diff = true;
for (i = 0; i < count; i++) {
a = formatsChg[i];
b = formatsCmp[i];
if ((a != nullptr) && (b != nullptr)) {
if (*a == *b) {
logln("formatsChg == formatsCmp at index %d", i);
diff = false;
}
}
}
if (!diff) {
errln("*** MSG getFormats diff err.");
return;
}
logln("MSG getFormats tested.");
msg.setFormats( formatsCmp, countCmp ); //tested function
formatsAct = msg.getFormats(countAct);
if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
errln("Error getting Formats");
return;
}
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern(patCmp.remove()));
// ICU 4.8 does not support toPattern() when there are custom formats (from setFormat() etc.).
// assertEquals("msg.toPattern()", formatStr, msg.toPattern(patAct.remove()));
msg.toPattern(patCmp.remove());
if (!patCmp.isBogus()) {
errln("msg.setFormat().toPattern() succeeds.");
}
for (i = 0; i < countAct; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != nullptr) && (b != nullptr)) {
if (*a != *b) {
logln("formatsAct != formatsCmp at index %d", i);
errln("a != b");
return;
}
}else if ((a != nullptr) || (b != nullptr)) {
errln("(a != nullptr) || (b != nullptr)");
return;
}
}
logln("MSG setFormats tested.");
//----
msg.applyPattern( formatStrChange, err ); //set msg formats to something different
formatsToAdopt = new Format* [countCmp];
if (!formatsToAdopt) {
errln("memory allocation error");
return;
}
for (i = 0; i < countCmp; i++) {
if (formatsCmp[i] == nullptr) {
formatsToAdopt[i] = nullptr;
}else{
formatsToAdopt[i] = formatsCmp[i]->clone();
if (!formatsToAdopt[i]) {
errln("Can't clone format at index %d", i);
return;
}
}
}
msg.adoptFormats( formatsToAdopt, countCmp ); // function to test
delete[] formatsToAdopt;
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern(patCmp.remove()));
// ICU 4.8 does not support toPattern() when there are custom formats (from setFormat() etc.).
// assertEquals("msg.toPattern()", formatStr, msg.toPattern(patAct.remove()));
formatsAct = msg.getFormats(countAct);
if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
errln("Error getting Formats");
return;
}
for (i = 0; i < countAct; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != nullptr) && (b != nullptr)) {
if (*a != *b) {
errln("a != b");
return;
}
}else if ((a != nullptr) || (b != nullptr)) {
errln("(a != nullptr) || (b != nullptr)");
return;
}
}
logln("MSG adoptFormats tested.");
//---- adoptFormat
msg.applyPattern( formatStrChange, err ); //set msg formats to something different
formatsToAdopt = new Format* [countCmp];
if (!formatsToAdopt) {
errln("memory allocation error");
return;
}
for (i = 0; i < countCmp; i++) {
if (formatsCmp[i] == nullptr) {
formatsToAdopt[i] = nullptr;
}else{
formatsToAdopt[i] = formatsCmp[i]->clone();
if (!formatsToAdopt[i]) {
errln("Can't clone format at index %d", i);
return;
}
}
}
for ( i = 0; i < countCmp; i++ ) {
msg.adoptFormat( i, formatsToAdopt[i] ); // function to test
}
delete[] formatsToAdopt; // array itself not needed in this case;
assertEquals("msgCmp.toPattern()", formatStr, msgCmp.toPattern(patCmp.remove()));
// ICU 4.8 does not support toPattern() when there are custom formats (from setFormat() etc.).
// assertEquals("msg.toPattern()", formatStr, msg.toPattern(patAct.remove()));
formatsAct = msg.getFormats(countAct);
if (!formatsAct || (countAct <=0) || (countAct != countCmp)) {
errln("Error getting Formats");
return;
}
for (i = 0; i < countAct; i++) {
a = formatsAct[i];
b = formatsCmp[i];
if ((a != nullptr) && (b != nullptr)) {
if (*a != *b) {
errln("a != b");
return;
}
}else if ((a != nullptr) || (b != nullptr)) {
errln("(a != nullptr) || (b != nullptr)");
return;
}
}
logln("MSG adoptFormat tested.");
}
// This test is a regression test for a fixed bug in the copy constructor.
// It is kept as a global function rather than as a method since the test depends on memory values.
// (At least before the bug was fixed, whether it showed up or not depended on memory contents,
// which is probably why it didn't show up in the regular test for the copy constructor.)
// For this reason, the test isn't changed even though it contains function calls whose results are
// not tested and had no problems. Actually, the test failed by *crashing*.
static void _testCopyConstructor2()
{
UErrorCode status = U_ZERO_ERROR;
UnicodeString formatStr("Hello World on {0,date,full}", "");
UnicodeString resultStr(" ", "");
UnicodeString result;
FieldPosition fp(FieldPosition::DONT_CARE);
UDate d = Calendar::getNow();
const Formattable fargs( d, Formattable::kIsDate );
MessageFormat* fmt1 = new MessageFormat( formatStr, status );
MessageFormat* fmt2 = nullptr;
MessageFormat* fmt3 = nullptr;
MessageFormat* fmt4 = nullptr;
if (fmt1 == nullptr) {
it_err("testCopyConstructor2: (fmt1 != nullptr)");
goto cleanup;
}
fmt2 = new MessageFormat( *fmt1 );
result = fmt1->format( &fargs, 1, resultStr, fp, status );
if (fmt2 == nullptr) {
it_err("testCopyConstructor2: (fmt2 != nullptr)");
goto cleanup;
}
fmt3 = fmt1->clone();
fmt4 = fmt2->clone();
if (fmt3 == nullptr) {
it_err("testCopyConstructor2: (fmt3 != nullptr)");
goto cleanup;
}
if (fmt4 == nullptr) {
it_err("testCopyConstructor2: (fmt4 != nullptr)");
goto cleanup;
}
result = fmt1->format( &fargs, 1, resultStr, fp, status );
result = fmt2->format( &fargs, 1, resultStr, fp, status );
result = fmt3->format( &fargs, 1, resultStr, fp, status );
result = fmt4->format( &fargs, 1, resultStr, fp, status );
cleanup:
delete fmt1;
delete fmt2;
delete fmt3;
delete fmt4;
}
void TestMessageFormat::testCopyConstructor2() {
_testCopyConstructor2();
}
/**
* Verify that MessageFormat accommodates more than 10 arguments and
* more than 10 subformats.
*/
void TestMessageFormat::TestUnlimitedArgsAndSubformats() {
UErrorCode ec = U_ZERO_ERROR;
const UnicodeString pattern =
"On {0,date} (aka {0,date,short}, aka {0,date,long}) "
"at {0,time} (aka {0,time,short}, aka {0,time,long}) "
"there were {1,number} werjes "
"(a {3,number,percent} increase over {2,number}) "
"despite the {4}''s efforts "
"and to delight of {5}, {6}, {7}, {8}, {9}, and {10} {11}.";
MessageFormat msg(pattern, ec);
if (U_FAILURE(ec)) {
dataerrln("FAIL: constructor failed - %s", u_errorName(ec));
return;
}
const Formattable ARGS[] = {
Formattable(static_cast<UDate>(1e13), Formattable::kIsDate),
Formattable(static_cast<int32_t>(1303)),
Formattable(static_cast<int32_t>(1202)),
Formattable(1303.0/1202 - 1),
Formattable("Glimmung"),
Formattable("the printers"),
Formattable("Nick"),
Formattable("his father"),
Formattable("his mother"),
Formattable("the spiddles"),
Formattable("of course"),
Formattable("Horace"),
};
const int32_t ARGS_LENGTH = UPRV_LENGTHOF(ARGS);
Formattable ARGS_OBJ(ARGS, ARGS_LENGTH);
UnicodeString expected =
u"On Nov 20, 2286 (aka 11/20/86, aka November 20, 2286) "
u"at 9:46:40\u202FAM (aka 9:46\u202FAM, aka 9:46:40\u202FAM PST) "
u"there were 1,303 werjes "
u"(a 8% increase over 1,202) "
u"despite the Glimmung's efforts "
u"and to delight of the printers, Nick, his father, "
u"his mother, the spiddles, and of course Horace.";
UnicodeString result;
msg.format(ARGS_OBJ, result, ec);
if (result == expected) {
logln(result);
} else {
errln(UnicodeString("FAIL: Got ") + result +
", expected " + expected);
}
}
// test RBNF extensions to message format
void TestMessageFormat::TestRBNF() {
// WARNING: this depends on the RBNF formats for en_US
Locale locale("en", "US", "");
UErrorCode ec = U_ZERO_ERROR;
UnicodeString values[] = {
// decimal values do not format completely for ordinal or duration, and
// do not always parse, so do not include them
"0", "1", "12", "100", "123", "1001", "123,456", "-17",
};
int32_t values_count = UPRV_LENGTHOF(values);
UnicodeString formats[] = {
"There are {0,spellout} files to search.",
"There are {0,spellout,%simplified} files to search.",
"The bogus spellout {0,spellout,%BOGUS} files behaves like the default.",
"This is the {0,ordinal} file to search.",
"Searching this file will take {0,duration} to complete.",
"Searching this file will take {0,duration,%with-words} to complete.",
};
int32_t formats_count = UPRV_LENGTHOF(formats);
Formattable args[1];
NumberFormat* numFmt = NumberFormat::createInstance(locale, ec);
if (U_FAILURE(ec)) {
dataerrln("Error calling NumberFormat::createInstance()");
return;
}
for (int i = 0; i < formats_count; ++i) {
MessageFormat* fmt = new MessageFormat(formats[i], locale, ec);
logln(UnicodeString("Testing format pattern: '") + formats[i] + "'");
for (int j = 0; j < values_count; ++j) {
ec = U_ZERO_ERROR;
numFmt->parse(values[j], args[0], ec);
if (U_FAILURE(ec)) {
errln(UnicodeString("Failed to parse test argument ") + values[j]);
} else {
FieldPosition fp(FieldPosition::DONT_CARE);
UnicodeString result;
fmt->format(args, 1, result, fp, ec);
logln(UnicodeString("value: ") + toString(args[0]) + " --> " + result + UnicodeString(" ec: ") + u_errorName(ec));
int32_t count = 0;
Formattable* parseResult = fmt->parse(result, count, ec);
if (count != 1) {
errln(UnicodeString("parse returned ") + count + " args");
} else if (parseResult[0] != args[0]) {
errln(UnicodeString("parsed argument ") + toString(parseResult[0]) + " != " + toString(args[0]));
}
delete []parseResult;
}
}
delete fmt;
}
delete numFmt;
}
UnicodeString TestMessageFormat::GetPatternAndSkipSyntax(const MessagePattern& pattern) {
UnicodeString us(pattern.getPatternString());
int count = pattern.countParts();
for (int i = count; i > 0;) {
const MessagePattern::Part& part = pattern.getPart(--i);
if (part.getType() == UMSGPAT_PART_TYPE_SKIP_SYNTAX) {
us.remove(part.getIndex(), part.getLimit() - part.getIndex());
}
}
return us;
}
void TestMessageFormat::TestApostropheMode() {
UErrorCode status = U_ZERO_ERROR;
MessagePattern *ado_mp = new MessagePattern(UMSGPAT_APOS_DOUBLE_OPTIONAL, status);
MessagePattern *adr_mp = new MessagePattern(UMSGPAT_APOS_DOUBLE_REQUIRED, status);
if (ado_mp->getApostropheMode() != UMSGPAT_APOS_DOUBLE_OPTIONAL) {
errln("wrong value from ado_mp->getApostropheMode().");
}
if (adr_mp->getApostropheMode() != UMSGPAT_APOS_DOUBLE_REQUIRED) {
errln("wrong value from adr_mp->getApostropheMode().");
}
UnicodeString tuples[] = {
// Desired output
// DOUBLE_OPTIONAL pattern
// DOUBLE_REQUIRED pattern (empty=same as DOUBLE_OPTIONAL)
"I see {many}", "I see '{many}'", "",
"I said {'Wow!'}", "I said '{''Wow!''}'", "",
"I dont know", "I dont know", "I don't know",
"I don't know", "I don't know", "I don''t know",
"I don't know", "I don''t know", "I don''t know"
};
int32_t tuples_count = UPRV_LENGTHOF(tuples);
for (int i = 0; i < tuples_count; i += 3) {
UnicodeString& desired = tuples[i];
UnicodeString& ado_pattern = tuples[i + 1];
UErrorCode status = U_ZERO_ERROR;
assertEquals("DOUBLE_OPTIONAL failure",
desired,
GetPatternAndSkipSyntax(ado_mp->parse(ado_pattern, nullptr, status)));
UnicodeString& adr_pattern = tuples[i + 2].isEmpty() ? ado_pattern : tuples[i + 2];
assertEquals("DOUBLE_REQUIRED failure", desired,
GetPatternAndSkipSyntax(adr_mp->parse(adr_pattern, nullptr, status)));
}
delete adr_mp;
delete ado_mp;
}
// Compare behavior of DOUBLE_OPTIONAL (new default) and DOUBLE_REQUIRED JDK-compatibility mode.
void TestMessageFormat::TestCompatibleApostrophe() {
// Message with choice argument which does not contain another argument.
// The JDK performs only one apostrophe-quoting pass on this pattern.
UnicodeString pattern = "ab{0,choice,0#1'2''3'''4''''.}yz";
UErrorCode ec = U_ZERO_ERROR;
MessageFormat compMsg("", Locale::getUS(), ec);
compMsg.applyPattern(pattern, UMSGPAT_APOS_DOUBLE_REQUIRED, nullptr, ec);
if (compMsg.getApostropheMode() != UMSGPAT_APOS_DOUBLE_REQUIRED) {
errln("wrong value from compMsg.getApostropheMode().");
}
MessageFormat icuMsg("", Locale::getUS(), ec);
icuMsg.applyPattern(pattern, UMSGPAT_APOS_DOUBLE_OPTIONAL, nullptr, ec);
if (icuMsg.getApostropheMode() != UMSGPAT_APOS_DOUBLE_OPTIONAL) {
errln("wrong value from icuMsg.getApostropheMode().");
}
Formattable zero0[] = {static_cast<int32_t>(0)};
FieldPosition fieldpos(FieldPosition::DONT_CARE);
UnicodeString buffer1, buffer2;
assertEquals("incompatible ICU MessageFormat compatibility-apostrophe behavior",
"ab12'3'4''.yz",
compMsg.format(zero0, 1, buffer1, fieldpos, ec));
assertEquals("unexpected ICU MessageFormat double-apostrophe-optional behavior",
"ab1'2'3''4''.yz",
icuMsg.format(zero0, 1, buffer2, fieldpos, ec));
// Message with choice argument which contains a nested simple argument.
// The DOUBLE_REQUIRED version performs two apostrophe-quoting passes.
buffer1.remove();
buffer2.remove();
pattern = "ab{0,choice,0#1'2''3'''4''''.{0,number,'#x'}}yz";
compMsg.applyPattern(pattern, ec);
icuMsg.applyPattern(pattern, ec);
if (U_FAILURE(ec)) {
dataerrln("Unable to applyPattern - %s", u_errorName(ec));
} else {
assertEquals("incompatible ICU MessageFormat compatibility-apostrophe behavior",
"ab1234'.0xyz",
compMsg.format(zero0, 1, buffer1, fieldpos, ec));
assertEquals("unexpected ICU MessageFormat double-apostrophe-optional behavior",
"ab1'2'3''4''.#x0yz",
icuMsg.format(zero0, 1, buffer2, fieldpos, ec));
}
// This part is copied over from Java tests but cannot be properly tested here
// because we do not have a live reference implementation with JDK behavior.
// The JDK ChoiceFormat itself always performs one apostrophe-quoting pass.
/*
ChoiceFormat choice = new ChoiceFormat("0#1'2''3'''4''''.");
assertEquals("unexpected JDK ChoiceFormat apostrophe behavior",
"12'3'4''.",
choice.format(0));
choice.applyPattern("0#1'2''3'''4''''.{0,number,'#x'}");
assertEquals("unexpected JDK ChoiceFormat apostrophe behavior",
"12'3'4''.{0,number,#x}",
choice.format(0));
*/
}
void TestMessageFormat::testAutoQuoteApostrophe() {
const char* patterns[] = { // pattern, expected pattern
"'", "''",
"''", "''",
"'{", "'{'",
"' {", "'' {",
"'a", "''a",
"'{'a", "'{'a",
"'{a'", "'{a'",
"'{}", "'{}'",
"{'", "{'",
"{'a", "{'a",
"{'a{}'a}'a", "{'a{}'a}''a",
"'}'", "'}'",
"'} '{'}'", "'} '{'}''",
"'} {{{''", "'} {{{'''",
};
int32_t pattern_count = UPRV_LENGTHOF(patterns);
for (int i = 0; i < pattern_count; i += 2) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString result = MessageFormat::autoQuoteApostrophe(patterns[i], status);
UnicodeString target(patterns[i+1]);
if (target != result) {
const int BUF2_LEN = 64;
char buf[256];
char buf2[BUF2_LEN];
int32_t len = result.extract(0, result.length(), buf2, BUF2_LEN);
if (len >= BUF2_LEN) {
buf2[BUF2_LEN-1] = 0;
}
snprintf(buf, sizeof(buf), "[%2d] test \"%s\": target (\"%s\") != result (\"%s\")\n", i/2, patterns[i], patterns[i+1], buf2);
errln(buf);
}
}
}
void TestMessageFormat::testCoverage() {
UErrorCode status = U_ZERO_ERROR;
UnicodeString testformat("{argument, plural, one{C''est # fichier} other {Ce sont # fichiers}} dans la liste.");
MessageFormat *msgfmt = new MessageFormat(testformat, Locale("fr"), status);
if (msgfmt == nullptr || U_FAILURE(status)) {
dataerrln("FAIL: Unable to create MessageFormat.: %s", u_errorName(status));
return;
}
if (!msgfmt->usesNamedArguments()) {
errln("FAIL: Unable to detect usage of named arguments.");
}
const double limit[] = {0.0, 1.0, 2.0};
const UnicodeString formats[] = {"0.0<=Arg<1.0",
"1.0<=Arg<2.0",
"2.0<-Arg"};
ChoiceFormat cf(limit, formats, 3);
msgfmt->setFormat("set", cf, status);
StringEnumeration *en = msgfmt->getFormatNames(status);
if (en == nullptr || U_FAILURE(status)) {
errln("FAIL: Unable to get format names enumeration.");
} else {
int32_t count = 0;
en->reset(status);
count = en->count(status);
if (U_FAILURE(status)) {
errln("FAIL: Unable to get format name enumeration count.");
} else {
for (int32_t i = 0; i < count; i++) {
en->snext(status);
if (U_FAILURE(status)) {
errln("FAIL: Error enumerating through names.");
break;
}
}
}
}
// adoptFormat() takes ownership of the input Format object.
// We need to clone the stack-allocated cf so that we do not attempt to delete cf.
Format *cfClone = cf.clone();
msgfmt->adoptFormat("adopt", cfClone, status);
delete en;
delete msgfmt;
msgfmt = new MessageFormat("'", status);
if (msgfmt == nullptr || U_FAILURE(status)) {
errln("FAIL: Unable to create MessageFormat.");
return;
}
if (msgfmt->usesNamedArguments()) {
errln("FAIL: Unable to detect usage of named arguments.");
}
// Starting with ICU 4.8, we support setFormat(name, ...) and getFormatNames()
// on a MessageFormat without named arguments.
msgfmt->setFormat("formatName", cf, status);
if (U_FAILURE(status)) {
errln("FAIL: Should work to setFormat(name, ...) regardless of pattern.");
}
status = U_ZERO_ERROR;
en = msgfmt->getFormatNames(status);
if (U_FAILURE(status)) {
errln("FAIL: Should work to get format names enumeration regardless of pattern.");
}
delete en;
delete msgfmt;
}
void TestMessageFormat::testGetFormatNames() {
IcuTestErrorCode errorCode(*this, "testGetFormatNames");
MessageFormat msgfmt("Hello, {alice,number} {oops,date,full} {zip,spellout} World.", Locale::getRoot(), errorCode);
if(errorCode.errDataIfFailureAndReset("MessageFormat() failed")) {
return;
}
LocalPointer<StringEnumeration> names(msgfmt.getFormatNames(errorCode));
if(errorCode.errIfFailureAndReset("msgfmt.getFormatNames() failed")) {
return;
}
const UnicodeString *name;
name = names->snext(errorCode);
if (name == nullptr || errorCode.isFailure()) {
errln("msgfmt.getFormatNames()[0] failed: %s", errorCode.errorName());
errorCode.reset();
return;
}
if (!assertEquals("msgfmt.getFormatNames()[0]", UNICODE_STRING_SIMPLE("alice"), *name)) {
return;
}
name = names->snext(errorCode);
if (name == nullptr || errorCode.isFailure()) {
errln("msgfmt.getFormatNames()[1] failed: %s", errorCode.errorName());
errorCode.reset();
return;
}
if (!assertEquals("msgfmt.getFormatNames()[1]", UNICODE_STRING_SIMPLE("oops"), *name)) {
return;
}
name = names->snext(errorCode);
if (name == nullptr || errorCode.isFailure()) {
errln("msgfmt.getFormatNames()[2] failed: %s", errorCode.errorName());
errorCode.reset();
return;
}
if (!assertEquals("msgfmt.getFormatNames()[2]", UNICODE_STRING_SIMPLE("zip"), *name)) {
return;
}
name = names->snext(errorCode);
if (name != nullptr) {
errln(UnicodeString("msgfmt.getFormatNames()[3] should be nullptr but is: ") + *name);
return;
}
}
void TestMessageFormat::TestTrimArgumentName() {
// ICU 4.8 allows and ignores white space around argument names and numbers.
IcuTestErrorCode errorCode(*this, "TestTrimArgumentName");
MessageFormat m("a { 0 , number , '#,#'#.0 } z", Locale::getEnglish(), errorCode);
if (errorCode.errDataIfFailureAndReset("Unable to instantiate MessageFormat")) {
return;
}
Formattable args[1] = {static_cast<int32_t>(2)};
FieldPosition ignore(FieldPosition::DONT_CARE);
UnicodeString result;
assertEquals("trim-numbered-arg format() failed", "a #,#2.0 z",
m.format(args, 1, result, ignore, errorCode));
m.applyPattern("x { _oOo_ , number , integer } y", errorCode);
UnicodeString argName = UNICODE_STRING_SIMPLE("_oOo_");
args[0].setLong(3);
result.remove();
assertEquals("trim-named-arg format() failed", "x 3 y",
m.format(&argName, args, 1, result, errorCode));
}
void TestMessageFormat::TestSelectOrdinal() {
IcuTestErrorCode errorCode(*this, "TestSelectOrdinal");
// Test plural & ordinal together,
// to make sure that we get the correct cached PluralSelector for each.
MessageFormat m(
"{0,plural,one{1 file}other{# files}}, "
"{0,selectordinal,one{#st file}two{#nd file}few{#rd file}other{#th file}}",
Locale::getEnglish(), errorCode);
if (errorCode.errDataIfFailureAndReset("Unable to instantiate MessageFormat")) {
return;
}
Formattable args[1] = {static_cast<int32_t>(21)};
FieldPosition ignore(FieldPosition::DONT_CARE);
UnicodeString result;
assertEquals("plural-and-ordinal format(21) failed", "21 files, 21st file",
m.format(args, 1, result, ignore, errorCode), true);
args[0].setLong(2);
assertEquals("plural-and-ordinal format(2) failed", "2 files, 2nd file",
m.format(args, 1, result.remove(), ignore, errorCode), true);
args[0].setLong(1);
assertEquals("plural-and-ordinal format(1) failed", "1 file, 1st file",
m.format(args, 1, result.remove(), ignore, errorCode), true);
args[0].setLong(3);
assertEquals("plural-and-ordinal format(3) failed", "3 files, 3rd file",
m.format(args, 1, result.remove(), ignore, errorCode), true);
errorCode.errDataIfFailureAndReset("");
}
void TestMessageFormat::TestDecimals() {
IcuTestErrorCode errorCode(*this, "TestDecimals");
// Simple number replacement.
MessageFormat m(
"{0,plural,one{one meter}other{# meters}}",
Locale::getEnglish(), errorCode);
Formattable args[1] = {static_cast<int32_t>(1)};
FieldPosition ignore;
UnicodeString result;
assertEquals("simple format(1)", "one meter",
m.format(args, 1, result, ignore, errorCode), true);
args[0] = 1.5;
result.remove();
assertEquals("simple format(1.5)", "1.5 meters",
m.format(args, 1, result, ignore, errorCode), true);
// Simple but explicit.
MessageFormat m0(
"{0,plural,one{one meter}other{{0} meters}}",
Locale::getEnglish(), errorCode);
args[0] = static_cast<int32_t>(1);
result.remove();
assertEquals("explicit format(1)", "one meter",
m0.format(args, 1, result, ignore, errorCode), true);
args[0] = 1.5;
result.remove();
assertEquals("explicit format(1.5)", "1.5 meters",
m0.format(args, 1, result, ignore, errorCode), true);
// With offset and specific simple format with optional decimals.
MessageFormat m1(
"{0,plural,offset:1 one{another meter}other{{0,number,00.#} meters}}",
Locale::getEnglish(), errorCode);
args[0] = static_cast<int32_t>(1);
result.remove();
assertEquals("offset format(1)", "01 meters",
m1.format(args, 1, result, ignore, errorCode), true);
args[0] = static_cast<int32_t>(2);
result.remove();
assertEquals("offset format(1)", "another meter",
m1.format(args, 1, result, ignore, errorCode), true);
args[0] = 2.5;
result.remove();
assertEquals("offset format(1)", "02.5 meters",
m1.format(args, 1, result, ignore, errorCode), true);
// With offset and specific simple format with forced decimals.
MessageFormat m2(
"{0,plural,offset:1 one{another meter}other{{0,number,0.0} meters}}",
Locale::getEnglish(), errorCode);
args[0] = static_cast<int32_t>(1);
result.remove();
assertEquals("offset-decimals format(1)", "1.0 meters",
m2.format(args, 1, result, ignore, errorCode), true);
args[0] = static_cast<int32_t>(2);
result.remove();
assertEquals("offset-decimals format(1)", "2.0 meters",
m2.format(args, 1, result, ignore, errorCode), true);
args[0] = 2.5;
result.remove();
assertEquals("offset-decimals format(1)", "2.5 meters",
m2.format(args, 1, result, ignore, errorCode), true);
errorCode.reset();
}
void TestMessageFormat::TestArgIsPrefixOfAnother() {
IcuTestErrorCode errorCode(*this, "TestArgIsPrefixOfAnother");
// Ticket #11952
MessageFormat mf1("{0,select,a{A}ab{AB}abc{ABC}other{?}}", Locale::getEnglish(), errorCode);
Formattable args[3];
FieldPosition ignore;
UnicodeString result;
args[0].setString("a");
assertEquals("a", "A", mf1.format(args, 1, result, ignore, errorCode));
args[0].setString("ab");
assertEquals("ab", "AB", mf1.format(args, 1, result.remove(), ignore, errorCode));
args[0].setString("abc");
assertEquals("abc", "ABC", mf1.format(args, 1, result.remove(), ignore, errorCode));
// Ticket #12172
MessageFormat mf2("{a} {aa} {aaa}", Locale::getEnglish(), errorCode);
UnicodeString argNames[3] = { "a", "aa", "aaa" };
args[0].setString("A");
args[1].setString("AB");
args[2].setString("ABC");
assertEquals("a aa aaa", "A AB ABC", mf2.format(argNames, args, 3, result.remove(), errorCode));
// Ticket #12172
MessageFormat mf3("{aa} {aaa}", Locale::getEnglish(), errorCode);
assertEquals("aa aaa", "AB ABC", mf3.format(argNames + 1, args + 1, 2, result.remove(), errorCode));
}
void TestMessageFormat::TestMessageFormatNumberSkeleton() {
IcuTestErrorCode status(*this, "TestMessageFormatNumberSkeleton");
static const struct TestCase {
const char16_t* messagePattern;
const char* localeName;
double arg;
const char16_t* expected;
} cases[] = {
{ u"{0,number,::percent}", "en", 50, u"50%" },
{ u"{0,number,::percent scale/100}", "en", 0.5, u"50%" },
{ u"{0,number, :: percent scale/100 }", "en", 0.5, u"50%" },
{ u"{0,number,::currency/USD}", "en", 23, u"$23.00" },
{ u"{0,number,::precision-integer}", "en", 514.23, u"514" },
{ u"{0,number,::.000}", "en", 514.23, u"514.230" },
{ u"{0,number,::.}", "en", 514.23, u"514" },
{ u"{0,number,::}", "fr", 514.23, u"514,23" },
{ u"Cost: {0,number,::currency/EUR}.", "en", 4.3, u"Cost: €4.30." },
{ u"{0,number,'::'0.00}", "en", 50, u"::50.00" }, // pattern literal
};
for (const auto& cas : cases) {
status.setScope(cas.messagePattern);
MessageFormat msgf(cas.messagePattern, cas.localeName, status);
UnicodeString sb;
FieldPosition fpos(FieldPosition::DONT_CARE);
Formattable argsArray[] = {{cas.arg}};
Formattable args(argsArray, 1);
msgf.format(args, sb, status);
assertEquals(cas.messagePattern, cas.expected, sb);
}
}
void TestMessageFormat::doTheRealDateTimeSkeletonTesting(UDate testDate,
const char16_t* messagePattern, const char* localeName, const char16_t* expected,
IcuTestErrorCode& status) {
status.setScope(messagePattern);
MessageFormat msgf(messagePattern, localeName, status);
UnicodeString sb;
FieldPosition fpos(FieldPosition::DONT_CARE);
Formattable argsArray[] = { Formattable(testDate, Formattable::kIsDate) };
Formattable args(argsArray, 1);
msgf.format(args, sb, status);
assertEquals(messagePattern, expected, sb);
}
void TestMessageFormat::TestMessageFormatDateSkeleton() {
IcuTestErrorCode status(*this, "TestMessageFormatDateSkeleton");
UDate date = LocaleTest::date(2021-1900, UCAL_NOVEMBER, 23, 16, 42, 55);
doTheRealDateTimeSkeletonTesting(date, u"{0,date,::MMMMd}", "en", u"November 23", status);
doTheRealDateTimeSkeletonTesting(date, u"{0,date,::yMMMMdjm}", "en", u"November 23, 2021 at 4:42\u202FPM", status);
doTheRealDateTimeSkeletonTesting(date, u"{0,date, :: yMMMMd }", "en", u"November 23, 2021", status);
doTheRealDateTimeSkeletonTesting(date, u"{0,date,::yMMMMd}", "fr", u"23 novembre 2021", status);
doTheRealDateTimeSkeletonTesting(date, u"Expiration: {0,date,::yMMM}!", "en", u"Expiration: Nov 2021!", status);
// pattern literal
doTheRealDateTimeSkeletonTesting(date, u"{0,date,'::'yMMMMd}", "en", u"::2021November23", status);
}
void TestMessageFormat::TestMessageFormatTimeSkeleton() {
IcuTestErrorCode status(*this, "TestMessageFormatTimeSkeleton");
UDate date = LocaleTest::date(2021-1900, UCAL_NOVEMBER, 23, 16, 42, 55);
doTheRealDateTimeSkeletonTesting(date, u"{0,time,::MMMMd}", "en", u"November 23", status);
doTheRealDateTimeSkeletonTesting(date, u"{0,time,::yMMMMdjm}", "en", u"November 23, 2021 at 4:42\u202FPM", status);
doTheRealDateTimeSkeletonTesting(date, u"{0,time, :: yMMMMd }", "en", u"November 23, 2021", status);
doTheRealDateTimeSkeletonTesting(date, u"{0,time,::yMMMMd}", "fr", u"23 novembre 2021", status);
doTheRealDateTimeSkeletonTesting(date, u"Expiration: {0,time,::yMMM}!", "en", u"Expiration: Nov 2021!", status);
// pattern literal
doTheRealDateTimeSkeletonTesting(date, u"{0,time,'::'yMMMMd}", "en", u"::2021November23", status);
}
void TestMessageFormat::TestNumberOverflow() {
IcuTestErrorCode status(*this, "TestNumberOverflow");
MessageFormat msgf(u"{90000000000}", status);
status.expectErrorAndReset(U_PATTERN_SYNTAX_ERROR);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|