diff --git a/icu4c/source/i18n/msgfmt.cpp b/icu4c/source/i18n/msgfmt.cpp index 5be6bc5bfe4..de28260a47c 100644 --- a/icu4c/source/i18n/msgfmt.cpp +++ b/icu4c/source/i18n/msgfmt.cpp @@ -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); diff --git a/icu4c/source/test/intltest/tmsgfmt.cpp b/icu4c/source/test/intltest/tmsgfmt.cpp index 41371a561c9..b45f7d003e2 100644 --- a/icu4c/source/test/intltest/tmsgfmt.cpp +++ b/icu4c/source/test/intltest/tmsgfmt.cpp @@ -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 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"); diff --git a/icu4c/source/test/intltest/tmsgfmt.h b/icu4c/source/test/intltest/tmsgfmt.h index 2e68da3cb31..63dbf77b07d 100644 --- a/icu4c/source/test/intltest/tmsgfmt.h +++ b/icu4c/source/test/intltest/tmsgfmt.h @@ -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); };