Skip to content

Commit

Permalink
Merge pull request #246 from rtjfarrimond/assertion-for-exception-mes…
Browse files Browse the repository at this point in the history
…sages

assertion-for-exception-messages
  • Loading branch information
djspiewak authored Jan 6, 2022
2 parents 82ecfcd + 3c81ae9 commit 8729e58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,32 @@ trait AssertingSyntax {
fail(s"Expected an exception of type ${ct.runtimeClass.getName} but got a result: $a")
)
}

/**
* Asserts that the `F[A]` fails with an exception of type `E` and an expected error message.
*/
def assertThrowsWithMessage[E <: Throwable](expectedMessage: String)(implicit F: Sync[F], ct: reflect.ClassTag[E]): F[Assertion] =
self.attempt.flatMap {
case Left(e: E) =>
if (e.getMessage == expectedMessage)
F.pure(Succeeded: Assertion)
else
F.delay(
fail(
s"Expected exception to have message '$expectedMessage' but got: ${e.getMessage}"
)
)
case Left(t) =>
F.delay(
fail(
s"Expected an exception of type ${ct.runtimeClass.getName} but got an exception: $t"
)
)
case Right(a) =>
F.delay(
fail(s"Expected an exception of type ${ct.runtimeClass.getName} but got a result: $a")
)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class IOSpecTests extends AsyncFreeSpec with AsyncIOSpec with Matchers {
"IO assert Exception" in {
IO.raiseError(AError).assertThrows[AError.type]
}

"IO assert Exception with message" in {
val expectedMessage = "foo"
IO.raiseError(AErrorWithMessage(expectedMessage))
.assertThrowsWithMessage[AErrorWithMessage](expectedMessage)
}

}

"Effect assertions" - {
Expand All @@ -60,3 +67,6 @@ class IOSpecTests extends AsyncFreeSpec with AsyncIOSpec with Matchers {
}

case object AError extends Throwable
case class AErrorWithMessage(message: String) extends Throwable {
override def getMessage: String = message
}

0 comments on commit 8729e58

Please sign in to comment.