Skip to content

Commit

Permalink
Fixed serialization problem for TestFailedException thrown from Eithe…
Browse files Browse the repository at this point in the history
…rValues.
  • Loading branch information
cheeseng committed Aug 18, 2022
1 parent 5def737 commit d2c9ecd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 10 additions & 4 deletions jvm/core/src/main/scala/org/scalatest/EitherValues.scala
Expand Up @@ -15,7 +15,7 @@
*/
package org.scalatest

import org.scalactic._
import org.scalactic.{Resources => _, _}
import org.scalatest.exceptions.StackDepthException
import org.scalatest.exceptions.TestFailedException

Expand Down Expand Up @@ -134,7 +134,9 @@ trait EitherValues extends Serializable {
}
catch {
case cause: NoSuchElementException =>
throw new TestFailedException((_: StackDepthException) => Some(Resources.eitherLeftValueNotDefined(leftProj.e)), Some(cause), pos)
val e = leftProj.e
val p = pos
throw new TestFailedException((_: StackDepthException) => Some(Resources.eitherLeftValueNotDefined(e)), Some(cause), p)
}
}
}
Expand Down Expand Up @@ -162,7 +164,9 @@ trait EitherValues extends Serializable {
}
catch {
case cause: NoSuchElementException =>
throw new TestFailedException((_: StackDepthException) => Some(Resources.eitherRightValueNotDefined(rightProj.e)), Some(cause), pos)
val e = rightProj.e
val p = pos
throw new TestFailedException((_: StackDepthException) => Some(Resources.eitherRightValueNotDefined(e)), Some(cause), p)
}
}
}
Expand Down Expand Up @@ -202,7 +206,9 @@ trait EitherValues extends Serializable {
either match {
case Right(r) => r
case _ =>
throw new TestFailedException((_: StackDepthException) => Some(Resources.eitherValueNotDefined(either)), None, pos)
val e = either
val p = pos
throw new TestFailedException((_: StackDepthException) => Some(Resources.eitherValueNotDefined(e)), None, p)
}
}
}
Expand Down
Expand Up @@ -24,7 +24,11 @@ import org.scalatest.OptionValues._
import org.scalatest.SharedHelpers.thisLineNumber
import org.scalatest.exceptions.TestFailedException
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers._
// SKIP-SCALATESTJS,NATIVE-START
import SharedHelpers.serializeRoundtrip
// SKIP-SCALATESTJS,NATIVE-END

class EitherValuesSpec extends AnyFunSpec {
describe("values on Either") {
Expand Down Expand Up @@ -111,5 +115,18 @@ class EitherValuesSpec extends AnyFunSpec {
it("should be able to used with OptionValues") {
class TestSpec extends AnyFunSpec with EitherValues with OptionValues
}

// SKIP-SCALATESTJS,NATIVE-START
it("should throw TestFailedException that is serializable") {
class TestEitherValues extends AnyFunSuite with EitherValues
val spec = new TestEitherValues
val e = Left("error"): Either[String, Int]
val v = spec.convertEitherToValuable(e)
val caught = intercept[TestFailedException] {
v.value
}
serializeRoundtrip(caught)
}
// SKIP-SCALATESTJS,NATIVE-END
}
}

0 comments on commit d2c9ecd

Please sign in to comment.