• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 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/i18n/word_iterator.h"
6 
7 #include "base/string_piece.h"
8 #include "base/string_util.h"
9 #include "base/sys_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
TEST(WordIteratorTest,BreakWord)12 TEST(WordIteratorTest, BreakWord) {
13   std::wstring str(L" foo bar! \npouet boom");
14   WordIterator iter(str, WordIterator::BREAK_WORD);
15   ASSERT_TRUE(iter.Init());
16   EXPECT_TRUE(iter.Advance());
17   EXPECT_FALSE(iter.IsWord());
18   EXPECT_EQ(L" ", iter.GetWord());
19   EXPECT_TRUE(iter.Advance());
20   EXPECT_TRUE(iter.IsWord());
21   EXPECT_EQ(L"foo", iter.GetWord());
22   EXPECT_TRUE(iter.Advance());
23   EXPECT_FALSE(iter.IsWord());
24   EXPECT_EQ(L" ", iter.GetWord());
25   EXPECT_TRUE(iter.Advance());
26   EXPECT_TRUE(iter.IsWord());
27   EXPECT_EQ(L"bar", iter.GetWord());
28   EXPECT_TRUE(iter.Advance());
29   EXPECT_FALSE(iter.IsWord());
30   EXPECT_EQ(L"!", iter.GetWord());
31   EXPECT_TRUE(iter.Advance());
32   EXPECT_FALSE(iter.IsWord());
33   EXPECT_EQ(L" ", iter.GetWord());
34   EXPECT_TRUE(iter.Advance());
35   EXPECT_FALSE(iter.IsWord());
36   EXPECT_EQ(L"\n", iter.GetWord());
37   EXPECT_TRUE(iter.Advance());
38   EXPECT_TRUE(iter.IsWord());
39   EXPECT_EQ(L"pouet", iter.GetWord());
40   EXPECT_TRUE(iter.Advance());
41   EXPECT_FALSE(iter.IsWord());
42   EXPECT_EQ(L" ", iter.GetWord());
43   EXPECT_TRUE(iter.Advance());
44   EXPECT_TRUE(iter.IsWord());
45   EXPECT_EQ(L"boom", iter.GetWord());
46   EXPECT_FALSE(iter.Advance());
47   EXPECT_FALSE(iter.IsWord());
48 }
49 
TEST(WordIteratorTest,BreakLine)50 TEST(WordIteratorTest, BreakLine) {
51   std::wstring str(L" foo bar! \npouet boom");
52   WordIterator iter(str, WordIterator::BREAK_LINE);
53   ASSERT_TRUE(iter.Init());
54   EXPECT_TRUE(iter.Advance());
55   EXPECT_FALSE(iter.IsWord());
56   EXPECT_EQ(L" ", iter.GetWord());
57   EXPECT_TRUE(iter.Advance());
58   EXPECT_FALSE(iter.IsWord());
59   EXPECT_EQ(L"foo ", iter.GetWord());
60   EXPECT_TRUE(iter.Advance());
61   EXPECT_TRUE(iter.IsWord());
62   EXPECT_EQ(L"bar! \n", iter.GetWord());
63   EXPECT_TRUE(iter.Advance());
64   EXPECT_FALSE(iter.IsWord());
65   EXPECT_EQ(L"pouet ", iter.GetWord());
66   EXPECT_TRUE(iter.Advance());
67   EXPECT_FALSE(iter.IsWord());
68   EXPECT_EQ(L"boom", iter.GetWord());
69   EXPECT_FALSE(iter.Advance());
70   EXPECT_FALSE(iter.IsWord());
71 }
72 
TEST(WordIteratorTest,BreakWide16)73 TEST(WordIteratorTest, BreakWide16) {
74   //  "Παγκόσμιος Ιστός"
75   const std::wstring str(L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
76                          L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2");
77   const std::wstring word1(str.substr(0, 10));
78   const std::wstring word2(str.substr(11, 5));
79   WordIterator iter(str, WordIterator::BREAK_WORD);
80   ASSERT_TRUE(iter.Init());
81   EXPECT_TRUE(iter.Advance());
82   EXPECT_TRUE(iter.IsWord());
83   EXPECT_EQ(word1, iter.GetWord());
84   EXPECT_TRUE(iter.Advance());
85   EXPECT_FALSE(iter.IsWord());
86   EXPECT_EQ(L" ", iter.GetWord());
87   EXPECT_TRUE(iter.Advance());
88   EXPECT_TRUE(iter.IsWord());
89   EXPECT_EQ(word2, iter.GetWord());
90   EXPECT_FALSE(iter.Advance());
91   EXPECT_FALSE(iter.IsWord());
92 }
93 
TEST(WordIteratorTest,BreakWide32)94 TEST(WordIteratorTest, BreakWide32) {
95   // U+1D49C MATHEMATICAL SCRIPT CAPITAL A
96   const char *very_wide_char = "\xF0\x9D\x92\x9C";
97   const std::wstring str(
98       base::SysUTF8ToWide(StringPrintf("%s a", very_wide_char)));
99 #if defined(WCHAR_T_IS_UTF16)
100   const std::wstring very_wide_word(str.substr(0, 2));
101 #elif defined(WCHAR_T_IS_UTF32)
102   const std::wstring very_wide_word(str.substr(0, 1));
103 #endif
104   WordIterator iter(str, WordIterator::BREAK_WORD);
105   ASSERT_TRUE(iter.Init());
106   EXPECT_TRUE(iter.Advance());
107   EXPECT_TRUE(iter.IsWord());
108   EXPECT_EQ(very_wide_word, iter.GetWord());
109   EXPECT_TRUE(iter.Advance());
110   EXPECT_FALSE(iter.IsWord());
111   EXPECT_EQ(L" ", iter.GetWord());
112   EXPECT_TRUE(iter.Advance());
113   EXPECT_TRUE(iter.IsWord());
114   EXPECT_EQ(L"a", iter.GetWord());
115   EXPECT_FALSE(iter.Advance());
116   EXPECT_FALSE(iter.IsWord());
117 }
118