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

Add assertions to assertThrows #358

Merged
merged 2 commits into from
Oct 29, 2022
Merged
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 @@ -50,8 +50,15 @@ trait AssertingSyntax {
* Asserts that the `F[A]` fails with an exception of type `E`.
*/
def assertThrows[E <: Throwable](implicit F: Sync[F], ct: reflect.ClassTag[E]): F[Assertion] =
assertThrowsError[E](_ => succeed)

/**
* Asserts that the `F[A]` fails with an exception of type `E` and an expected error.
*/
def assertThrowsError[E <: Throwable](test: E => Assertion)(implicit F: Sync[F], ct: reflect.ClassTag[E]): F[Assertion] =
self.attempt.flatMap {
case Left(_: E) => F.pure(Succeeded: Assertion)
case Left(e: E) =>
F.delay(test(e))
case Left(t) =>
F.delay(
fail(
Expand All @@ -68,25 +75,12 @@ trait AssertingSyntax {
* 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")
assertThrowsError[E] { e =>
if (e.getMessage == expectedMessage)
succeed
else
fail(
s"Expected exception to have message '$expectedMessage' but got: ${e.getMessage}"
)
}

Expand Down