Skip to content

Commit 3e0082f

Browse files
committed
Merge branch 'cheeseng-neg-non-zero-scala-210-test-fix' into 3.1.x
2 parents 2b90b8f + 64c7dbb commit 3e0082f

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ trait NegIntSpecSupport {
4848
case Success(bFloat: Float) if bFloat.isNaN => true
4949
case _ => false
5050
}
51+
5152
case _: Success[_] => a == b
5253
case Failure(ex) => b match {
5354
case _: Success[_] => false
@@ -334,10 +335,20 @@ class NegIntSpec extends FunSpec with Matchers with GeneratorDrivenPropertyCheck
334335

335336
it("should offer 'to' and 'until' methods that are consistent with Int") {
336337
forAll { (pint: NegInt, end: Int, step: Int) =>
337-
Try(pint.to(end)) shouldEqual Try(pint.toInt.to(end))
338-
Try(pint.to(end, step)) shouldEqual Try(pint.toInt.to(end, step))
339-
Try(pint.until(end)) shouldEqual Try(pint.toInt.until(end))
340-
Try(pint.until(end, step)) shouldEqual Try(pint.toInt.until(end, step))
338+
// The reason we need this is that in Scala 2.10, the equals check (used by shouldEqual below) will call range.length
339+
// and it'll cause IllegalArgumentException to be thrown when we do the Try(x) shouldEqual Try(y) assertion below,
340+
// while starting from scala 2.11 the equals call implementation does not call .length.
341+
// To make the behavior consistent for all scala versions, we explicitly call .length for all returned Range, and
342+
// shall it throws IllegalArgumentException, it will be wrapped as Failure for the Try.
343+
def ensuringValid(range: Range): Range = {
344+
range.length // IllegalArgumentException will be thrown if it is an invalid range, this will turn the Success to Failure for Try
345+
range
346+
}
347+
348+
Try(ensuringValid(pint.to(end))) shouldEqual Try(ensuringValid(pint.toInt.to(end)))
349+
Try(ensuringValid(pint.to(end, step))) shouldEqual Try(ensuringValid(pint.toInt.to(end, step)))
350+
Try(ensuringValid(pint.until(end))) shouldEqual Try(ensuringValid(pint.toInt.until(end)))
351+
Try(ensuringValid(pint.until(end, step))) shouldEqual Try(ensuringValid(pint.toInt.until(end, step)))
341352
}
342353
}
343354

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,21 @@ class NegZIntSpec extends FunSpec with Matchers with GeneratorDrivenPropertyChec
333333
}
334334

335335
it("should offer 'to' and 'until' methods that are consistent with Int") {
336+
// The reason we need this is that in Scala 2.10, the equals check (used by shouldEqual below) will call range.length
337+
// and it'll cause IllegalArgumentException to be thrown when we do the Try(x) shouldEqual Try(y) assertion below,
338+
// while starting from scala 2.11 the equals call implementation does not call .length.
339+
// To make the behavior consistent for all scala versions, we explicitly call .length for all returned Range, and
340+
// shall it throws IllegalArgumentException, it will be wrapped as Failure for the Try.
341+
def ensuringValid(range: Range): Range = {
342+
range.length // IllegalArgumentException will be thrown if it is an invalid range, this will turn the Success to Failure for Try
343+
range
344+
}
345+
336346
forAll { (pzint: NegZInt, end: Int, step: Int) =>
337-
Try(pzint.to(end)) shouldEqual Try(pzint.toInt.to(end))
338-
Try(pzint.to(end, step)) shouldEqual Try(pzint.toInt.to(end, step))
339-
Try(pzint.until(end)) shouldEqual Try(pzint.toInt.until(end))
340-
Try(pzint.until(end, step)) shouldEqual Try(pzint.toInt.until(end, step))
347+
Try(ensuringValid(pzint.to(end)))shouldEqual Try(ensuringValid(pzint.toInt.to(end)))
348+
Try(ensuringValid(pzint.to(end, step))) shouldEqual Try(ensuringValid(pzint.toInt.to(end, step)))
349+
Try(ensuringValid(pzint.until(end))) shouldEqual Try(ensuringValid(pzint.toInt.until(end)))
350+
Try(ensuringValid(pzint.until(end, step))) shouldEqual Try(ensuringValid(pzint.toInt.until(end, step)))
341351
}
342352
}
343353

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,20 @@ class NonZeroIntSpec extends FunSpec with Matchers with GeneratorDrivenPropertyC
361361

362362
it("should offer 'to' and 'until' methods that are consistent with Int") {
363363
forAll { (nzint: NonZeroInt, end: Int, step: Int) =>
364-
Try(nzint.to(end)) shouldEqual Try(nzint.toInt.to(end))
365-
Try(nzint.to(end, step)) shouldEqual Try(nzint.toInt.to(end, step))
366-
Try(nzint.until(end)) shouldEqual Try(nzint.toInt.until(end))
367-
Try(nzint.until(end, step)) shouldEqual Try(nzint.toInt.until(end, step))
364+
// The reason we need this is that in Scala 2.10, the equals check (used by shouldEqual below) will call range.length
365+
// and it'll cause IllegalArgumentException to be thrown when we do the Try(x) shouldEqual Try(y) assertion below,
366+
// while starting from scala 2.11 the equals call implementation does not call .length.
367+
// To make the behavior consistent for all scala versions, we explicitly call .length for all returned Range, and
368+
// shall it throws IllegalArgumentException, it will be wrapped as Failure for the Try.
369+
def ensuringValid(range: Range): Range = {
370+
range.length // IllegalArgumentException will be thrown if it is an invalid range, this will turn the Success to Failure for Try
371+
range
372+
}
373+
374+
Try(ensuringValid(nzint.to(end))) shouldEqual Try(ensuringValid(nzint.toInt.to(end)))
375+
Try(ensuringValid(nzint.to(end, step))) shouldEqual Try(ensuringValid(nzint.toInt.to(end, step)))
376+
Try(ensuringValid(nzint.until(end))) shouldEqual Try(ensuringValid(nzint.toInt.until(end)))
377+
Try(ensuringValid(nzint.until(end, step))) shouldEqual Try(ensuringValid(nzint.toInt.until(end, step)))
368378
}
369379
}
370380

0 commit comments

Comments
 (0)