From 73bfbe41f91df33a78e25294b23a9624737e3c2f Mon Sep 17 00:00:00 2001 From: kusamakura <882152+kusamakura@users.noreply.github.com> Date: Wed, 20 Dec 2017 15:34:54 +0900 Subject: [PATCH 1/4] Add string to valid number predicates --- .../scala/eu/timepit/refined/string.scala | 30 +++++++++++++++++++ .../timepit/refined/StringValidateSpec.scala | 21 ++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala b/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala index 77b56851d..0a03b8422 100644 --- a/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala +++ b/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala @@ -56,6 +56,21 @@ object string extends StringValidate with StringInference { /** Predicate that checks if a `String` is a valid XPath expression. */ final case class XPath() + + /** Predicate that checks if a `String` can be a valid `Int`. */ + final case class ValidInt() + + /** Predicate that checks if a `String` can be a valid `Long`. */ + final case class ValidLong() + + /** Predicate that checks if a `String` can be a valid `Double`. */ + final case class ValidDouble() + + /** Predicate that checks if a `String` can be a valid `BigInt`. */ + final case class ValidBigInt() + + /** Predicate that checks if a `String` can be a valid `BigDecimal`. */ + final case class ValidBigDecimal() } private[refined] trait StringValidate { @@ -100,6 +115,21 @@ private[refined] trait StringValidate { implicit def xpathValidate: Validate.Plain[String, XPath] = Validate .fromPartial(javax.xml.xpath.XPathFactory.newInstance().newXPath().compile, "XPath", XPath()) + + implicit def validIntValidate: Validate.Plain[String, ValidInt] = + Validate.fromPartial(_.toInt, "Int", ValidInt()) + + implicit def validLongValidate: Validate.Plain[String, ValidLong] = + Validate.fromPartial(_.toLong, "Long", ValidLong()) + + implicit def validDoubleValidate: Validate.Plain[String, ValidDouble] = + Validate.fromPartial(_.toDouble, "Double", ValidDouble()) + + implicit def validBigIntValidate: Validate.Plain[String, ValidBigInt] = + Validate.fromPartial(BigInt(_), "BigInt", ValidBigInt()) + + implicit def validBigDecimalValidate: Validate.Plain[String, ValidBigDecimal] = + Validate.fromPartial(BigDecimal(_), "BigDecimal", ValidBigDecimal()) } private[refined] trait StringInference { diff --git a/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala b/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala index 9180efba7..18bf35885 100644 --- a/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala +++ b/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala @@ -1,9 +1,10 @@ package eu.timepit.refined import eu.timepit.refined.TestUtils._ +import eu.timepit.refined.api.Validate import eu.timepit.refined.string._ +import org.scalacheck.{Arbitrary, Properties} import org.scalacheck.Prop._ -import org.scalacheck.Properties import shapeless.Witness class StringValidateSpec extends Properties("StringValidate") { @@ -81,4 +82,22 @@ class StringValidateSpec extends Properties("StringValidate") { property("IPv4.showResult.Failed") = secure { showResult[IPv4]("::1") ?= "Predicate failed: ::1 is a valid IPv4." } + + private def validNumber[N: Arbitrary, P](name: String, invalidValue: String)(implicit v: Validate[String, P]) = { + property(s"Valid$name") = secure { + forAll { (n: N) => + isValid[P](n.toString) && + (showResult[P](n.toString) ?= s"$name predicate passed.") + } + } + property(s"Valid$name.showResult.Failed") = secure { + showResult[P](invalidValue) ?= s"Predicate failed: $name" + } + } + + validNumber[Int, ValidInt]("Int", Long.MaxValue.toString) + validNumber[Long, ValidLong]("Long", "1.0") + validNumber[Double, ValidDouble]("Double", "a") + validNumber[BigInt, ValidBigInt]("BigInt", "1.0") + validNumber[BigDecimal, ValidBigDecimal]("BigDecimal", "a") } From 7d318117bfeee4bd6c378df907639f0949f43c98 Mon Sep 17 00:00:00 2001 From: kusamakura <882152+kusamakura@users.noreply.github.com> Date: Wed, 20 Dec 2017 15:48:45 +0900 Subject: [PATCH 2/4] Fix test errors --- .../main/scala/eu/timepit/refined/string.scala | 10 +++++----- .../eu/timepit/refined/StringValidateSpec.scala | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala b/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala index 0a03b8422..23e505cb1 100644 --- a/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala +++ b/modules/core/shared/src/main/scala/eu/timepit/refined/string.scala @@ -117,19 +117,19 @@ private[refined] trait StringValidate { .fromPartial(javax.xml.xpath.XPathFactory.newInstance().newXPath().compile, "XPath", XPath()) implicit def validIntValidate: Validate.Plain[String, ValidInt] = - Validate.fromPartial(_.toInt, "Int", ValidInt()) + Validate.fromPartial(_.toInt, "ValidInt", ValidInt()) implicit def validLongValidate: Validate.Plain[String, ValidLong] = - Validate.fromPartial(_.toLong, "Long", ValidLong()) + Validate.fromPartial(_.toLong, "ValidLong", ValidLong()) implicit def validDoubleValidate: Validate.Plain[String, ValidDouble] = - Validate.fromPartial(_.toDouble, "Double", ValidDouble()) + Validate.fromPartial(_.toDouble, "ValidDouble", ValidDouble()) implicit def validBigIntValidate: Validate.Plain[String, ValidBigInt] = - Validate.fromPartial(BigInt(_), "BigInt", ValidBigInt()) + Validate.fromPartial(BigInt(_), "ValidBigInt", ValidBigInt()) implicit def validBigDecimalValidate: Validate.Plain[String, ValidBigDecimal] = - Validate.fromPartial(BigDecimal(_), "BigDecimal", ValidBigDecimal()) + Validate.fromPartial(BigDecimal(_), "ValidBigDecimal", ValidBigDecimal()) } private[refined] trait StringInference { diff --git a/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala b/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala index 18bf35885..a2e0b199c 100644 --- a/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala +++ b/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala @@ -84,20 +84,20 @@ class StringValidateSpec extends Properties("StringValidate") { } private def validNumber[N: Arbitrary, P](name: String, invalidValue: String)(implicit v: Validate[String, P]) = { - property(s"Valid$name") = secure { + property(name) = secure { forAll { (n: N) => isValid[P](n.toString) && (showResult[P](n.toString) ?= s"$name predicate passed.") } } - property(s"Valid$name.showResult.Failed") = secure { - showResult[P](invalidValue) ?= s"Predicate failed: $name" + property(s"$name.showResult.Failed") = secure { + showResult[P](invalidValue).startsWith(s"$name predicate failed") } } - validNumber[Int, ValidInt]("Int", Long.MaxValue.toString) - validNumber[Long, ValidLong]("Long", "1.0") - validNumber[Double, ValidDouble]("Double", "a") - validNumber[BigInt, ValidBigInt]("BigInt", "1.0") - validNumber[BigDecimal, ValidBigDecimal]("BigDecimal", "a") + validNumber[Int, ValidInt]("ValidInt", Long.MaxValue.toString) + validNumber[Long, ValidLong]("ValidLong", "1.0") + validNumber[Double, ValidDouble]("ValidDouble", "a") + validNumber[BigInt, ValidBigInt]("ValidBigInt", "1.0") + validNumber[BigDecimal, ValidBigDecimal]("ValidBigDecimal", "a") } From 49772950341397ead80f7d227ebf31617e6700e9 Mon Sep 17 00:00:00 2001 From: kusamakura <882152+kusamakura@users.noreply.github.com> Date: Thu, 21 Dec 2017 16:46:28 +0900 Subject: [PATCH 3/4] Fix scalafmt --- .../test/scala/eu/timepit/refined/StringValidateSpec.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala b/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala index a2e0b199c..a0516d5be 100644 --- a/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala +++ b/modules/core/shared/src/test/scala/eu/timepit/refined/StringValidateSpec.scala @@ -83,11 +83,12 @@ class StringValidateSpec extends Properties("StringValidate") { showResult[IPv4]("::1") ?= "Predicate failed: ::1 is a valid IPv4." } - private def validNumber[N: Arbitrary, P](name: String, invalidValue: String)(implicit v: Validate[String, P]) = { + private def validNumber[N: Arbitrary, P](name: String, invalidValue: String)( + implicit v: Validate[String, P]) = { property(name) = secure { forAll { (n: N) => isValid[P](n.toString) && - (showResult[P](n.toString) ?= s"$name predicate passed.") + (showResult[P](n.toString) ?= s"$name predicate passed.") } } property(s"$name.showResult.Failed") = secure { From 40c0cc90ccbc46fb0c6220e12b4cdefc452336c5 Mon Sep 17 00:00:00 2001 From: kusamakura <882152+kusamakura@users.noreply.github.com> Date: Thu, 21 Dec 2017 17:31:39 +0900 Subject: [PATCH 4/4] Add problem filter for StringValidate --- build.sbt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/build.sbt b/build.sbt index 07f08e9fe..28dce29c3 100644 --- a/build.sbt +++ b/build.sbt @@ -268,8 +268,7 @@ lazy val moduleJvmSettings = Def.settings( "eu.timepit.refined.NumericValidate.moduloValidateNat"), ProblemFilters.exclude[MissingClassProblem]("eu.timepit.refined.scalacheck.util.OurMath"), ProblemFilters.exclude[MissingClassProblem]("eu.timepit.refined.scalacheck.util.OurMath$"), - ProblemFilters.exclude[ReversedMissingMethodProblem]( - "eu.timepit.refined.StringValidate.ipv4Validate"), + ProblemFilters.exclude[ReversedMissingMethodProblem]("eu.timepit.refined.StringValidate.*"), ProblemFilters.exclude[ReversedMissingMethodProblem]("eu.timepit.refined.types.*") ) }