Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 kTestCases[i].allow_contraction)); | 134 kTestCases[i].allow_contraction)); |
| 135 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length())); | 135 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length())); |
| 136 | 136 |
| 137 std::vector<base::string16> expected_words = base::SplitString( | 137 std::vector<base::string16> expected_words = base::SplitString( |
| 138 base::WideToUTF16(kTestCases[i].expected_words), | 138 base::WideToUTF16(kTestCases[i].expected_words), |
| 139 base::string16(1, ' '), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 139 base::string16(1, ' '), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 140 | 140 |
| 141 base::string16 actual_word; | 141 base::string16 actual_word; |
| 142 int actual_start, actual_end; | 142 int actual_start, actual_end; |
| 143 size_t index = 0; | 143 size_t index = 0; |
| 144 while (iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) { | 144 for (SpellcheckWordIterator::WordIteratorStatus status = |
| 145 iterator.GetNextWord(&actual_word, &actual_start, &actual_end); | |
| 146 status != SpellcheckWordIterator::IS_END_OF_TEXT; | |
| 147 status = | |
| 148 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) { | |
| 149 if (status == SpellcheckWordIterator::WordIteratorStatus::IS_SKIPPABLE) | |
| 150 continue; | |
| 151 | |
| 145 EXPECT_TRUE(index < expected_words.size()); | 152 EXPECT_TRUE(index < expected_words.size()); |
| 146 if (index < expected_words.size()) | 153 if (index < expected_words.size()) |
| 147 EXPECT_EQ(expected_words[index], actual_word); | 154 EXPECT_EQ(expected_words[index], actual_word); |
| 148 ++index; | 155 ++index; |
| 149 } | 156 } |
| 150 } | 157 } |
| 151 } | 158 } |
| 152 | 159 |
| 153 // Tests whether our SpellcheckWordIterator extracts an empty word without | 160 // Tests whether our SpellcheckWordIterator extracts an empty word without |
| 154 // getting stuck in an infinite loop when inputting a Khmer text. (This is a | 161 // getting stuck in an infinite loop when inputting a Khmer text. (This is a |
| 155 // regression test for Issue 46278.) | 162 // regression test for Issue 46278.) |
| 156 TEST(SpellcheckWordIteratorTest, RuleSetConsistency) { | 163 TEST(SpellcheckWordIteratorTest, RuleSetConsistency) { |
| 157 SpellcheckCharAttribute attributes; | 164 SpellcheckCharAttribute attributes; |
| 158 attributes.SetDefaultLanguage("en-US"); | 165 attributes.SetDefaultLanguage("en-US"); |
| 159 | 166 |
| 160 const wchar_t kTestText[] = L"\x1791\x17c1\x002e"; | 167 const wchar_t kTestText[] = L"\x1791\x17c1\x002e"; |
| 161 base::string16 input(base::WideToUTF16(kTestText)); | 168 base::string16 input(base::WideToUTF16(kTestText)); |
| 162 | 169 |
| 163 SpellcheckWordIterator iterator; | 170 SpellcheckWordIterator iterator; |
| 164 EXPECT_TRUE(iterator.Initialize(&attributes, true)); | 171 EXPECT_TRUE(iterator.Initialize(&attributes, true)); |
| 165 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length())); | 172 EXPECT_TRUE(iterator.SetText(input.c_str(), input.length())); |
| 166 | 173 |
| 167 // When SpellcheckWordIterator uses an inconsistent ICU ruleset, the following | 174 // When SpellcheckWordIterator uses an inconsistent ICU ruleset, the following |
| 168 // iterator.GetNextWord() call gets stuck in an infinite loop. Therefore, this | 175 // iterator.GetNextWord() call gets stuck in an infinite loop. Therefore, this |
|
please use gerrit instead
2015/08/12 21:01:09
This comment was written with a single iterator.Ge
Julius
2015/08/12 23:42:28
Done.
| |
| 169 // test succeeds if this call returns without timeouts. | 176 // test succeeds if this call returns without timeouts. |
| 170 base::string16 actual_word; | 177 base::string16 actual_word; |
| 171 int actual_start, actual_end; | 178 int actual_start, actual_end; |
| 172 EXPECT_FALSE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end)); | 179 SpellcheckWordIterator::WordIteratorStatus status; |
| 180 for (status = iterator.GetNextWord(&actual_word, &actual_start, &actual_end); | |
| 181 status == SpellcheckWordIterator::IS_SKIPPABLE; | |
| 182 status = iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) | |
| 183 continue; | |
|
please use gerrit instead
2015/08/12 21:01:09
We usually use curly braces for the body of a for
Julius
2015/08/12 23:42:28
Yeah, wasn't quite sure about this. Didn't see it
| |
| 184 | |
| 185 EXPECT_EQ(SpellcheckWordIterator::WordIteratorStatus::IS_END_OF_TEXT, status); | |
| 173 EXPECT_EQ(0, actual_start); | 186 EXPECT_EQ(0, actual_start); |
| 174 EXPECT_EQ(0, actual_end); | 187 EXPECT_EQ(0, actual_end); |
| 175 } | 188 } |
| 176 | 189 |
| 177 // Vertify our SpellcheckWordIterator can treat ASCII numbers as word characters | 190 // 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 | 191 // 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 | 192 // word characters on RTL languages because they change the text direction from |
| 180 // RTL to LTR. | 193 // RTL to LTR. |
| 181 TEST(SpellcheckWordIteratorTest, TreatNumbersAsWordCharacters) { | 194 TEST(SpellcheckWordIteratorTest, TreatNumbersAsWordCharacters) { |
| 182 // A set of a language, a dummy word, and a text direction used in this test. | 195 // 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 Loading... | |
| 223 SpellcheckCharAttribute attributes; | 236 SpellcheckCharAttribute attributes; |
| 224 attributes.SetDefaultLanguage(kTestCases[i].language); | 237 attributes.SetDefaultLanguage(kTestCases[i].language); |
| 225 | 238 |
| 226 base::string16 input_word(base::WideToUTF16(kTestCases[i].text)); | 239 base::string16 input_word(base::WideToUTF16(kTestCases[i].text)); |
| 227 SpellcheckWordIterator iterator; | 240 SpellcheckWordIterator iterator; |
| 228 EXPECT_TRUE(iterator.Initialize(&attributes, true)); | 241 EXPECT_TRUE(iterator.Initialize(&attributes, true)); |
| 229 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length())); | 242 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length())); |
| 230 | 243 |
| 231 base::string16 actual_word; | 244 base::string16 actual_word; |
| 232 int actual_start, actual_end; | 245 int actual_start, actual_end; |
| 233 EXPECT_TRUE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end)); | 246 SpellcheckWordIterator::WordIteratorStatus status; |
| 247 for (status = | |
| 248 iterator.GetNextWord(&actual_word, &actual_start, &actual_end); | |
| 249 status == SpellcheckWordIterator::IS_SKIPPABLE; | |
| 250 status = | |
| 251 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) | |
| 252 continue; | |
| 253 | |
| 254 EXPECT_EQ(SpellcheckWordIterator::WordIteratorStatus::IS_WORD, status); | |
| 234 if (kTestCases[i].left_to_right) | 255 if (kTestCases[i].left_to_right) |
| 235 EXPECT_EQ(input_word, actual_word); | 256 EXPECT_EQ(input_word, actual_word); |
| 236 else | 257 else |
| 237 EXPECT_NE(input_word, actual_word); | 258 EXPECT_NE(input_word, actual_word); |
| 238 } | 259 } |
| 239 } | 260 } |
| 240 | 261 |
| 241 // Vertify SpellcheckWordIterator treats typographical apostrophe as a part of | 262 // Vertify SpellcheckWordIterator treats typographical apostrophe as a part of |
| 242 // the word. | 263 // the word. |
| 243 TEST(SpellcheckWordIteratorTest, TypographicalApostropheIsPartOfWord) { | 264 TEST(SpellcheckWordIteratorTest, TypographicalApostropheIsPartOfWord) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 271 SpellcheckCharAttribute attributes; | 292 SpellcheckCharAttribute attributes; |
| 272 attributes.SetDefaultLanguage(kTestCases[i].language); | 293 attributes.SetDefaultLanguage(kTestCases[i].language); |
| 273 | 294 |
| 274 base::string16 input_word(base::WideToUTF16(kTestCases[i].word)); | 295 base::string16 input_word(base::WideToUTF16(kTestCases[i].word)); |
| 275 SpellcheckWordIterator iterator; | 296 SpellcheckWordIterator iterator; |
| 276 EXPECT_TRUE(iterator.Initialize(&attributes, true)); | 297 EXPECT_TRUE(iterator.Initialize(&attributes, true)); |
| 277 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length())); | 298 EXPECT_TRUE(iterator.SetText(input_word.c_str(), input_word.length())); |
| 278 | 299 |
| 279 base::string16 actual_word; | 300 base::string16 actual_word; |
| 280 int actual_start, actual_end; | 301 int actual_start, actual_end; |
| 281 EXPECT_TRUE(iterator.GetNextWord(&actual_word, &actual_start, &actual_end)); | 302 SpellcheckWordIterator::WordIteratorStatus status; |
| 303 for (status = | |
| 304 iterator.GetNextWord(&actual_word, &actual_start, &actual_end); | |
| 305 status == SpellcheckWordIterator::IS_SKIPPABLE; | |
| 306 iterator.GetNextWord(&actual_word, &actual_start, &actual_end)) | |
| 307 continue; | |
| 308 | |
| 309 EXPECT_EQ(SpellcheckWordIterator::WordIteratorStatus::IS_WORD, status); | |
| 282 EXPECT_EQ(input_word, actual_word); | 310 EXPECT_EQ(input_word, actual_word); |
| 283 EXPECT_EQ(0, actual_start); | 311 EXPECT_EQ(0, actual_start); |
| 284 EXPECT_EQ(input_word.length(), | 312 EXPECT_EQ(input_word.length(), |
| 285 static_cast<base::string16::size_type>(actual_end)); | 313 static_cast<base::string16::size_type>(actual_end)); |
| 286 } | 314 } |
| 287 } | 315 } |
| 288 | 316 |
| 289 TEST(SpellcheckWordIteratorTest, Initialization) { | 317 TEST(SpellcheckWordIteratorTest, Initialization) { |
| 290 // Test initialization works when a default language is set. | 318 // Test initialization works when a default language is set. |
| 291 { | 319 { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 EXPECT_EQ(base::UTF8ToUTF16("."), iter.GetString()); | 487 EXPECT_EQ(base::UTF8ToUTF16("."), iter.GetString()); |
| 460 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); | 488 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); |
| 461 EXPECT_TRUE(iter.Advance()); | 489 EXPECT_TRUE(iter.Advance()); |
| 462 EXPECT_EQ(base::UTF8ToUTF16(" "), iter.GetString()); | 490 EXPECT_EQ(base::UTF8ToUTF16(" "), iter.GetString()); |
| 463 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); | 491 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); |
| 464 EXPECT_TRUE(iter.Advance()); | 492 EXPECT_TRUE(iter.Advance()); |
| 465 EXPECT_EQ(base::UTF8ToUTF16(","), iter.GetString()); | 493 EXPECT_EQ(base::UTF8ToUTF16(","), iter.GetString()); |
| 466 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); | 494 EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD); |
| 467 EXPECT_FALSE(iter.Advance()); | 495 EXPECT_FALSE(iter.Advance()); |
| 468 } | 496 } |
| OLD | NEW |