Update asyoutypeformatter_test.js
[platform/upstream/libphonenumber.git] / javascript / i18n / phonenumbers / asyoutypeformatter_test.js
index 49363d8..f892c51 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * @license
- * Copyright (C) 2010 The Libphonenumber Authors
+ * Copyright (C) 2010 The Libphonenumber Authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ function testInvalidPlusSign() {
 }
 
 function testTooLongNumberMatchingMultipleLeadingDigits() {
-  // 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.
   /** @type {i18n.phonenumbers.AsYouTypeFormatter} */
@@ -1116,6 +1116,17 @@ function testAYTFNoNationalPrefix() {
   assertEquals('333 333', f.inputDigit('3'));
 }
 
+function testAYTFNoNationalPrefixFormattingRule() {
+  /** @type {i18n.phonenumbers.AsYouTypeFormatter} */
+  var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.AO);
+  assertEquals('3', f.inputDigit('3'));
+  assertEquals('33', f.inputDigit('3'));
+  assertEquals('333', f.inputDigit('3'));
+  assertEquals('333 3', f.inputDigit('3'));
+  assertEquals('333 33', f.inputDigit('3'));
+  assertEquals('333 333', f.inputDigit('3'));
+}
+
 function testAYTFShortNumberFormattingFix_US() {
   // For the US, an initial 1 is treated specially.
   /** @type {i18n.phonenumbers.AsYouTypeFormatter} */
@@ -1138,3 +1149,57 @@ function testAYTFShortNumberFormattingFix_US() {
   assertEquals('12', f.inputDigit('2'));
   assertEquals('1 22', f.inputDigit('2'));
 }
+
+function testAYTFClearNDDAfterIddExtraction() {
+  /** @type {i18n.phonenumbers.AsYouTypeFormatter} */
+  var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.KR);
+
+  assertEquals('0', f.inputDigit('0'));
+  assertEquals('00', f.inputDigit('0'));
+  assertEquals('007', f.inputDigit('7'));
+  assertEquals('0070', f.inputDigit('0'));
+  assertEquals('00700', f.inputDigit('0'));
+  // NDD should be '0' at this stage.
+  assertEquals('00700 1 ', f.inputDigit('1'));
+  // NDD should be cleared here because IDD '1' was extracted.
+  assertEquals('00700 1 2', f.inputDigit('2'));
+  // The remaining long sequence of inputs is because the original bug that
+  // this test if for only triggered after a lot of subsequent inputs.
+  assertEquals('00700 1 23', f.inputDigit('3'));
+  assertEquals('00700 1 234', f.inputDigit('4'));
+  assertEquals('00700 1 234 5', f.inputDigit('5'));
+  assertEquals('00700 1 234 56', f.inputDigit('6'));
+  assertEquals('00700 1 234 567', f.inputDigit('7'));
+  assertEquals('00700 1 234 567 8', f.inputDigit('8'));
+  assertEquals('00700 1 234 567 89', f.inputDigit('9'));
+  assertEquals('00700 1 234 567 890', f.inputDigit('0'));
+  assertEquals('00700 1 234 567 8901', f.inputDigit('1'));
+  assertEquals('00700123456789012', f.inputDigit('2'));
+  assertEquals('007001234567890123', f.inputDigit('3'));
+  assertEquals('0070012345678901234', f.inputDigit('4'));
+  assertEquals('00700123456789012345', f.inputDigit('5'));
+  assertEquals('007001234567890123456', f.inputDigit('6'));
+  assertEquals('0070012345678901234567', f.inputDigit('7'));
+}
+
+function testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss() {
+  /** @type {i18n.phonenumbers.AsYouTypeFormatter} */
+  var f = new i18n.phonenumbers.AsYouTypeFormatter(RegionCode.CN);
+
+  assertEquals('+', f.inputDigit('+'));
+  assertEquals('+8', f.inputDigit('8'));
+  assertEquals('+86 ', f.inputDigit('6'));
+  assertEquals('+86 9', f.inputDigit('9'));
+  assertEquals('+86 98', f.inputDigit('8'));
+  assertEquals('+86 988', f.inputDigit('8'));
+  assertEquals('+86 988 1', f.inputDigit('1'));
+  // 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
+  assertEquals('+8698812', f.inputDigit('2'));
+  assertEquals('+86988123', f.inputDigit('3'));
+  assertEquals('+869881234', f.inputDigit('4'));
+  assertEquals('+8698812345', f.inputDigit('5'));
+}