Skip to content

Instantly share code, notes, and snippets.

@arturoherrero
Created February 1, 2012 23:55
Show Gist options
  • Save arturoherrero/1720240 to your computer and use it in GitHub Desktop.
Save arturoherrero/1720240 to your computer and use it in GitHub Desktop.
Scala micro test lib
//@kikito's micro test lib, simply awesome! http://stackoverflow.com/q/7866079/462015
class AssertException(msg: String) extends Exception(msg: String)
def assertThrows[E](f: => Unit)(implicit eType:ClassManifest[E]) {
try {
f
} catch {
case e: Exception =>
if (eType.erasure.isAssignableFrom(e.getClass)) { return }
}
throw new AssertException("Expected error of type " + eType.erasure.getName )
}
def assert(condition: Boolean, message: String) {
if(!condition) { throw new AssertException(message) }
}
def assertNot(condition: Boolean, message: String) {
assert(!condition, message)
}
// assertThrows tests
assertThrows[NullPointerException] { throw new NullPointerException() }
assertThrows[Exception] { throw new NullPointerException() }
assertThrows[AssertException] { assertThrows[Exception]{ } }
// assert tests
assertThrows[AssertException] { assert(1 > 2, "Maths don't work") }
assert(1 <3 -1, "1 loves minus 1")
// asertNot tests
assertThrows[AssertException] { assertNot(1 < 2, "Maths still don't work") }
assertNot(2 <3 -1, "2 does (not) love -1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment