Skip to content

Commit b6350f2

Browse files
cheesengbvenners
authored andcommitted
Fixed failing tests in PosIntSpec and PosZIntSpec when built under Scala 2.10.
1 parent 3e0082f commit b6350f2

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

scalactic-test/src/test/scala/org/scalactic/anyvals/PosIntSpec.scala

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,21 @@ class PosIntSpec extends FunSpec with Matchers with GeneratorDrivenPropertyCheck
333333
}
334334

335335
it("should offer 'to' and 'until' methods that are consistent with Int") {
336-
forAll { (pint: PosInt, end: Int, generatedStep: PosInt) =>
337-
// We need to make sure the range produced's size is less than or equal to Int.MaxValue,
338-
// so the following code avoid produce range the goes overflow of int values.
339-
val step: Int = if (end < 0) -generatedStep.value else generatedStep.value
340-
341-
Try(pint.to(end)) shouldEqual Try(pint.toInt.to(end))
342-
Try(pint.to(end, step)) shouldEqual Try(pint.toInt.to(end, step))
343-
Try(pint.until(end)) shouldEqual Try(pint.toInt.until(end))
344-
Try(pint.until(end, step)) shouldEqual Try(pint.toInt.until(end, step))
336+
forAll { (pint: PosInt, end: Int, step: Int) =>
337+
// The reason we need this is that in Scala 2.10, the equals check (used by shouldEqual below) will call range.length
338+
// and it'll cause IllegalArgumentException to be thrown when we do the Try(x) shouldEqual Try(y) assertion below,
339+
// while starting from scala 2.11 the equals call implementation does not call .length.
340+
// To make the behavior consistent for all scala versions, we explicitly call .length for all returned Range, and
341+
// shall it throws IllegalArgumentException, it will be wrapped as Failure for the Try.
342+
def ensuringValid(range: Range): Range = {
343+
range.length // IllegalArgumentException will be thrown if it is an invalid range, this will turn the Success to Failure for Try
344+
range
345+
}
346+
347+
Try(ensuringValid(pint.to(end))) shouldEqual Try(ensuringValid(pint.toInt.to(end)))
348+
Try(ensuringValid(pint.to(end, step))) shouldEqual Try(ensuringValid(pint.toInt.to(end, step)))
349+
Try(ensuringValid(pint.until(end))) shouldEqual Try(ensuringValid(pint.toInt.until(end)))
350+
Try(ensuringValid(pint.until(end, step))) shouldEqual Try(ensuringValid(pint.toInt.until(end, step)))
345351
}
346352
}
347353

scalactic-test/src/test/scala/org/scalactic/anyvals/PosZIntSpec.scala

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ trait PosZIntSpecSupport {
3636

3737
implicit val arbPosZInt: Arbitrary[PosZInt] = Arbitrary(posZIntGen)
3838

39-
val posIntGen: Gen[PosInt] =
40-
for {i <- choose(0, Int.MaxValue)} yield PosInt.from(i).get
41-
42-
implicit val arbPosInt: Arbitrary[PosInt] = Arbitrary(posIntGen)
43-
4439
implicit def tryEquality[T]: Equality[Try[T]] = new Equality[Try[T]] {
4540
override def areEqual(a: Try[T], b: Any): Boolean = a match {
4641
case Success(double: Double) if double.isNaN => // This is because in scala.js x/0 results to NaN not ArithmetricException like in jvm, and we need to make sure Success(NaN) == Success(NaN) is true to pass the test.
@@ -340,15 +335,21 @@ class PosZIntSpec extends FunSpec with Matchers with GeneratorDrivenPropertyChec
340335
}
341336

342337
it("should offer 'to' and 'until' methods that are consistent with Int") {
343-
forAll { (pzint: PosZInt, end: Int, generatedStep: PosInt) =>
344-
// We need to make sure the range produced's size is less than or equal to Int.MaxValue,
345-
// so the following code avoid produce range the goes overflow of int values.
346-
val step: Int = if (end < 0) -generatedStep.value else generatedStep.value
347-
348-
Try(pzint.to(end)) shouldEqual Try(pzint.toInt.to(end))
349-
Try(pzint.to(end, step)) shouldEqual Try(pzint.toInt.to(end, step))
350-
Try(pzint.until(end)) shouldEqual Try(pzint.toInt.until(end))
351-
Try(pzint.until(end, step)) shouldEqual Try(pzint.toInt.until(end, step))
338+
forAll { (pzint: PosZInt, end: Int, step: Int) =>
339+
// The reason we need this is that in Scala 2.10, the equals check (used by shouldEqual below) will call range.length
340+
// and it'll cause IllegalArgumentException to be thrown when we do the Try(x) shouldEqual Try(y) assertion below,
341+
// while starting from scala 2.11 the equals call implementation does not call .length.
342+
// To make the behavior consistent for all scala versions, we explicitly call .length for all returned Range, and
343+
// shall it throws IllegalArgumentException, it will be wrapped as Failure for the Try.
344+
def ensuringValid(range: Range): Range = {
345+
range.length // IllegalArgumentException will be thrown if it is an invalid range, this will turn the Success to Failure for Try
346+
range
347+
}
348+
349+
Try(ensuringValid(pzint.to(end))) shouldEqual Try(ensuringValid(pzint.toInt.to(end)))
350+
Try(ensuringValid(pzint.to(end, step))) shouldEqual Try(ensuringValid(pzint.toInt.to(end, step)))
351+
Try(ensuringValid(pzint.until(end))) shouldEqual Try(ensuringValid(pzint.toInt.until(end)))
352+
Try(ensuringValid(pzint.until(end, step))) shouldEqual Try(ensuringValid(pzint.toInt.until(end, step)))
352353
}
353354
}
354355

0 commit comments

Comments
 (0)