OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/i18n/break_iterator.h" | 5 #include "base/i18n/break_iterator.h" |
6 | 6 |
7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 EXPECT_TRUE(iter.IsWord()); | 82 EXPECT_TRUE(iter.IsWord()); |
83 EXPECT_EQ(word2, iter.GetString()); | 83 EXPECT_EQ(word2, iter.GetString()); |
84 EXPECT_FALSE(iter.Advance()); | 84 EXPECT_FALSE(iter.Advance()); |
85 EXPECT_FALSE(iter.IsWord()); | 85 EXPECT_FALSE(iter.IsWord()); |
86 EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end. | 86 EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end. |
87 EXPECT_FALSE(iter.IsWord()); | 87 EXPECT_FALSE(iter.IsWord()); |
88 } | 88 } |
89 | 89 |
90 TEST(BreakIteratorTest, BreakWide32) { | 90 TEST(BreakIteratorTest, BreakWide32) { |
91 // U+1D49C MATHEMATICAL SCRIPT CAPITAL A | 91 // U+1D49C MATHEMATICAL SCRIPT CAPITAL A |
92 const char* very_wide_char = "\xF0\x9D\x92\x9C"; | 92 const char very_wide_char[] = "\xF0\x9D\x92\x9C"; |
93 const string16 str( | 93 const string16 str( |
94 UTF8ToUTF16(base::StringPrintf("%s a", very_wide_char))); | 94 UTF8ToUTF16(base::StringPrintf("%s a", very_wide_char))); |
95 const string16 very_wide_word(str.substr(0, 2)); | 95 const string16 very_wide_word(str.substr(0, 2)); |
96 | 96 |
97 BreakIterator iter(str, BreakIterator::BREAK_WORD); | 97 BreakIterator iter(str, BreakIterator::BREAK_WORD); |
98 ASSERT_TRUE(iter.Init()); | 98 ASSERT_TRUE(iter.Init()); |
99 EXPECT_TRUE(iter.Advance()); | 99 EXPECT_TRUE(iter.Advance()); |
100 EXPECT_TRUE(iter.IsWord()); | 100 EXPECT_TRUE(iter.IsWord()); |
101 EXPECT_EQ(very_wide_word, iter.GetString()); | 101 EXPECT_EQ(very_wide_word, iter.GetString()); |
102 EXPECT_TRUE(iter.Advance()); | 102 EXPECT_TRUE(iter.Advance()); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 EXPECT_FALSE(iter.IsWord()); | 187 EXPECT_FALSE(iter.IsWord()); |
188 EXPECT_EQ(word2, iter.GetString()); | 188 EXPECT_EQ(word2, iter.GetString()); |
189 EXPECT_FALSE(iter.Advance()); | 189 EXPECT_FALSE(iter.Advance()); |
190 EXPECT_FALSE(iter.IsWord()); | 190 EXPECT_FALSE(iter.IsWord()); |
191 EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end. | 191 EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end. |
192 EXPECT_FALSE(iter.IsWord()); | 192 EXPECT_FALSE(iter.IsWord()); |
193 } | 193 } |
194 | 194 |
195 TEST(BreakIteratorTest, BreakSpaceWide32) { | 195 TEST(BreakIteratorTest, BreakSpaceWide32) { |
196 // U+1D49C MATHEMATICAL SCRIPT CAPITAL A | 196 // U+1D49C MATHEMATICAL SCRIPT CAPITAL A |
197 const char* very_wide_char = "\xF0\x9D\x92\x9C"; | 197 const char very_wide_char[] = "\xF0\x9D\x92\x9C"; |
198 const string16 str( | 198 const string16 str( |
199 UTF8ToUTF16(base::StringPrintf("%s a", very_wide_char))); | 199 UTF8ToUTF16(base::StringPrintf("%s a", very_wide_char))); |
200 const string16 very_wide_word(str.substr(0, 3)); | 200 const string16 very_wide_word(str.substr(0, 3)); |
201 | 201 |
202 BreakIterator iter(str, BreakIterator::BREAK_SPACE); | 202 BreakIterator iter(str, BreakIterator::BREAK_SPACE); |
203 ASSERT_TRUE(iter.Init()); | 203 ASSERT_TRUE(iter.Init()); |
204 EXPECT_TRUE(iter.Advance()); | 204 EXPECT_TRUE(iter.Advance()); |
205 EXPECT_FALSE(iter.IsWord()); | 205 EXPECT_FALSE(iter.IsWord()); |
206 EXPECT_EQ(very_wide_word, iter.GetString()); | 206 EXPECT_EQ(very_wide_word, iter.GetString()); |
207 EXPECT_TRUE(iter.Advance()); | 207 EXPECT_TRUE(iter.Advance()); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 EXPECT_FALSE(iter.IsWord()); | 285 EXPECT_FALSE(iter.IsWord()); |
286 EXPECT_EQ(line2, iter.GetString()); | 286 EXPECT_EQ(line2, iter.GetString()); |
287 EXPECT_FALSE(iter.Advance()); | 287 EXPECT_FALSE(iter.Advance()); |
288 EXPECT_FALSE(iter.IsWord()); | 288 EXPECT_FALSE(iter.IsWord()); |
289 EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end. | 289 EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end. |
290 EXPECT_FALSE(iter.IsWord()); | 290 EXPECT_FALSE(iter.IsWord()); |
291 } | 291 } |
292 | 292 |
293 TEST(BreakIteratorTest, BreakLineWide32) { | 293 TEST(BreakIteratorTest, BreakLineWide32) { |
294 // U+1D49C MATHEMATICAL SCRIPT CAPITAL A | 294 // U+1D49C MATHEMATICAL SCRIPT CAPITAL A |
295 const char* very_wide_char = "\xF0\x9D\x92\x9C"; | 295 const char very_wide_char[] = "\xF0\x9D\x92\x9C"; |
296 const string16 str( | 296 const string16 str( |
297 UTF8ToUTF16(base::StringPrintf("%s\na", very_wide_char))); | 297 UTF8ToUTF16(base::StringPrintf("%s\na", very_wide_char))); |
298 const string16 very_wide_line(str.substr(0, 3)); | 298 const string16 very_wide_line(str.substr(0, 3)); |
299 BreakIterator iter(str, BreakIterator::BREAK_NEWLINE); | 299 BreakIterator iter(str, BreakIterator::BREAK_NEWLINE); |
300 ASSERT_TRUE(iter.Init()); | 300 ASSERT_TRUE(iter.Init()); |
301 EXPECT_TRUE(iter.Advance()); | 301 EXPECT_TRUE(iter.Advance()); |
302 EXPECT_FALSE(iter.IsWord()); | 302 EXPECT_FALSE(iter.IsWord()); |
303 EXPECT_EQ(very_wide_line, iter.GetString()); | 303 EXPECT_EQ(very_wide_line, iter.GetString()); |
304 EXPECT_TRUE(iter.Advance()); | 304 EXPECT_TRUE(iter.Advance()); |
305 EXPECT_FALSE(iter.IsWord()); | 305 EXPECT_FALSE(iter.IsWord()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 // We should be able to get valid substrings with GetString() function | 338 // We should be able to get valid substrings with GetString() function |
339 // after setting new content by calling SetText(). | 339 // after setting new content by calling SetText(). |
340 TEST(BreakIteratorTest, GetStringAfterSetText) { | 340 TEST(BreakIteratorTest, GetStringAfterSetText) { |
341 const string16 initial_string(ASCIIToUTF16("str")); | 341 const string16 initial_string(ASCIIToUTF16("str")); |
342 BreakIterator iter(initial_string, BreakIterator::BREAK_WORD); | 342 BreakIterator iter(initial_string, BreakIterator::BREAK_WORD); |
343 ASSERT_TRUE(iter.Init()); | 343 ASSERT_TRUE(iter.Init()); |
344 | 344 |
345 const string16 long_string(ASCIIToUTF16("another,string")); | 345 const string16 long_string(ASCIIToUTF16("another,string")); |
346 EXPECT_TRUE(iter.SetText(long_string.c_str(), long_string.size())); | 346 EXPECT_TRUE(iter.SetText(long_string.c_str(), long_string.size())); |
347 EXPECT_TRUE(iter.Advance()); | 347 EXPECT_TRUE(iter.Advance()); |
348 EXPECT_TRUE(iter.Advance()); // Advance to ',' in |long_string| | 348 EXPECT_TRUE(iter.Advance()); // Advance to ',' in |long_string| |
349 | 349 |
350 // Check that the current position is out of bounds of the |initial_string|. | 350 // Check that the current position is out of bounds of the |initial_string|. |
351 EXPECT_LT(initial_string.size(), iter.pos()); | 351 EXPECT_LT(initial_string.size(), iter.pos()); |
352 | 352 |
353 // Check that we can get a valid substring of |long_string|. | 353 // Check that we can get a valid substring of |long_string|. |
354 EXPECT_EQ(ASCIIToUTF16(","), iter.GetString()); | 354 EXPECT_EQ(ASCIIToUTF16(","), iter.GetString()); |
355 } | 355 } |
356 | 356 |
357 TEST(BreakIteratorTest, GetStringPiece) { | 357 TEST(BreakIteratorTest, GetStringPiece) { |
358 const string16 initial_string(ASCIIToUTF16("some string")); | 358 const string16 initial_string(ASCIIToUTF16("some string")); |
359 BreakIterator iter(initial_string, BreakIterator::BREAK_WORD); | 359 BreakIterator iter(initial_string, BreakIterator::BREAK_WORD); |
360 ASSERT_TRUE(iter.Init()); | 360 ASSERT_TRUE(iter.Init()); |
361 | 361 |
362 EXPECT_TRUE(iter.Advance()); | 362 EXPECT_TRUE(iter.Advance()); |
363 EXPECT_EQ(iter.GetString(), iter.GetStringPiece().as_string()); | 363 EXPECT_EQ(iter.GetString(), iter.GetStringPiece().as_string()); |
364 EXPECT_EQ(StringPiece16(ASCIIToUTF16("some")), iter.GetStringPiece()); | 364 EXPECT_EQ(StringPiece16(ASCIIToUTF16("some")), iter.GetStringPiece()); |
365 | 365 |
366 EXPECT_TRUE(iter.Advance()); | 366 EXPECT_TRUE(iter.Advance()); |
367 EXPECT_TRUE(iter.Advance()); | 367 EXPECT_TRUE(iter.Advance()); |
368 EXPECT_EQ(iter.GetString(), iter.GetStringPiece().as_string()); | 368 EXPECT_EQ(iter.GetString(), iter.GetStringPiece().as_string()); |
369 EXPECT_EQ(StringPiece16(ASCIIToUTF16("string")), iter.GetStringPiece()); | 369 EXPECT_EQ(StringPiece16(ASCIIToUTF16("string")), iter.GetStringPiece()); |
370 } | 370 } |
371 | 371 |
372 } // namespace i18n | 372 } // namespace i18n |
373 } // namespace base | 373 } // namespace base |
OLD | NEW |