void TestMessageFormat::testFormat() { UErrorCode err = U_ZERO_ERROR; GregorianCalendar cal(err); const Formattable ftarray[] = { Formattable( UDate(8.71068e+011), Formattable::kIsDate ) }; const int32_t ft_cnt = sizeof(ftarray) / sizeof(Formattable); Formattable ft_arr( ftarray, ft_cnt ); Formattable* fmt = new Formattable(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(0); result = ""; fp = 0; result = msg.format( *fmt, result, //FieldPosition(0), fp, err); if (err != U_ILLEGAL_ARGUMENT_ERROR) { errln("*** MSG format without expected error code."); } err = U_ZERO_ERROR; result = ""; fp = 0; result = msg.format( ft_arr, result, //FieldPosition(0), fp, err); logln("MSG format( Formattable&, ... ) expected:" + compareStr); logln("MSG format( Formattable&, ... ) result:" + result); if (result != compareStr) { errln("*** MSG format( Formattable&, .... ) err."); }else{ logln("MSG format( Formattable&, ... ) tested."); } delete fmt; }
/* 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[] = { (int32_t)7, Formattable(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)) { errln("TestTurkishCasing #1 with error code %s", u_errorName(err)); return; } const UnicodeString expected( "At 12:20:00 on 08.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::testStaticFormat() { UErrorCode err = U_ZERO_ERROR; Formattable arguments[] = { (int32_t)7, Formattable(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)) { errln("TestMessageFormat::testStaticFormat #1"); logln(UnicodeString("TestMessageFormat::testStaticFormat failed test #1 with error code ")+(int32_t)err); return; } const UnicodeString expected( "At 12:20:00 PM on Aug 8, 1997, there was a disturbance in the Force on planet 7.", ""); if (result != expected) { errln("TestMessageFormat::testStaticFormat failed on test"); logln( UnicodeString(" Result: ") + result ); logln( UnicodeString(" Expected: ") + expected ); } }
/** * Verify that MessageFormat accomodates 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)) { errln("FAIL: constructor failed"); return; } const Formattable ARGS[] = { Formattable(UDate(1e13), Formattable::kIsDate), Formattable((int32_t)1303), Formattable((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 = sizeof(ARGS) / sizeof(ARGS[0]); Formattable ARGS_OBJ(ARGS, ARGS_LENGTH); UnicodeString expected = "On Nov 20, 2286 (aka 11/20/86, aka November 20, 2286) " "at 9:46:40 AM (aka 9:46 AM, aka 9:46:40 AM PST) " "there were 1,303 werjes " "(a 8% increase over 1,202) " "despite the Glimmung's efforts " "and to delight of the printers, Nick, his father, " "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); } }
UnicodeString& TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const { // SRL TODO: cache the SDF, just like java. UErrorCode status = U_ZERO_ERROR; SimpleDateFormat format(style == LONG ? "zzzz" : "z",locale,status); if(!U_SUCCESS(status)) { // *** SRL what do I do here?!! return result.remove(); } // Create a new SimpleTimeZone as a stand-in for this zone; the // stand-in will have no DST, or all DST, but the same ID and offset, // and hence the same display name. // We don't cache these because they're small and cheap to create. UnicodeString tempID; SimpleTimeZone *tz = daylight ? // For the pure-DST zone, we use JANUARY and DECEMBER new SimpleTimeZone(getRawOffset(), getID(tempID), UCAL_JANUARY , 1, 0, 0, UCAL_DECEMBER , 31, 0, U_MILLIS_PER_DAY, status) : new SimpleTimeZone(getRawOffset(), getID(tempID)); format.applyPattern(style == LONG ? "zzzz" : "z"); Calendar *myCalendar = (Calendar*)format.getCalendar(); myCalendar->setTimeZone(*tz); // copy delete tz; FieldPosition pos(FieldPosition::DONT_CARE); return format.format(UDate(196262345678.), result, pos); // Must use a valid date here. }
void TestMessageFormat::testSetLocale() { UErrorCode err = U_ZERO_ERROR; GregorianCalendar cal(err); Formattable arguments[] = { 456.83, Formattable(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 += (UChar) 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 += (UChar) 0x00a4; compareStrGer += "."; MessageFormat msg( formatStr, err); result = ""; FieldPosition pos(0); result = msg.format( arguments, 3, result, pos, err); logln(result); if (result != compareStrEng) { errln("*** MSG format err."); } msg.setLocale(Locale::getEnglish()); UBool getLocale_ok = TRUE; if (msg.getLocale() != Locale::getEnglish()) { errln("*** MSG getLocal 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{ errln( "*** MSG setLocale err."); } if (getLocale_ok) { logln("MSG getLocale tested."); } }
void TestMessageFormat::PatternTest() { Formattable testArgs[] = { Formattable(double(1)), Formattable(double(3456)), Formattable("Disk"), Formattable(UDate((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,#,##}", }; 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[] = { "Quotes ', {, a 1 {0}", "Quotes ', {, a 1 {0}", "{1,number,#,##} #34,56", "There are 3,456 files on Disk at 1/12/70 5:46 AM.", "On Disk, there are 3,456 files, with $1.00.", "{1,number,percent}, 345,600%,", "{1,date,full}, Wednesday, December 31, 1969,", "{3,date,full}, Monday, January 12, 1970,", "{1,number,#,##} 34,56" }; for (int32_t i = 0; i < 9; ++i) { //it_out << "\nPat in: " << testCases[i]); MessageFormat *form = 0; UErrorCode success = U_ZERO_ERROR; UnicodeString buffer; form = new MessageFormat(testCases[i], Locale::getUS(), success); if (U_FAILURE(success)) { errln("MessageFormat creation failed.#1"); logln(((UnicodeString)"MessageFormat for ") + testCases[i] + " creation failed.\n"); continue; } if (form->toPattern(buffer) != testResultPatterns[i]) { errln(UnicodeString("TestMessageFormat::PatternTest failed test #2, i = ") + i); //form->toPattern(buffer); errln(((UnicodeString)" Orig: ") + testCases[i]); errln(((UnicodeString)" Exp: ") + testResultPatterns[i]); errln(((UnicodeString)" Got: ") + buffer); } //it_out << "Pat out: " << form->toPattern(buffer)); UnicodeString result; int32_t count = 4; FieldPosition fieldpos(0); form->format(testArgs, count, result, fieldpos, success); if (U_FAILURE(success)) { errln("MessageFormat failed test #3"); logln("TestMessageFormat::PatternTest failed test #3"); continue; } if (result != testResultStrings[i]) { errln("TestMessageFormat::PatternTest failed test #4"); logln("TestMessageFormat::PatternTest failed #4."); logln(UnicodeString(" Result: ") + result ); logln(UnicodeString(" 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 delete form; } }