diff --git a/src/kits/locale/MessageFormat.cpp b/src/kits/locale/MessageFormat.cpp index e2e249d895..ccb3bab07d 100644 --- a/src/kits/locale/MessageFormat.cpp +++ b/src/kits/locale/MessageFormat.cpp @@ -77,6 +77,9 @@ BMessageFormat::SetFormattingConventions( status_t BMessageFormat::Format(BString& output, const int32 arg) const { + if (fInitStatus != B_OK) + return fInitStatus; + BAutolock lock(fLock); if (!lock.IsLocked()) return B_ERROR; diff --git a/src/tests/kits/locale/MessageFormatTest.cpp b/src/tests/kits/locale/MessageFormatTest.cpp index e91b5f07fd..47265f18bf 100644 --- a/src/tests/kits/locale/MessageFormatTest.cpp +++ b/src/tests/kits/locale/MessageFormatTest.cpp @@ -34,7 +34,7 @@ MessageFormatTest::TestFormat() int32 number; const char* expected; }; - + static const char* polishTemplate = "{0, plural, one{Wybrano # obiekt} " "few{Wybrano # obiekty} many{Wybrano # obiektów} " "other{Wybrano # obyektu}}"; @@ -64,6 +64,40 @@ MessageFormatTest::TestFormat() } +void +MessageFormatTest::TestBogus() +{ + struct Test { + const char* pattern; + }; + + static const Test tests[] = { + { "{0, plural, one{# dog} other{# dogs}" }, // Missing closing brace + { "{0, plural, one{# dog}, other{# dogs}}" }, // Extra comma + { "{0, plural, one{# dog}" }, // Missing "other" + { "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg + { "{0, invalid, one{# dog} other{# dogs}}" }, // Invalid rule + { NULL } + }; + + for (int i = 0; tests[i].pattern != NULL; i++) { + NextSubTest(); + + status_t result; + BString output; + + BMessageFormat formatter(tests[i].pattern); + CPPUNIT_ASSERT(formatter.InitCheck() != B_OK); + + result = formatter.Format(output, 1); + CPPUNIT_ASSERT(result != B_OK); + + result = formatter.Format(output, 2); + CPPUNIT_ASSERT(result != B_OK); + } +} + + /*static*/ void MessageFormatTest::AddTests(BTestSuite& parent) { @@ -71,6 +105,8 @@ MessageFormatTest::AddTests(BTestSuite& parent) suite.addTest(new CppUnit::TestCaller( "MessageFormatTest::TestFormat", &MessageFormatTest::TestFormat)); + suite.addTest(new CppUnit::TestCaller( + "MessageFormatTest::TestBogus", &MessageFormatTest::TestBogus)); parent.addTest("MessageFormatTest", &suite); } diff --git a/src/tests/kits/locale/MessageFormatTest.h b/src/tests/kits/locale/MessageFormatTest.h index 924f679623..4e3c56d751 100644 --- a/src/tests/kits/locale/MessageFormatTest.h +++ b/src/tests/kits/locale/MessageFormatTest.h @@ -16,6 +16,7 @@ public: virtual ~MessageFormatTest(); void TestFormat(); + void TestBogus(); static void AddTests(BTestSuite& suite); };