-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from geirolz/fix_long_numbers_problem
Fix long numbers problem
- Loading branch information
Showing
5 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cats.xml | ||
|
||
import cats.xml.testing.VeryLongNumericString | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Prop.forAll | ||
|
||
import scala.reflect.ClassTag | ||
|
||
class XmlDataSuite extends munit.ScalaCheckSuite { | ||
|
||
// fromNumberString | ||
testFromNumberString[Int]() | ||
testFromNumberString[Short]() | ||
testFromNumberString[Long]() | ||
testFromNumberString[Float]() | ||
testFromNumberString[Double]() | ||
testFromNumberString[BigInt]() | ||
testFromNumberString[BigDecimal]() | ||
testFromNumberString[String](shouldFail = true) | ||
testFromNumberString[VeryLongNumericString](shouldFail = true) | ||
|
||
// fromDataString | ||
testFromDataString[String] | ||
testFromDataString[Char] | ||
testFromDataString[Boolean] | ||
testFromDataString[Int] | ||
testFromDataString[Short] | ||
testFromDataString[Long] | ||
testFromDataString[Float] | ||
testFromDataString[Double] | ||
testFromDataString[BigInt] | ||
testFromDataString[BigDecimal] | ||
testFromDataString[VeryLongNumericString] | ||
|
||
private def testFromNumberString[T: Arbitrary](shouldFail: Boolean = false)(implicit | ||
c: ClassTag[T] | ||
): Unit = | ||
if (shouldFail) | ||
property(s"Xml.fromNumberString return None with ${c.runtimeClass.getSimpleName}") { | ||
forAll { (value: T) => | ||
assertEquals( | ||
obtained = Xml.fromNumberString(value.toString), | ||
expected = None | ||
) | ||
} | ||
} | ||
else | ||
property(s"Xml.fromNumberString works with ${c.runtimeClass.getSimpleName}") { | ||
forAll { (value: T) => | ||
assertEquals( | ||
Xml.fromNumberString(value.toString).get.toBigDecimal, | ||
Some(BigDecimal(value.toString)) | ||
) | ||
} | ||
} | ||
|
||
private def testFromDataString[T: Arbitrary](implicit | ||
c: ClassTag[T] | ||
): Unit = | ||
property(s"Xml.fromDataString works with ${c.runtimeClass.getSimpleName}") { | ||
forAll { (value: T) => | ||
Xml.fromDataString(value.toString) match { | ||
case number: XmlData.XmlNumber => | ||
assertEquals( | ||
number.toBigDecimal, | ||
Some(BigDecimal(value.toString)) | ||
) | ||
case other => | ||
assertEquals( | ||
obtained = other.toString, | ||
expected = value.toString | ||
) | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +0,0 @@ | ||
package cats.xml | ||
|
||
//TODO | ||
class XmlNumberSuite extends munit.ScalaCheckSuite { | ||
|
||
test("") { | ||
|
||
Console.println(Xml.fromDataString("5340595900475325933418219074917").getClass) | ||
assertEquals( | ||
obtained = Xml.fromDataString("5340595900475325933418219074917"), | ||
expected = Xml.ofString("5340595900475325933418219074917") | ||
) | ||
} | ||
} | ||
15 changes: 15 additions & 0 deletions
15
core/src/test/scala/cats/xml/testing/VeryLongNumericString.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cats.xml.testing | ||
|
||
import org.scalacheck.{Arbitrary, Gen} | ||
|
||
case class VeryLongNumericString private (str: String) extends AnyVal | ||
object VeryLongNumericString { | ||
|
||
private[VeryLongNumericString] def apply(str: String): VeryLongNumericString = | ||
new VeryLongNumericString(str) | ||
|
||
implicit val arb: Arbitrary[VeryLongNumericString] = | ||
Arbitrary( | ||
Gen.numStr.suchThat(_.length > 31).map(VeryLongNumericString(_)) | ||
) | ||
} |