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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
76 #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.
77 { \
78 std::string result = UTF16ToUTF8( \
79 formatter.GetPluralString(codes[msg].test_value)); \
80 std::string expected = GetExpectedString(msg); \
81 EXPECT_STREQ(expected.c_str(), result.c_str()); \
82 }
83
84 #define TEST_MESSAGE_NOT_USED(msg) \
85 { \
86 std::string result = UTF16ToUTF8( \
87 formatter.GetPluralString(codes[msg].test_value)); \
88 std::string expected = GetExpectedString(msg); \
89 EXPECT_STRNE(expected.c_str(), result.c_str()); \
90 }
91
92 TEST_F(PluralFormatterTest, Basic) {
93 PluralFormatter formatter(message_ids);
94 TEST_MESSAGE_USED(MSG_OTHER);
95 }
96
97 // Testing the following locales: en fr ar cs ga hr ja kn ko lt lv pl ro ru
98 // sk sl tr uk vi zh_CN zh_TW
99 //
100 // Most of these are locales that have interesting rules regarding
101 // plurals (and I threw in en and fr just to make sure nothing's wonky
102 // with simpler locales).
103
104 TEST_F(PluralFormatterTest, TestLocales) {
105 {
106 PluralFormatter::SetOverrideLocale("en");
107 PluralFormatter formatter(message_ids);
108 TEST_MESSAGE_NOT_USED(MSG_ZERO);
109 TEST_MESSAGE_USED(MSG_ONE);
110 TEST_MESSAGE_NOT_USED(MSG_TWO);
111 TEST_MESSAGE_NOT_USED(MSG_FEW);
112 TEST_MESSAGE_NOT_USED(MSG_MANY);
113 TEST_MESSAGE_USED(MSG_OTHER);
114 }
115
116 {
117 PluralFormatter::SetOverrideLocale("fr");
118 PluralFormatter formatter(message_ids);
119 TEST_MESSAGE_NOT_USED(MSG_ZERO);
120 TEST_MESSAGE_USED(MSG_ONE);
121 TEST_MESSAGE_NOT_USED(MSG_TWO);
122 TEST_MESSAGE_NOT_USED(MSG_FEW);
123 TEST_MESSAGE_NOT_USED(MSG_MANY);
124 TEST_MESSAGE_USED(MSG_OTHER);
125 }
126
127 {
128 PluralFormatter::SetOverrideLocale("ar");
129 PluralFormatter formatter(message_ids);
130 TEST_MESSAGE_USED(MSG_ZERO);
131 TEST_MESSAGE_USED(MSG_ONE);
132 TEST_MESSAGE_USED(MSG_TWO);
133 TEST_MESSAGE_USED(MSG_FEW);
134 TEST_MESSAGE_USED(MSG_MANY);
135 TEST_MESSAGE_NOT_USED(MSG_OTHER);
136 }
137
138 {
139 PluralFormatter::SetOverrideLocale("cs");
140 PluralFormatter formatter(message_ids);
141 TEST_MESSAGE_NOT_USED(MSG_ZERO);
142 TEST_MESSAGE_USED(MSG_ONE);
143 TEST_MESSAGE_NOT_USED(MSG_TWO);
144 TEST_MESSAGE_USED(MSG_FEW);
145 TEST_MESSAGE_NOT_USED(MSG_MANY);
146 TEST_MESSAGE_USED(MSG_OTHER);
147 }
148
149 {
150 PluralFormatter::SetOverrideLocale("ga");
151 PluralFormatter formatter(message_ids);
152 TEST_MESSAGE_NOT_USED(MSG_ZERO);
153 TEST_MESSAGE_USED(MSG_ONE);
154 TEST_MESSAGE_USED(MSG_TWO);
155 TEST_MESSAGE_NOT_USED(MSG_FEW);
156 TEST_MESSAGE_NOT_USED(MSG_MANY);
157 TEST_MESSAGE_USED(MSG_OTHER);
158 }
159
160 {
161 PluralFormatter::SetOverrideLocale("hr");
162 PluralFormatter formatter(message_ids);
163 TEST_MESSAGE_NOT_USED(MSG_ZERO);
164 TEST_MESSAGE_USED(MSG_ONE);
165 TEST_MESSAGE_NOT_USED(MSG_TWO);
166 TEST_MESSAGE_USED(MSG_FEW);
167 TEST_MESSAGE_USED(MSG_MANY);
168 TEST_MESSAGE_NOT_USED(MSG_OTHER);
169 }
170
171 {
172 PluralFormatter::SetOverrideLocale("ja");
173 PluralFormatter formatter(message_ids);
174 TEST_MESSAGE_NOT_USED(MSG_ZERO);
175 TEST_MESSAGE_NOT_USED(MSG_ONE);
176 TEST_MESSAGE_NOT_USED(MSG_TWO);
177 TEST_MESSAGE_NOT_USED(MSG_FEW);
178 TEST_MESSAGE_NOT_USED(MSG_MANY);
179 TEST_MESSAGE_USED(MSG_OTHER);
180 }
181
182 {
183 PluralFormatter::SetOverrideLocale("kn");
184 PluralFormatter formatter(message_ids);
185 TEST_MESSAGE_NOT_USED(MSG_ZERO);
186 TEST_MESSAGE_NOT_USED(MSG_ONE);
187 TEST_MESSAGE_NOT_USED(MSG_TWO);
188 TEST_MESSAGE_NOT_USED(MSG_FEW);
189 TEST_MESSAGE_NOT_USED(MSG_MANY);
190 TEST_MESSAGE_USED(MSG_OTHER);
191 }
192
193 {
194 PluralFormatter::SetOverrideLocale("ko");
195 PluralFormatter formatter(message_ids);
196 TEST_MESSAGE_NOT_USED(MSG_ZERO);
197 TEST_MESSAGE_NOT_USED(MSG_ONE);
198 TEST_MESSAGE_NOT_USED(MSG_TWO);
199 TEST_MESSAGE_NOT_USED(MSG_FEW);
200 TEST_MESSAGE_NOT_USED(MSG_MANY);
201 TEST_MESSAGE_USED(MSG_OTHER);
202 }
203
204 {
205 PluralFormatter::SetOverrideLocale("lt");
206 PluralFormatter formatter(message_ids);
207 TEST_MESSAGE_NOT_USED(MSG_ZERO);
208 TEST_MESSAGE_USED(MSG_ONE);
209 TEST_MESSAGE_NOT_USED(MSG_TWO);
210 TEST_MESSAGE_USED(MSG_FEW);
211 TEST_MESSAGE_NOT_USED(MSG_MANY);
212 TEST_MESSAGE_NOT_USED(MSG_OTHER);
213 }
214
215 {
216 PluralFormatter::SetOverrideLocale("lv");
217 PluralFormatter formatter(message_ids);
218 TEST_MESSAGE_USED(MSG_ZERO);
219 TEST_MESSAGE_USED(MSG_ONE);
220 TEST_MESSAGE_NOT_USED(MSG_TWO);
221 TEST_MESSAGE_NOT_USED(MSG_FEW);
222 TEST_MESSAGE_NOT_USED(MSG_MANY);
223 TEST_MESSAGE_USED(MSG_OTHER);
224 }
225
226 {
227 PluralFormatter::SetOverrideLocale("pl");
228 PluralFormatter formatter(message_ids);
229 TEST_MESSAGE_NOT_USED(MSG_ZERO);
230 TEST_MESSAGE_USED(MSG_ONE);
231 TEST_MESSAGE_NOT_USED(MSG_TWO);
232 TEST_MESSAGE_USED(MSG_FEW);
233 TEST_MESSAGE_USED(MSG_MANY);
234 TEST_MESSAGE_NOT_USED(MSG_OTHER);
235 }
236
237 {
238 PluralFormatter::SetOverrideLocale("ro");
239 PluralFormatter formatter(message_ids);
240 TEST_MESSAGE_NOT_USED(MSG_ZERO);
241 TEST_MESSAGE_USED(MSG_ONE);
242 TEST_MESSAGE_NOT_USED(MSG_TWO);
243 TEST_MESSAGE_USED(MSG_FEW);
244 TEST_MESSAGE_NOT_USED(MSG_MANY);
245 TEST_MESSAGE_USED(MSG_OTHER);
246 }
247
248 {
249 PluralFormatter::SetOverrideLocale("ru");
250 PluralFormatter formatter(message_ids);
251 TEST_MESSAGE_NOT_USED(MSG_ZERO);
252 TEST_MESSAGE_USED(MSG_ONE);
253 TEST_MESSAGE_NOT_USED(MSG_TWO);
254 TEST_MESSAGE_USED(MSG_FEW);
255 TEST_MESSAGE_USED(MSG_MANY);
256 TEST_MESSAGE_NOT_USED(MSG_OTHER);
257 }
258
259 {
260 PluralFormatter::SetOverrideLocale("sk");
261 PluralFormatter formatter(message_ids);
262 TEST_MESSAGE_NOT_USED(MSG_ZERO);
263 TEST_MESSAGE_USED(MSG_ONE);
264 TEST_MESSAGE_NOT_USED(MSG_TWO);
265 TEST_MESSAGE_USED(MSG_FEW);
266 TEST_MESSAGE_NOT_USED(MSG_MANY);
267 TEST_MESSAGE_USED(MSG_OTHER);
268 }
269
270 {
271 PluralFormatter::SetOverrideLocale("sl");
272 PluralFormatter formatter(message_ids);
273 TEST_MESSAGE_NOT_USED(MSG_ZERO);
274 TEST_MESSAGE_USED(MSG_ONE);
275 TEST_MESSAGE_USED(MSG_TWO);
276 TEST_MESSAGE_USED(MSG_FEW);
277 TEST_MESSAGE_NOT_USED(MSG_MANY);
278 TEST_MESSAGE_USED(MSG_OTHER);
279 }
280
281 {
282 PluralFormatter::SetOverrideLocale("tr");
283 PluralFormatter formatter(message_ids);
284 TEST_MESSAGE_NOT_USED(MSG_ZERO);
285 TEST_MESSAGE_NOT_USED(MSG_ONE);
286 TEST_MESSAGE_NOT_USED(MSG_TWO);
287 TEST_MESSAGE_NOT_USED(MSG_FEW);
288 TEST_MESSAGE_NOT_USED(MSG_MANY);
289 TEST_MESSAGE_USED(MSG_OTHER);
290 }
291
292 {
293 PluralFormatter::SetOverrideLocale("uk");
294 PluralFormatter formatter(message_ids);
295 TEST_MESSAGE_NOT_USED(MSG_ZERO);
296 TEST_MESSAGE_USED(MSG_ONE);
297 TEST_MESSAGE_NOT_USED(MSG_TWO);
298 TEST_MESSAGE_USED(MSG_FEW);
299 TEST_MESSAGE_USED(MSG_MANY);
300 TEST_MESSAGE_NOT_USED(MSG_OTHER);
301 }
302
303 {
304 PluralFormatter::SetOverrideLocale("vi");
305 PluralFormatter formatter(message_ids);
306 TEST_MESSAGE_NOT_USED(MSG_ZERO);
307 TEST_MESSAGE_NOT_USED(MSG_ONE);
308 TEST_MESSAGE_NOT_USED(MSG_TWO);
309 TEST_MESSAGE_NOT_USED(MSG_FEW);
310 TEST_MESSAGE_NOT_USED(MSG_MANY);
311 TEST_MESSAGE_USED(MSG_OTHER);
312 }
313
314 {
315 PluralFormatter::SetOverrideLocale("zh_CN");
316 PluralFormatter formatter(message_ids);
317 TEST_MESSAGE_NOT_USED(MSG_ZERO);
318 TEST_MESSAGE_NOT_USED(MSG_ONE);
319 TEST_MESSAGE_NOT_USED(MSG_TWO);
320 TEST_MESSAGE_NOT_USED(MSG_FEW);
321 TEST_MESSAGE_NOT_USED(MSG_MANY);
322 TEST_MESSAGE_USED(MSG_OTHER);
323 }
324
325 {
326 PluralFormatter::SetOverrideLocale("zh_TW");
327 PluralFormatter formatter(message_ids);
328 TEST_MESSAGE_NOT_USED(MSG_ZERO);
329 TEST_MESSAGE_NOT_USED(MSG_ONE);
330 TEST_MESSAGE_NOT_USED(MSG_TWO);
331 TEST_MESSAGE_NOT_USED(MSG_FEW);
332 TEST_MESSAGE_NOT_USED(MSG_MANY);
333 TEST_MESSAGE_USED(MSG_OTHER);
334 }
335 }
OLDNEW
« 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