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

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: upload after rebase Created 9 years, 2 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
« no previous file with comments | « chrome/common/plural_formatter_example.grd ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..f1a3717965f886e3ad91ede11065f7ec4268b83b
--- /dev/null
+++ b/chrome/common/plural_formatter_unittest.cc
@@ -0,0 +1,359 @@
+// 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();
+ }
+
+ static bool TestMessageUsed(const PluralFormatter& formatter,
+ int message_id) {
+ std::string result = UTF16ToUTF8(
+ formatter.GetPluralString(codes[message_id].test_value));
+ std::string expected = GetExpectedString(message_id);
+ return expected == result;
+ }
+};
+
+TEST_F(PluralFormatterTest, Basic) {
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+
+ PluralFormatter formatter1;
+ EXPECT_TRUE(formatter1.Init(MSG_ZERO,
+ MSG_ONE,
+ MSG_TWO,
+ MSG_FEW,
+ MSG_MANY,
+ MSG_OTHER));
+ EXPECT_TRUE(TestMessageUsed(formatter1, MSG_ONE));
+ EXPECT_TRUE(TestMessageUsed(formatter1, 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;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("fr");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ar");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("cs");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ga");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("hr");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ja");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("kn");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ko");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("lt");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("lv");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("pl");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ro");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("ru");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("sk");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("sl");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("tr");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("uk");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("vi");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("zh_CN");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+
+ {
+ PluralFormatter::SetOverrideLocale("zh_TW");
+ PluralFormatter formatter;
+ ASSERT_TRUE(formatter.Init(message_ids));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW));
+ EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY));
+ EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER));
+ }
+}
« no previous file with comments | « chrome/common/plural_formatter_example.grd ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698