diff options
| author | Schuyler Eldridge | 2020-04-08 12:53:31 -0400 |
|---|---|---|
| committer | Schuyler Eldridge | 2020-06-22 19:33:01 -0400 |
| commit | cc4fa583690292d690804144fe92427f0c9f5fdf (patch) | |
| tree | f91165d502dabecff6d97b3aadaff002e6ad8752 | |
| parent | d099d01ae6b11d8befdf7b32ab74c3167a552984 (diff) | |
Add containsCause exception search testing util
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>
| -rw-r--r-- | src/test/scala/chiselTests/ChiselSpec.scala | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/ChiselSpec.scala b/src/test/scala/chiselTests/ChiselSpec.scala index 801dbac2..fa05ab86 100644 --- a/src/test/scala/chiselTests/ChiselSpec.scala +++ b/src/test/scala/chiselTests/ChiselSpec.scala @@ -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 { @@ -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 + } + } + + } + } |
