OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/basictypes.h" |
| 6 #include "base/string16.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/common/plural_formatter.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/platform_test.h" |
| 11 #include "ui/base/l10n/l10n_util.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 enum MsgId { |
| 16 MSG_ZERO = 0, |
| 17 MSG_ONE, |
| 18 MSG_TWO, |
| 19 MSG_FEW, |
| 20 MSG_MANY, |
| 21 MSG_OTHER |
| 22 }; |
| 23 |
| 24 struct MsgCode { |
| 25 int id; |
| 26 int test_value; |
| 27 const char *format; |
| 28 const char *expected_string; |
| 29 }; |
| 30 |
| 31 const int message_ids[] = { |
| 32 MSG_ZERO, |
| 33 MSG_ONE, |
| 34 MSG_TWO, |
| 35 MSG_FEW, |
| 36 MSG_MANY, |
| 37 MSG_OTHER |
| 38 }; |
| 39 |
| 40 const MsgCode codes[] = { |
| 41 { MSG_ZERO, 0, "zero = # is zero", "zero = 0 is zero" }, |
| 42 { MSG_ONE, 1, "one = # is one", "one = 1 is one" }, |
| 43 { MSG_TWO, 2, "two = # is a pair", "two = 2 is a pair" }, |
| 44 { MSG_FEW, 3, "few = # is a few", "few = 3 is a few" }, |
| 45 { MSG_MANY, 15, "many = # is many", "many = 15 is many" }, |
| 46 { MSG_OTHER, 123, "number = # is a lot", "number = 123 is a lot" } |
| 47 }; |
| 48 |
| 49 std::string GetTestString(int msg_id) { |
| 50 return codes[msg_id].format; |
| 51 } |
| 52 |
| 53 std::string GetExpectedString(int msg_id) { |
| 54 return codes[msg_id].expected_string; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 // Icu could be doing some platform-specific things, so we make this a |
| 60 // platform test. |
| 61 class PluralFormatterTest : public PlatformTest { |
| 62 protected: |
| 63 virtual void SetUp() { |
| 64 PlatformTest::SetUp(); |
| 65 PluralFormatter::SetOverrideLocale("en"); |
| 66 PluralFormatter::SetStringSource(GetTestString); |
| 67 } |
| 68 |
| 69 virtual void TearDown() { |
| 70 PluralFormatter::SetOverrideLocale(NULL); |
| 71 PluralFormatter::SetStringSource(l10n_util::GetStringUTF8); |
| 72 PlatformTest::TearDown(); |
| 73 } |
| 74 |
| 75 static bool TestMessageUsed(const PluralFormatter& formatter, |
| 76 int message_id) { |
| 77 std::string result = UTF16ToUTF8( |
| 78 formatter.GetPluralString(codes[message_id].test_value)); |
| 79 std::string expected = GetExpectedString(message_id); |
| 80 return expected == result; |
| 81 } |
| 82 }; |
| 83 |
| 84 TEST_F(PluralFormatterTest, Basic) { |
| 85 PluralFormatter formatter; |
| 86 ASSERT_TRUE(formatter.Init(message_ids)); |
| 87 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 88 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 89 |
| 90 PluralFormatter formatter1; |
| 91 EXPECT_TRUE(formatter1.Init(MSG_ZERO, |
| 92 MSG_ONE, |
| 93 MSG_TWO, |
| 94 MSG_FEW, |
| 95 MSG_MANY, |
| 96 MSG_OTHER)); |
| 97 EXPECT_TRUE(TestMessageUsed(formatter1, MSG_ONE)); |
| 98 EXPECT_TRUE(TestMessageUsed(formatter1, MSG_OTHER)); |
| 99 } |
| 100 |
| 101 // Testing the following locales: en fr ar cs ga hr ja kn ko lt lv pl |
| 102 // ro ru sk sl tr uk vi zh_CN zh_TW |
| 103 // |
| 104 // Most of these are locales that have interesting rules regarding |
| 105 // plurals (and I threw in en and fr just to make sure nothing's wonky |
| 106 // with simpler locales). |
| 107 TEST_F(PluralFormatterTest, TestLocales) { |
| 108 { |
| 109 PluralFormatter::SetOverrideLocale("en"); |
| 110 PluralFormatter formatter; |
| 111 ASSERT_TRUE(formatter.Init(message_ids)); |
| 112 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 113 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 114 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 115 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 116 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 117 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 118 } |
| 119 |
| 120 { |
| 121 PluralFormatter::SetOverrideLocale("fr"); |
| 122 PluralFormatter formatter; |
| 123 ASSERT_TRUE(formatter.Init(message_ids)); |
| 124 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 125 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 126 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 127 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 128 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 129 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 130 } |
| 131 |
| 132 { |
| 133 PluralFormatter::SetOverrideLocale("ar"); |
| 134 PluralFormatter formatter; |
| 135 ASSERT_TRUE(formatter.Init(message_ids)); |
| 136 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ZERO)); |
| 137 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 138 EXPECT_TRUE(TestMessageUsed(formatter, MSG_TWO)); |
| 139 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 140 EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY)); |
| 141 EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER)); |
| 142 } |
| 143 |
| 144 { |
| 145 PluralFormatter::SetOverrideLocale("cs"); |
| 146 PluralFormatter formatter; |
| 147 ASSERT_TRUE(formatter.Init(message_ids)); |
| 148 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 149 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 150 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 151 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 152 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 153 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 154 } |
| 155 |
| 156 { |
| 157 PluralFormatter::SetOverrideLocale("ga"); |
| 158 PluralFormatter formatter; |
| 159 ASSERT_TRUE(formatter.Init(message_ids)); |
| 160 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 161 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 162 EXPECT_TRUE(TestMessageUsed(formatter, MSG_TWO)); |
| 163 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 164 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 165 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 166 } |
| 167 |
| 168 { |
| 169 PluralFormatter::SetOverrideLocale("hr"); |
| 170 PluralFormatter formatter; |
| 171 ASSERT_TRUE(formatter.Init(message_ids)); |
| 172 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 173 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 174 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 175 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 176 EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY)); |
| 177 EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER)); |
| 178 } |
| 179 |
| 180 { |
| 181 PluralFormatter::SetOverrideLocale("ja"); |
| 182 PluralFormatter formatter; |
| 183 ASSERT_TRUE(formatter.Init(message_ids)); |
| 184 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 185 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 186 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 187 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 188 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 189 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 190 } |
| 191 |
| 192 { |
| 193 PluralFormatter::SetOverrideLocale("kn"); |
| 194 PluralFormatter formatter; |
| 195 ASSERT_TRUE(formatter.Init(message_ids)); |
| 196 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 197 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 198 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 199 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 200 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 201 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 202 } |
| 203 |
| 204 { |
| 205 PluralFormatter::SetOverrideLocale("ko"); |
| 206 PluralFormatter formatter; |
| 207 ASSERT_TRUE(formatter.Init(message_ids)); |
| 208 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 209 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 210 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 211 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 212 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 213 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 214 } |
| 215 |
| 216 { |
| 217 PluralFormatter::SetOverrideLocale("lt"); |
| 218 PluralFormatter formatter; |
| 219 ASSERT_TRUE(formatter.Init(message_ids)); |
| 220 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 221 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 222 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 223 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 224 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 225 EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER)); |
| 226 } |
| 227 |
| 228 { |
| 229 PluralFormatter::SetOverrideLocale("lv"); |
| 230 PluralFormatter formatter; |
| 231 ASSERT_TRUE(formatter.Init(message_ids)); |
| 232 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ZERO)); |
| 233 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 234 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 235 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 236 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 237 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 238 } |
| 239 |
| 240 { |
| 241 PluralFormatter::SetOverrideLocale("pl"); |
| 242 PluralFormatter formatter; |
| 243 ASSERT_TRUE(formatter.Init(message_ids)); |
| 244 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 245 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 246 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 247 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 248 EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY)); |
| 249 EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER)); |
| 250 } |
| 251 |
| 252 { |
| 253 PluralFormatter::SetOverrideLocale("ro"); |
| 254 PluralFormatter formatter; |
| 255 ASSERT_TRUE(formatter.Init(message_ids)); |
| 256 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 257 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 258 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 259 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 260 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 261 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 262 } |
| 263 |
| 264 { |
| 265 PluralFormatter::SetOverrideLocale("ru"); |
| 266 PluralFormatter formatter; |
| 267 ASSERT_TRUE(formatter.Init(message_ids)); |
| 268 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 269 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 270 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 271 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 272 EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY)); |
| 273 EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER)); |
| 274 } |
| 275 |
| 276 { |
| 277 PluralFormatter::SetOverrideLocale("sk"); |
| 278 PluralFormatter formatter; |
| 279 ASSERT_TRUE(formatter.Init(message_ids)); |
| 280 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 281 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 282 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 283 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 284 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 285 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 286 } |
| 287 |
| 288 { |
| 289 PluralFormatter::SetOverrideLocale("sl"); |
| 290 PluralFormatter formatter; |
| 291 ASSERT_TRUE(formatter.Init(message_ids)); |
| 292 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 293 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 294 EXPECT_TRUE(TestMessageUsed(formatter, MSG_TWO)); |
| 295 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 296 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 297 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 298 } |
| 299 |
| 300 { |
| 301 PluralFormatter::SetOverrideLocale("tr"); |
| 302 PluralFormatter formatter; |
| 303 ASSERT_TRUE(formatter.Init(message_ids)); |
| 304 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 305 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 306 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 307 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 308 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 309 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 310 } |
| 311 |
| 312 { |
| 313 PluralFormatter::SetOverrideLocale("uk"); |
| 314 PluralFormatter formatter; |
| 315 ASSERT_TRUE(formatter.Init(message_ids)); |
| 316 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 317 EXPECT_TRUE(TestMessageUsed(formatter, MSG_ONE)); |
| 318 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 319 EXPECT_TRUE(TestMessageUsed(formatter, MSG_FEW)); |
| 320 EXPECT_TRUE(TestMessageUsed(formatter, MSG_MANY)); |
| 321 EXPECT_FALSE(TestMessageUsed(formatter, MSG_OTHER)); |
| 322 } |
| 323 |
| 324 { |
| 325 PluralFormatter::SetOverrideLocale("vi"); |
| 326 PluralFormatter formatter; |
| 327 ASSERT_TRUE(formatter.Init(message_ids)); |
| 328 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 329 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 330 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 331 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 332 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 333 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 334 } |
| 335 |
| 336 { |
| 337 PluralFormatter::SetOverrideLocale("zh_CN"); |
| 338 PluralFormatter formatter; |
| 339 ASSERT_TRUE(formatter.Init(message_ids)); |
| 340 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 341 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 342 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 343 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 344 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 345 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 346 } |
| 347 |
| 348 { |
| 349 PluralFormatter::SetOverrideLocale("zh_TW"); |
| 350 PluralFormatter formatter; |
| 351 ASSERT_TRUE(formatter.Init(message_ids)); |
| 352 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ZERO)); |
| 353 EXPECT_FALSE(TestMessageUsed(formatter, MSG_ONE)); |
| 354 EXPECT_FALSE(TestMessageUsed(formatter, MSG_TWO)); |
| 355 EXPECT_FALSE(TestMessageUsed(formatter, MSG_FEW)); |
| 356 EXPECT_FALSE(TestMessageUsed(formatter, MSG_MANY)); |
| 357 EXPECT_TRUE(TestMessageUsed(formatter, MSG_OTHER)); |
| 358 } |
| 359 } |
OLD | NEW |