update libphonenumber issue links from Google Code to github
[platform/upstream/libphonenumber.git] / cpp / test / phonenumbers / asyoutypeformatter_test.cc
index 7581f45..4b3886c 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "phonenumbers/base/logging.h"
 #include "phonenumbers/base/memory/scoped_ptr.h"
+#include "phonenumbers/default_logger.h"
 #include "phonenumbers/phonenumberutil.h"
 #include "phonenumbers/test_util.h"
 
@@ -35,12 +36,17 @@ class PhoneMetadata;
 class AsYouTypeFormatterTest : public testing::Test {
  protected:
   AsYouTypeFormatterTest() : phone_util_(*PhoneNumberUtil::GetInstance()) {
+    PhoneNumberUtil::GetInstance()->SetLogger(new StdoutLogger());
   }
 
   const PhoneMetadata* GetCurrentMetadata() const {
     return formatter_->current_metadata_;
   }
 
+  const string& GetExtractedNationalPrefix() const {
+    return formatter_->GetExtractedNationalPrefix();
+  }
+
   int ConvertUnicodeStringPosition(const UnicodeString& s, int pos) const {
     return AsYouTypeFormatter::ConvertUnicodeStringPosition(s, pos);
   }
@@ -88,7 +94,7 @@ TEST_F(AsYouTypeFormatterTest, InvalidPlusSign) {
 }
 
 TEST_F(AsYouTypeFormatterTest, TooLongNumberMatchingMultipleLeadingDigits) {
-  // See http://code.google.com/p/libphonenumber/issues/detail?id=36
+  // See https://github.com/googlei18n/libphonenumber/issues/36
   // The bug occurred last time for countries which have two formatting rules
   // with exactly the same leading digits pattern but differ in length.
   formatter_.reset(phone_util_.GetAsYouTypeFormatter(RegionCode::GetUnknown()));
@@ -1137,6 +1143,17 @@ TEST_F(AsYouTypeFormatterTest, AYTF_NoNationalPrefix) {
   EXPECT_EQ("333 333", formatter_->InputDigit('3', &result_));
 }
 
+TEST_F(AsYouTypeFormatterTest, AYTF_NoNationalPrefixFormattingRule) {
+  formatter_.reset(phone_util_.GetAsYouTypeFormatter(RegionCode::AO()));
+
+  EXPECT_EQ("3", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("33", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("333", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("333 3", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("333 33", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("333 333", formatter_->InputDigit('3', &result_));
+}
+
 TEST_F(AsYouTypeFormatterTest, AYTF_ShortNumberFormattingFix_US) {
   // For the US, an initial 1 is treated specially.
   formatter_.reset(phone_util_.GetAsYouTypeFormatter(RegionCode::US()));
@@ -1159,5 +1176,62 @@ TEST_F(AsYouTypeFormatterTest, AYTF_ShortNumberFormattingFix_US) {
   EXPECT_EQ("1 22", formatter_->InputDigit('2', &result_));
 }
 
+TEST_F(AsYouTypeFormatterTest, AYTF_ClearNDDAfterIDDExtraction) {
+  formatter_.reset(phone_util_.GetAsYouTypeFormatter(RegionCode::KR()));
+
+  // Check that when we have successfully extracted an IDD, the previously
+  // extracted NDD is cleared since it is no longer valid.
+  EXPECT_EQ("0", formatter_->InputDigit('0', &result_));
+  EXPECT_EQ("00", formatter_->InputDigit('0', &result_));
+  EXPECT_EQ("007", formatter_->InputDigit('7', &result_));
+  EXPECT_EQ("0070", formatter_->InputDigit('0', &result_));
+  EXPECT_EQ("00700", formatter_->InputDigit('0', &result_));
+  EXPECT_EQ("0", GetExtractedNationalPrefix());
+
+  // Once the IDD "00700" has been extracted, it no longer makes sense for the
+  // initial "0" to be treated as an NDD.
+  EXPECT_EQ("00700 1 ", formatter_->InputDigit('1', &result_));
+  EXPECT_EQ("", GetExtractedNationalPrefix());
+
+  EXPECT_EQ("00700 1 2", formatter_->InputDigit('2', &result_));
+  EXPECT_EQ("00700 1 23", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("00700 1 234", formatter_->InputDigit('4', &result_));
+  EXPECT_EQ("00700 1 234 5", formatter_->InputDigit('5', &result_));
+  EXPECT_EQ("00700 1 234 56", formatter_->InputDigit('6', &result_));
+  EXPECT_EQ("00700 1 234 567", formatter_->InputDigit('7', &result_));
+  EXPECT_EQ("00700 1 234 567 8", formatter_->InputDigit('8', &result_));
+  EXPECT_EQ("00700 1 234 567 89", formatter_->InputDigit('9', &result_));
+  EXPECT_EQ("00700 1 234 567 890", formatter_->InputDigit('0', &result_));
+  EXPECT_EQ("00700 1 234 567 8901", formatter_->InputDigit('1', &result_));
+  EXPECT_EQ("00700123456789012", formatter_->InputDigit('2', &result_));
+  EXPECT_EQ("007001234567890123", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("0070012345678901234", formatter_->InputDigit('4', &result_));
+  EXPECT_EQ("00700123456789012345", formatter_->InputDigit('5', &result_));
+  EXPECT_EQ("007001234567890123456", formatter_->InputDigit('6', &result_));
+  EXPECT_EQ("0070012345678901234567", formatter_->InputDigit('7', &result_));
+}
+
+TEST_F(AsYouTypeFormatterTest,
+       NumberPatternsBecomingInvalidShouldNotResultInDigitLoss) {
+  formatter_.reset(phone_util_.GetAsYouTypeFormatter(RegionCode::CN()));
+
+  EXPECT_EQ("+", formatter_->InputDigit('+', &result_));
+  EXPECT_EQ("+8", formatter_->InputDigit('8', &result_));
+  EXPECT_EQ("+86 ", formatter_->InputDigit('6', &result_));
+  EXPECT_EQ("+86 9", formatter_->InputDigit('9', &result_));
+  EXPECT_EQ("+86 98", formatter_->InputDigit('8', &result_));
+  EXPECT_EQ("+86 988", formatter_->InputDigit('8', &result_));
+  EXPECT_EQ("+86 988 1", formatter_->InputDigit('1', &result_));
+  // Now the number pattern is no longer valid because there are multiple
+  // leading digit patterns; when we try again to extract a country code we
+  // should ensure we use the last leading digit pattern, rather than the first
+  // one such that it *thinks* it's found a valid formatting rule again.
+  // https://github.com/googlei18n/libphonenumber/issues/437
+  EXPECT_EQ("+8698812", formatter_->InputDigit('2', &result_));
+  EXPECT_EQ("+86988123", formatter_->InputDigit('3', &result_));
+  EXPECT_EQ("+869881234", formatter_->InputDigit('4', &result_));
+  EXPECT_EQ("+8698812345", formatter_->InputDigit('5', &result_));
+}
+
 }  // namespace phonenumbers
 }  // namespace i18n