OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2009 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: Shaopeng Jia |
| 16 // Author: Lara Rennie |
| 17 // Open-sourced by: Philippe Liard |
| 18 |
| 19 #include <iostream> |
| 20 #include <set> |
| 21 #include <string> |
| 22 |
| 23 #include <gtest/gtest.h> |
| 24 |
| 25 #include "phonemetadata.pb.h" |
| 26 #include "phonenumber.h" |
| 27 #include "phonenumber.pb.h" |
| 28 #include "phonenumberutil.h" |
| 29 #include "regexp_adapter.h" |
| 30 #include "test_metadata.h" |
| 31 |
| 32 namespace i18n { |
| 33 namespace phonenumbers { |
| 34 |
| 35 using std::endl; |
| 36 using std::make_pair; |
| 37 using std::ostream; |
| 38 |
| 39 using google::protobuf::RepeatedPtrField; |
| 40 |
| 41 namespace { |
| 42 |
| 43 // Class containing string constants of region codes for easier testing. |
| 44 class RegionCode { |
| 45 public: |
| 46 static const string& AD() { |
| 47 static const string s = "AD"; |
| 48 return s; |
| 49 } |
| 50 |
| 51 static const string& AO() { |
| 52 static const string s = "AO"; |
| 53 return s; |
| 54 } |
| 55 |
| 56 static const string& AR() { |
| 57 static const string s = "AR"; |
| 58 return s; |
| 59 } |
| 60 |
| 61 static const string& AU() { |
| 62 static const string s = "AU"; |
| 63 return s; |
| 64 } |
| 65 |
| 66 static const string& BS() { |
| 67 static const string s = "BS"; |
| 68 return s; |
| 69 } |
| 70 |
| 71 static const string& CN() { |
| 72 static const string s = "CN"; |
| 73 return s; |
| 74 } |
| 75 |
| 76 static const string& CS() { |
| 77 static const string s = "CS"; |
| 78 return s; |
| 79 } |
| 80 |
| 81 static const string& DE() { |
| 82 static const string s = "DE"; |
| 83 return s; |
| 84 } |
| 85 |
| 86 static const string& GB() { |
| 87 static const string s = "GB"; |
| 88 return s; |
| 89 } |
| 90 |
| 91 static const string& IT() { |
| 92 static const string s = "IT"; |
| 93 return s; |
| 94 } |
| 95 |
| 96 static const string& KR() { |
| 97 static const string s = "KR"; |
| 98 return s; |
| 99 } |
| 100 |
| 101 static const string& MX() { |
| 102 static const string s = "MX"; |
| 103 return s; |
| 104 } |
| 105 |
| 106 static const string& NZ() { |
| 107 static const string s = "NZ"; |
| 108 return s; |
| 109 } |
| 110 |
| 111 static const string& PL() { |
| 112 static const string s = "PL"; |
| 113 return s; |
| 114 } |
| 115 |
| 116 static const string& RE() { |
| 117 static const string s = "RE"; |
| 118 return s; |
| 119 } |
| 120 |
| 121 static const string& SG() { |
| 122 static const string s = "SG"; |
| 123 return s; |
| 124 } |
| 125 |
| 126 static const string& US() { |
| 127 static const string s = "US"; |
| 128 return s; |
| 129 } |
| 130 |
| 131 static const string& YT() { |
| 132 static const string s = "YT"; |
| 133 return s; |
| 134 } |
| 135 |
| 136 // Official code for the unknown region. |
| 137 static const string& ZZ() { |
| 138 static const string s = "ZZ"; |
| 139 return s; |
| 140 } |
| 141 }; |
| 142 |
| 143 } // namespace |
| 144 |
| 145 class TestMetadataProvider : public PhoneNumberUtil::MetadataProvider { |
| 146 public: |
| 147 virtual ~TestMetadataProvider() {} |
| 148 |
| 149 pair<const void*, unsigned> operator()() { |
| 150 return make_pair(test_metadata_get(), test_metadata_size()); |
| 151 } |
| 152 }; |
| 153 |
| 154 class PhoneNumberUtilTest : public testing::Test { |
| 155 protected: |
| 156 PhoneNumberUtilTest() : phone_util_(&provider_) {} |
| 157 |
| 158 // Wrapper functions for private functions that we want to test. |
| 159 const PhoneMetadata* GetPhoneMetadata(const string& region_code) const { |
| 160 return phone_util_.GetMetadataForRegion(region_code); |
| 161 } |
| 162 |
| 163 void GetSupportedRegions(set<string>* regions) { |
| 164 phone_util_.GetSupportedRegions(regions); |
| 165 } |
| 166 |
| 167 void ExtractPossibleNumber(const string& number, |
| 168 string* extracted_number) const { |
| 169 PhoneNumberUtil::ExtractPossibleNumber(number, extracted_number); |
| 170 } |
| 171 |
| 172 bool IsViablePhoneNumber(const string& number) const { |
| 173 return PhoneNumberUtil::IsViablePhoneNumber(number); |
| 174 } |
| 175 |
| 176 void Normalize(string* number) const { |
| 177 phone_util_.Normalize(number); |
| 178 } |
| 179 |
| 180 bool IsLeadingZeroPossible(int country_calling_code) const { |
| 181 return phone_util_.IsLeadingZeroPossible(country_calling_code); |
| 182 } |
| 183 |
| 184 PhoneNumber::CountryCodeSource MaybeStripInternationalPrefixAndNormalize( |
| 185 const string& possible_idd_prefix, |
| 186 string* number) const { |
| 187 return phone_util_.MaybeStripInternationalPrefixAndNormalize( |
| 188 possible_idd_prefix, |
| 189 number); |
| 190 } |
| 191 |
| 192 void MaybeStripNationalPrefixAndCarrierCode(const PhoneMetadata& metadata, |
| 193 string* number, |
| 194 string* carrier_code) const { |
| 195 PhoneNumberUtil::MaybeStripNationalPrefixAndCarrierCode(metadata, number, |
| 196 carrier_code); |
| 197 } |
| 198 |
| 199 bool MaybeStripExtension(string* number, string* extension) const { |
| 200 return PhoneNumberUtil::MaybeStripExtension(number, extension); |
| 201 } |
| 202 |
| 203 PhoneNumberUtil::ErrorType MaybeExtractCountryCode( |
| 204 const PhoneMetadata* default_region_metadata, |
| 205 bool keep_raw_input, |
| 206 string* national_number, |
| 207 PhoneNumber* phone_number) const { |
| 208 return phone_util_.MaybeExtractCountryCode(default_region_metadata, |
| 209 keep_raw_input, |
| 210 national_number, |
| 211 phone_number); |
| 212 } |
| 213 |
| 214 void GetNddPrefixForRegion(const string& region, |
| 215 bool strip_non_digits, |
| 216 string* ndd_prefix) const { |
| 217 // For testing purposes, we check this is empty first. |
| 218 ndd_prefix->clear(); |
| 219 phone_util_.GetNddPrefixForRegion(region, strip_non_digits, ndd_prefix); |
| 220 } |
| 221 |
| 222 static bool Equals(const PhoneNumberDesc& expected_number, |
| 223 const PhoneNumberDesc& actual_number) { |
| 224 return ExactlySameAs(expected_number, actual_number); |
| 225 } |
| 226 |
| 227 TestMetadataProvider provider_; |
| 228 PhoneNumberUtil phone_util_; |
| 229 }; |
| 230 |
| 231 // Provides PhoneNumber comparison operators to support the use of EXPECT_EQ and |
| 232 // EXPECT_NE in the unittests. |
| 233 bool operator==(const PhoneNumber& number1, const PhoneNumber& number2) { |
| 234 return ExactlySameAs(number1, number2); |
| 235 } |
| 236 |
| 237 bool operator!=(const PhoneNumber& number1, const PhoneNumber& number2) { |
| 238 return !(number1 == number2); |
| 239 } |
| 240 |
| 241 // Needed by Google Test to display errors. |
| 242 ostream& operator<<(ostream& os, const PhoneNumber& number) { |
| 243 os << endl |
| 244 << "country_code: " << number.country_code() << endl |
| 245 << "national_number: " << number.national_number() << endl; |
| 246 if (number.has_extension()) { |
| 247 os << "extension: " << number.extension() << endl; |
| 248 } |
| 249 if (number.has_italian_leading_zero()) { |
| 250 os << "italian_leading_zero: " << number.italian_leading_zero() << endl; |
| 251 } |
| 252 if (number.has_raw_input()) { |
| 253 os << "raw_input: " << number.raw_input() << endl; |
| 254 } |
| 255 if (number.has_country_code_source()) { |
| 256 os << "country_code_source: " << number.country_code_source() << endl; |
| 257 } |
| 258 if (number.has_preferred_domestic_carrier_code()) { |
| 259 os << "preferred_domestic_carrier_code: " |
| 260 << number.preferred_domestic_carrier_code() << endl; |
| 261 } |
| 262 return os; |
| 263 } |
| 264 |
| 265 TEST_F(PhoneNumberUtilTest, GetSupportedRegions) { |
| 266 set<string> regions; |
| 267 |
| 268 GetSupportedRegions(®ions); |
| 269 EXPECT_GT(regions.size(), 0U); |
| 270 } |
| 271 |
| 272 TEST_F(PhoneNumberUtilTest, GetInstanceLoadUSMetadata) { |
| 273 const PhoneMetadata* metadata = GetPhoneMetadata(RegionCode::US()); |
| 274 EXPECT_EQ("US", metadata->id()); |
| 275 EXPECT_EQ(1, metadata->country_code()); |
| 276 EXPECT_EQ("011", metadata->international_prefix()); |
| 277 EXPECT_TRUE(metadata->has_national_prefix()); |
| 278 ASSERT_EQ(2, metadata->number_format_size()); |
| 279 EXPECT_EQ("(\\d{3})(\\d{3})(\\d{4})", |
| 280 metadata->number_format(0).pattern()); |
| 281 EXPECT_EQ("$1 $2 $3", metadata->number_format(0).format()); |
| 282 EXPECT_EQ("[13-9]\\d{9}|2[0-35-9]\\d{8}", |
| 283 metadata->general_desc().national_number_pattern()); |
| 284 EXPECT_EQ("\\d{7}(?:\\d{3})?", |
| 285 metadata->general_desc().possible_number_pattern()); |
| 286 EXPECT_TRUE(Equals(metadata->general_desc(), metadata->fixed_line())); |
| 287 EXPECT_EQ("\\d{10}", metadata->toll_free().possible_number_pattern()); |
| 288 EXPECT_EQ("900\\d{7}", metadata->premium_rate().national_number_pattern()); |
| 289 // No shared-cost data is available, so it should be initialised to "NA". |
| 290 EXPECT_EQ("NA", metadata->shared_cost().national_number_pattern()); |
| 291 EXPECT_EQ("NA", metadata->shared_cost().possible_number_pattern()); |
| 292 } |
| 293 |
| 294 TEST_F(PhoneNumberUtilTest, GetInstanceLoadDEMetadata) { |
| 295 const PhoneMetadata* metadata = GetPhoneMetadata(RegionCode::DE()); |
| 296 EXPECT_EQ("DE", metadata->id()); |
| 297 EXPECT_EQ(49, metadata->country_code()); |
| 298 EXPECT_EQ("00", metadata->international_prefix()); |
| 299 EXPECT_EQ("0", metadata->national_prefix()); |
| 300 ASSERT_EQ(6, metadata->number_format_size()); |
| 301 EXPECT_EQ(1, metadata->number_format(5).leading_digits_pattern_size()); |
| 302 EXPECT_EQ("900", metadata->number_format(5).leading_digits_pattern(0)); |
| 303 EXPECT_EQ("(\\d{3})(\\d{3,4})(\\d{4})", |
| 304 metadata->number_format(5).pattern()); |
| 305 EXPECT_EQ("$1 $2 $3", metadata->number_format(5).format()); |
| 306 EXPECT_EQ("(?:[24-6]\\d{2}|3[03-9]\\d|[789](?:[1-9]\\d|0[2-9]))\\d{3,8}", |
| 307 metadata->fixed_line().national_number_pattern()); |
| 308 EXPECT_EQ("\\d{2,14}", metadata->fixed_line().possible_number_pattern()); |
| 309 EXPECT_EQ("30123456", metadata->fixed_line().example_number()); |
| 310 EXPECT_EQ("\\d{10}", metadata->toll_free().possible_number_pattern()); |
| 311 EXPECT_EQ("900([135]\\d{6}|9\\d{7})", |
| 312 metadata->premium_rate().national_number_pattern()); |
| 313 } |
| 314 |
| 315 TEST_F(PhoneNumberUtilTest, GetInstanceLoadARMetadata) { |
| 316 const PhoneMetadata* metadata = GetPhoneMetadata(RegionCode::AR()); |
| 317 EXPECT_EQ("AR", metadata->id()); |
| 318 EXPECT_EQ(54, metadata->country_code()); |
| 319 EXPECT_EQ("00", metadata->international_prefix()); |
| 320 EXPECT_EQ("0", metadata->national_prefix()); |
| 321 EXPECT_EQ("0(?:(11|343|3715)15)?", metadata->national_prefix_for_parsing()); |
| 322 EXPECT_EQ("9$1", metadata->national_prefix_transform_rule()); |
| 323 ASSERT_EQ(5, metadata->number_format_size()); |
| 324 EXPECT_EQ("$1 15 $2-$3", metadata->number_format(2).format()); |
| 325 EXPECT_EQ("9(\\d{4})(\\d{2})(\\d{4})", metadata->number_format(3).pattern()); |
| 326 EXPECT_EQ("(9)(\\d{4})(\\d{2})(\\d{4})", |
| 327 metadata->intl_number_format(3).pattern()); |
| 328 EXPECT_EQ("$1 $2 $3 $4", metadata->intl_number_format(3).format()); |
| 329 } |
| 330 |
| 331 TEST_F(PhoneNumberUtilTest, GetNationalSignificantNumber) { |
| 332 PhoneNumber number; |
| 333 number.set_country_code(1); |
| 334 number.set_national_number(6502530000ULL); |
| 335 string national_significant_number; |
| 336 phone_util_.GetNationalSignificantNumber(number, |
| 337 &national_significant_number); |
| 338 EXPECT_EQ("6502530000", national_significant_number); |
| 339 |
| 340 // An Italian mobile number. |
| 341 national_significant_number.clear(); |
| 342 number.set_country_code(39); |
| 343 number.set_national_number(312345678ULL); |
| 344 phone_util_.GetNationalSignificantNumber(number, |
| 345 &national_significant_number); |
| 346 EXPECT_EQ("312345678", national_significant_number); |
| 347 |
| 348 // An Italian fixed line number. |
| 349 national_significant_number.clear(); |
| 350 number.set_country_code(39); |
| 351 number.set_national_number(236618300ULL); |
| 352 number.set_italian_leading_zero(true); |
| 353 phone_util_.GetNationalSignificantNumber(number, |
| 354 &national_significant_number); |
| 355 EXPECT_EQ("0236618300", national_significant_number); |
| 356 } |
| 357 |
| 358 TEST_F(PhoneNumberUtilTest, GetExampleNumber) { |
| 359 PhoneNumber de_number; |
| 360 de_number.set_country_code(49); |
| 361 de_number.set_national_number(30123456ULL); |
| 362 PhoneNumber test_number; |
| 363 bool success = phone_util_.GetExampleNumber(RegionCode::DE(), &test_number); |
| 364 EXPECT_TRUE(success); |
| 365 EXPECT_EQ(de_number, test_number); |
| 366 success = phone_util_.GetExampleNumberForType(RegionCode::DE(), |
| 367 PhoneNumberUtil::FIXED_LINE, |
| 368 &test_number); |
| 369 EXPECT_TRUE(success); |
| 370 EXPECT_EQ(de_number, test_number); |
| 371 test_number.Clear(); |
| 372 success = phone_util_.GetExampleNumberForType(RegionCode::DE(), |
| 373 PhoneNumberUtil::MOBILE, |
| 374 &test_number); |
| 375 // Here we test that an example number was not returned, and that the number |
| 376 // passed in was not modified. |
| 377 EXPECT_FALSE(success); |
| 378 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 379 // For the US, the example number is placed under general description, and |
| 380 // hence should be used for both fixed line and mobile, so neither of these |
| 381 // should return null. |
| 382 test_number.Clear(); |
| 383 success = phone_util_.GetExampleNumberForType(RegionCode::US(), |
| 384 PhoneNumberUtil::FIXED_LINE, |
| 385 &test_number); |
| 386 // Here we test that the call to get an example number succeeded, and that the |
| 387 // number passed in was modified. |
| 388 EXPECT_TRUE(success); |
| 389 EXPECT_NE(PhoneNumber::default_instance(), test_number); |
| 390 test_number.Clear(); |
| 391 success = phone_util_.GetExampleNumberForType(RegionCode::US(), |
| 392 PhoneNumberUtil::MOBILE, |
| 393 &test_number); |
| 394 EXPECT_TRUE(success); |
| 395 EXPECT_NE(PhoneNumber::default_instance(), test_number); |
| 396 } |
| 397 |
| 398 TEST_F(PhoneNumberUtilTest, FormatUSNumber) { |
| 399 PhoneNumber test_number; |
| 400 string formatted_number; |
| 401 test_number.set_country_code(1); |
| 402 test_number.set_national_number(6502530000ULL); |
| 403 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 404 EXPECT_EQ("650 253 0000", formatted_number); |
| 405 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 406 &formatted_number); |
| 407 EXPECT_EQ("+1 650 253 0000", formatted_number); |
| 408 |
| 409 test_number.set_national_number(8002530000ULL); |
| 410 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 411 EXPECT_EQ("800 253 0000", formatted_number); |
| 412 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 413 &formatted_number); |
| 414 EXPECT_EQ("+1 800 253 0000", formatted_number); |
| 415 |
| 416 test_number.set_national_number(9002530000ULL); |
| 417 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 418 EXPECT_EQ("900 253 0000", formatted_number); |
| 419 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 420 &formatted_number); |
| 421 EXPECT_EQ("+1 900 253 0000", formatted_number); |
| 422 phone_util_.Format(test_number, PhoneNumberUtil::RFC3966, &formatted_number); |
| 423 EXPECT_EQ("+1-900-253-0000", formatted_number); |
| 424 } |
| 425 |
| 426 TEST_F(PhoneNumberUtilTest, FormatBSNumber) { |
| 427 PhoneNumber test_number; |
| 428 string formatted_number; |
| 429 test_number.set_country_code(1); |
| 430 test_number.set_national_number(2421234567ULL); |
| 431 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 432 EXPECT_EQ("242 123 4567", formatted_number); |
| 433 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 434 &formatted_number); |
| 435 EXPECT_EQ("+1 242 123 4567", formatted_number); |
| 436 |
| 437 test_number.set_national_number(8002530000ULL); |
| 438 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 439 EXPECT_EQ("800 253 0000", formatted_number); |
| 440 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 441 &formatted_number); |
| 442 EXPECT_EQ("+1 800 253 0000", formatted_number); |
| 443 |
| 444 test_number.set_national_number(9002530000ULL); |
| 445 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 446 EXPECT_EQ("900 253 0000", formatted_number); |
| 447 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 448 &formatted_number); |
| 449 EXPECT_EQ("+1 900 253 0000", formatted_number); |
| 450 } |
| 451 |
| 452 TEST_F(PhoneNumberUtilTest, FormatGBNumber) { |
| 453 PhoneNumber test_number; |
| 454 string formatted_number; |
| 455 test_number.set_country_code(44); |
| 456 test_number.set_national_number(2087389353ULL); |
| 457 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 458 EXPECT_EQ("(020) 8738 9353", formatted_number); |
| 459 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 460 &formatted_number); |
| 461 EXPECT_EQ("+44 20 8738 9353", formatted_number); |
| 462 |
| 463 test_number.set_national_number(7912345678ULL); |
| 464 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 465 EXPECT_EQ("(07912) 345 678", formatted_number); |
| 466 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 467 &formatted_number); |
| 468 EXPECT_EQ("+44 7912 345 678", formatted_number); |
| 469 } |
| 470 |
| 471 TEST_F(PhoneNumberUtilTest, FormatDENumber) { |
| 472 PhoneNumber test_number; |
| 473 string formatted_number; |
| 474 test_number.set_country_code(49); |
| 475 test_number.set_national_number(301234ULL); |
| 476 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 477 EXPECT_EQ("030/1234", formatted_number); |
| 478 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 479 &formatted_number); |
| 480 EXPECT_EQ("+49 30/1234", formatted_number); |
| 481 phone_util_.Format(test_number, PhoneNumberUtil::RFC3966, &formatted_number); |
| 482 EXPECT_EQ("+49-30-1234", formatted_number); |
| 483 |
| 484 test_number.set_national_number(291123ULL); |
| 485 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 486 EXPECT_EQ("0291 123", formatted_number); |
| 487 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 488 &formatted_number); |
| 489 EXPECT_EQ("+49 291 123", formatted_number); |
| 490 |
| 491 test_number.set_national_number(29112345678ULL); |
| 492 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 493 EXPECT_EQ("0291 12345678", formatted_number); |
| 494 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 495 &formatted_number); |
| 496 EXPECT_EQ("+49 291 12345678", formatted_number); |
| 497 |
| 498 test_number.set_national_number(9123123ULL); |
| 499 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 500 EXPECT_EQ("09123 123", formatted_number); |
| 501 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 502 &formatted_number); |
| 503 EXPECT_EQ("+49 9123 123", formatted_number); |
| 504 |
| 505 test_number.set_national_number(80212345ULL); |
| 506 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 507 EXPECT_EQ("08021 2345", formatted_number); |
| 508 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 509 &formatted_number); |
| 510 EXPECT_EQ("+49 8021 2345", formatted_number); |
| 511 |
| 512 test_number.set_national_number(1234ULL); |
| 513 // Note this number is correctly formatted without national prefix. Most of |
| 514 // the numbers that are treated as invalid numbers by the library are short |
| 515 // numbers, and they are usually not dialed with national prefix. |
| 516 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 517 EXPECT_EQ("1234", formatted_number); |
| 518 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 519 &formatted_number); |
| 520 EXPECT_EQ("+49 1234", formatted_number); |
| 521 } |
| 522 |
| 523 TEST_F(PhoneNumberUtilTest, FormatITNumber) { |
| 524 PhoneNumber test_number; |
| 525 string formatted_number; |
| 526 test_number.set_country_code(39); |
| 527 test_number.set_national_number(236618300ULL); |
| 528 test_number.set_italian_leading_zero(true); |
| 529 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 530 EXPECT_EQ("02 3661 8300", formatted_number); |
| 531 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 532 &formatted_number); |
| 533 EXPECT_EQ("+39 02 3661 8300", formatted_number); |
| 534 phone_util_.Format(test_number, PhoneNumberUtil::E164, |
| 535 &formatted_number); |
| 536 EXPECT_EQ("+390236618300", formatted_number); |
| 537 |
| 538 test_number.set_national_number(345678901ULL); |
| 539 test_number.set_italian_leading_zero(false); |
| 540 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 541 EXPECT_EQ("345 678 901", formatted_number); |
| 542 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 543 &formatted_number); |
| 544 EXPECT_EQ("+39 345 678 901", formatted_number); |
| 545 phone_util_.Format(test_number, PhoneNumberUtil::E164, |
| 546 &formatted_number); |
| 547 EXPECT_EQ("+39345678901", formatted_number); |
| 548 } |
| 549 |
| 550 TEST_F(PhoneNumberUtilTest, FormatAUNumber) { |
| 551 PhoneNumber test_number; |
| 552 string formatted_number; |
| 553 test_number.set_country_code(61); |
| 554 test_number.set_national_number(236618300ULL); |
| 555 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 556 EXPECT_EQ("02 3661 8300", formatted_number); |
| 557 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 558 &formatted_number); |
| 559 EXPECT_EQ("+61 2 3661 8300", formatted_number); |
| 560 phone_util_.Format(test_number, PhoneNumberUtil::E164, |
| 561 &formatted_number); |
| 562 EXPECT_EQ("+61236618300", formatted_number); |
| 563 |
| 564 test_number.set_national_number(1800123456ULL); |
| 565 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 566 EXPECT_EQ("1800 123 456", formatted_number); |
| 567 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 568 &formatted_number); |
| 569 EXPECT_EQ("+61 1800 123 456", formatted_number); |
| 570 phone_util_.Format(test_number, PhoneNumberUtil::E164, |
| 571 &formatted_number); |
| 572 EXPECT_EQ("+611800123456", formatted_number); |
| 573 } |
| 574 |
| 575 TEST_F(PhoneNumberUtilTest, FormatARNumber) { |
| 576 PhoneNumber test_number; |
| 577 string formatted_number; |
| 578 test_number.set_country_code(54); |
| 579 test_number.set_national_number(1187654321ULL); |
| 580 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 581 EXPECT_EQ("011 8765-4321", formatted_number); |
| 582 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 583 &formatted_number); |
| 584 EXPECT_EQ("+54 11 8765-4321", formatted_number); |
| 585 phone_util_.Format(test_number, PhoneNumberUtil::E164, |
| 586 &formatted_number); |
| 587 EXPECT_EQ("+541187654321", formatted_number); |
| 588 |
| 589 test_number.set_national_number(91187654321ULL); |
| 590 phone_util_.Format(test_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 591 EXPECT_EQ("011 15 8765-4321", formatted_number); |
| 592 phone_util_.Format(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 593 &formatted_number); |
| 594 EXPECT_EQ("+54 9 11 8765 4321", formatted_number); |
| 595 phone_util_.Format(test_number, PhoneNumberUtil::E164, |
| 596 &formatted_number); |
| 597 EXPECT_EQ("+5491187654321", formatted_number); |
| 598 } |
| 599 |
| 600 TEST_F(PhoneNumberUtilTest, FormatOutOfCountryCallingNumber) { |
| 601 PhoneNumber test_number; |
| 602 string formatted_number; |
| 603 test_number.set_country_code(1); |
| 604 test_number.set_national_number(9002530000ULL); |
| 605 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::DE(), |
| 606 &formatted_number); |
| 607 EXPECT_EQ("00 1 900 253 0000", formatted_number); |
| 608 |
| 609 test_number.set_national_number(6502530000ULL); |
| 610 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::BS(), |
| 611 &formatted_number); |
| 612 EXPECT_EQ("1 650 253 0000", formatted_number); |
| 613 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::PL(), |
| 614 &formatted_number); |
| 615 EXPECT_EQ("0~0 1 650 253 0000", formatted_number); |
| 616 |
| 617 test_number.set_country_code(44); |
| 618 test_number.set_national_number(7912345678ULL); |
| 619 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::US(), |
| 620 &formatted_number); |
| 621 EXPECT_EQ("011 44 7912 345 678", formatted_number); |
| 622 |
| 623 test_number.set_country_code(49); |
| 624 test_number.set_national_number(1234ULL); |
| 625 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::GB(), |
| 626 &formatted_number); |
| 627 EXPECT_EQ("00 49 1234", formatted_number); |
| 628 // Note this number is correctly formatted without national prefix. Most of |
| 629 // the numbers that are treated as invalid numbers by the library are short |
| 630 // numbers, and they are usually not dialed with national prefix. |
| 631 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::DE(), |
| 632 &formatted_number); |
| 633 EXPECT_EQ("1234", formatted_number); |
| 634 |
| 635 test_number.set_country_code(39); |
| 636 test_number.set_national_number(236618300ULL); |
| 637 test_number.set_italian_leading_zero(true); |
| 638 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::US(), |
| 639 &formatted_number); |
| 640 EXPECT_EQ("011 39 02 3661 8300", formatted_number); |
| 641 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::IT(), |
| 642 &formatted_number); |
| 643 EXPECT_EQ("02 3661 8300", formatted_number); |
| 644 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::SG(), |
| 645 &formatted_number); |
| 646 EXPECT_EQ("+39 02 3661 8300", formatted_number); |
| 647 |
| 648 test_number.set_country_code(65); |
| 649 test_number.set_national_number(94777892ULL); |
| 650 test_number.set_italian_leading_zero(false); |
| 651 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::SG(), |
| 652 &formatted_number); |
| 653 EXPECT_EQ("9477 7892", formatted_number); |
| 654 |
| 655 test_number.set_country_code(54); |
| 656 test_number.set_national_number(91187654321ULL); |
| 657 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::US(), |
| 658 &formatted_number); |
| 659 EXPECT_EQ("011 54 9 11 8765 4321", formatted_number); |
| 660 |
| 661 test_number.set_extension("1234"); |
| 662 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::US(), |
| 663 &formatted_number); |
| 664 EXPECT_EQ("011 54 9 11 8765 4321 ext. 1234", formatted_number); |
| 665 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::AU(), |
| 666 &formatted_number); |
| 667 EXPECT_EQ("0011 54 9 11 8765 4321 ext. 1234", formatted_number); |
| 668 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::AR(), |
| 669 &formatted_number); |
| 670 EXPECT_EQ("011 15 8765-4321 ext. 1234", formatted_number); |
| 671 } |
| 672 |
| 673 TEST_F(PhoneNumberUtilTest, FormatOutOfCountryWithPreferredIntlPrefix) { |
| 674 PhoneNumber test_number; |
| 675 string formatted_number; |
| 676 test_number.set_country_code(39); |
| 677 test_number.set_national_number(236618300ULL); |
| 678 test_number.set_italian_leading_zero(true); |
| 679 // This should use 0011, since that is the preferred international prefix |
| 680 // (both 0011 and 0012 are accepted as possible international prefixes in our |
| 681 // test metadta.) |
| 682 phone_util_.FormatOutOfCountryCallingNumber(test_number, RegionCode::AU(), |
| 683 &formatted_number); |
| 684 EXPECT_EQ("0011 39 02 3661 8300", formatted_number); |
| 685 } |
| 686 |
| 687 TEST_F(PhoneNumberUtilTest, FormatOutOfCountryKeepingAlphaChars) { |
| 688 PhoneNumber alpha_numeric_number; |
| 689 string formatted_number; |
| 690 alpha_numeric_number.set_country_code(1); |
| 691 alpha_numeric_number.set_national_number(8007493524ULL); |
| 692 alpha_numeric_number.set_raw_input("1800 six-flag"); |
| 693 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 694 RegionCode::AU(), |
| 695 &formatted_number); |
| 696 EXPECT_EQ("0011 1 800 SIX-FLAG", formatted_number); |
| 697 |
| 698 formatted_number.clear(); |
| 699 alpha_numeric_number.set_raw_input("1-800-SIX-flag"); |
| 700 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 701 RegionCode::AU(), |
| 702 &formatted_number); |
| 703 EXPECT_EQ("0011 1 800-SIX-FLAG", formatted_number); |
| 704 |
| 705 formatted_number.clear(); |
| 706 alpha_numeric_number.set_raw_input("Call us from UK: 00 1 800 SIX-flag"); |
| 707 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 708 RegionCode::AU(), |
| 709 &formatted_number); |
| 710 EXPECT_EQ("0011 1 800 SIX-FLAG", formatted_number); |
| 711 |
| 712 formatted_number.clear(); |
| 713 alpha_numeric_number.set_raw_input("800 SIX-flag"); |
| 714 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 715 RegionCode::AU(), |
| 716 &formatted_number); |
| 717 EXPECT_EQ("0011 1 800 SIX-FLAG", formatted_number); |
| 718 |
| 719 // Formatting from within the NANPA region. |
| 720 formatted_number.clear(); |
| 721 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 722 RegionCode::US(), |
| 723 &formatted_number); |
| 724 EXPECT_EQ("1 800 SIX-FLAG", formatted_number); |
| 725 formatted_number.clear(); |
| 726 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 727 RegionCode::BS(), |
| 728 &formatted_number); |
| 729 EXPECT_EQ("1 800 SIX-FLAG", formatted_number); |
| 730 |
| 731 // Testing that if the raw input doesn't exist, it is formatted using |
| 732 // FormatOutOfCountryCallingNumber. |
| 733 alpha_numeric_number.clear_raw_input(); |
| 734 formatted_number.clear(); |
| 735 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 736 RegionCode::DE(), |
| 737 &formatted_number); |
| 738 EXPECT_EQ("00 1 800 749 3524", formatted_number); |
| 739 |
| 740 // Testing AU alpha number formatted from Australia. |
| 741 alpha_numeric_number.set_country_code(61); |
| 742 alpha_numeric_number.set_national_number(827493524ULL); |
| 743 alpha_numeric_number.set_raw_input("+61 82749-FLAG"); |
| 744 formatted_number.clear(); |
| 745 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 746 RegionCode::AU(), |
| 747 &formatted_number); |
| 748 // This number should have the national prefix prefixed. |
| 749 EXPECT_EQ("082749-FLAG", formatted_number); |
| 750 |
| 751 alpha_numeric_number.set_raw_input("082749-FLAG"); |
| 752 formatted_number.clear(); |
| 753 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 754 RegionCode::AU(), |
| 755 &formatted_number); |
| 756 EXPECT_EQ("082749-FLAG", formatted_number); |
| 757 |
| 758 alpha_numeric_number.set_national_number(18007493524ULL); |
| 759 alpha_numeric_number.set_raw_input("1-800-SIX-flag"); |
| 760 formatted_number.clear(); |
| 761 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 762 RegionCode::AU(), |
| 763 &formatted_number); |
| 764 // This number should not have the national prefix prefixed, in accordance |
| 765 // with the override for this specific formatting rule. |
| 766 EXPECT_EQ("1-800-SIX-FLAG", formatted_number); |
| 767 // The metadata should not be permanently changed, since we copied it before |
| 768 // modifying patterns. Here we check this. |
| 769 formatted_number.clear(); |
| 770 alpha_numeric_number.set_national_number(1800749352ULL); |
| 771 phone_util_.FormatOutOfCountryCallingNumber(alpha_numeric_number, |
| 772 RegionCode::AU(), |
| 773 &formatted_number); |
| 774 EXPECT_EQ("1800 749 352", formatted_number); |
| 775 |
| 776 // Testing a country with multiple international prefixes. |
| 777 formatted_number.clear(); |
| 778 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 779 RegionCode::SG(), |
| 780 &formatted_number); |
| 781 EXPECT_EQ("+61 1-800-SIX-FLAG", formatted_number); |
| 782 |
| 783 // Testing the case with an invalid country code. |
| 784 formatted_number.clear(); |
| 785 alpha_numeric_number.set_country_code(0); |
| 786 alpha_numeric_number.set_national_number(18007493524ULL); |
| 787 alpha_numeric_number.set_raw_input("1-800-SIX-flag"); |
| 788 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 789 RegionCode::DE(), |
| 790 &formatted_number); |
| 791 // Uses the raw input only. |
| 792 EXPECT_EQ("1-800-SIX-flag", formatted_number); |
| 793 |
| 794 // Testing the case of an invalid alpha number. |
| 795 formatted_number.clear(); |
| 796 alpha_numeric_number.set_country_code(1); |
| 797 alpha_numeric_number.set_national_number(80749ULL); |
| 798 alpha_numeric_number.set_raw_input("180-SIX"); |
| 799 phone_util_.FormatOutOfCountryKeepingAlphaChars(alpha_numeric_number, |
| 800 RegionCode::DE(), |
| 801 &formatted_number); |
| 802 // No country-code stripping can be done. |
| 803 EXPECT_EQ("00 1 180-SIX", formatted_number); |
| 804 } |
| 805 |
| 806 TEST_F(PhoneNumberUtilTest, FormatWithCarrierCode) { |
| 807 // We only support this for AR in our test metadata. |
| 808 PhoneNumber ar_number; |
| 809 string formatted_number; |
| 810 ar_number.set_country_code(54); |
| 811 ar_number.set_national_number(91234125678ULL); |
| 812 phone_util_.Format(ar_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 813 EXPECT_EQ("01234 12-5678", formatted_number); |
| 814 // Test formatting with a carrier code. |
| 815 phone_util_.FormatNationalNumberWithCarrierCode(ar_number, "15", |
| 816 &formatted_number); |
| 817 EXPECT_EQ("01234 15 12-5678", formatted_number); |
| 818 phone_util_.FormatNationalNumberWithCarrierCode(ar_number, "", |
| 819 &formatted_number); |
| 820 EXPECT_EQ("01234 12-5678", formatted_number); |
| 821 // Here the international rule is used, so no carrier code should be present. |
| 822 phone_util_.Format(ar_number, PhoneNumberUtil::E164, &formatted_number); |
| 823 EXPECT_EQ("+5491234125678", formatted_number); |
| 824 // We don't support this for the US so there should be no change. |
| 825 PhoneNumber us_number; |
| 826 us_number.set_country_code(1); |
| 827 us_number.set_national_number(4241231234ULL); |
| 828 phone_util_.Format(us_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 829 EXPECT_EQ("424 123 1234", formatted_number); |
| 830 phone_util_.FormatNationalNumberWithCarrierCode(us_number, "15", |
| 831 &formatted_number); |
| 832 EXPECT_EQ("424 123 1234", formatted_number); |
| 833 } |
| 834 |
| 835 TEST_F(PhoneNumberUtilTest, FormatWithPreferredCarrierCode) { |
| 836 // We only support this for AR in our test metadata. |
| 837 PhoneNumber ar_number; |
| 838 string formatted_number; |
| 839 ar_number.set_country_code(54); |
| 840 ar_number.set_national_number(91234125678ULL); |
| 841 // Test formatting with no preferred carrier code stored in the number itself. |
| 842 phone_util_.FormatNationalNumberWithPreferredCarrierCode(ar_number, "15", |
| 843 &formatted_number); |
| 844 EXPECT_EQ("01234 15 12-5678", formatted_number); |
| 845 phone_util_.FormatNationalNumberWithPreferredCarrierCode(ar_number, "", |
| 846 &formatted_number); |
| 847 EXPECT_EQ("01234 12-5678", formatted_number); |
| 848 // Test formatting with preferred carrier code present. |
| 849 ar_number.set_preferred_domestic_carrier_code("19"); |
| 850 phone_util_.Format(ar_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 851 EXPECT_EQ("01234 12-5678", formatted_number); |
| 852 phone_util_.FormatNationalNumberWithPreferredCarrierCode(ar_number, "15", |
| 853 &formatted_number); |
| 854 EXPECT_EQ("01234 19 12-5678", formatted_number); |
| 855 phone_util_.FormatNationalNumberWithPreferredCarrierCode(ar_number, "", |
| 856 &formatted_number); |
| 857 EXPECT_EQ("01234 19 12-5678", formatted_number); |
| 858 // When the preferred_domestic_carrier_code is present (even when it contains |
| 859 // an empty string), use it instead of the default carrier code passed in. |
| 860 ar_number.set_preferred_domestic_carrier_code(""); |
| 861 phone_util_.FormatNationalNumberWithPreferredCarrierCode(ar_number, "15", |
| 862 &formatted_number); |
| 863 EXPECT_EQ("01234 12-5678", formatted_number); |
| 864 // We don't support this for the US so there should be no change. |
| 865 PhoneNumber us_number; |
| 866 us_number.set_country_code(1); |
| 867 us_number.set_national_number(4241231234ULL); |
| 868 us_number.set_preferred_domestic_carrier_code("99"); |
| 869 phone_util_.Format(us_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 870 EXPECT_EQ("424 123 1234", formatted_number); |
| 871 phone_util_.FormatNationalNumberWithPreferredCarrierCode(us_number, "15", |
| 872 &formatted_number); |
| 873 EXPECT_EQ("424 123 1234", formatted_number); |
| 874 } |
| 875 |
| 876 TEST_F(PhoneNumberUtilTest, FormatByPattern) { |
| 877 PhoneNumber test_number; |
| 878 string formatted_number; |
| 879 test_number.set_country_code(1); |
| 880 test_number.set_national_number(6502530000ULL); |
| 881 |
| 882 RepeatedPtrField<NumberFormat> number_formats; |
| 883 NumberFormat* number_format = number_formats.Add(); |
| 884 number_format->set_pattern("(\\d{3})(\\d{3})(\\d{4})"); |
| 885 number_format->set_format("($1) $2-$3"); |
| 886 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::NATIONAL, |
| 887 number_formats, |
| 888 &formatted_number); |
| 889 EXPECT_EQ("(650) 253-0000", formatted_number); |
| 890 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 891 number_formats, |
| 892 &formatted_number); |
| 893 EXPECT_EQ("+1 (650) 253-0000", formatted_number); |
| 894 |
| 895 // $NP is set to '1' for the US. Here we check that for other NANPA countries |
| 896 // the US rules are followed. |
| 897 number_format->set_national_prefix_formatting_rule("$NP ($FG)"); |
| 898 number_format->set_format("$1 $2-$3"); |
| 899 test_number.set_country_code(1); |
| 900 test_number.set_national_number(4168819999ULL); |
| 901 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::NATIONAL, |
| 902 number_formats, |
| 903 &formatted_number); |
| 904 EXPECT_EQ("1 (416) 881-9999", formatted_number); |
| 905 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 906 number_formats, |
| 907 &formatted_number); |
| 908 EXPECT_EQ("+1 416 881-9999", formatted_number); |
| 909 |
| 910 test_number.set_country_code(39); |
| 911 test_number.set_national_number(236618300ULL); |
| 912 test_number.set_italian_leading_zero(true); |
| 913 number_format->set_pattern("(\\d{2})(\\d{5})(\\d{3})"); |
| 914 number_format->set_format("$1-$2 $3"); |
| 915 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::NATIONAL, |
| 916 number_formats, |
| 917 &formatted_number); |
| 918 EXPECT_EQ("02-36618 300", formatted_number); |
| 919 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 920 number_formats, |
| 921 &formatted_number); |
| 922 EXPECT_EQ("+39 02-36618 300", formatted_number); |
| 923 |
| 924 test_number.set_country_code(44); |
| 925 test_number.set_national_number(2012345678ULL); |
| 926 test_number.set_italian_leading_zero(false); |
| 927 number_format->set_national_prefix_formatting_rule("$NP$FG"); |
| 928 number_format->set_pattern("(\\d{2})(\\d{4})(\\d{4})"); |
| 929 number_format->set_format("$1 $2 $3"); |
| 930 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::NATIONAL, |
| 931 number_formats, |
| 932 &formatted_number); |
| 933 EXPECT_EQ("020 1234 5678", formatted_number); |
| 934 |
| 935 number_format->set_national_prefix_formatting_rule("($NP$FG)"); |
| 936 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::NATIONAL, |
| 937 number_formats, |
| 938 &formatted_number); |
| 939 EXPECT_EQ("(020) 1234 5678", formatted_number); |
| 940 number_format->set_national_prefix_formatting_rule(""); |
| 941 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::NATIONAL, |
| 942 number_formats, |
| 943 &formatted_number); |
| 944 EXPECT_EQ("20 1234 5678", formatted_number); |
| 945 number_format->set_national_prefix_formatting_rule(""); |
| 946 phone_util_.FormatByPattern(test_number, PhoneNumberUtil::INTERNATIONAL, |
| 947 number_formats, |
| 948 &formatted_number); |
| 949 EXPECT_EQ("+44 20 1234 5678", formatted_number); |
| 950 } |
| 951 |
| 952 TEST_F(PhoneNumberUtilTest, FormatE164Number) { |
| 953 PhoneNumber test_number; |
| 954 string formatted_number; |
| 955 test_number.set_country_code(1); |
| 956 test_number.set_national_number(6502530000ULL); |
| 957 phone_util_.Format(test_number, PhoneNumberUtil::E164, &formatted_number); |
| 958 EXPECT_EQ("+16502530000", formatted_number); |
| 959 |
| 960 test_number.set_country_code(49); |
| 961 test_number.set_national_number(301234ULL); |
| 962 phone_util_.Format(test_number, PhoneNumberUtil::E164, &formatted_number); |
| 963 EXPECT_EQ("+49301234", formatted_number); |
| 964 } |
| 965 |
| 966 TEST_F(PhoneNumberUtilTest, FormatNumberWithExtension) { |
| 967 PhoneNumber nz_number; |
| 968 nz_number.set_country_code(64); |
| 969 nz_number.set_national_number(33316005ULL); |
| 970 nz_number.set_extension("1234"); |
| 971 string formatted_number; |
| 972 // Uses default extension prefix: |
| 973 phone_util_.Format(nz_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 974 EXPECT_EQ("03-331 6005 ext. 1234", formatted_number); |
| 975 // Uses RFC 3966 syntax. |
| 976 phone_util_.Format(nz_number, PhoneNumberUtil::RFC3966, &formatted_number); |
| 977 EXPECT_EQ("+64-3-331-6005;ext=1234", formatted_number); |
| 978 // Extension prefix overridden in the territory information for the US: |
| 979 PhoneNumber us_number_with_extension; |
| 980 us_number_with_extension.set_country_code(1); |
| 981 us_number_with_extension.set_national_number(6502530000ULL); |
| 982 us_number_with_extension.set_extension("4567"); |
| 983 phone_util_.Format(us_number_with_extension, |
| 984 PhoneNumberUtil::NATIONAL, &formatted_number); |
| 985 EXPECT_EQ("650 253 0000 extn. 4567", formatted_number); |
| 986 } |
| 987 |
| 988 TEST_F(PhoneNumberUtilTest, GetLengthOfGeographicalAreaCode) { |
| 989 PhoneNumber number; |
| 990 // Google MTV, which has area code "650". |
| 991 number.set_country_code(1); |
| 992 number.set_national_number(6502530000ULL); |
| 993 EXPECT_EQ(3, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 994 |
| 995 // A North America toll-free number, which has no area code. |
| 996 number.set_country_code(1); |
| 997 number.set_national_number(8002530000ULL); |
| 998 EXPECT_EQ(0, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 999 |
| 1000 // An invalid US number (1 digit shorter), which has no area code. |
| 1001 number.set_country_code(1); |
| 1002 number.set_national_number(650253000ULL); |
| 1003 EXPECT_EQ(0, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 1004 |
| 1005 // Google London, which has area code "20". |
| 1006 number.set_country_code(44); |
| 1007 number.set_national_number(2070313000ULL); |
| 1008 EXPECT_EQ(2, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 1009 |
| 1010 // A UK mobile phone, which has no area code. |
| 1011 number.set_country_code(44); |
| 1012 number.set_national_number(7123456789ULL); |
| 1013 EXPECT_EQ(0, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 1014 |
| 1015 // Google Buenos Aires, which has area code "11". |
| 1016 number.set_country_code(54); |
| 1017 number.set_national_number(1155303000ULL); |
| 1018 EXPECT_EQ(2, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 1019 |
| 1020 // Google Sydney, which has area code "2". |
| 1021 number.set_country_code(61); |
| 1022 number.set_national_number(293744000ULL); |
| 1023 EXPECT_EQ(1, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 1024 |
| 1025 // Google Singapore. Singapore has no area code and no national prefix. |
| 1026 number.set_country_code(65); |
| 1027 number.set_national_number(65218000ULL); |
| 1028 EXPECT_EQ(0, phone_util_.GetLengthOfGeographicalAreaCode(number)); |
| 1029 } |
| 1030 |
| 1031 TEST_F(PhoneNumberUtilTest, GetLengthOfNationalDestinationCode) { |
| 1032 PhoneNumber number; |
| 1033 // Google MTV, which has national destination code (NDC) "650". |
| 1034 number.set_country_code(1); |
| 1035 number.set_national_number(6502530000ULL); |
| 1036 EXPECT_EQ(3, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1037 |
| 1038 // A North America toll-free number, which has NDC "800". |
| 1039 number.set_country_code(1); |
| 1040 number.set_national_number(8002530000ULL); |
| 1041 EXPECT_EQ(3, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1042 |
| 1043 // Google London, which has NDC "20". |
| 1044 number.set_country_code(44); |
| 1045 number.set_national_number(2070313000ULL); |
| 1046 EXPECT_EQ(2, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1047 |
| 1048 // A UK mobile phone, which has NDC "7123" |
| 1049 number.set_country_code(44); |
| 1050 number.set_national_number(7123456789ULL); |
| 1051 EXPECT_EQ(4, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1052 |
| 1053 // Google Buenos Aires, which has NDC "11". |
| 1054 number.set_country_code(54); |
| 1055 number.set_national_number(1155303000ULL); |
| 1056 EXPECT_EQ(2, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1057 |
| 1058 // Google Sydney, which has NDC "2". |
| 1059 number.set_country_code(61); |
| 1060 number.set_national_number(293744000ULL); |
| 1061 EXPECT_EQ(1, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1062 |
| 1063 // Google Singapore. Singapore has NDC "6521". |
| 1064 number.set_country_code(65); |
| 1065 number.set_national_number(65218000ULL); |
| 1066 EXPECT_EQ(4, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1067 |
| 1068 // An invalid US number (1 digit shorter), which has no NDC. |
| 1069 number.set_country_code(1); |
| 1070 number.set_national_number(650253000ULL); |
| 1071 EXPECT_EQ(0, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1072 |
| 1073 // A number containing an invalid country code, which shouldn't have any NDC. |
| 1074 number.set_country_code(123); |
| 1075 number.set_national_number(650253000ULL); |
| 1076 EXPECT_EQ(0, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1077 |
| 1078 // A number that has only one group of digits after country code when |
| 1079 // formatted in the international format. |
| 1080 number.set_country_code(376); |
| 1081 number.set_national_number(12345ULL); |
| 1082 EXPECT_EQ(0, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1083 |
| 1084 // The same number above, but with an extension. |
| 1085 number.set_country_code(376); |
| 1086 number.set_national_number(12345ULL); |
| 1087 number.set_extension("321"); |
| 1088 EXPECT_EQ(0, phone_util_.GetLengthOfNationalDestinationCode(number)); |
| 1089 } |
| 1090 |
| 1091 TEST_F(PhoneNumberUtilTest, ExtractPossibleNumber) { |
| 1092 // Removes preceding funky punctuation and letters but leaves the rest |
| 1093 // untouched. |
| 1094 string extracted_number; |
| 1095 ExtractPossibleNumber("Tel:0800-345-600", &extracted_number); |
| 1096 EXPECT_EQ("0800-345-600", extracted_number); |
| 1097 ExtractPossibleNumber("Tel:0800 FOR PIZZA", &extracted_number); |
| 1098 EXPECT_EQ("0800 FOR PIZZA", extracted_number); |
| 1099 |
| 1100 // Should not remove plus sign. |
| 1101 ExtractPossibleNumber("Tel:+800-345-600", &extracted_number); |
| 1102 EXPECT_EQ("+800-345-600", extracted_number); |
| 1103 // Should recognise wide digits as possible start values. |
| 1104 ExtractPossibleNumber("023", &extracted_number); |
| 1105 EXPECT_EQ("023", extracted_number); |
| 1106 // Dashes are not possible start values and should be removed. |
| 1107 ExtractPossibleNumber("Num-123", &extracted_number); |
| 1108 EXPECT_EQ("123", extracted_number); |
| 1109 // If not possible number present, return empty string. |
| 1110 ExtractPossibleNumber("Num-....", &extracted_number); |
| 1111 EXPECT_EQ("", extracted_number); |
| 1112 // Leading brackets are stripped - these are not used when parsing. |
| 1113 ExtractPossibleNumber("(650) 253-0000", &extracted_number); |
| 1114 EXPECT_EQ("650) 253-0000", extracted_number); |
| 1115 |
| 1116 // Trailing non-alpha-numeric characters should be removed. |
| 1117 ExtractPossibleNumber("(650) 253-0000..- ..", &extracted_number); |
| 1118 EXPECT_EQ("650) 253-0000", extracted_number); |
| 1119 ExtractPossibleNumber("(650) 253-0000.", &extracted_number); |
| 1120 EXPECT_EQ("650) 253-0000", extracted_number); |
| 1121 // This case has a trailing RTL char. |
| 1122 ExtractPossibleNumber("(650) 253-0000", &extracted_number); |
| 1123 EXPECT_EQ("650) 253-0000", extracted_number); |
| 1124 } |
| 1125 |
| 1126 TEST_F(PhoneNumberUtilTest, IsNANPACountry) { |
| 1127 EXPECT_TRUE(phone_util_.IsNANPACountry(RegionCode::US())); |
| 1128 EXPECT_TRUE(phone_util_.IsNANPACountry(RegionCode::BS())); |
| 1129 } |
| 1130 |
| 1131 TEST_F(PhoneNumberUtilTest, IsValidNumber) { |
| 1132 PhoneNumber us_number; |
| 1133 us_number.set_country_code(1); |
| 1134 us_number.set_national_number(6502530000ULL); |
| 1135 EXPECT_TRUE(phone_util_.IsValidNumber(us_number)); |
| 1136 |
| 1137 PhoneNumber it_number; |
| 1138 it_number.set_country_code(39); |
| 1139 it_number.set_national_number(236618300ULL); |
| 1140 it_number.set_italian_leading_zero(true); |
| 1141 EXPECT_TRUE(phone_util_.IsValidNumber(it_number)); |
| 1142 |
| 1143 PhoneNumber gb_number; |
| 1144 gb_number.set_country_code(44); |
| 1145 gb_number.set_national_number(7912345678ULL); |
| 1146 EXPECT_TRUE(phone_util_.IsValidNumber(gb_number)); |
| 1147 |
| 1148 PhoneNumber nz_number; |
| 1149 nz_number.set_country_code(64); |
| 1150 nz_number.set_national_number(21387835ULL); |
| 1151 EXPECT_TRUE(phone_util_.IsValidNumber(nz_number)); |
| 1152 } |
| 1153 |
| 1154 TEST_F(PhoneNumberUtilTest, IsValidForRegion) { |
| 1155 // This number is valid for the Bahamas, but is not a valid US number. |
| 1156 PhoneNumber bs_number; |
| 1157 bs_number.set_country_code(1); |
| 1158 bs_number.set_national_number(2423232345ULL); |
| 1159 EXPECT_TRUE(phone_util_.IsValidNumber(bs_number)); |
| 1160 EXPECT_TRUE(phone_util_.IsValidNumberForRegion(bs_number, RegionCode::BS())); |
| 1161 EXPECT_FALSE(phone_util_.IsValidNumberForRegion(bs_number, RegionCode::US())); |
| 1162 bs_number.set_national_number(2421232345ULL); |
| 1163 // This number is no longer valid. |
| 1164 EXPECT_FALSE(phone_util_.IsValidNumber(bs_number)); |
| 1165 |
| 1166 // La Mayotte and Réunion use 'leadingDigits' to differentiate them. |
| 1167 PhoneNumber re_number; |
| 1168 re_number.set_country_code(262); |
| 1169 re_number.set_national_number(262123456ULL); |
| 1170 EXPECT_TRUE(phone_util_.IsValidNumber(re_number)); |
| 1171 EXPECT_TRUE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::RE())); |
| 1172 EXPECT_FALSE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::YT())); |
| 1173 // Now change the number to be a number for La Mayotte. |
| 1174 re_number.set_national_number(269601234ULL); |
| 1175 EXPECT_TRUE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::YT())); |
| 1176 EXPECT_FALSE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::RE())); |
| 1177 // This number is no longer valid. |
| 1178 re_number.set_national_number(269123456ULL); |
| 1179 EXPECT_FALSE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::YT())); |
| 1180 EXPECT_FALSE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::RE())); |
| 1181 EXPECT_FALSE(phone_util_.IsValidNumber(re_number)); |
| 1182 // However, it should be recognised as from La Mayotte. |
| 1183 string region_code; |
| 1184 phone_util_.GetRegionCodeForNumber(re_number, ®ion_code); |
| 1185 EXPECT_EQ("YT", region_code); |
| 1186 // This number is valid in both places. |
| 1187 re_number.set_national_number(800123456ULL); |
| 1188 EXPECT_TRUE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::YT())); |
| 1189 EXPECT_TRUE(phone_util_.IsValidNumberForRegion(re_number, RegionCode::RE())); |
| 1190 } |
| 1191 |
| 1192 TEST_F(PhoneNumberUtilTest, IsNotValidNumber) { |
| 1193 PhoneNumber us_number; |
| 1194 us_number.set_country_code(1); |
| 1195 us_number.set_national_number(2530000ULL); |
| 1196 EXPECT_FALSE(phone_util_.IsValidNumber(us_number)); |
| 1197 |
| 1198 PhoneNumber it_number; |
| 1199 it_number.set_country_code(39); |
| 1200 it_number.set_national_number(23661830000ULL); |
| 1201 it_number.set_italian_leading_zero(true); |
| 1202 EXPECT_FALSE(phone_util_.IsValidNumber(it_number)); |
| 1203 |
| 1204 PhoneNumber gb_number; |
| 1205 gb_number.set_country_code(44); |
| 1206 gb_number.set_national_number(791234567ULL); |
| 1207 EXPECT_FALSE(phone_util_.IsValidNumber(gb_number)); |
| 1208 |
| 1209 PhoneNumber de_number; |
| 1210 de_number.set_country_code(49); |
| 1211 de_number.set_national_number(1234ULL); |
| 1212 EXPECT_FALSE(phone_util_.IsValidNumber(de_number)); |
| 1213 |
| 1214 PhoneNumber nz_number; |
| 1215 nz_number.set_country_code(64); |
| 1216 nz_number.set_national_number(3316005ULL); |
| 1217 EXPECT_FALSE(phone_util_.IsValidNumber(nz_number)); |
| 1218 } |
| 1219 |
| 1220 TEST_F(PhoneNumberUtilTest, IsPossibleNumber) { |
| 1221 PhoneNumber number; |
| 1222 number.set_country_code(1); |
| 1223 number.set_national_number(6502530000ULL); |
| 1224 EXPECT_TRUE(phone_util_.IsPossibleNumber(number)); |
| 1225 |
| 1226 number.set_country_code(1); |
| 1227 number.set_national_number(2530000ULL); |
| 1228 EXPECT_TRUE(phone_util_.IsPossibleNumber(number)); |
| 1229 |
| 1230 number.set_country_code(44); |
| 1231 number.set_national_number(2070313000ULL); |
| 1232 EXPECT_TRUE(phone_util_.IsPossibleNumber(number)); |
| 1233 |
| 1234 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("+1 650 253 0000", |
| 1235 RegionCode::US())); |
| 1236 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("+1 650 GOO OGLE", |
| 1237 RegionCode::US())); |
| 1238 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("(650) 253-0000", |
| 1239 RegionCode::US())); |
| 1240 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("253-0000", |
| 1241 RegionCode::US())); |
| 1242 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("+1 650 253 0000", |
| 1243 RegionCode::GB())); |
| 1244 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("+44 20 7031 3000", |
| 1245 RegionCode::GB())); |
| 1246 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("(020) 7031 3000", |
| 1247 RegionCode::GB())); |
| 1248 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("7031 3000", |
| 1249 RegionCode::GB())); |
| 1250 EXPECT_TRUE(phone_util_.IsPossibleNumberForString("3331 6005", |
| 1251 RegionCode::NZ())); |
| 1252 } |
| 1253 |
| 1254 TEST_F(PhoneNumberUtilTest, IsPossibleNumberWithReason) { |
| 1255 // FYI, national numbers for country code +1 that are within 7 to 10 digits |
| 1256 // are possible. |
| 1257 PhoneNumber number; |
| 1258 number.set_country_code(1); |
| 1259 number.set_national_number(6502530000ULL); |
| 1260 EXPECT_EQ(PhoneNumberUtil::IS_POSSIBLE, |
| 1261 phone_util_.IsPossibleNumberWithReason(number)); |
| 1262 |
| 1263 number.set_country_code(1); |
| 1264 number.set_national_number(2530000ULL); |
| 1265 EXPECT_EQ(PhoneNumberUtil::IS_POSSIBLE, |
| 1266 phone_util_.IsPossibleNumberWithReason(number)); |
| 1267 |
| 1268 number.set_country_code(0); |
| 1269 number.set_national_number(2530000ULL); |
| 1270 EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE, |
| 1271 phone_util_.IsPossibleNumberWithReason(number)); |
| 1272 |
| 1273 number.set_country_code(1); |
| 1274 number.set_national_number(253000ULL); |
| 1275 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT, |
| 1276 phone_util_.IsPossibleNumberWithReason(number)); |
| 1277 |
| 1278 number.set_country_code(1); |
| 1279 number.set_national_number(65025300000ULL); |
| 1280 EXPECT_EQ(PhoneNumberUtil::TOO_LONG, |
| 1281 phone_util_.IsPossibleNumberWithReason(number)); |
| 1282 |
| 1283 number.set_country_code(44); |
| 1284 number.set_national_number(2070310000ULL); |
| 1285 EXPECT_EQ(PhoneNumberUtil::IS_POSSIBLE, |
| 1286 phone_util_.IsPossibleNumberWithReason(number)); |
| 1287 |
| 1288 number.set_country_code(49); |
| 1289 number.set_national_number(30123456ULL); |
| 1290 EXPECT_EQ(PhoneNumberUtil::IS_POSSIBLE, |
| 1291 phone_util_.IsPossibleNumberWithReason(number)); |
| 1292 |
| 1293 number.set_country_code(65); |
| 1294 number.set_national_number(1234567890ULL); |
| 1295 EXPECT_EQ(PhoneNumberUtil::IS_POSSIBLE, |
| 1296 phone_util_.IsPossibleNumberWithReason(number)); |
| 1297 |
| 1298 // Try with number that we don't have metadata for. |
| 1299 PhoneNumber ad_number; |
| 1300 ad_number.set_country_code(376); |
| 1301 ad_number.set_national_number(12345ULL); |
| 1302 EXPECT_EQ(PhoneNumberUtil::IS_POSSIBLE, |
| 1303 phone_util_.IsPossibleNumberWithReason(ad_number)); |
| 1304 ad_number.set_country_code(376); |
| 1305 ad_number.set_national_number(13ULL); |
| 1306 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT, |
| 1307 phone_util_.IsPossibleNumberWithReason(ad_number)); |
| 1308 ad_number.set_country_code(376); |
| 1309 ad_number.set_national_number(1234567890123456ULL); |
| 1310 EXPECT_EQ(PhoneNumberUtil::TOO_LONG, |
| 1311 phone_util_.IsPossibleNumberWithReason(ad_number)); |
| 1312 } |
| 1313 |
| 1314 TEST_F(PhoneNumberUtilTest, IsNotPossibleNumber) { |
| 1315 PhoneNumber number; |
| 1316 number.set_country_code(1); |
| 1317 number.set_national_number(65025300000ULL); |
| 1318 EXPECT_FALSE(phone_util_.IsPossibleNumber(number)); |
| 1319 |
| 1320 number.set_country_code(1); |
| 1321 number.set_national_number(253000ULL); |
| 1322 EXPECT_FALSE(phone_util_.IsPossibleNumber(number)); |
| 1323 |
| 1324 number.set_country_code(44); |
| 1325 number.set_national_number(300ULL); |
| 1326 EXPECT_FALSE(phone_util_.IsPossibleNumber(number)); |
| 1327 |
| 1328 EXPECT_FALSE(phone_util_.IsPossibleNumberForString("+1 650 253 00000", |
| 1329 RegionCode::US())); |
| 1330 EXPECT_FALSE(phone_util_.IsPossibleNumberForString("(650) 253-00000", |
| 1331 RegionCode::US())); |
| 1332 EXPECT_FALSE(phone_util_.IsPossibleNumberForString("I want a Pizza", |
| 1333 RegionCode::US())); |
| 1334 EXPECT_FALSE(phone_util_.IsPossibleNumberForString("253-000", |
| 1335 RegionCode::US())); |
| 1336 EXPECT_FALSE(phone_util_.IsPossibleNumberForString("1 3000", |
| 1337 RegionCode::GB())); |
| 1338 EXPECT_FALSE(phone_util_.IsPossibleNumberForString("+44 300", |
| 1339 RegionCode::GB())); |
| 1340 } |
| 1341 |
| 1342 TEST_F(PhoneNumberUtilTest, TruncateTooLongNumber) { |
| 1343 // US number 650-253-0000, but entered with one additional digit at the end. |
| 1344 PhoneNumber too_long_number; |
| 1345 too_long_number.set_country_code(1); |
| 1346 too_long_number.set_national_number(65025300001ULL); |
| 1347 PhoneNumber valid_number; |
| 1348 valid_number.set_country_code(1); |
| 1349 valid_number.set_national_number(6502530000ULL); |
| 1350 EXPECT_TRUE(phone_util_.TruncateTooLongNumber(&too_long_number)); |
| 1351 EXPECT_EQ(valid_number, too_long_number); |
| 1352 |
| 1353 // GB number 080 1234 5678, but entered with 4 extra digits at the end. |
| 1354 too_long_number.set_country_code(44); |
| 1355 too_long_number.set_national_number(80123456780123ULL); |
| 1356 valid_number.set_country_code(44); |
| 1357 valid_number.set_national_number(8012345678ULL); |
| 1358 EXPECT_TRUE(phone_util_.TruncateTooLongNumber(&too_long_number)); |
| 1359 EXPECT_EQ(valid_number, too_long_number); |
| 1360 |
| 1361 // IT number 022 3456 7890, but entered with 3 extra digits at the end. |
| 1362 too_long_number.set_country_code(39); |
| 1363 too_long_number.set_national_number(2234567890123ULL); |
| 1364 too_long_number.set_italian_leading_zero(true); |
| 1365 valid_number.set_country_code(39); |
| 1366 valid_number.set_national_number(2234567890ULL); |
| 1367 valid_number.set_italian_leading_zero(true); |
| 1368 EXPECT_TRUE(phone_util_.TruncateTooLongNumber(&too_long_number)); |
| 1369 EXPECT_EQ(valid_number, too_long_number); |
| 1370 |
| 1371 // Tests what happens when a valid number is passed in. |
| 1372 PhoneNumber valid_number_copy(valid_number); |
| 1373 EXPECT_TRUE(phone_util_.TruncateTooLongNumber(&valid_number)); |
| 1374 // Tests the number is not modified. |
| 1375 EXPECT_EQ(valid_number_copy, valid_number); |
| 1376 |
| 1377 // Tests what happens when a number with invalid prefix is passed in. |
| 1378 PhoneNumber number_with_invalid_prefix; |
| 1379 number_with_invalid_prefix.set_country_code(1); |
| 1380 // The test metadata says US numbers cannot have prefix 240. |
| 1381 number_with_invalid_prefix.set_national_number(2401234567ULL); |
| 1382 PhoneNumber invalid_number_copy(number_with_invalid_prefix); |
| 1383 EXPECT_FALSE(phone_util_.TruncateTooLongNumber(&number_with_invalid_prefix)); |
| 1384 // Tests the number is not modified. |
| 1385 EXPECT_EQ(invalid_number_copy, number_with_invalid_prefix); |
| 1386 |
| 1387 // Tests what happens when a too short number is passed in. |
| 1388 PhoneNumber too_short_number; |
| 1389 too_short_number.set_country_code(1); |
| 1390 too_short_number.set_national_number(1234ULL); |
| 1391 PhoneNumber too_short_number_copy(too_short_number); |
| 1392 EXPECT_FALSE(phone_util_.TruncateTooLongNumber(&too_short_number)); |
| 1393 // Tests the number is not modified. |
| 1394 EXPECT_EQ(too_short_number_copy, too_short_number); |
| 1395 } |
| 1396 |
| 1397 TEST_F(PhoneNumberUtilTest, IsLeadingZeroPossible) { |
| 1398 EXPECT_TRUE(IsLeadingZeroPossible(39)); // Italy |
| 1399 EXPECT_FALSE(IsLeadingZeroPossible(1)); // USA |
| 1400 EXPECT_FALSE(IsLeadingZeroPossible(800)); // Not in metadata file, should |
| 1401 // return default value of false. |
| 1402 } |
| 1403 |
| 1404 TEST_F(PhoneNumberUtilTest, FormatUsingOriginalNumberFormat) { |
| 1405 PhoneNumber phone_number; |
| 1406 string formatted_number; |
| 1407 |
| 1408 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1409 phone_util_.ParseAndKeepRawInput("+442087654321", RegionCode::GB(), |
| 1410 &phone_number)); |
| 1411 phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), |
| 1412 &formatted_number); |
| 1413 EXPECT_EQ("+44 20 8765 4321", formatted_number); |
| 1414 |
| 1415 phone_number.Clear(); |
| 1416 formatted_number.clear(); |
| 1417 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1418 phone_util_.ParseAndKeepRawInput("02087654321", RegionCode::GB(), |
| 1419 &phone_number)); |
| 1420 phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), |
| 1421 &formatted_number); |
| 1422 EXPECT_EQ("(020) 8765 4321", formatted_number); |
| 1423 |
| 1424 phone_number.Clear(); |
| 1425 formatted_number.clear(); |
| 1426 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1427 phone_util_.ParseAndKeepRawInput("011442087654321", |
| 1428 RegionCode::US(), &phone_number)); |
| 1429 phone_util_.FormatInOriginalFormat(phone_number, RegionCode::US(), |
| 1430 &formatted_number); |
| 1431 EXPECT_EQ("011 44 20 8765 4321", formatted_number); |
| 1432 |
| 1433 phone_number.Clear(); |
| 1434 formatted_number.clear(); |
| 1435 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1436 phone_util_.ParseAndKeepRawInput("442087654321", RegionCode::GB(), |
| 1437 &phone_number)); |
| 1438 phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), |
| 1439 &formatted_number); |
| 1440 EXPECT_EQ("44 20 8765 4321", formatted_number); |
| 1441 |
| 1442 phone_number.Clear(); |
| 1443 formatted_number.clear(); |
| 1444 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1445 phone_util_.Parse("+442087654321", RegionCode::GB(), |
| 1446 &phone_number)); |
| 1447 phone_util_.FormatInOriginalFormat(phone_number, RegionCode::GB(), |
| 1448 &formatted_number); |
| 1449 EXPECT_EQ("(020) 8765 4321", formatted_number); |
| 1450 } |
| 1451 |
| 1452 TEST_F(PhoneNumberUtilTest, IsPremiumRate) { |
| 1453 PhoneNumber number; |
| 1454 number.set_country_code(1); |
| 1455 number.set_national_number(9004433030ULL); |
| 1456 EXPECT_EQ(PhoneNumberUtil::PREMIUM_RATE, phone_util_.GetNumberType(number)); |
| 1457 |
| 1458 number.set_country_code(39); |
| 1459 number.set_national_number(892123ULL); |
| 1460 EXPECT_EQ(PhoneNumberUtil::PREMIUM_RATE, phone_util_.GetNumberType(number)); |
| 1461 |
| 1462 number.set_country_code(44); |
| 1463 number.set_national_number(9187654321ULL); |
| 1464 EXPECT_EQ(PhoneNumberUtil::PREMIUM_RATE, phone_util_.GetNumberType(number)); |
| 1465 |
| 1466 number.set_country_code(49); |
| 1467 number.set_national_number(9001654321ULL); |
| 1468 EXPECT_EQ(PhoneNumberUtil::PREMIUM_RATE, phone_util_.GetNumberType(number)); |
| 1469 |
| 1470 number.set_country_code(49); |
| 1471 number.set_national_number(90091234567ULL); |
| 1472 EXPECT_EQ(PhoneNumberUtil::PREMIUM_RATE, phone_util_.GetNumberType(number)); |
| 1473 } |
| 1474 |
| 1475 TEST_F(PhoneNumberUtilTest, IsTollFree) { |
| 1476 PhoneNumber number; |
| 1477 number.set_country_code(1); |
| 1478 number.set_national_number(8881234567ULL); |
| 1479 EXPECT_EQ(PhoneNumberUtil::TOLL_FREE, phone_util_.GetNumberType(number)); |
| 1480 |
| 1481 number.set_country_code(39); |
| 1482 number.set_national_number(803123ULL); |
| 1483 EXPECT_EQ(PhoneNumberUtil::TOLL_FREE, phone_util_.GetNumberType(number)); |
| 1484 |
| 1485 number.set_country_code(44); |
| 1486 number.set_national_number(8012345678ULL); |
| 1487 EXPECT_EQ(PhoneNumberUtil::TOLL_FREE, phone_util_.GetNumberType(number)); |
| 1488 |
| 1489 number.set_country_code(49); |
| 1490 number.set_national_number(8001234567ULL); |
| 1491 EXPECT_EQ(PhoneNumberUtil::TOLL_FREE, phone_util_.GetNumberType(number)); |
| 1492 } |
| 1493 |
| 1494 TEST_F(PhoneNumberUtilTest, IsMobile) { |
| 1495 PhoneNumber number; |
| 1496 // A Bahama mobile number |
| 1497 number.set_country_code(1); |
| 1498 number.set_national_number(2423570000ULL); |
| 1499 EXPECT_EQ(PhoneNumberUtil::MOBILE, phone_util_.GetNumberType(number)); |
| 1500 |
| 1501 number.set_country_code(39); |
| 1502 number.set_national_number(312345678ULL); |
| 1503 EXPECT_EQ(PhoneNumberUtil::MOBILE, phone_util_.GetNumberType(number)); |
| 1504 |
| 1505 number.set_country_code(44); |
| 1506 number.set_national_number(7912345678ULL); |
| 1507 EXPECT_EQ(PhoneNumberUtil::MOBILE, phone_util_.GetNumberType(number)); |
| 1508 |
| 1509 number.set_country_code(49); |
| 1510 number.set_national_number(15123456789ULL); |
| 1511 EXPECT_EQ(PhoneNumberUtil::MOBILE, phone_util_.GetNumberType(number)); |
| 1512 |
| 1513 number.set_country_code(54); |
| 1514 number.set_national_number(91187654321ULL); |
| 1515 EXPECT_EQ(PhoneNumberUtil::MOBILE, phone_util_.GetNumberType(number)); |
| 1516 } |
| 1517 |
| 1518 TEST_F(PhoneNumberUtilTest, IsFixedLine) { |
| 1519 PhoneNumber number; |
| 1520 // A Bahama fixed-line number |
| 1521 number.set_country_code(1); |
| 1522 number.set_national_number(2423651234ULL); |
| 1523 EXPECT_EQ(PhoneNumberUtil::FIXED_LINE, phone_util_.GetNumberType(number)); |
| 1524 |
| 1525 // An Italian fixed-line number |
| 1526 number.Clear(); |
| 1527 number.set_country_code(39); |
| 1528 number.set_national_number(236618300ULL); |
| 1529 number.set_italian_leading_zero(true); |
| 1530 EXPECT_EQ(PhoneNumberUtil::FIXED_LINE, phone_util_.GetNumberType(number)); |
| 1531 |
| 1532 number.Clear(); |
| 1533 number.set_country_code(44); |
| 1534 number.set_national_number(2012345678ULL); |
| 1535 EXPECT_EQ(PhoneNumberUtil::FIXED_LINE, phone_util_.GetNumberType(number)); |
| 1536 |
| 1537 number.set_country_code(49); |
| 1538 number.set_national_number(301234ULL); |
| 1539 EXPECT_EQ(PhoneNumberUtil::FIXED_LINE, phone_util_.GetNumberType(number)); |
| 1540 } |
| 1541 |
| 1542 TEST_F(PhoneNumberUtilTest, IsFixedLineAndMobile) { |
| 1543 PhoneNumber number; |
| 1544 number.set_country_code(1); |
| 1545 number.set_national_number(6502531111ULL); |
| 1546 EXPECT_EQ(PhoneNumberUtil::FIXED_LINE_OR_MOBILE, |
| 1547 phone_util_.GetNumberType(number)); |
| 1548 |
| 1549 number.set_country_code(54); |
| 1550 number.set_national_number(1987654321ULL); |
| 1551 EXPECT_EQ(PhoneNumberUtil::FIXED_LINE_OR_MOBILE, |
| 1552 phone_util_.GetNumberType(number)); |
| 1553 } |
| 1554 |
| 1555 TEST_F(PhoneNumberUtilTest, IsSharedCost) { |
| 1556 PhoneNumber number; |
| 1557 number.set_country_code(44); |
| 1558 number.set_national_number(8431231234ULL); |
| 1559 EXPECT_EQ(PhoneNumberUtil::SHARED_COST, phone_util_.GetNumberType(number)); |
| 1560 } |
| 1561 |
| 1562 TEST_F(PhoneNumberUtilTest, IsVoip) { |
| 1563 PhoneNumber number; |
| 1564 number.set_country_code(44); |
| 1565 number.set_national_number(5631231234ULL); |
| 1566 EXPECT_EQ(PhoneNumberUtil::VOIP, phone_util_.GetNumberType(number)); |
| 1567 } |
| 1568 |
| 1569 TEST_F(PhoneNumberUtilTest, IsPersonalNumber) { |
| 1570 PhoneNumber number; |
| 1571 number.set_country_code(44); |
| 1572 number.set_national_number(7031231234ULL); |
| 1573 EXPECT_EQ(PhoneNumberUtil::PERSONAL_NUMBER, |
| 1574 phone_util_.GetNumberType(number)); |
| 1575 } |
| 1576 |
| 1577 TEST_F(PhoneNumberUtilTest, IsUnknown) { |
| 1578 PhoneNumber number; |
| 1579 number.set_country_code(1); |
| 1580 number.set_national_number(65025311111ULL); |
| 1581 EXPECT_EQ(PhoneNumberUtil::UNKNOWN, phone_util_.GetNumberType(number)); |
| 1582 } |
| 1583 |
| 1584 TEST_F(PhoneNumberUtilTest, GetCountryCodeForRegion) { |
| 1585 EXPECT_EQ(1, phone_util_.GetCountryCodeForRegion(RegionCode::US())); |
| 1586 EXPECT_EQ(64, phone_util_.GetCountryCodeForRegion(RegionCode::NZ())); |
| 1587 EXPECT_EQ(0, phone_util_.GetCountryCodeForRegion(RegionCode::ZZ())); |
| 1588 // CS is already deprecated so the library doesn't support it. |
| 1589 EXPECT_EQ(0, phone_util_.GetCountryCodeForRegion(RegionCode::CS())); |
| 1590 } |
| 1591 |
| 1592 TEST_F(PhoneNumberUtilTest, GetNationalDiallingPrefixForRegion) { |
| 1593 string ndd_prefix; |
| 1594 GetNddPrefixForRegion(RegionCode::US(), false, &ndd_prefix); |
| 1595 EXPECT_EQ("1", ndd_prefix); |
| 1596 |
| 1597 // Test non-main country to see it gets the national dialling prefix for the |
| 1598 // main country with that country calling code. |
| 1599 GetNddPrefixForRegion(RegionCode::BS(), false, &ndd_prefix); |
| 1600 EXPECT_EQ("1", ndd_prefix); |
| 1601 |
| 1602 GetNddPrefixForRegion(RegionCode::NZ(), false, &ndd_prefix); |
| 1603 EXPECT_EQ("0", ndd_prefix); |
| 1604 |
| 1605 // Test case with non digit in the national prefix. |
| 1606 GetNddPrefixForRegion(RegionCode::AO(), false, &ndd_prefix); |
| 1607 EXPECT_EQ("0~0", ndd_prefix); |
| 1608 |
| 1609 GetNddPrefixForRegion(RegionCode::AO(), true, &ndd_prefix); |
| 1610 EXPECT_EQ("00", ndd_prefix); |
| 1611 |
| 1612 // Test cases with invalid regions. |
| 1613 GetNddPrefixForRegion(RegionCode::ZZ(), false, &ndd_prefix); |
| 1614 EXPECT_EQ("", ndd_prefix); |
| 1615 |
| 1616 // CS is already deprecated so the library doesn't support it. |
| 1617 GetNddPrefixForRegion(RegionCode::CS(), false, &ndd_prefix); |
| 1618 EXPECT_EQ("", ndd_prefix); |
| 1619 } |
| 1620 |
| 1621 TEST_F(PhoneNumberUtilTest, IsViablePhoneNumber) { |
| 1622 // Only one or two digits before strange non-possible punctuation. |
| 1623 EXPECT_FALSE(IsViablePhoneNumber("12. March")); |
| 1624 EXPECT_FALSE(IsViablePhoneNumber("1+1+1")); |
| 1625 EXPECT_FALSE(IsViablePhoneNumber("80+0")); |
| 1626 EXPECT_FALSE(IsViablePhoneNumber("00")); |
| 1627 // Three digits is viable. |
| 1628 EXPECT_TRUE(IsViablePhoneNumber("111")); |
| 1629 // Alpha numbers. |
| 1630 EXPECT_TRUE(IsViablePhoneNumber("0800-4-pizza")); |
| 1631 EXPECT_TRUE(IsViablePhoneNumber("0800-4-PIZZA")); |
| 1632 // Only one or two digits before possible punctuation followed by more digits. |
| 1633 // The punctuation used here is the unicode character u+3000. |
| 1634 EXPECT_TRUE(IsViablePhoneNumber("1 34")); |
| 1635 EXPECT_FALSE(IsViablePhoneNumber("1 3+4")); |
| 1636 // Unicode variants of possible starting character and other allowed |
| 1637 // punctuation/digits. |
| 1638 EXPECT_TRUE(IsViablePhoneNumber("(1) 3456789")); |
| 1639 // Testing a leading + is okay. |
| 1640 EXPECT_TRUE(IsViablePhoneNumber("+1) 3456789")); |
| 1641 } |
| 1642 |
| 1643 TEST_F(PhoneNumberUtilTest, NormaliseRemovePunctuation) { |
| 1644 string input_number("034-56&+#234"); |
| 1645 Normalize(&input_number); |
| 1646 static const string kExpectedOutput("03456234"); |
| 1647 EXPECT_EQ(kExpectedOutput, input_number) |
| 1648 << "Conversion did not correctly remove punctuation"; |
| 1649 } |
| 1650 |
| 1651 TEST_F(PhoneNumberUtilTest, NormaliseReplaceAlphaCharacters) { |
| 1652 string input_number("034-I-am-HUNGRY"); |
| 1653 Normalize(&input_number); |
| 1654 static const string kExpectedOutput("034426486479"); |
| 1655 EXPECT_EQ(kExpectedOutput, input_number) |
| 1656 << "Conversion did not correctly replace alpha characters"; |
| 1657 } |
| 1658 |
| 1659 TEST_F(PhoneNumberUtilTest, NormaliseOtherDigits) { |
| 1660 // The first digit is a full-width 2, the last digit is an Arabic-indic digit |
| 1661 // 5. |
| 1662 string input_number("25٥"); |
| 1663 Normalize(&input_number); |
| 1664 static const string kExpectedOutput("255"); |
| 1665 EXPECT_EQ(kExpectedOutput, input_number) |
| 1666 << "Conversion did not correctly replace non-latin digits"; |
| 1667 // The first digit is an Eastern-Arabic 5, the latter an Eastern-Arabic 0. |
| 1668 string eastern_arabic_input_number("۵2۰"); |
| 1669 Normalize(&eastern_arabic_input_number); |
| 1670 static const string kExpectedOutput2("520"); |
| 1671 EXPECT_EQ(kExpectedOutput2, eastern_arabic_input_number) |
| 1672 << "Conversion did not correctly replace non-latin digits"; |
| 1673 } |
| 1674 |
| 1675 TEST_F(PhoneNumberUtilTest, NormaliseStripAlphaCharacters) { |
| 1676 string input_number("034-56&+a#234"); |
| 1677 phone_util_.NormalizeDigitsOnly(&input_number); |
| 1678 static const string kExpectedOutput("03456234"); |
| 1679 EXPECT_EQ(kExpectedOutput, input_number) |
| 1680 << "Conversion did not correctly remove alpha characters"; |
| 1681 } |
| 1682 |
| 1683 TEST_F(PhoneNumberUtilTest, MaybeStripInternationalPrefix) { |
| 1684 string international_prefix("00[39]"); |
| 1685 string number_to_strip("0034567700-3898003"); |
| 1686 // Note the dash is removed as part of the normalization. |
| 1687 string stripped_number("45677003898003"); |
| 1688 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITH_IDD, |
| 1689 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1690 &number_to_strip)); |
| 1691 EXPECT_EQ(stripped_number, number_to_strip) |
| 1692 << "The number was not stripped of its international prefix."; |
| 1693 |
| 1694 // Now the number no longer starts with an IDD prefix, so it should now report |
| 1695 // FROM_DEFAULT_COUNTRY. |
| 1696 EXPECT_EQ(PhoneNumber::FROM_DEFAULT_COUNTRY, |
| 1697 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1698 &number_to_strip)); |
| 1699 |
| 1700 number_to_strip.assign("00945677003898003"); |
| 1701 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITH_IDD, |
| 1702 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1703 &number_to_strip)); |
| 1704 EXPECT_EQ(stripped_number, number_to_strip) |
| 1705 << "The number was not stripped of its international prefix."; |
| 1706 |
| 1707 // Test it works when the international prefix is broken up by spaces. |
| 1708 number_to_strip.assign("00 9 45677003898003"); |
| 1709 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITH_IDD, |
| 1710 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1711 &number_to_strip)); |
| 1712 EXPECT_EQ(stripped_number, number_to_strip) |
| 1713 << "The number was not stripped of its international prefix."; |
| 1714 // Now the number no longer starts with an IDD prefix, so it should now report |
| 1715 // FROM_DEFAULT_COUNTRY. |
| 1716 EXPECT_EQ(PhoneNumber::FROM_DEFAULT_COUNTRY, |
| 1717 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1718 &number_to_strip)); |
| 1719 |
| 1720 // Test the + symbol is also recognised and stripped. |
| 1721 number_to_strip.assign("+45677003898003"); |
| 1722 stripped_number.assign("45677003898003"); |
| 1723 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN, |
| 1724 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1725 &number_to_strip)); |
| 1726 EXPECT_EQ(stripped_number, number_to_strip) |
| 1727 << "The number supplied was not stripped of the plus symbol."; |
| 1728 |
| 1729 // If the number afterwards is a zero, we should not strip this - no country |
| 1730 // code begins with 0. |
| 1731 number_to_strip.assign("0090112-3123"); |
| 1732 stripped_number.assign("00901123123"); |
| 1733 EXPECT_EQ(PhoneNumber::FROM_DEFAULT_COUNTRY, |
| 1734 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1735 &number_to_strip)); |
| 1736 EXPECT_EQ(stripped_number, number_to_strip) |
| 1737 << "The number had a 0 after the match so shouldn't be stripped."; |
| 1738 // Here the 0 is separated by a space from the IDD. |
| 1739 number_to_strip.assign("009 0-112-3123"); |
| 1740 EXPECT_EQ(PhoneNumber::FROM_DEFAULT_COUNTRY, |
| 1741 MaybeStripInternationalPrefixAndNormalize(international_prefix, |
| 1742 &number_to_strip)); |
| 1743 } |
| 1744 |
| 1745 TEST_F(PhoneNumberUtilTest, MaybeStripNationalPrefixAndCarrierCode) { |
| 1746 PhoneMetadata metadata; |
| 1747 metadata.set_national_prefix_for_parsing("34"); |
| 1748 metadata.mutable_general_desc()->set_national_number_pattern("\\d{4,8}"); |
| 1749 string number_to_strip("34356778"); |
| 1750 string stripped_number("356778"); |
| 1751 string carrier_code; |
| 1752 MaybeStripNationalPrefixAndCarrierCode(metadata, &number_to_strip, |
| 1753 &carrier_code); |
| 1754 EXPECT_EQ(stripped_number, number_to_strip) |
| 1755 << "Should have had national prefix stripped."; |
| 1756 EXPECT_EQ("", carrier_code) << "Should have had no carrier code stripped."; |
| 1757 // Retry stripping - now the number should not start with the national prefix, |
| 1758 // so no more stripping should occur. |
| 1759 MaybeStripNationalPrefixAndCarrierCode(metadata, &number_to_strip, |
| 1760 &carrier_code); |
| 1761 EXPECT_EQ(stripped_number, number_to_strip) |
| 1762 << "Should have had no change - no national prefix present."; |
| 1763 // Some countries have no national prefix. Repeat test with none specified. |
| 1764 metadata.clear_national_prefix_for_parsing(); |
| 1765 MaybeStripNationalPrefixAndCarrierCode(metadata, &number_to_strip, |
| 1766 &carrier_code); |
| 1767 EXPECT_EQ(stripped_number, number_to_strip) |
| 1768 << "Should have had no change - empty national prefix."; |
| 1769 // If the resultant number doesn't match the national rule, it shouldn't be |
| 1770 // stripped. |
| 1771 metadata.set_national_prefix_for_parsing("3"); |
| 1772 number_to_strip.assign("3123"); |
| 1773 stripped_number.assign("3123"); |
| 1774 MaybeStripNationalPrefixAndCarrierCode(metadata, &number_to_strip, |
| 1775 &carrier_code); |
| 1776 EXPECT_EQ(stripped_number, number_to_strip) |
| 1777 << "Should have had no change - after stripping, it wouldn't have " |
| 1778 << "matched the national rule."; |
| 1779 // Test extracting carrier selection code. |
| 1780 metadata.set_national_prefix_for_parsing("0(81)?"); |
| 1781 number_to_strip.assign("08122123456"); |
| 1782 stripped_number.assign("22123456"); |
| 1783 MaybeStripNationalPrefixAndCarrierCode(metadata, &number_to_strip, |
| 1784 &carrier_code); |
| 1785 EXPECT_EQ("81", carrier_code) << "Should have had carrier code stripped."; |
| 1786 EXPECT_EQ(stripped_number, number_to_strip) |
| 1787 << "Should have had national prefix and carrier code stripped."; |
| 1788 // If there was a transform rule, check it was applied. |
| 1789 metadata.set_national_prefix_transform_rule("5$15"); |
| 1790 // Note that a capturing group is present here. |
| 1791 metadata.set_national_prefix_for_parsing("0(\\d{2})"); |
| 1792 number_to_strip.assign("031123"); |
| 1793 string transformed_number("5315123"); |
| 1794 MaybeStripNationalPrefixAndCarrierCode(metadata, &number_to_strip, |
| 1795 &carrier_code); |
| 1796 EXPECT_EQ(transformed_number, number_to_strip) |
| 1797 << "Was not successfully transformed."; |
| 1798 } |
| 1799 |
| 1800 TEST_F(PhoneNumberUtilTest, MaybeStripExtension) { |
| 1801 // One with extension. |
| 1802 string number("1234576 ext. 1234"); |
| 1803 string extension; |
| 1804 string expected_extension("1234"); |
| 1805 string stripped_number("1234576"); |
| 1806 EXPECT_TRUE(MaybeStripExtension(&number, &extension)); |
| 1807 EXPECT_EQ(stripped_number, number); |
| 1808 EXPECT_EQ(expected_extension, extension); |
| 1809 |
| 1810 // One without extension. |
| 1811 number.assign("1234-576"); |
| 1812 extension.clear(); |
| 1813 stripped_number.assign("1234-576"); |
| 1814 EXPECT_FALSE(MaybeStripExtension(&number, &extension)); |
| 1815 EXPECT_EQ(stripped_number, number); |
| 1816 EXPECT_TRUE(extension.empty()); |
| 1817 |
| 1818 // One with an extension caught by the second capturing group in |
| 1819 // kKnownExtnPatterns. |
| 1820 number.assign("1234576-123#"); |
| 1821 extension.clear(); |
| 1822 expected_extension.assign("123"); |
| 1823 stripped_number.assign("1234576"); |
| 1824 EXPECT_TRUE(MaybeStripExtension(&number, &extension)); |
| 1825 EXPECT_EQ(stripped_number, number); |
| 1826 EXPECT_EQ(expected_extension, extension); |
| 1827 |
| 1828 number.assign("1234576 ext.123#"); |
| 1829 extension.clear(); |
| 1830 EXPECT_TRUE(MaybeStripExtension(&number, &extension)); |
| 1831 EXPECT_EQ(stripped_number, number); |
| 1832 EXPECT_EQ(expected_extension, extension); |
| 1833 } |
| 1834 |
| 1835 TEST_F(PhoneNumberUtilTest, MaybeExtractCountryCode) { |
| 1836 PhoneNumber number; |
| 1837 const PhoneMetadata* metadata = GetPhoneMetadata(RegionCode::US()); |
| 1838 // Note that for the US, the IDD is 011. |
| 1839 string phone_number("011112-3456789"); |
| 1840 string stripped_number("123456789"); |
| 1841 int expected_country_code = 1; |
| 1842 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1843 MaybeExtractCountryCode(metadata, true, &phone_number, &number)); |
| 1844 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1845 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITH_IDD, number.country_code_source()); |
| 1846 EXPECT_EQ(stripped_number, phone_number); |
| 1847 |
| 1848 number.Clear(); |
| 1849 phone_number.assign("+6423456789"); |
| 1850 stripped_number.assign("23456789"); |
| 1851 expected_country_code = 64; |
| 1852 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1853 MaybeExtractCountryCode(metadata, true, &phone_number, &number)); |
| 1854 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1855 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN, |
| 1856 number.country_code_source()); |
| 1857 EXPECT_EQ(stripped_number, phone_number); |
| 1858 |
| 1859 // Should not have extracted a country code - no international prefix present. |
| 1860 number.Clear(); |
| 1861 expected_country_code = 0; |
| 1862 phone_number.assign("2345-6789"); |
| 1863 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1864 MaybeExtractCountryCode(metadata, true, &phone_number, &number)); |
| 1865 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1866 EXPECT_EQ(PhoneNumber::FROM_DEFAULT_COUNTRY, number.country_code_source()); |
| 1867 EXPECT_EQ(stripped_number, phone_number); |
| 1868 |
| 1869 expected_country_code = 0; |
| 1870 phone_number.assign("0119991123456789"); |
| 1871 stripped_number.assign(phone_number); |
| 1872 EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR, |
| 1873 MaybeExtractCountryCode(metadata, true, &phone_number, &number)); |
| 1874 |
| 1875 number.Clear(); |
| 1876 phone_number.assign("(1 610) 619 4466"); |
| 1877 stripped_number.assign("6106194466"); |
| 1878 expected_country_code = 1; |
| 1879 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1880 MaybeExtractCountryCode(metadata, true, &phone_number, &number)); |
| 1881 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1882 EXPECT_EQ(PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN, |
| 1883 number.country_code_source()); |
| 1884 EXPECT_EQ(stripped_number, phone_number); |
| 1885 |
| 1886 number.Clear(); |
| 1887 phone_number.assign("(1 610) 619 4466"); |
| 1888 stripped_number.assign("6106194466"); |
| 1889 expected_country_code = 1; |
| 1890 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1891 MaybeExtractCountryCode(metadata, false, &phone_number, &number)); |
| 1892 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1893 EXPECT_FALSE(number.has_country_code_source()); |
| 1894 EXPECT_EQ(stripped_number, phone_number); |
| 1895 |
| 1896 // Should not have extracted a country code - invalid number after extraction |
| 1897 // of uncertain country code. |
| 1898 number.Clear(); |
| 1899 phone_number.assign("(1 610) 619 446"); |
| 1900 stripped_number.assign("1610619446"); |
| 1901 expected_country_code = 0; |
| 1902 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1903 MaybeExtractCountryCode(metadata, false, &phone_number, &number)); |
| 1904 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1905 EXPECT_FALSE(number.has_country_code_source()); |
| 1906 EXPECT_EQ(stripped_number, phone_number); |
| 1907 |
| 1908 number.Clear(); |
| 1909 phone_number.assign("(1 610) 619"); |
| 1910 stripped_number.assign("1610619"); |
| 1911 expected_country_code = 0; |
| 1912 // Should not have extracted a country code - invalid number both before and |
| 1913 // after extraction of uncertain country code. |
| 1914 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 1915 MaybeExtractCountryCode(metadata, true, &phone_number, &number)); |
| 1916 EXPECT_EQ(expected_country_code, number.country_code()); |
| 1917 EXPECT_EQ(PhoneNumber::FROM_DEFAULT_COUNTRY, number.country_code_source()); |
| 1918 EXPECT_EQ(stripped_number, phone_number); |
| 1919 } |
| 1920 |
| 1921 TEST_F(PhoneNumberUtilTest, CountryWithNoNumberDesc) { |
| 1922 string formatted_number; |
| 1923 // Andorra is a country where we don't have PhoneNumberDesc info in the |
| 1924 // metadata. |
| 1925 PhoneNumber ad_number; |
| 1926 ad_number.set_country_code(376); |
| 1927 ad_number.set_national_number(12345ULL); |
| 1928 phone_util_.Format(ad_number, PhoneNumberUtil::INTERNATIONAL, |
| 1929 &formatted_number); |
| 1930 EXPECT_EQ("+376 12345", formatted_number); |
| 1931 phone_util_.Format(ad_number, PhoneNumberUtil::E164, &formatted_number); |
| 1932 EXPECT_EQ("+37612345", formatted_number); |
| 1933 phone_util_.Format(ad_number, PhoneNumberUtil::NATIONAL, &formatted_number); |
| 1934 EXPECT_EQ("12345", formatted_number); |
| 1935 EXPECT_EQ(PhoneNumberUtil::UNKNOWN, phone_util_.GetNumberType(ad_number)); |
| 1936 EXPECT_TRUE(phone_util_.IsValidNumber(ad_number)); |
| 1937 |
| 1938 // Test dialing a US number from within Andorra. |
| 1939 PhoneNumber us_number; |
| 1940 us_number.set_country_code(1); |
| 1941 us_number.set_national_number(6502530000ULL); |
| 1942 phone_util_.FormatOutOfCountryCallingNumber(us_number, RegionCode::AD(), |
| 1943 &formatted_number); |
| 1944 EXPECT_EQ("00 1 650 253 0000", formatted_number); |
| 1945 } |
| 1946 |
| 1947 TEST_F(PhoneNumberUtilTest, UnknownCountryCallingCodeForValidation) { |
| 1948 PhoneNumber invalid_number; |
| 1949 invalid_number.set_country_code(0); |
| 1950 invalid_number.set_national_number(1234ULL); |
| 1951 EXPECT_FALSE(phone_util_.IsValidNumber(invalid_number)); |
| 1952 } |
| 1953 |
| 1954 TEST_F(PhoneNumberUtilTest, IsNumberMatchMatches) { |
| 1955 // Test simple matches where formatting is different, or leading zeroes, or |
| 1956 // country code has been specified. |
| 1957 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1958 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331 6005", |
| 1959 "+64 03 331 6005")); |
| 1960 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1961 phone_util_.IsNumberMatchWithTwoStrings("+64 03 331-6005", |
| 1962 "+64 03331 6005")); |
| 1963 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1964 phone_util_.IsNumberMatchWithTwoStrings("+643 331-6005", |
| 1965 "+64033316005")); |
| 1966 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1967 phone_util_.IsNumberMatchWithTwoStrings("+643 331-6005", |
| 1968 "+6433316005")); |
| 1969 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1970 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005", |
| 1971 "+6433316005")); |
| 1972 // Test alpha numbers. |
| 1973 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1974 phone_util_.IsNumberMatchWithTwoStrings("+1800 siX-Flags", |
| 1975 "+1 800 7493 5247")); |
| 1976 // Test numbers with extensions. |
| 1977 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1978 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005 extn 1234", |
| 1979 "+6433316005#1234")); |
| 1980 // Test proto buffers. |
| 1981 PhoneNumber nz_number; |
| 1982 nz_number.set_country_code(64); |
| 1983 nz_number.set_national_number(33316005ULL); |
| 1984 nz_number.set_extension("3456"); |
| 1985 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1986 phone_util_.IsNumberMatchWithOneString(nz_number, |
| 1987 "+643 331 6005 ext 3456")); |
| 1988 nz_number.clear_extension(); |
| 1989 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1990 phone_util_.IsNumberMatchWithOneString(nz_number, |
| 1991 "+643 331 6005")); |
| 1992 // Check empty extensions are ignored. |
| 1993 nz_number.set_extension(""); |
| 1994 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 1995 phone_util_.IsNumberMatchWithOneString(nz_number, |
| 1996 "+643 331 6005")); |
| 1997 // Check variant with two proto buffers. |
| 1998 PhoneNumber nz_number_2; |
| 1999 nz_number_2.set_country_code(64); |
| 2000 nz_number_2.set_national_number(33316005ULL); |
| 2001 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 2002 phone_util_.IsNumberMatch(nz_number, nz_number_2)); |
| 2003 |
| 2004 // Check raw_input, country_code_source and preferred_domestic_carrier_code |
| 2005 // are ignored. |
| 2006 PhoneNumber br_number_1; |
| 2007 PhoneNumber br_number_2; |
| 2008 br_number_1.set_country_code(55); |
| 2009 br_number_1.set_national_number(3121286979ULL); |
| 2010 br_number_1.set_country_code_source(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); |
| 2011 br_number_1.set_preferred_domestic_carrier_code("12"); |
| 2012 br_number_1.set_raw_input("012 3121286979"); |
| 2013 br_number_2.set_country_code(55); |
| 2014 br_number_2.set_national_number(3121286979ULL); |
| 2015 br_number_2.set_country_code_source(PhoneNumber::FROM_DEFAULT_COUNTRY); |
| 2016 br_number_2.set_preferred_domestic_carrier_code("14"); |
| 2017 br_number_2.set_raw_input("143121286979"); |
| 2018 EXPECT_EQ(PhoneNumberUtil::EXACT_MATCH, |
| 2019 phone_util_.IsNumberMatch(br_number_1, br_number_2)); |
| 2020 } |
| 2021 |
| 2022 TEST_F(PhoneNumberUtilTest, IsNumberMatchNonMetches) { |
| 2023 // NSN matches. |
| 2024 EXPECT_EQ(PhoneNumberUtil::NO_MATCH, |
| 2025 phone_util_.IsNumberMatchWithTwoStrings("03 331 6005", |
| 2026 "03 331 6006")); |
| 2027 // Different country code, partial number match. |
| 2028 EXPECT_EQ(PhoneNumberUtil::NO_MATCH, |
| 2029 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005", |
| 2030 "+16433316005")); |
| 2031 // Different country code, same number. |
| 2032 EXPECT_EQ(PhoneNumberUtil::NO_MATCH, |
| 2033 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005", |
| 2034 "+6133316005")); |
| 2035 // Extension different, all else the same. |
| 2036 EXPECT_EQ(PhoneNumberUtil::NO_MATCH, |
| 2037 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005 extn 1234", |
| 2038 "+0116433316005#1235")); |
| 2039 // NSN matches, but extension is different - not the same number. |
| 2040 EXPECT_EQ(PhoneNumberUtil::NO_MATCH, |
| 2041 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005 ext.1235", |
| 2042 "3 331 6005#1234")); |
| 2043 // Invalid numbers that can't be parsed. |
| 2044 EXPECT_EQ(PhoneNumberUtil::INVALID_NUMBER, |
| 2045 phone_util_.IsNumberMatchWithTwoStrings("43", "3 331 6043")); |
| 2046 // Invalid numbers that can't be parsed. |
| 2047 EXPECT_EQ(PhoneNumberUtil::INVALID_NUMBER, |
| 2048 phone_util_.IsNumberMatchWithTwoStrings("+43", "+64 3 331 6005")); |
| 2049 EXPECT_EQ(PhoneNumberUtil::INVALID_NUMBER, |
| 2050 phone_util_.IsNumberMatchWithTwoStrings("+43", "64 3 331 6005")); |
| 2051 EXPECT_EQ(PhoneNumberUtil::INVALID_NUMBER, |
| 2052 phone_util_.IsNumberMatchWithTwoStrings("Dog", "64 3 331 6005")); |
| 2053 } |
| 2054 |
| 2055 TEST_F(PhoneNumberUtilTest, IsNumberMatchNsnMatches) { |
| 2056 // NSN matches. |
| 2057 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2058 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005", |
| 2059 "03 331 6005")); |
| 2060 |
| 2061 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2062 phone_util_.IsNumberMatchWithTwoStrings("3 331-6005", |
| 2063 "03 331 6005")); |
| 2064 |
| 2065 PhoneNumber nz_number; |
| 2066 nz_number.set_country_code(64); |
| 2067 nz_number.set_national_number(33316005ULL); |
| 2068 nz_number.set_extension(""); |
| 2069 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2070 phone_util_.IsNumberMatchWithOneString(nz_number, "03 331 6005")); |
| 2071 // Here the second number possibly starts with the country code for New |
| 2072 // Zealand, although we are unsure. |
| 2073 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2074 phone_util_.IsNumberMatchWithOneString(nz_number, |
| 2075 "(64-3) 331 6005")); |
| 2076 |
| 2077 // Here, the 1 might be a national prefix, if we compare it to the US number, |
| 2078 // so the resultant match is an NSN match. |
| 2079 PhoneNumber us_number; |
| 2080 us_number.set_country_code(1); |
| 2081 us_number.set_national_number(2345678901ULL); |
| 2082 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2083 phone_util_.IsNumberMatchWithOneString(us_number, |
| 2084 "1-234-567-8901")); |
| 2085 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2086 phone_util_.IsNumberMatchWithOneString(us_number, "2345678901")); |
| 2087 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2088 phone_util_.IsNumberMatchWithTwoStrings("+1 234-567 8901", |
| 2089 "1 234 567 8901")); |
| 2090 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2091 phone_util_.IsNumberMatchWithTwoStrings("1 234-567 8901", |
| 2092 "1 234 567 8901")); |
| 2093 EXPECT_EQ(PhoneNumberUtil::NSN_MATCH, |
| 2094 phone_util_.IsNumberMatchWithTwoStrings("1 234-567 8901", |
| 2095 "+1 234 567 8901")); |
| 2096 // For this case, the match will be a short NSN match, because we cannot |
| 2097 // assume that the 1 might be a national prefix, so don't remove it when |
| 2098 // parsing. |
| 2099 PhoneNumber random_number; |
| 2100 random_number.set_country_code(41); |
| 2101 random_number.set_national_number(2345678901ULL); |
| 2102 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2103 phone_util_.IsNumberMatchWithOneString(random_number, |
| 2104 "1-234-567-8901")); |
| 2105 } |
| 2106 |
| 2107 TEST_F(PhoneNumberUtilTest, IsNumberMatchShortNsnMatches) { |
| 2108 // Short NSN matches with the country not specified for either one or both |
| 2109 // numbers. |
| 2110 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2111 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005", |
| 2112 "331 6005")); |
| 2113 |
| 2114 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2115 phone_util_.IsNumberMatchWithTwoStrings("3 331-6005", |
| 2116 "331 6005")); |
| 2117 |
| 2118 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2119 phone_util_.IsNumberMatchWithTwoStrings("3 331-6005", |
| 2120 "+64 331 6005")); |
| 2121 |
| 2122 // Short NSN match with the country specified. |
| 2123 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2124 phone_util_.IsNumberMatchWithTwoStrings("03 331-6005", |
| 2125 "331 6005")); |
| 2126 |
| 2127 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2128 phone_util_.IsNumberMatchWithTwoStrings("1 234 345 6789", |
| 2129 "345 6789")); |
| 2130 |
| 2131 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2132 phone_util_.IsNumberMatchWithTwoStrings("+1 (234) 345 6789", |
| 2133 "345 6789")); |
| 2134 |
| 2135 // NSN matches, country code omitted for one number, extension missing for |
| 2136 // one. |
| 2137 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2138 phone_util_.IsNumberMatchWithTwoStrings("+64 3 331-6005", |
| 2139 "3 331 6005#1234")); |
| 2140 |
| 2141 // One has Italian leading zero, one does not. |
| 2142 PhoneNumber it_number_1, it_number_2; |
| 2143 it_number_1.set_country_code(39); |
| 2144 it_number_1.set_national_number(1234ULL); |
| 2145 it_number_1.set_italian_leading_zero(true); |
| 2146 it_number_2.set_country_code(39); |
| 2147 it_number_2.set_national_number(1234ULL); |
| 2148 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2149 phone_util_.IsNumberMatch(it_number_1, it_number_2)); |
| 2150 |
| 2151 // One has an extension, the other has an extension of "". |
| 2152 it_number_1.set_extension("1234"); |
| 2153 it_number_1.clear_italian_leading_zero(); |
| 2154 it_number_2.set_extension(""); |
| 2155 EXPECT_EQ(PhoneNumberUtil::SHORT_NSN_MATCH, |
| 2156 phone_util_.IsNumberMatch(it_number_1, it_number_2)); |
| 2157 } |
| 2158 |
| 2159 TEST_F(PhoneNumberUtilTest, ParseNationalNumber) { |
| 2160 PhoneNumber nz_number; |
| 2161 nz_number.set_country_code(64); |
| 2162 nz_number.set_national_number(33316005ULL); |
| 2163 PhoneNumber test_number; |
| 2164 // National prefix attached. |
| 2165 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2166 phone_util_.Parse("033316005", RegionCode::NZ(), &test_number)); |
| 2167 EXPECT_EQ(nz_number, test_number); |
| 2168 // National prefix missing. |
| 2169 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2170 phone_util_.Parse("33316005", RegionCode::NZ(), &test_number)); |
| 2171 EXPECT_EQ(nz_number, test_number); |
| 2172 // National prefix attached and some formatting present. |
| 2173 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2174 phone_util_.Parse("03-331 6005", RegionCode::NZ(), &test_number)); |
| 2175 EXPECT_EQ(nz_number, test_number); |
| 2176 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2177 phone_util_.Parse("03 331 6005", RegionCode::NZ(), &test_number)); |
| 2178 EXPECT_EQ(nz_number, test_number); |
| 2179 |
| 2180 // Testing international prefixes. |
| 2181 // Should strip country code. |
| 2182 test_number.Clear(); |
| 2183 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2184 phone_util_.Parse("0064 3 331 6005", |
| 2185 RegionCode::NZ(), &test_number)); |
| 2186 EXPECT_EQ(nz_number, test_number); |
| 2187 // Try again, but this time we have an international number with Region Code |
| 2188 // US. It should recognise the country code and parse accordingly. |
| 2189 test_number.Clear(); |
| 2190 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2191 phone_util_.Parse("01164 3 331 6005", |
| 2192 RegionCode::US(), &test_number)); |
| 2193 EXPECT_EQ(nz_number, test_number); |
| 2194 test_number.Clear(); |
| 2195 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2196 phone_util_.Parse("+64 3 331 6005", |
| 2197 RegionCode::US(), &test_number)); |
| 2198 EXPECT_EQ(nz_number, test_number); |
| 2199 |
| 2200 // Test for http://b/issue?id=2247493 |
| 2201 nz_number.Clear(); |
| 2202 nz_number.set_country_code(64); |
| 2203 nz_number.set_national_number(64123456ULL); |
| 2204 test_number.Clear(); |
| 2205 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2206 phone_util_.Parse("+64(0)64123456", |
| 2207 RegionCode::US(), &test_number)); |
| 2208 EXPECT_EQ(nz_number, test_number); |
| 2209 |
| 2210 // Check that using a "/" is fine in a phone number. |
| 2211 PhoneNumber de_number; |
| 2212 de_number.set_country_code(49); |
| 2213 de_number.set_national_number(12345678ULL); |
| 2214 test_number.Clear(); |
| 2215 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2216 phone_util_.Parse("123/45678", RegionCode::DE(), &test_number)); |
| 2217 EXPECT_EQ(de_number, test_number); |
| 2218 |
| 2219 PhoneNumber us_number; |
| 2220 us_number.set_country_code(1); |
| 2221 // Check it doesn't use the '1' as a country code when parsing if the phone |
| 2222 // number was already possible. |
| 2223 us_number.set_national_number(1234567890ULL); |
| 2224 test_number.Clear(); |
| 2225 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2226 phone_util_.Parse("123-456-7890", RegionCode::US(), &test_number)); |
| 2227 EXPECT_EQ(us_number, test_number); |
| 2228 } |
| 2229 |
| 2230 TEST_F(PhoneNumberUtilTest, ParseNumberWithAlphaCharacters) { |
| 2231 // Test case with alpha characters. |
| 2232 PhoneNumber test_number; |
| 2233 PhoneNumber tollfree_number; |
| 2234 tollfree_number.set_country_code(64); |
| 2235 tollfree_number.set_national_number(800332005ULL); |
| 2236 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2237 phone_util_.Parse("0800 DDA 005", RegionCode::NZ(), &test_number)); |
| 2238 EXPECT_EQ(tollfree_number, test_number); |
| 2239 |
| 2240 test_number.Clear(); |
| 2241 PhoneNumber premium_number; |
| 2242 premium_number.set_country_code(64); |
| 2243 premium_number.set_national_number(9003326005ULL); |
| 2244 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2245 phone_util_.Parse("0900 DDA 6005", RegionCode::NZ(), &test_number)); |
| 2246 EXPECT_EQ(premium_number, test_number); |
| 2247 |
| 2248 // Not enough alpha characters for them to be considered intentional, so they |
| 2249 // are stripped. |
| 2250 test_number.Clear(); |
| 2251 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2252 phone_util_.Parse("0900 332 6005a", |
| 2253 RegionCode::NZ(), &test_number)); |
| 2254 EXPECT_EQ(premium_number, test_number); |
| 2255 test_number.Clear(); |
| 2256 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2257 phone_util_.Parse("0900 332 600a5", |
| 2258 RegionCode::NZ(), &test_number)); |
| 2259 EXPECT_EQ(premium_number, test_number); |
| 2260 |
| 2261 test_number.Clear(); |
| 2262 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2263 phone_util_.Parse("0900 332 600A5", |
| 2264 RegionCode::NZ(), &test_number)); |
| 2265 EXPECT_EQ(premium_number, test_number); |
| 2266 |
| 2267 test_number.Clear(); |
| 2268 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2269 phone_util_.Parse("0900 a332 600A5", |
| 2270 RegionCode::NZ(), &test_number)); |
| 2271 EXPECT_EQ(premium_number, test_number); |
| 2272 } |
| 2273 |
| 2274 TEST_F(PhoneNumberUtilTest, ParseWithInternationalPrefixes) { |
| 2275 PhoneNumber us_number; |
| 2276 us_number.set_country_code(1); |
| 2277 us_number.set_national_number(6503336000ULL); |
| 2278 PhoneNumber test_number; |
| 2279 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2280 phone_util_.Parse("+1 (650) 333-6000", |
| 2281 RegionCode::US(), &test_number)); |
| 2282 EXPECT_EQ(us_number, test_number); |
| 2283 test_number.Clear(); |
| 2284 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2285 phone_util_.Parse("+1-650-333-6000", |
| 2286 RegionCode::US(), &test_number)); |
| 2287 EXPECT_EQ(us_number, test_number); |
| 2288 |
| 2289 // Calling the US number from Singapore by using different service providers |
| 2290 // 1st test: calling using SingTel IDD service (IDD is 001) |
| 2291 test_number.Clear(); |
| 2292 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2293 phone_util_.Parse("0011-650-333-6000", |
| 2294 RegionCode::SG(), &test_number)); |
| 2295 EXPECT_EQ(us_number, test_number); |
| 2296 // 2nd test: calling using StarHub IDD service (IDD is 008) |
| 2297 test_number.Clear(); |
| 2298 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2299 phone_util_.Parse("0081-650-333-6000", |
| 2300 RegionCode::SG(), &test_number)); |
| 2301 EXPECT_EQ(us_number, test_number); |
| 2302 // 3rd test: calling using SingTel V019 service (IDD is 019) |
| 2303 test_number.Clear(); |
| 2304 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2305 phone_util_.Parse("0191-650-333-6000", |
| 2306 RegionCode::SG(), &test_number)); |
| 2307 EXPECT_EQ(us_number, test_number); |
| 2308 // Calling the US number from Poland |
| 2309 test_number.Clear(); |
| 2310 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2311 phone_util_.Parse("0~01-650-333-6000", |
| 2312 RegionCode::PL(), &test_number)); |
| 2313 EXPECT_EQ(us_number, test_number); |
| 2314 |
| 2315 // Using "++" at the start. |
| 2316 test_number.Clear(); |
| 2317 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2318 phone_util_.Parse("++1 (650) 333-6000", |
| 2319 RegionCode::PL(), &test_number)); |
| 2320 EXPECT_EQ(us_number, test_number); |
| 2321 // Using a full-width plus sign. |
| 2322 test_number.Clear(); |
| 2323 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2324 phone_util_.Parse("+1 (650) 333-6000", |
| 2325 RegionCode::SG(), &test_number)); |
| 2326 EXPECT_EQ(us_number, test_number); |
| 2327 // The whole number, including punctuation, is here represented in full-width |
| 2328 // form. |
| 2329 test_number.Clear(); |
| 2330 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2331 phone_util_.Parse("+1 (650) 333-6000", |
| 2332 RegionCode::SG(), &test_number)); |
| 2333 EXPECT_EQ(us_number, test_number); |
| 2334 |
| 2335 // Using the U+30FC dash. |
| 2336 test_number.Clear(); |
| 2337 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2338 phone_util_.Parse("+1 (650) 333ー6000", |
| 2339 RegionCode::SG(), &test_number)); |
| 2340 EXPECT_EQ(us_number, test_number); |
| 2341 } |
| 2342 |
| 2343 TEST_F(PhoneNumberUtilTest, ParseWithLeadingZero) { |
| 2344 PhoneNumber it_number; |
| 2345 it_number.set_country_code(39); |
| 2346 it_number.set_national_number(236618300ULL); |
| 2347 it_number.set_italian_leading_zero(true); |
| 2348 PhoneNumber test_number; |
| 2349 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2350 phone_util_.Parse("+39 02-36618 300", |
| 2351 RegionCode::NZ(), &test_number)); |
| 2352 EXPECT_EQ(it_number, test_number); |
| 2353 test_number.Clear(); |
| 2354 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2355 phone_util_.Parse("02-36618 300", RegionCode::IT(), &test_number)); |
| 2356 EXPECT_EQ(it_number, test_number); |
| 2357 |
| 2358 it_number.Clear(); |
| 2359 it_number.set_country_code(39); |
| 2360 it_number.set_national_number(312345678ULL); |
| 2361 test_number.Clear(); |
| 2362 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2363 phone_util_.Parse("312 345 678", RegionCode::IT(), &test_number)); |
| 2364 EXPECT_EQ(it_number, test_number); |
| 2365 } |
| 2366 |
| 2367 TEST_F(PhoneNumberUtilTest, ParseNationalNumberArgentina) { |
| 2368 // Test parsing mobile numbers of Argentina. |
| 2369 PhoneNumber ar_number; |
| 2370 ar_number.set_country_code(54); |
| 2371 ar_number.set_national_number(93435551212ULL); |
| 2372 PhoneNumber test_number; |
| 2373 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2374 phone_util_.Parse("+54 9 343 555 1212", RegionCode::AR(), |
| 2375 &test_number)); |
| 2376 EXPECT_EQ(ar_number, test_number); |
| 2377 test_number.Clear(); |
| 2378 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2379 phone_util_.Parse("0343 15 555 1212", RegionCode::AR(), |
| 2380 &test_number)); |
| 2381 EXPECT_EQ(ar_number, test_number); |
| 2382 |
| 2383 ar_number.set_national_number(93715654320ULL); |
| 2384 test_number.Clear(); |
| 2385 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2386 phone_util_.Parse("+54 9 3715 65 4320", RegionCode::AR(), |
| 2387 &test_number)); |
| 2388 EXPECT_EQ(ar_number, test_number); |
| 2389 test_number.Clear(); |
| 2390 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2391 phone_util_.Parse("03715 15 65 4320", RegionCode::AR(), |
| 2392 &test_number)); |
| 2393 EXPECT_EQ(ar_number, test_number); |
| 2394 |
| 2395 // Test parsing fixed-line numbers of Argentina. |
| 2396 ar_number.set_national_number(1137970000ULL); |
| 2397 test_number.Clear(); |
| 2398 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2399 phone_util_.Parse("+54 11 3797 0000", RegionCode::AR(), |
| 2400 &test_number)); |
| 2401 EXPECT_EQ(ar_number, test_number); |
| 2402 test_number.Clear(); |
| 2403 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2404 phone_util_.Parse("011 3797 0000", RegionCode::AR(), &test_number)); |
| 2405 EXPECT_EQ(ar_number, test_number); |
| 2406 |
| 2407 ar_number.set_national_number(3715654321ULL); |
| 2408 test_number.Clear(); |
| 2409 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2410 phone_util_.Parse("+54 3715 65 4321", RegionCode::AR(), |
| 2411 &test_number)); |
| 2412 EXPECT_EQ(ar_number, test_number); |
| 2413 test_number.Clear(); |
| 2414 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2415 phone_util_.Parse("03715 65 4321", RegionCode::AR(), &test_number)); |
| 2416 EXPECT_EQ(ar_number, test_number); |
| 2417 |
| 2418 ar_number.set_national_number(2312340000ULL); |
| 2419 test_number.Clear(); |
| 2420 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2421 phone_util_.Parse("+54 23 1234 0000", RegionCode::AR(), |
| 2422 &test_number)); |
| 2423 EXPECT_EQ(ar_number, test_number); |
| 2424 test_number.Clear(); |
| 2425 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2426 phone_util_.Parse("023 1234 0000", RegionCode::AR(), &test_number)); |
| 2427 EXPECT_EQ(ar_number, test_number); |
| 2428 } |
| 2429 |
| 2430 TEST_F(PhoneNumberUtilTest, ParseWithXInNumber) { |
| 2431 // Test that having an 'x' in the phone number at the start is ok and that it |
| 2432 // just gets removed. |
| 2433 PhoneNumber ar_number; |
| 2434 ar_number.set_country_code(54); |
| 2435 ar_number.set_national_number(123456789ULL); |
| 2436 PhoneNumber test_number; |
| 2437 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2438 phone_util_.Parse("0123456789", RegionCode::AR(), &test_number)); |
| 2439 EXPECT_EQ(ar_number, test_number); |
| 2440 test_number.Clear(); |
| 2441 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2442 phone_util_.Parse("(0) 123456789", RegionCode::AR(), &test_number)); |
| 2443 EXPECT_EQ(ar_number, test_number); |
| 2444 test_number.Clear(); |
| 2445 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2446 phone_util_.Parse("0 123456789", RegionCode::AR(), &test_number)); |
| 2447 EXPECT_EQ(ar_number, test_number); |
| 2448 test_number.Clear(); |
| 2449 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2450 phone_util_.Parse("(0xx) 123456789", RegionCode::AR(), |
| 2451 &test_number)); |
| 2452 EXPECT_EQ(ar_number, test_number); |
| 2453 |
| 2454 PhoneNumber ar_from_us; |
| 2455 ar_from_us.set_country_code(54); |
| 2456 ar_from_us.set_national_number(81429712ULL); |
| 2457 // This test is intentionally constructed such that the number of digit after |
| 2458 // xx is larger than 7, so that the number won't be mistakenly treated as an |
| 2459 // extension, as we allow extensions up to 7 digits. This assumption is okay |
| 2460 // for now as all the countries where a carrier selection code is written in |
| 2461 // the form of xx have a national significant number of length larger than 7. |
| 2462 test_number.Clear(); |
| 2463 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2464 phone_util_.Parse("011xx5481429712", RegionCode::US(), |
| 2465 &test_number)); |
| 2466 EXPECT_EQ(ar_from_us, test_number); |
| 2467 } |
| 2468 |
| 2469 TEST_F(PhoneNumberUtilTest, ParseNumbersMexico) { |
| 2470 // Test parsing fixed-line numbers of Mexico. |
| 2471 PhoneNumber mx_number; |
| 2472 |
| 2473 mx_number.set_country_code(52); |
| 2474 mx_number.set_national_number(4499780001ULL); |
| 2475 PhoneNumber test_number; |
| 2476 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2477 phone_util_.Parse("+52 (449)978-0001", RegionCode::MX(), |
| 2478 &test_number)); |
| 2479 EXPECT_EQ(mx_number, test_number); |
| 2480 test_number.Clear(); |
| 2481 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2482 phone_util_.Parse("01 (449)978-0001", RegionCode::MX(), |
| 2483 &test_number)); |
| 2484 EXPECT_EQ(mx_number, test_number); |
| 2485 test_number.Clear(); |
| 2486 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2487 phone_util_.Parse("(449)978-0001", RegionCode::MX(), |
| 2488 &test_number)); |
| 2489 EXPECT_EQ(mx_number, test_number); |
| 2490 |
| 2491 // Test parsing mobile numbers of Mexico. |
| 2492 mx_number.Clear(); |
| 2493 mx_number.set_country_code(52); |
| 2494 mx_number.set_national_number(13312345678ULL); |
| 2495 test_number.Clear(); |
| 2496 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2497 phone_util_.Parse("+52 1 33 1234-5678", RegionCode::MX(), |
| 2498 &test_number)); |
| 2499 EXPECT_EQ(mx_number, test_number); |
| 2500 test_number.Clear(); |
| 2501 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2502 phone_util_.Parse("044 (33) 1234-5678", RegionCode::MX(), |
| 2503 &test_number)); |
| 2504 EXPECT_EQ(mx_number, test_number); |
| 2505 test_number.Clear(); |
| 2506 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2507 phone_util_.Parse("045 33 1234-5678", RegionCode::MX(), |
| 2508 &test_number)); |
| 2509 EXPECT_EQ(mx_number, test_number); |
| 2510 } |
| 2511 |
| 2512 TEST_F(PhoneNumberUtilTest, FailedParseOnInvalidNumbers) { |
| 2513 PhoneNumber test_number; |
| 2514 EXPECT_EQ(PhoneNumberUtil::NOT_A_NUMBER, |
| 2515 phone_util_.Parse("This is not a phone number", RegionCode::NZ(), |
| 2516 &test_number)); |
| 2517 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2518 |
| 2519 EXPECT_EQ(PhoneNumberUtil::TOO_LONG_NSN, |
| 2520 phone_util_.Parse("01495 72553301873 810104", RegionCode::GB(), |
| 2521 &test_number)); |
| 2522 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2523 |
| 2524 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT_NSN, |
| 2525 phone_util_.Parse("+49 0", RegionCode::DE(), |
| 2526 &test_number)); |
| 2527 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2528 |
| 2529 EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR, |
| 2530 phone_util_.Parse("+210 3456 56789", RegionCode::NZ(), |
| 2531 &test_number)); |
| 2532 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2533 |
| 2534 EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR, |
| 2535 phone_util_.Parse("123 456 7890", RegionCode::ZZ(), |
| 2536 &test_number)); |
| 2537 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2538 |
| 2539 EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR, |
| 2540 phone_util_.Parse("123 456 7890", RegionCode::CS(), |
| 2541 &test_number)); |
| 2542 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2543 |
| 2544 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT_AFTER_IDD, |
| 2545 phone_util_.Parse("0044-----", RegionCode::GB(), |
| 2546 &test_number)); |
| 2547 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2548 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT_AFTER_IDD, |
| 2549 phone_util_.Parse("0044", RegionCode::GB(), |
| 2550 &test_number)); |
| 2551 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2552 |
| 2553 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT_AFTER_IDD, |
| 2554 phone_util_.Parse("011", RegionCode::US(), |
| 2555 &test_number)); |
| 2556 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2557 EXPECT_EQ(PhoneNumberUtil::TOO_SHORT_AFTER_IDD, |
| 2558 phone_util_.Parse("0119", RegionCode::US(), |
| 2559 &test_number)); |
| 2560 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2561 } |
| 2562 |
| 2563 TEST_F(PhoneNumberUtilTest, ParseNumbersWithPlusWithNoRegion) { |
| 2564 PhoneNumber nz_number; |
| 2565 nz_number.set_country_code(64); |
| 2566 nz_number.set_national_number(33316005ULL); |
| 2567 // "ZZ" is allowed only if the number starts with a '+' - then |
| 2568 // the country code can be calculated. |
| 2569 PhoneNumber result_proto; |
| 2570 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2571 phone_util_.Parse("+64 3 331 6005", RegionCode::ZZ(), |
| 2572 &result_proto)); |
| 2573 EXPECT_EQ(nz_number, result_proto); |
| 2574 |
| 2575 // Test with full-width plus. |
| 2576 result_proto.Clear(); |
| 2577 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2578 phone_util_.Parse("+64 3 331 6005", RegionCode::ZZ(), |
| 2579 &result_proto)); |
| 2580 EXPECT_EQ(nz_number, result_proto); |
| 2581 // Test with normal plus but leading characters that need to be stripped. |
| 2582 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2583 phone_util_.Parse(" +64 3 331 6005", RegionCode::ZZ(), |
| 2584 &result_proto)); |
| 2585 EXPECT_EQ(nz_number, result_proto); |
| 2586 |
| 2587 nz_number.set_raw_input("+64 3 331 6005"); |
| 2588 nz_number.set_country_code_source(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); |
| 2589 // It is important that we set this to an empty string, since we used |
| 2590 // ParseAndKeepRawInput and no carrrier code was found. |
| 2591 nz_number.set_preferred_domestic_carrier_code(""); |
| 2592 result_proto.Clear(); |
| 2593 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2594 phone_util_.ParseAndKeepRawInput("+64 3 331 6005", RegionCode::ZZ(), |
| 2595 &result_proto)); |
| 2596 EXPECT_EQ(nz_number, result_proto); |
| 2597 } |
| 2598 |
| 2599 TEST_F(PhoneNumberUtilTest, ParseExtensions) { |
| 2600 PhoneNumber nz_number; |
| 2601 nz_number.set_country_code(64); |
| 2602 nz_number.set_national_number(33316005ULL); |
| 2603 nz_number.set_extension("3456"); |
| 2604 PhoneNumber test_number; |
| 2605 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2606 phone_util_.Parse("03 331 6005 ext 3456", RegionCode::NZ(), |
| 2607 &test_number)); |
| 2608 EXPECT_EQ(nz_number, test_number); |
| 2609 test_number.Clear(); |
| 2610 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2611 phone_util_.Parse("03 331 6005x3456", RegionCode::NZ(), |
| 2612 &test_number)); |
| 2613 EXPECT_EQ(nz_number, test_number); |
| 2614 test_number.Clear(); |
| 2615 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2616 phone_util_.Parse("03-331 6005 int.3456", RegionCode::NZ(), |
| 2617 &test_number)); |
| 2618 EXPECT_EQ(nz_number, test_number); |
| 2619 test_number.Clear(); |
| 2620 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2621 phone_util_.Parse("03 331 6005 #3456", RegionCode::NZ(), |
| 2622 &test_number)); |
| 2623 EXPECT_EQ(nz_number, test_number); |
| 2624 |
| 2625 // Test the following do not extract extensions: |
| 2626 PhoneNumber non_extn_number; |
| 2627 non_extn_number.set_country_code(1); |
| 2628 non_extn_number.set_national_number(80074935247ULL); |
| 2629 test_number.Clear(); |
| 2630 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2631 phone_util_.Parse("1800 six-flags", RegionCode::US(), |
| 2632 &test_number)); |
| 2633 EXPECT_EQ(non_extn_number, test_number); |
| 2634 test_number.Clear(); |
| 2635 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2636 phone_util_.Parse("1800 SIX-FLAGS", RegionCode::US(), |
| 2637 &test_number)); |
| 2638 EXPECT_EQ(non_extn_number, test_number); |
| 2639 test_number.Clear(); |
| 2640 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2641 phone_util_.Parse("0~0 1800 7493 5247", RegionCode::PL(), |
| 2642 &test_number)); |
| 2643 EXPECT_EQ(non_extn_number, test_number); |
| 2644 test_number.Clear(); |
| 2645 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2646 phone_util_.Parse("(1800) 7493.5247", RegionCode::US(), |
| 2647 &test_number)); |
| 2648 EXPECT_EQ(non_extn_number, test_number); |
| 2649 |
| 2650 // Check that the last instance of an extension token is matched. |
| 2651 PhoneNumber extn_number; |
| 2652 extn_number.set_country_code(1); |
| 2653 extn_number.set_national_number(80074935247ULL); |
| 2654 extn_number.set_extension("1234"); |
| 2655 test_number.Clear(); |
| 2656 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2657 phone_util_.Parse("0~0 1800 7493 5247 ~1234", RegionCode::PL(), |
| 2658 &test_number)); |
| 2659 EXPECT_EQ(extn_number, test_number); |
| 2660 |
| 2661 // Verifying bug-fix where the last digit of a number was previously omitted |
| 2662 // if it was a 0 when extracting the extension. Also verifying a few different |
| 2663 // cases of extensions. |
| 2664 PhoneNumber uk_number; |
| 2665 uk_number.set_country_code(44); |
| 2666 uk_number.set_national_number(2034567890ULL); |
| 2667 uk_number.set_extension("456"); |
| 2668 test_number.Clear(); |
| 2669 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2670 phone_util_.Parse("+44 2034567890x456", RegionCode::NZ(), |
| 2671 &test_number)); |
| 2672 EXPECT_EQ(uk_number, test_number); |
| 2673 test_number.Clear(); |
| 2674 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2675 phone_util_.Parse("+44 2034567890x456", RegionCode::GB(), |
| 2676 &test_number)); |
| 2677 EXPECT_EQ(uk_number, test_number); |
| 2678 test_number.Clear(); |
| 2679 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2680 phone_util_.Parse("+44 2034567890 x456", RegionCode::GB(), |
| 2681 &test_number)); |
| 2682 EXPECT_EQ(uk_number, test_number); |
| 2683 test_number.Clear(); |
| 2684 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2685 phone_util_.Parse("+44 2034567890 X456", RegionCode::GB(), |
| 2686 &test_number)); |
| 2687 EXPECT_EQ(uk_number, test_number); |
| 2688 test_number.Clear(); |
| 2689 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2690 phone_util_.Parse("+44 2034567890 X 456", RegionCode::GB(), |
| 2691 &test_number)); |
| 2692 EXPECT_EQ(uk_number, test_number); |
| 2693 test_number.Clear(); |
| 2694 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2695 phone_util_.Parse("+44 2034567890 X 456", RegionCode::GB(), |
| 2696 &test_number)); |
| 2697 EXPECT_EQ(uk_number, test_number); |
| 2698 test_number.Clear(); |
| 2699 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2700 phone_util_.Parse("+44 2034567890 x 456 ", RegionCode::GB(), |
| 2701 &test_number)); |
| 2702 EXPECT_EQ(uk_number, test_number); |
| 2703 test_number.Clear(); |
| 2704 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2705 phone_util_.Parse("+44 2034567890 X 456", RegionCode::GB(), |
| 2706 &test_number)); |
| 2707 EXPECT_EQ(uk_number, test_number); |
| 2708 test_number.Clear(); |
| 2709 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2710 phone_util_.Parse("+44-2034567890;ext=456", RegionCode::GB(), |
| 2711 &test_number)); |
| 2712 EXPECT_EQ(uk_number, test_number); |
| 2713 |
| 2714 PhoneNumber us_with_extension; |
| 2715 us_with_extension.set_country_code(1); |
| 2716 us_with_extension.set_national_number(8009013355ULL); |
| 2717 us_with_extension.set_extension("7246433"); |
| 2718 test_number.Clear(); |
| 2719 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2720 phone_util_.Parse("(800) 901-3355 x 7246433", RegionCode::US(), |
| 2721 &test_number)); |
| 2722 EXPECT_EQ(us_with_extension, test_number); |
| 2723 test_number.Clear(); |
| 2724 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2725 phone_util_.Parse("(800) 901-3355 , ext 7246433", RegionCode::US(), |
| 2726 &test_number)); |
| 2727 EXPECT_EQ(us_with_extension, test_number); |
| 2728 test_number.Clear(); |
| 2729 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2730 phone_util_.Parse("(800) 901-3355 ,extension 7246433", |
| 2731 RegionCode::US(), |
| 2732 &test_number)); |
| 2733 EXPECT_EQ(us_with_extension, test_number); |
| 2734 test_number.Clear(); |
| 2735 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2736 phone_util_.Parse("(800) 901-3355 ,extensión 7246433", |
| 2737 RegionCode::US(), |
| 2738 &test_number)); |
| 2739 EXPECT_EQ(us_with_extension, test_number); |
| 2740 test_number.Clear(); |
| 2741 // Repeat with the small letter o with acute accent created by combining |
| 2742 // characters. |
| 2743 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2744 phone_util_.Parse("(800) 901-3355 ,extensión 7246433", |
| 2745 RegionCode::US(), |
| 2746 &test_number)); |
| 2747 EXPECT_EQ(us_with_extension, test_number); |
| 2748 test_number.Clear(); |
| 2749 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2750 phone_util_.Parse("(800) 901-3355 , 7246433", RegionCode::US(), |
| 2751 &test_number)); |
| 2752 EXPECT_EQ(us_with_extension, test_number); |
| 2753 test_number.Clear(); |
| 2754 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2755 phone_util_.Parse("(800) 901-3355 ext: 7246433", RegionCode::US(), |
| 2756 &test_number)); |
| 2757 EXPECT_EQ(us_with_extension, test_number); |
| 2758 |
| 2759 // Test that if a number has two extensions specified, we ignore the second. |
| 2760 PhoneNumber us_with_two_extensions_number; |
| 2761 us_with_two_extensions_number.set_country_code(1); |
| 2762 us_with_two_extensions_number.set_national_number(2121231234ULL); |
| 2763 us_with_two_extensions_number.set_extension("508"); |
| 2764 test_number.Clear(); |
| 2765 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2766 phone_util_.Parse("(212)123-1234 x508/x1234", RegionCode::US(), |
| 2767 &test_number)); |
| 2768 EXPECT_EQ(us_with_two_extensions_number, test_number); |
| 2769 test_number.Clear(); |
| 2770 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2771 phone_util_.Parse("(212)123-1234 x508/ x1234", RegionCode::US(), |
| 2772 &test_number)); |
| 2773 EXPECT_EQ(us_with_two_extensions_number, test_number); |
| 2774 test_number.Clear(); |
| 2775 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2776 phone_util_.Parse("(212)123-1234 x508\\x1234", RegionCode::US(), |
| 2777 &test_number)); |
| 2778 EXPECT_EQ(us_with_two_extensions_number, test_number); |
| 2779 |
| 2780 // Test parsing numbers in the form (645) 123-1234-910# works, where the last |
| 2781 // 3 digits before the # are an extension. |
| 2782 us_with_extension.Clear(); |
| 2783 us_with_extension.set_country_code(1); |
| 2784 us_with_extension.set_national_number(6451231234ULL); |
| 2785 us_with_extension.set_extension("910"); |
| 2786 test_number.Clear(); |
| 2787 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2788 phone_util_.Parse("+1 (645) 123 1234-910#", RegionCode::US(), |
| 2789 &test_number)); |
| 2790 EXPECT_EQ(us_with_extension, test_number); |
| 2791 } |
| 2792 |
| 2793 TEST_F(PhoneNumberUtilTest, ParseAndKeepRaw) { |
| 2794 PhoneNumber alpha_numeric_number; |
| 2795 alpha_numeric_number.set_country_code(1); |
| 2796 alpha_numeric_number.set_national_number(80074935247ULL); |
| 2797 alpha_numeric_number.set_raw_input("800 six-flags"); |
| 2798 alpha_numeric_number.set_country_code_source( |
| 2799 PhoneNumber::FROM_DEFAULT_COUNTRY); |
| 2800 alpha_numeric_number.set_preferred_domestic_carrier_code(""); |
| 2801 |
| 2802 PhoneNumber test_number; |
| 2803 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2804 phone_util_.ParseAndKeepRawInput("800 six-flags", RegionCode::US(), |
| 2805 &test_number)); |
| 2806 EXPECT_EQ(alpha_numeric_number, test_number); |
| 2807 |
| 2808 alpha_numeric_number.set_national_number(8007493524ULL); |
| 2809 alpha_numeric_number.set_raw_input("1800 six-flag"); |
| 2810 alpha_numeric_number.set_country_code_source( |
| 2811 PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN); |
| 2812 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2813 phone_util_.ParseAndKeepRawInput("1800 six-flag", RegionCode::US(), |
| 2814 &test_number)); |
| 2815 EXPECT_EQ(alpha_numeric_number, test_number); |
| 2816 |
| 2817 alpha_numeric_number.set_raw_input("+1800 six-flag"); |
| 2818 alpha_numeric_number.set_country_code_source( |
| 2819 PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN); |
| 2820 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2821 phone_util_.ParseAndKeepRawInput("+1800 six-flag", RegionCode::CN(), |
| 2822 &test_number)); |
| 2823 EXPECT_EQ(alpha_numeric_number, test_number); |
| 2824 |
| 2825 alpha_numeric_number.set_raw_input("001800 six-flag"); |
| 2826 alpha_numeric_number.set_country_code_source( |
| 2827 PhoneNumber::FROM_NUMBER_WITH_IDD); |
| 2828 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2829 phone_util_.ParseAndKeepRawInput("001800 six-flag", |
| 2830 RegionCode::NZ(), |
| 2831 &test_number)); |
| 2832 EXPECT_EQ(alpha_numeric_number, test_number); |
| 2833 |
| 2834 // Try with invalid region - expect failure. |
| 2835 test_number.Clear(); |
| 2836 EXPECT_EQ(PhoneNumberUtil::INVALID_COUNTRY_CODE_ERROR, |
| 2837 phone_util_.Parse("123 456 7890", RegionCode::CS(), &test_number)); |
| 2838 EXPECT_EQ(PhoneNumber::default_instance(), test_number); |
| 2839 |
| 2840 PhoneNumber korean_number; |
| 2841 korean_number.set_country_code(82); |
| 2842 korean_number.set_national_number(22123456); |
| 2843 korean_number.set_raw_input("08122123456"); |
| 2844 korean_number.set_country_code_source(PhoneNumber::FROM_DEFAULT_COUNTRY); |
| 2845 korean_number.set_preferred_domestic_carrier_code("81"); |
| 2846 EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, |
| 2847 phone_util_.ParseAndKeepRawInput("08122123456", |
| 2848 RegionCode::KR(), |
| 2849 &test_number)); |
| 2850 EXPECT_EQ(korean_number, test_number); |
| 2851 } |
| 2852 |
| 2853 TEST_F(PhoneNumberUtilTest, IsAlphaNumber) { |
| 2854 static const string kAlphaNumber("1800 six-flags"); |
| 2855 EXPECT_TRUE(phone_util_.IsAlphaNumber(kAlphaNumber)); |
| 2856 static const string kAlphaNumberWithExtension = "1800 six-flags ext. 1234"; |
| 2857 EXPECT_TRUE(phone_util_.IsAlphaNumber(kAlphaNumberWithExtension)); |
| 2858 static const string kNonAlphaNumber("1800 123-1234"); |
| 2859 EXPECT_FALSE(phone_util_.IsAlphaNumber(kNonAlphaNumber)); |
| 2860 static const string kNonAlphaNumberWithExtension( |
| 2861 "1800 123-1234 extension: 1234"); |
| 2862 EXPECT_FALSE(phone_util_.IsAlphaNumber(kNonAlphaNumberWithExtension)); |
| 2863 } |
| 2864 |
| 2865 } // namespace phonenumbers |
| 2866 } // namespace i18n |
OLD | NEW |