void formatErrorMessage(UErrorCode &realStatus, const UnicodeString& pattern, const Locale& theLocale, UErrorCode inStatus0, /* statusString 1 */ const Locale &inCountry2, double currency3, // these numbers are the message arguments. UnicodeString &result) { if(U_FAILURE(realStatus)) return; // you messed up UnicodeString errString1(u_errorName(inStatus0)); UnicodeString countryName2; inCountry2.getDisplayCountry(theLocale,countryName2); Formattable myArgs[] = { Formattable((int32_t)inStatus0), // inStatus0 {0} Formattable(errString1), // statusString1 {1} Formattable(countryName2), // inCountry2 {2} Formattable(currency3)// currency3 {3,number,currency} }; MessageFormat *fmt = new MessageFormat("MessageFormat's API is broken!!!!!!!!!!!",realStatus); fmt->setLocale(theLocale); fmt->applyPattern(pattern, realStatus); if (U_FAILURE(realStatus)) { delete fmt; return; } FieldPosition ignore = 0; fmt->format(myArgs,4,result,ignore,realStatus); delete fmt; }
void MessageFormatRegressionTest::Test4116444() { UnicodeString patterns [] = { (UnicodeString)"", (UnicodeString)"one", (UnicodeString) "{0,date,short}" }; UErrorCode status = U_ZERO_ERROR; MessageFormat *mf = new MessageFormat("", status); failure(status, "new MessageFormat"); for (int i = 0; i < 3; i++) { UnicodeString pattern = patterns[i]; mf->applyPattern(pattern, status); failure(status, "mf->applyPattern", TRUE); //try { int32_t count = 0; ParsePosition pp(0); Formattable *array = mf->parse(UnicodeString(""), pp, count); logln("pattern: \"" + pattern + "\""); log(" parsedObjects: "); if (array != NULL) { log("{"); for (int j = 0; j < count; j++) { //if (array[j] != null) UnicodeString dummy; err("\"" + array[j].getString(dummy) + "\""); //else // log("null"); if (j < count- 1) log(","); } log("}") ; delete[] array; } else { log("null"); } logln(""); /*} catch (Exception e) { errln("pattern: \"" + pattern + "\""); errln(" Exception: " + e.getMessage()); }*/ } delete mf; }
void MessageFormatRegressionTest::Test4118594() { UErrorCode status = U_ZERO_ERROR; const UBool possibleDataError = TRUE; MessageFormat *mf = new MessageFormat("{0}, {0}, {0}", status); failure(status, "new MessageFormat"); UnicodeString forParsing("x, y, z"); //Object[] objs = mf.parse(forParsing, new ParsePosition(0)); int32_t count = 0; ParsePosition pp(0); Formattable *objs = mf->parse(forParsing, pp, count); UnicodeString pat; logln("pattern: \"" + mf->toPattern(pat) + "\""); logln("text for parsing: \"" + forParsing + "\""); UnicodeString str; if (objs[0].getString(str) != "z") errln("argument0: \"" + objs[0].getString(str) + "\""); mf->applyPattern("{0,number,#.##}, {0,number,#.#}", status); failure(status, "mf->applyPattern", possibleDataError); //Object[] oldobjs = {new Double(3.1415)}; Formattable oldobjs [] = {Formattable(3.1415)}; UnicodeString result; FieldPosition pos(FieldPosition::DONT_CARE); result = mf->format( oldobjs, 1, result, pos, status ); failure(status, "mf->format", possibleDataError); pat.remove(); logln("pattern: \"" + mf->toPattern(pat) + "\""); logln("text for parsing: \"" + result + "\""); // result now equals "3.14, 3.1" if (result != "3.14, 3.1") dataerrln("result = " + result + " - " + u_errorName(status)); //Object[] newobjs = mf.parse(result, new ParsePosition(0)); int32_t count1 = 0; pp.setIndex(0); Formattable *newobjs = mf->parse(result, pp, count1); // newobjs now equals {new Double(3.1)} if (newobjs == NULL) { dataerrln("Error calling MessageFormat::parse"); } else { if (newobjs[0].getDouble() != 3.1) errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble()); } delete [] objs; delete [] newobjs; delete mf; }
void MessageFormatRegressionTest::Test4114743() { UnicodeString originalPattern("initial pattern"); UErrorCode status = U_ZERO_ERROR; MessageFormat *mf = new MessageFormat(originalPattern, status); failure(status, "new MessageFormat"); //try { UnicodeString illegalPattern("ab { '}' de"); mf->applyPattern(illegalPattern, status); if( ! U_FAILURE(status)) errln("illegal pattern: \"" + illegalPattern + "\""); /*} catch (IllegalArgumentException foo) { if (!originalPattern.equals(mf.toPattern())) errln("pattern after: \"" + mf.toPattern() + "\""); }*/ delete mf; }
void MessageFormatRegressionTest::Test4113018() { UnicodeString originalPattern("initial pattern"); UErrorCode status = U_ZERO_ERROR; MessageFormat *mf = new MessageFormat(originalPattern, status); failure(status, "new messageFormat"); UnicodeString illegalPattern("format: {0, xxxYYY}"); UnicodeString pat; logln("pattern before: \"" + mf->toPattern(pat) + "\""); logln("illegal pattern: \"" + illegalPattern + "\""); //try { mf->applyPattern(illegalPattern, status); if( ! U_FAILURE(status)) errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern); /*} catch (IllegalArgumentException e) { if (!originalPattern.equals(mf.toPattern())) errln("pattern after: \"" + mf.toPattern() + "\""); }*/ delete mf; }
void MessageFormatRegressionTest::Test4118592() { UErrorCode status = U_ZERO_ERROR; MessageFormat *mf = new MessageFormat("", status); failure(status, "new messageFormat"); UnicodeString pattern("{0,choice,1#YES|2#NO}"); UnicodeString prefix(""); Formattable *objs = 0; for (int i = 0; i < 5; i++) { UnicodeString formatted; formatted = prefix + "YES"; mf->applyPattern(prefix + pattern, status); failure(status, "mf->applyPattern"); prefix += "x"; //Object[] objs = mf.parse(formatted, new ParsePosition(0)); int32_t count = 0; ParsePosition pp(0); objs = mf->parse(formatted, pp, count); UnicodeString pat; logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\""); log(" \"" + formatted + "\" parsed as "); if (objs == NULL) logln(" null"); else { UnicodeString temp; if(objs[0].getType() == Formattable::kString) logln((UnicodeString)" " + objs[0].getString(temp)); else logln((UnicodeString)" " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble())); delete[] objs; } } delete mf; }
void MessageFormatRegressionTest::Test4074764() { UnicodeString pattern [] = { "Message without param", "Message with param:{0}", "Longer Message with param {0}" }; //difference between the two param strings are that //in the first one, the param position is within the //length of the string without param while it is not so //in the other case. UErrorCode status = U_ZERO_ERROR; MessageFormat *messageFormatter = new MessageFormat("", status); failure(status, "couldn't create MessageFormat"); //try { //Apply pattern with param and print the result messageFormatter->applyPattern(pattern[1], status); failure(status, "messageFormat->applyPattern"); //Object[] params = {new UnicodeString("BUG"), new Date()}; Formattable params [] = { Formattable(UnicodeString("BUG")), Formattable(0, Formattable::kIsDate) }; UnicodeString tempBuffer; FieldPosition pos(FieldPosition::DONT_CARE); tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status); if( tempBuffer != "Message with param:BUG" || failure(status, "messageFormat->format")) errln("MessageFormat with one param test failed."); logln("Formatted with one extra param : " + tempBuffer); //Apply pattern without param and print the result messageFormatter->applyPattern(pattern[0], status); failure(status, "messageFormatter->applyPattern"); // {sfb} how much does this apply in C++? // do we want to verify that the Formattable* array is not NULL, // or is that the user's responsibility? // additionally, what should be the item count? // for bug testing purposes, assume that something was set to // NULL by mistake, and that the length should be non-zero //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status); tempBuffer.remove(); tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status); if( tempBuffer != "Message without param" || failure(status, "messageFormat->format")) errln("MessageFormat with no param test failed."); logln("Formatted with no params : " + tempBuffer); tempBuffer.remove(); tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status); if (tempBuffer != "Message without param" || failure(status, "messageFormat->format")) errln("Formatted with arguments > subsitution failed. result = " + tempBuffer); logln("Formatted with extra params : " + tempBuffer); //This statement gives an exception while formatting... //If we use pattern[1] for the message with param, //we get an NullPointerException in MessageFormat.java(617) //If we use pattern[2] for the message with param, //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614) //Both are due to maxOffset not being reset to -1 //in applyPattern() when the pattern does not //contain any param. /*} catch (Exception foo) { errln("Exception when formatting with no params."); }*/ delete messageFormatter; }
void MessageFormatRegressionTest::Test4031438() { UErrorCode status = U_ZERO_ERROR; UnicodeString pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}."); UnicodeString pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'."); MessageFormat *messageFormatter = new MessageFormat("", status); failure(status, "new MessageFormat"); const UBool possibleDataError = TRUE; //try { logln("Apply with pattern : " + pattern1); messageFormatter->applyPattern(pattern1, status); failure(status, "messageFormat->applyPattern"); //Object[] params = {new Integer(7)}; Formattable params []= { Formattable((int32_t)7) }; UnicodeString tempBuffer; FieldPosition pos(FieldPosition::DONT_CARE); tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status); if(tempBuffer != "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status, "MessageFormat::format")) dataerrln("Tests arguments < substitution failed"); logln("Formatted with 7 : " + tempBuffer); ParsePosition pp(0); int32_t count = 0; Formattable *objs = messageFormatter->parse(tempBuffer, pp, count); //if(objs[7/*params.length*/] != NULL) // errln("Parse failed with more than expected arguments"); NumberFormat *fmt = 0; UnicodeString temp, temp1; for (int i = 0; i < count; i++) { // convert to string if not already Formattable obj = objs[i]; temp.remove(); if(obj.getType() == Formattable::kString) temp = obj.getString(temp); else { fmt = NumberFormat::createInstance(status); switch (obj.getType()) { case Formattable::kLong: fmt->format(obj.getLong(), temp); break; case Formattable::kInt64: fmt->format(obj.getInt64(), temp); break; case Formattable::kDouble: fmt->format(obj.getDouble(), temp); break; default: break; } } // convert to string if not already Formattable obj1 = params[i]; temp1.remove(); if(obj1.getType() == Formattable::kString) temp1 = obj1.getString(temp1); else { fmt = NumberFormat::createInstance(status); switch (obj1.getType()) { case Formattable::kLong: fmt->format(obj1.getLong(), temp1); break; case Formattable::kInt64: fmt->format(obj1.getInt64(), temp1); break; case Formattable::kDouble: fmt->format(obj1.getDouble(), temp1); break; default: break; } } //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) { if (temp != temp1) { errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i); } } delete fmt; delete [] objs; // {sfb} does this apply? no way to really pass a null Formattable, // only a null array /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status); if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format")) errln("Tests with no arguments failed"); logln("Formatted with null : " + tempBuffer);*/ logln("Apply with pattern : " + pattern2); messageFormatter->applyPattern(pattern2, status); failure(status, "messageFormatter->applyPattern", possibleDataError); tempBuffer.remove(); tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status); if (tempBuffer != "Double ' Quotes 7 test and quoted {1} test plus other {2} stuff.") dataerrln("quote format test (w/ params) failed. - %s", u_errorName(status)); logln("Formatted with params : " + tempBuffer); /*tempBuffer = messageFormatter->format(null); if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff.")) errln("quote format test (w/ null) failed."); logln("Formatted with null : " + tempBuffer); logln("toPattern : " + messageFormatter.toPattern());*/ /*} catch (Exception foo) { errln("Exception when formatting in bug 4031438. "+foo.getMessage()); }*/ delete messageFormatter; }