Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertion-for-exception-messages #246

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}