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

Side by Side Diff: chrome/renderer/spellchecker/spellcheck_worditerator_unittest.cc

Issue 1269343005: Updates SpellcheckWordIterator::GetNextWord to return an enum. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@break-iter
Patch Set: Addressed comments. Created 5 years, 4 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
« no previous file with comments | « chrome/renderer/spellchecker/spellcheck_worditerator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/format_macros.h" 8 #include "base/format_macros.h"
9 #include "base/i18n/break_iterator.h" 9 #include "base/i18n/break_iterator.h"
10 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
(...skipping 13 matching lines...) Expand all
24 }; 24 };
25 25
26 base::string16 GetRulesForLanguage(const std::string& language) { 26 base::string16 GetRulesForLanguage(const std::string& language) {
27 SpellcheckCharAttribute attribute; 27 SpellcheckCharAttribute attribute;
28 attribute.SetDefaultLanguage(language); 28 attribute.SetDefaultLanguage(language);
29 return attribute.GetRuleSet(true); 29 return attribute.GetRuleSet(true);
30 } 30 }
31 31
32 } // namespace 32 } // namespace
33 33
34 // Tests whether or not our SpellcheckWordIterator can extract only words used 34 // Tests whether or not our SpellcheckWordIterator can extract words used by the
35 // by the specified language from a multi-language text. 35 // specified language from a multi-language text.
36 TEST(SpellcheckWordIteratorTest, SplitWord) { 36 TEST(SpellcheckWordIteratorTest, SplitWord) {
37 // An input text. This text includes words of several languages. (Some words 37 // An input text. This text includes words of several languages. (Some words
38 // are not separated with whitespace characters.) Our SpellcheckWordIterator 38 // are not separated with whitespace characters.) Our SpellcheckWordIterator
39 // should extract only the words used by the specified language from this text 39 // should extract the words used by the specified language from this text and
40 // and normalize them so our spell-checker can check their spellings. 40 // normalize them so our spell-checker can check their spellings. If
41 // characters are found that are not from the specified language the test
42 // skips them.
41 const wchar_t kTestText[] = 43 const wchar_t kTestText[] =
42 // Graphic characters 44 // Graphic characters
43 L"!@#$%^&*()" 45 L"!@#$%^&*()"
44 // Latin (including a contraction character and a ligature). 46 // Latin (including a contraction character and a ligature).
45 L"hello:hello a\xFB03x" 47 L"hello:hello a\xFB03x"
46 // Greek 48 // Greek
47 L"\x03B3\x03B5\x03B9\x03AC\x0020\x03C3\x03BF\x03C5" 49 L"\x03B3\x03B5\x03B9\x03AC\x0020\x03C3\x03BF\x03C5"
48 // Cyrillic 50 // Cyrillic
49 L"\x0437\x0434\x0440\x0430\x0432\x0441\x0442\x0432" 51 L"\x0437\x0434\x0440\x0430\x0432\x0441\x0442\x0432"
50 L"\x0443\x0439\x0442\x0435" 52 L"\x0443\x0439\x0442\x0435"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 kTestCases[i].allow_contraction)); 136 kTestCases[i].allow_contraction));
135 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length())); 137 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length()));
136 138
137 std::vector<base::string16> expected_words = base::SplitString( 139 std::vector<base::string16> expected_words = base::SplitString(
138 base::WideToUTF16(kTestCases[i].expected_words), 140 base::WideToUTF16(kTestCases[i].expected_words),
139 base::string16(1, ' '), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 141 base::string16(1, ' '), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
140 142
141 base::string16 actual_word; 143 base::string16 actual_word;
142 int actual_start, actual_end; 144 int actual_start, actual_end;
143 size_t index = 0; 145 size_t index = 0;
144 while (iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) { 146 for (SpellcheckWordIterator::WordIteratorStatus status =
147 iterator.GetNextWord(&actual_word, &actual_start, &actual_end);
148 status != SpellcheckWordIterator::IS_END_OF_TEXT;
149 status =
150 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) {
151 if (status == SpellcheckWordIterator::WordIteratorStatus::IS_SKIPPABLE)
152 continue;
153
145 EXPECT_TRUE(index < expected_words.size()); 154 EXPECT_TRUE(index < expected_words.size());
146 if (index < expected_words.size()) 155 if (index < expected_words.size())
147 EXPECT_EQ(expected_words[index], actual_word); 156 EXPECT_EQ(expected_words[index], actual_word);
148 ++index; 157 ++index;
149 } 158 }
150 } 159 }
151 } 160 }
152 161
153 // Tests whether our SpellcheckWordIterator extracts an empty word without 162 // Tests whether our SpellcheckWordIterator extracts an empty word without
154 // getting stuck in an infinite loop when inputting a Khmer text. (This is a 163 // getting stuck in an infinite loop when inputting a Khmer text. (This is a
155 // regression test for Issue 46278.) 164 // regression test for Issue 46278.)
156 TEST(SpellcheckWordIteratorTest, RuleSetConsistency) { 165 TEST(SpellcheckWordIteratorTest, RuleSetConsistency) {
157 SpellcheckCharAttribute attributes; 166 SpellcheckCharAttribute attributes;
158 attributes.SetDefaultLanguage("en-US"); 167 attributes.SetDefaultLanguage("en-US");
159 168
160 const wchar_t kTestText[] = L"\x1791\x17c1\x002e"; 169 const wchar_t kTestText[] = L"\x1791\x17c1\x002e";
161 base::string16 input(base::WideToUTF16(kTestText)); 170 base::string16 input(base::WideToUTF16(kTestText));
162 171
163 SpellcheckWordIterator iterator; 172 SpellcheckWordIterator iterator;
164 EXPECT_TRUE(iterator.Initialize(&attributes, true)); 173 EXPECT_TRUE(iterator.Initialize(&attributes, true));
165 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length())); 174 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length()));
166 175
167 // When SpellcheckWordIterator uses an inconsistent ICU ruleset, the following 176 // When SpellcheckWordIterator uses an inconsistent ICU ruleset, the following
168 // iterator.GetNextWord() call gets stuck in an infinite loop. Therefore, this 177 // iterator.GetNextWord() calls get stuck in an infinite loop. Therefore, this
169 // test succeeds if this call returns without timeouts. 178 // test succeeds if this call returns without timeouts.
170 base::string16 actual_word; 179 base::string16 actual_word;
171 int actual_start, actual_end; 180 int actual_start, actual_end;
172 EXPECT_FALSE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end)); 181 SpellcheckWordIterator::WordIteratorStatus status;
182 for (status = iterator.GetNextWord(&actual_word, &actual_start, &actual_end);
183 status == SpellcheckWordIterator::IS_SKIPPABLE;
184 status =
185 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) {
186 continue;
187 }
188
189 EXPECT_EQ(SpellcheckWordIterator::WordIteratorStatus::IS_END_OF_TEXT, status);
173 EXPECT_EQ(0, actual_start); 190 EXPECT_EQ(0, actual_start);
174 EXPECT_EQ(0, actual_end); 191 EXPECT_EQ(0, actual_end);
175 } 192 }
176 193
177 // Vertify our SpellcheckWordIterator can treat ASCII numbers as word characters 194 // Vertify our SpellcheckWordIterator can treat ASCII numbers as word characters
178 // on LTR languages. On the other hand, it should not treat ASCII numbers as 195 // on LTR languages. On the other hand, it should not treat ASCII numbers as
179 // word characters on RTL languages because they change the text direction from 196 // word characters on RTL languages because they change the text direction from
180 // RTL to LTR. 197 // RTL to LTR.
181 TEST(SpellcheckWordIteratorTest, TreatNumbersAsWordCharacters) { 198 TEST(SpellcheckWordIteratorTest, TreatNumbersAsWordCharacters) {
182 // A set of a language, a dummy word, and a text direction used in this test. 199 // A set of a language, a dummy word, and a text direction used in this test.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 SpellcheckCharAttribute attributes; 240 SpellcheckCharAttribute attributes;
224 attributes.SetDefaultLanguage(kTestCases[i].language); 241 attributes.SetDefaultLanguage(kTestCases[i].language);
225 242
226 base::string16 input_word(base::WideToUTF16(kTestCases[i].text)); 243 base::string16 input_word(base::WideToUTF16(kTestCases[i].text));
227 SpellcheckWordIterator iterator; 244 SpellcheckWordIterator iterator;
228 EXPECT_TRUE(iterator.Initialize(&attributes, true)); 245 EXPECT_TRUE(iterator.Initialize(&attributes, true));
229 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length())); 246 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length()));
230 247
231 base::string16 actual_word; 248 base::string16 actual_word;
232 int actual_start, actual_end; 249 int actual_start, actual_end;
233 EXPECT_TRUE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end)); 250 SpellcheckWordIterator::WordIteratorStatus status;
251 for (status =
252 iterator.GetNextWord(&actual_word, &actual_start, &actual_end);
253 status == SpellcheckWordIterator::IS_SKIPPABLE;
254 status =
255 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) {
256 continue;
257 }
258
259 EXPECT_EQ(SpellcheckWordIterator::WordIteratorStatus::IS_WORD, status);
234 if (kTestCases[i].left_to_right) 260 if (kTestCases[i].left_to_right)
235 EXPECT_EQ(input_word, actual_word); 261 EXPECT_EQ(input_word, actual_word);
236 else 262 else
237 EXPECT_NE(input_word, actual_word); 263 EXPECT_NE(input_word, actual_word);
238 } 264 }
239 } 265 }
240 266
241 // Vertify SpellcheckWordIterator treats typographical apostrophe as a part of 267 // Vertify SpellcheckWordIterator treats typographical apostrophe as a part of
242 // the word. 268 // the word.
243 TEST(SpellcheckWordIteratorTest, TypographicalApostropheIsPartOfWord) { 269 TEST(SpellcheckWordIteratorTest, TypographicalApostropheIsPartOfWord) {
(...skipping 27 matching lines...) Expand all
271 SpellcheckCharAttribute attributes; 297 SpellcheckCharAttribute attributes;
272 attributes.SetDefaultLanguage(kTestCases[i].language); 298 attributes.SetDefaultLanguage(kTestCases[i].language);
273 299
274 base::string16 input_word(base::WideToUTF16(kTestCases[i].word)); 300 base::string16 input_word(base::WideToUTF16(kTestCases[i].word));
275 SpellcheckWordIterator iterator; 301 SpellcheckWordIterator iterator;
276 EXPECT_TRUE(iterator.Initialize(&attributes, true)); 302 EXPECT_TRUE(iterator.Initialize(&attributes, true));
277 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length())); 303 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length()));
278 304
279 base::string16 actual_word; 305 base::string16 actual_word;
280 int actual_start, actual_end; 306 int actual_start, actual_end;
281 EXPECT_TRUE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end)); 307 SpellcheckWordIterator::WordIteratorStatus status;
308 for (status =
309 iterator.GetNextWord(&actual_word, &actual_start, &actual_end);
310 status == SpellcheckWordIterator::IS_SKIPPABLE;
311 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) {
Nico 2016/01/05 22:13:23 You forgot to assign to status here (you got it ri
groby-ooo-7-16 2016/01/05 22:36:11 I _think_ the test still passes because the very f
312 continue;
313 }
314
315 EXPECT_EQ(SpellcheckWordIterator::WordIteratorStatus::IS_WORD, status);
282 EXPECT_EQ(input_word, actual_word); 316 EXPECT_EQ(input_word, actual_word);
283 EXPECT_EQ(0, actual_start); 317 EXPECT_EQ(0, actual_start);
284 EXPECT_EQ(input_word.length(), 318 EXPECT_EQ(input_word.length(),
285 static_cast<base::string16::size_type>(actual_end)); 319 static_cast<base::string16::size_type>(actual_end));
286 } 320 }
287 } 321 }
288 322
289 TEST(SpellcheckWordIteratorTest, Initialization) { 323 TEST(SpellcheckWordIteratorTest, Initialization) {
290 // Test initialization works when a default language is set. 324 // Test initialization works when a default language is set.
291 { 325 {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 EXPECT_EQ(base::UTF8ToUTF16("."), iter.GetString()); 493 EXPECT_EQ(base::UTF8ToUTF16("."), iter.GetString());
460 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); 494 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
461 EXPECT_TRUE(iter.Advance()); 495 EXPECT_TRUE(iter.Advance());
462 EXPECT_EQ(base::UTF8ToUTF16(" "), iter.GetString()); 496 EXPECT_EQ(base::UTF8ToUTF16(" "), iter.GetString());
463 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); 497 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
464 EXPECT_TRUE(iter.Advance()); 498 EXPECT_TRUE(iter.Advance());
465 EXPECT_EQ(base::UTF8ToUTF16(","), iter.GetString()); 499 EXPECT_EQ(base::UTF8ToUTF16(","), iter.GetString());
466 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); 500 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
467 EXPECT_FALSE(iter.Advance()); 501 EXPECT_FALSE(iter.Advance());
468 } 502 }
OLDNEW
« no previous file with comments | « chrome/renderer/spellchecker/spellcheck_worditerator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698