Skip to content

Commit

Permalink
Add containsCause exception search testing util
Browse files Browse the repository at this point in the history
Adds a new method, chiselTests.Util.containsCause, that will search
for a polymorphic exception anywhere in a stack trace. This is useful
if exceptions may move around (e.g., if they are suddenly wrapped in a
StageError).

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
  • Loading branch information
seldridge committed Jun 22, 2020
1 parent d099d01 commit cc4fa58
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/test/scala/chiselTests/ChiselSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import firrtl.util.BackendCompilationUtilities
import java.io.ByteArrayOutputStream
import java.security.Permission
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import scala.reflect.ClassTag

/** Common utility functions for Chisel unit tests. */
trait ChiselRunners extends Assertions with BackendCompilationUtilities {
Expand Down Expand Up @@ -284,4 +285,33 @@ trait Utils {
}
}

/** Run some code extracting an exception cause that matches a type parameter
* @param thunk some code to run
* @tparam A the type of the exception to expect
* @return nothing
* @throws the exception of type parameter A if it was found
*/
def extractCause[A <: Throwable : ClassTag](thunk: => Any): Unit = {
def unrollCauses(a: Throwable): Seq[Throwable] = a match {
case null => Seq.empty
case _ => a +: unrollCauses(a.getCause)
}

val exceptions: Seq[_ <: Throwable] = try {
thunk
Seq.empty
} catch {
case a: Throwable => unrollCauses(a)
}

exceptions.collectFirst{ case a: A => a } match {
case Some(a) => throw a
case None => exceptions match {
case Nil => Unit
case h :: t => throw h
}
}

}

}

0 comments on commit cc4fa58

Please sign in to comment.