Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Unified Diff: chrome/common/plural_formatter_unittest.cc

Issue 6736003: This adds a formatter for plurals that works for all locales. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing bogus DCHECK Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/plural_formatter_unittest.cc
diff --git a/chrome/common/plural_formatter_unittest.cc b/chrome/common/plural_formatter_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..71d1189724d7a0a4057dfb6bb585869afc8fe2c8
--- /dev/null
+++ b/chrome/common/plural_formatter_unittest.cc
@@ -0,0 +1,335 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/basictypes.h"
+#include "base/string16.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/common/plural_formatter.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+#include "ui/base/l10n/l10n_util.h"
+
+namespace {
+
+enum MsgId {
+ MSG_ZERO = 0,
+ MSG_ONE,
+ MSG_TWO,
+ MSG_FEW,
+ MSG_MANY,
+ MSG_OTHER
+};
+
+struct MsgCode {
+ int id;
+ int test_value;
+ const char *format;
+ const char *expected_string;
+};
+
+const int message_ids[] = {
+ MSG_ZERO,
+ MSG_ONE,
+ MSG_TWO,
+ MSG_FEW,
+ MSG_MANY,
+ MSG_OTHER
+};
+
+const MsgCode codes[] = {
+ { MSG_ZERO, 0, "zero = # is zero", "zero = 0 is zero" },
+ { MSG_ONE, 1, "one = # is one", "one = 1 is one" },
+ { MSG_TWO, 2, "two = # is a pair", "two = 2 is a pair" },
+ { MSG_FEW, 3, "few = # is a few", "few = 3 is a few" },
+ { MSG_MANY, 15, "many = # is many", "many = 15 is many" },
+ { MSG_OTHER, 123, "number = # is a lot", "number = 123 is a lot" }
+};
+
+std::string GetTestString(int msg_id) {
+ return codes[msg_id].format;
+}
+
+std::string GetExpectedString(int msg_id) {
+ return codes[msg_id].expected_string;
+}
+
+} // namespace
+
+// Icu could be doing some platform-specific things, so we make this a
+// platform test.
+class PluralFormatterTest : public PlatformTest {
+ protected:
+ virtual void SetUp() {
+ PlatformTest::SetUp();
+ PluralFormatter::SetOverrideLocale("en");
+ PluralFormatter::SetStringSource(GetTestString);
+ }
+
+ virtual void TearDown() {
+ PluralFormatter::SetOverrideLocale(NULL);
+ PluralFormatter::SetStringSource(l10n_util::GetStringUTF8);
+ PlatformTest::TearDown();
+ }
+};
+
+#define TEST_MESSAGE_USED(msg) \
Paweł Hajdan Jr. 2011/03/25 09:32:19 Do we really need to have macros for those? How ab
Greg Spencer (Chromium) 2011/03/25 23:05:31 Good point! Done.
+{ \
+ std::string result = UTF16ToUTF8( \
+ formatter.GetPluralString(codes[msg].test_value)); \
+ std::string expected = GetExpectedString(msg); \
+ EXPECT_STREQ(expected.c_str(), result.c_str()); \
+}
+
+#define TEST_MESSAGE_NOT_USED(msg) \
+{ \
+ std::string result = UTF16ToUTF8( \
+ formatter.GetPluralString(codes[msg].test_value)); \
+ std::string expected = GetExpectedString(msg); \
+ EXPECT_STRNE(expected.c_str(), result.c_str()); \
+}
+
+TEST_F(PluralFormatterTest, Basic) {
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_USED(MSG_OTHER);
+}
+
+// Testing the following locales: en fr ar cs ga hr ja kn ko lt lv pl ro ru
+// sk sl tr uk vi zh_CN zh_TW
+//
+// Most of these are locales that have interesting rules regarding
+// plurals (and I threw in en and fr just to make sure nothing's wonky
+// with simpler locales).
+
+TEST_F(PluralFormatterTest, TestLocales) {
+ {
+ PluralFormatter::SetOverrideLocale("en");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("fr");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ar");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_USED(MSG_MANY);
+ TEST_MESSAGE_NOT_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("cs");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ga");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("hr");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_USED(MSG_MANY);
+ TEST_MESSAGE_NOT_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ja");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("kn");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ko");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("lt");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_NOT_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("lv");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("pl");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_USED(MSG_MANY);
+ TEST_MESSAGE_NOT_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ro");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ru");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_USED(MSG_MANY);
+ TEST_MESSAGE_NOT_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("sk");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("sl");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("tr");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("uk");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_USED(MSG_FEW);
+ TEST_MESSAGE_USED(MSG_MANY);
+ TEST_MESSAGE_NOT_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("vi");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("zh_CN");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("zh_TW");
+ PluralFormatter formatter(message_ids);
+ TEST_MESSAGE_NOT_USED(MSG_ZERO);
+ TEST_MESSAGE_NOT_USED(MSG_ONE);
+ TEST_MESSAGE_NOT_USED(MSG_TWO);
+ TEST_MESSAGE_NOT_USED(MSG_FEW);
+ TEST_MESSAGE_NOT_USED(MSG_MANY);
+ TEST_MESSAGE_USED(MSG_OTHER);
+ }
+}
« chrome/common/plural_formatter.cc ('K') | « chrome/common/plural_formatter_example.grd ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698