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

Side by Side Diff: third_party/libphonenumber/cpp/src/regexp_adapter_test.cc

Issue 8736001: Pull the phone library directly. Delete old version. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years 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 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Author: George Yakovlev
16 // Philippe Liard
17
18 #include "regexp_adapter.h"
19
20 #include <string>
21
22 #include <gtest/gtest.h>
23
24 #include "base/memory/scoped_ptr.h"
25
26 namespace i18n {
27 namespace phonenumbers {
28
29 using std::string;
30
31 class RegExpAdapterTest : public testing::Test {
32 protected:
33 RegExpAdapterTest()
34 : digits_(RegExp::Create("\\d+")),
35 parentheses_digits_(RegExp::Create("\\((\\d+)\\)")),
36 single_digit_(RegExp::Create("\\d")),
37 two_digit_groups_(RegExp::Create("(\\d+)-(\\d+)")) {}
38
39 const scoped_ptr<const RegExp> digits_;
40 const scoped_ptr<const RegExp> parentheses_digits_;
41 const scoped_ptr<const RegExp> single_digit_;
42 const scoped_ptr<const RegExp> two_digit_groups_;
43 };
44
45 TEST_F(RegExpAdapterTest, TestConsumeNoMatch) {
46 const scoped_ptr<RegExpInput> input(RegExpInput::Create("+1-123-456-789"));
47
48 // When 'true' is passed to Consume(), the match occurs from the beginning of
49 // the input.
50 ASSERT_FALSE(digits_->Consume(input.get(), true, NULL, NULL, NULL));
51 ASSERT_EQ("+1-123-456-789", input->ToString());
52
53 string res1;
54 ASSERT_FALSE(parentheses_digits_->Consume(
55 input.get(), true, &res1, NULL, NULL));
56 ASSERT_EQ("+1-123-456-789", input->ToString());
57 ASSERT_EQ("", res1);
58 }
59
60 TEST_F(RegExpAdapterTest, TestConsumeWithNull) {
61 const scoped_ptr<RegExpInput> input(RegExpInput::Create("+123"));
62 const scoped_ptr<const RegExp> plus_sign(RegExp::Create("(\\+)"));
63
64 ASSERT_TRUE(plus_sign->Consume(input.get(), true, NULL, NULL, NULL));
65 ASSERT_EQ("123", input->ToString());
66 }
67
68 TEST_F(RegExpAdapterTest, TestConsumeRetainsMatches) {
69 const scoped_ptr<RegExpInput> input(RegExpInput::Create("1-123-456-789"));
70
71 string res1, res2;
72 ASSERT_TRUE(two_digit_groups_->Consume(
73 input.get(), true, &res1, &res2, NULL));
74 ASSERT_EQ("-456-789", input->ToString());
75 ASSERT_EQ("1", res1);
76 ASSERT_EQ("123", res2);
77 }
78
79 TEST_F(RegExpAdapterTest, TestFindAndConsume) {
80 const scoped_ptr<RegExpInput> input(RegExpInput::Create("+1-123-456-789"));
81
82 // When 'false' is passed to Consume(), the match can occur from any place in
83 // the input.
84 ASSERT_TRUE(digits_->Consume(input.get(), false, NULL, NULL, NULL));
85 ASSERT_EQ("-123-456-789", input->ToString());
86
87 ASSERT_TRUE(digits_->Consume(input.get(), false, NULL, NULL, NULL));
88 ASSERT_EQ("-456-789", input->ToString());
89
90 ASSERT_FALSE(parentheses_digits_->Consume(
91 input.get(), false, NULL, NULL, NULL));
92 ASSERT_EQ("-456-789", input->ToString());
93
94 string res1, res2;
95 ASSERT_TRUE(two_digit_groups_->Consume(
96 input.get(), false, &res1, &res2, NULL));
97 ASSERT_EQ("", input->ToString());
98 ASSERT_EQ("456", res1);
99 ASSERT_EQ("789", res2);
100 }
101
102 TEST(RegExpAdapter, TestPartialMatch) {
103 const scoped_ptr<const RegExp> reg_exp(RegExp::Create("([\\da-z]+)"));
104 string matched;
105
106 EXPECT_TRUE(reg_exp->PartialMatch("12345af", &matched));
107 EXPECT_EQ("12345af", matched);
108
109 EXPECT_TRUE(reg_exp->PartialMatch("12345af", NULL));
110
111 EXPECT_TRUE(reg_exp->PartialMatch("[12]", &matched));
112 EXPECT_EQ("12", matched);
113
114 matched.clear();
115 EXPECT_FALSE(reg_exp->PartialMatch("[]", &matched));
116 EXPECT_EQ("", matched);
117 }
118
119 TEST(RegExpAdapter, TestFullMatch) {
120 const scoped_ptr<const RegExp> reg_exp(RegExp::Create("([\\da-z]+)"));
121 string matched;
122
123 EXPECT_TRUE(reg_exp->FullMatch("12345af", &matched));
124 EXPECT_EQ("12345af", matched);
125
126 EXPECT_TRUE(reg_exp->FullMatch("12345af", NULL));
127
128 matched.clear();
129 EXPECT_FALSE(reg_exp->FullMatch("[12]", &matched));
130 EXPECT_EQ("", matched);
131
132 matched.clear();
133 EXPECT_FALSE(reg_exp->FullMatch("[]", &matched));
134 EXPECT_EQ("", matched);
135 }
136
137 TEST_F(RegExpAdapterTest, TestReplace) {
138 string input("123-4567 ");
139
140 ASSERT_TRUE(single_digit_->Replace(&input, "+"));
141 ASSERT_EQ("+23-4567 ", input);
142
143 ASSERT_TRUE(single_digit_->Replace(&input, "+"));
144 ASSERT_EQ("++3-4567 ", input);
145
146 const scoped_ptr<const RegExp> single_letter(RegExp::Create("[a-z]"));
147 ASSERT_FALSE(single_letter->Replace(&input, "+"));
148 ASSERT_EQ("++3-4567 ", input);
149 }
150
151 TEST_F(RegExpAdapterTest, TestReplaceWithGroup) {
152 // Make sure referencing groups in the regexp in the replacement string works.
153 // $[0-9] notation is used.
154 string input = "123-4567 abc";
155 ASSERT_TRUE(two_digit_groups_->Replace(&input, "$2"));
156 ASSERT_EQ("4567 abc", input);
157
158 input = "123-4567";
159 ASSERT_TRUE(two_digit_groups_->Replace(&input, "$1"));
160 ASSERT_EQ("123", input);
161
162 input = "123-4567";
163 ASSERT_TRUE(two_digit_groups_->Replace(&input, "$2"));
164 ASSERT_EQ("4567", input);
165
166 input = "123-4567";
167 ASSERT_TRUE(two_digit_groups_->Replace(&input, "$1 $2"));
168 ASSERT_EQ("123 4567", input);
169 }
170
171 TEST_F(RegExpAdapterTest, TestReplaceWithDollarSign) {
172 // Make sure '$' can be used in the replacement string when escaped.
173 string input = "123-4567";
174 ASSERT_TRUE(two_digit_groups_->Replace(&input, "\\$1 \\$2"));
175 ASSERT_EQ("$1 $2", input);
176 }
177
178 TEST_F(RegExpAdapterTest, TestGlobalReplace) {
179 string input("123-4567 ");
180
181 ASSERT_TRUE(single_digit_->GlobalReplace(&input, "*"));
182 ASSERT_EQ("***-**** ", input);
183
184 ASSERT_FALSE(single_digit_->GlobalReplace(&input, "*"));
185 ASSERT_EQ("***-**** ", input);
186 }
187
188 TEST(RegExpAdapter, TestUtf8) {
189 const scoped_ptr<const RegExp> reg_exp(RegExp::Create("\xE2\x84\xA1\xE2\x8A\x8 F([\xCE\xB1-\xCF\x89]*)\xE2\x8A\x90"));
190 string matched;
191
192 EXPECT_FALSE(reg_exp->Match("\xE2\x84\xA1\xE2\x8A\x8F" "123\xE2\x8A\x90", true , &matched));
193 EXPECT_TRUE(reg_exp->Match("\xE2\x84\xA1\xE2\x8A\x8F\xCE\xB1\xCE\xB2\xE2\x8A\x 90", true, &matched));
194 EXPECT_EQ("\xCE\xB1\xCE\xB2", matched);
195 }
196
197 } // namespace phonenumbers
198 } // namespace i18n
OLDNEW
« no previous file with comments | « third_party/libphonenumber/cpp/src/regexp_adapter_re2.cc ('k') | third_party/libphonenumber/cpp/src/regexp_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698