From 1b979318d85ea6035084253596cf076151cef309 Mon Sep 17 00:00:00 2001 From: Sergey Nastich Date: Wed, 19 Sep 2018 13:57:53 -0400 Subject: Improve PhoneNumber (#222) * Add support for extensions * Add PathMatcher and allow parsing JSON from string * Add a number of convenience methods which are to be used instead of `toString` --- src/test/scala/xyz/driver/core/JsonTest.scala | 15 +++++++++ .../scala/xyz/driver/core/PhoneNumberTest.scala | 38 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) (limited to 'src/test') diff --git a/src/test/scala/xyz/driver/core/JsonTest.scala b/src/test/scala/xyz/driver/core/JsonTest.scala index 2aa3572..fd693f9 100644 --- a/src/test/scala/xyz/driver/core/JsonTest.scala +++ b/src/test/scala/xyz/driver/core/JsonTest.scala @@ -247,6 +247,21 @@ class JsonTest extends WordSpec with Matchers with Inspectors { json.phoneNumberFormat.read(phoneJson) }.getMessage shouldBe "Invalid phone number" } + + "parse phone number from string" in { + JsString("+14243039608").convertTo[PhoneNumber] shouldBe PhoneNumber("1", "4243039608") + } + } + + "Path matcher for PhoneNumber" should { + "read valid phone number" in { + val string = "+14243039608x23" + val phone = PhoneNumber("1", "4243039608", Some("23")) + + val matcher = PathMatcher("foo") / PhoneInPath + + matcher(Uri.Path("foo") / string / "bar") shouldBe Matched(Uri.Path./("bar"), Tuple1(phone)) + } } "Json format for ADT mappings" should { diff --git a/src/test/scala/xyz/driver/core/PhoneNumberTest.scala b/src/test/scala/xyz/driver/core/PhoneNumberTest.scala index 384c7be..729302b 100644 --- a/src/test/scala/xyz/driver/core/PhoneNumberTest.scala +++ b/src/test/scala/xyz/driver/core/PhoneNumberTest.scala @@ -44,6 +44,21 @@ class PhoneNumberTest extends FlatSpec with Matchers { PhoneNumber.parse("+86 134 52 52 2256") shouldBe Some(PhoneNumber("86", "13452522256")) } + it should "parse numbers with extensions in different formats" in { + // format: off + val numbers = List( + "+1 800 525 22 25 x23", + "+18005252225 ext. 23", + "+18005252225,23" + ) + // format: on + + val parsed = numbers.flatMap(PhoneNumber.parse) + + parsed should have size numbers.size + parsed should contain only PhoneNumber("1", "8005252225", Some("23")) + } + it should "return None on numbers that are shorter than the minimum number of digits for the country (i.e. US - 10, AR - 11)" in { withClue("US and CN numbers are 10 digits - 9 digit (and shorter) numbers should not fit") { // format: off @@ -76,4 +91,27 @@ class PhoneNumberTest extends FlatSpec with Matchers { List(PhoneNumber("45", "27452522"), PhoneNumber("86", "13452522256")) } + "PhoneNumber.toCompactString/toE164String" should "produce phone number in international format without whitespaces" in { + PhoneNumber.parse("+1 800 5252225").get.toCompactString shouldBe "+18005252225" + PhoneNumber.parse("+1 800 5252225").get.toE164String shouldBe "+18005252225" + + PhoneNumber.parse("+1 800 5252225 x23").get.toCompactString shouldBe "+18005252225;ext=23" + PhoneNumber.parse("+1 800 5252225 x23").get.toE164String shouldBe "+18005252225;ext=23" + } + + "PhoneNumber.toHumanReadableString" should "produce nice readable result for different countries" in { + PhoneNumber.parse("+14154234567").get.toHumanReadableString shouldBe "+1 415-423-4567" + PhoneNumber.parse("+14154234567,23").get.toHumanReadableString shouldBe "+1 415-423-4567 ext. 23" + + PhoneNumber.parse("+78005252225").get.toHumanReadableString shouldBe "+7 800 525-22-25" + + PhoneNumber.parse("+41219437898").get.toHumanReadableString shouldBe "+41 21 943 78 98" + } + + it should "throw an IllegalArgumentException if the PhoneNumber object is not parsable/valid" in { + intercept[IllegalStateException] { + PhoneNumber("+123", "1238123120938120938").toHumanReadableString + }.getMessage should include("+123 1238123120938120938 is not a valid number") + } + } -- cgit v1.2.3