Skip to content

Commit

Permalink
Merge pull request #358 from custommonkey/series/1.x
Browse files Browse the repository at this point in the history
Add assertions to assertThrows
  • Loading branch information
djspiewak authored Oct 29, 2022
2 parents 93cc5e3 + 19b72fb commit 296ae22
Showing 1 changed file with 14 additions and 20 deletions.
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

0 comments on commit 296ae22

Please sign in to comment.