-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix long numbers problem #98
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
Check notice Code scanning / Codacy-scalameta-pro (reported by Codacy) Values should have minimum scope to avoid misuses.
testFromNumberString should have a smaller scope by using private[this]
|
||
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, | ||
Check notice Code scanning / Codacy-scalameta-pro (reported by Codacy) Calling get should be avoided on Option and Either.
Usage of get on optional type.
|
||
Some(BigDecimal(value.toString)) | ||
) | ||
} | ||
} | ||
|
||
private def testFromDataString[T: Arbitrary](implicit | ||
Check notice Code scanning / Codacy-scalameta-pro (reported by Codacy) Values should have minimum scope to avoid misuses.
testFromDataString should have a smaller scope by using private[this]
|
||
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 => | ||
Check notice Code scanning / Codacy-scalameta-pro (reported by Codacy) Prohibit case statement pattern match from being lowercase.
Lower case pattern matching.
|
||
assertEquals( | ||
obtained = other.toString, | ||
expected = value.toString | ||
) | ||
} | ||
|
||
} | ||
} | ||
} |
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") | ||
) | ||
} | ||
} | ||
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(_)) | ||
) | ||
} |
Check notice
Code scanning / Codacy-scalameta-pro (reported by Codacy)
Prohibits same expression on cases inside the same scope