summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/test/scala/chiselTests/ChiselSpec.scala30
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
+ }
+ }
+
+ }
+
}