Skip to content

Commit

Permalink
Merge pull request #47 from jupco/feature/InterceptReturnsThrowable
Browse files Browse the repository at this point in the history
Changes the signature of `intercept` to return the Throwable.
  • Loading branch information
alexandru authored Feb 21, 2020
2 parents 213aab9 + f79c03e commit aef7a63
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions shared/src/main/scala/minitest/api/Asserts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ trait Asserts {
macro Asserts.DoesNotCompileMacros.applyImpl

def intercept[E <: Throwable : ClassTag](callback: => Unit)
(implicit pos: SourceLocation): Unit = {
(implicit pos: SourceLocation): Throwable = {

val E = implicitly[ClassTag[E]]
try {
Expand All @@ -89,7 +89,7 @@ trait Asserts {
case ex: InterceptException =>
throw new AssertionException(ex.getMessage, pos)
case ex: Throwable if E.runtimeClass.isInstance(ex) =>
() // Do nothing!
ex
}
}

Expand Down
10 changes: 7 additions & 3 deletions shared/src/test/scala/minitest/tests/SimpleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ object SimpleTest extends SimpleTestSuite {
intercept[AssertionException] {
assertEquals("dummy", s)
}
()
}

test("intercept") {
class DummyException extends RuntimeException
class DummyException(message: String) extends RuntimeException(message)
def test = 1

intercept[DummyException] {
if (test != 2) throw new DummyException
val ex = intercept[DummyException] {
if (test != 2) throw new DummyException("value was not equal to 2")
}
assertEquals(ex.getMessage, "value was not equal to 2")
}

testAsync("asynchronous test") {
Expand All @@ -102,11 +104,13 @@ object SimpleTest extends SimpleTestSuite {
if (hello(1) != 2) throw new DummyException
}
}
()
}

test("fail()") {
def x = 1
intercept[AssertionException] { if (x == 1) fail() }
()
}

test("fail(reason)") {
Expand Down

0 comments on commit aef7a63

Please sign in to comment.