+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 ")+(int32_t)err);
+ return;
+ }
+ Formattable testArgs1((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 ")+(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((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 ")+(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 ")+(int32_t)err);
+ delete msgFmt;
+ return;
+ }
+ Formattable testArgs3((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[] = { (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
+ 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 ")+(int32_t)status);
+ return NULL;
+ }
+ return msgFmt;
+}
+
+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",(int32_t)6};
+ Formattable testArgs11[] = {"Kirti","female",100.100};
+ Formattable testArgs12[] = {"Kirti","other",(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");
+ if (!U_FAILURE(err)) {
+ //Arguments
+ Formattable testArgs10[] = {"Kirti",(int32_t)6,"female"};
+ Formattable testArgs11[] = {"Kirti",(int32_t)1,"female"};
+ Formattable testArgs12[] = {"Ash",(int32_t)1,"other"};
+ Formattable testArgs13[] = {"Ash",(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
+ MessageFormat* msgFmt6 = internalCreate(t6, Locale("de"),err,(char*)"From TestMessageFormat::TestSelectFormat create t6");
+ if (!U_FAILURE(err)) {
+ //Arguments
+ Formattable testArgs10[] = {"Kirti","other",(int32_t)1,"other"};
+ Formattable testArgs11[] = {"Kirti","other",(int32_t)6,"other"};
+ Formattable testArgs12[] = {"Kirti","other",(int32_t)1,"female"};
+ Formattable testArgs13[] = {"Kirti","other",(int32_t)3,"female"};
+ Formattable testArgs14[] = {"Kirti","female",(int32_t)1,"female"};
+ Formattable testArgs15[] = {"Kirti","female",(int32_t)5,"female"};
+ Formattable testArgs16[] = {"Kirti","female",(int32_t)1,"other"};
+ Formattable testArgs17[] = {"Kirti","female",(int32_t)5,"other"};
+ Formattable testArgs18[] = {"Kirti","mixed",(int32_t)1,"mixed"};
+ Formattable testArgs19[] = {"Kirti","mixed",(int32_t)1,"other"};
+ Formattable testArgs20[] = {"Kirti","female",(int32_t)1,"mixed"};
+ Formattable testArgs21[] = {"Kirti","mixed",(int32_t)5,"mixed"};
+ Formattable testArgs22[] = {"Kirti","mixed",(int32_t)5,"other"};
+ Formattable testArgs23[] = {"Kirti","female",(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 , testArgs[i], 4, exp[i] ,(char*)"From TestMessageFormat::testSelectFormat format t6");
+ }
+ }
+ delete msgFmt6;
+}
+