]> granicus.if.org Git - icu/commitdiff
ICU-9294 test & fix MessageFormat::getFormatNames()
authorMarkus Scherer <markus.icu@gmail.com>
Sun, 7 Oct 2012 01:27:19 +0000 (01:27 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Sun, 7 Oct 2012 01:27:19 +0000 (01:27 +0000)
X-SVN-Rev: 32540

icu4c/source/i18n/msgfmt.cpp
icu4c/source/test/intltest/tmsgfmt.cpp
icu4c/source/test/intltest/tmsgfmt.h

index 5be6bc5bfe44e2c65d008a3aa8dfd7f498ae8e5a..de28260a47c7467baafbe6abed664b5ce0964835 100644 (file)
@@ -851,7 +851,7 @@ MessageFormat::getFormatNames(UErrorCode& status) {
     fFormatNames->setDeleter(uprv_deleteUObject);
 
     for (int32_t partIndex = 0; (partIndex = nextTopLevelArgStart(partIndex)) >= 0;) {
-        fFormatNames->addElement(new UnicodeString(getArgName(partIndex)), status);
+        fFormatNames->addElement(new UnicodeString(getArgName(partIndex + 1)), status);
     }
 
     StringEnumeration* nameEnumerator = new FormatNameEnumeration(fFormatNames, status);
index 41371a561c91b187de486470ba91eb108766ee82..b45f7d003e2897cfc7bca9d37295ed71d7b6408f 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "unicode/format.h"
 #include "unicode/decimfmt.h"
+#include "unicode/localpointer.h"
 #include "unicode/locid.h"
 #include "unicode/msgfmt.h"
 #include "unicode/numfmt.h"
@@ -63,6 +64,7 @@ TestMessageFormat::runIndexedTest(int32_t index, UBool exec,
     TESTCASE_AUTO(TestApostropheMode);
     TESTCASE_AUTO(TestCompatibleApostrophe);
     TESTCASE_AUTO(testCoverage);
+    TESTCASE_AUTO(testGetFormatNames);
     TESTCASE_AUTO(TestTrimArgumentName);
     TESTCASE_AUTO(TestSelectOrdinal);
     TESTCASE_AUTO_END;
@@ -1792,6 +1794,51 @@ void TestMessageFormat::testCoverage(void) {
     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.logIfFailureAndReset("MessageFormat() failed")) {
+        return;
+    }
+    LocalPointer<StringEnumeration> names(msgfmt.getFormatNames(errorCode));
+    if(errorCode.logIfFailureAndReset("msgfmt.getFormatNames() failed")) {
+        return;
+    }
+    const UnicodeString *name;
+    name = names->snext(errorCode);
+    if (name == NULL || 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 == NULL || 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 == NULL || 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 != NULL) {
+        errln(UnicodeString("msgfmt.getFormatNames()[3] should be NULL but is: ") + *name);
+        return;
+    }
+}
+
 void TestMessageFormat::TestTrimArgumentName() {
     // ICU 4.8 allows and ignores white space around argument names and numbers.
     IcuTestErrorCode errorCode(*this, "TestTrimArgumentName");
index 2e68da3cb31964d925239990697a2fdc756a1c2e..63dbf77b07dc81f7153f290f4153940c705d241c 100644 (file)
@@ -113,12 +113,11 @@ public:
     void testAdopt(void);
     void TestTurkishCasing(void);
     void testAutoQuoteApostrophe(void);
+    void testCoverage();
+    void testGetFormatNames();
     void TestTrimArgumentName();
     void TestSelectOrdinal();
 
-    /* Provide better code coverage */
-    void testCoverage(void);
-
 private:
     UnicodeString GetPatternAndSkipSyntax(const MessagePattern& pattern);
 };