summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/testing/AssertUtil.scala
blob: 9b4833d46b2a2217519dad30ff86cdd918024499 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package scala.tools
package testing

/** This module contains additional higher-level assert statements
 *  that are ultimately based on junit.Assert primitives.
 */
object AssertUtil {
  /**
   * Check if throwable T (or a subclass) was thrown during evaluation of f, and that its message
   * satisfies the `checkMessage` predicate.
   * If any other exception will be re-thrown.
   */
  def assertThrows[T <: Throwable](f: => Any,
                                   checkMessage: String => Boolean = s => true)
                                  (implicit manifest: Manifest[T]): Unit = {
    try f
    catch {
      case e: Throwable if checkMessage(e.getMessage) =>
        val clazz = manifest.runtimeClass
        if (!clazz.isAssignableFrom(e.getClass))
          throw e
        else return
    }
    throw new AssertionError("Expression did not throw!")
  }
}