Skip to content

Commit 5a84743

Browse files
authored
Add stack trace to output for beSuccessfulTry on failure (etorreborre#1163)
* Add stack trace to output for beSuccessfulTry on failure * Add some types to TryMatchers
1 parent 8b488a0 commit 5a84743

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

matcher/shared/src/main/scala/org/specs2/matcher/TryMatchers.scala

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package matcher
33

44
import scala.reflect.ClassTag
55
import util.*
6+
import execute.{Result, AsResult}
67
import execute.ResultImplicits.*
78
import org.specs2.matcher.describe.Diffable
89
import text.NotNullStrings.*
@@ -45,11 +46,17 @@ trait TryMatchers:
4546

4647
object TryeMatchers extends TryMatchers
4748

48-
case class TrySuccessMatcher[T]() extends OptionLikeMatcher[Try[T], T]("a Success", (_: Try[T]).toOption):
49-
def withValue(t: ValueCheck[T]) = TrySuccessCheckedMatcher(t)
49+
case class TrySuccessMatcher[T]() extends Matcher[Try[T]]:
50+
def apply[S <: Try[T]](value: Expectable[S]): Result = checkSuccess(value, ValueCheck.alwaysOk)
5051

51-
case class TrySuccessCheckedMatcher[T](check: ValueCheck[T])
52-
extends OptionLikeCheckedMatcher[Try[T], T]("a Success", (_: Try[T]).toOption, check)
52+
def withValue(t: ValueCheck[T]): Matcher[Try[T]] = TrySuccessCheckedMatcher(t)
53+
54+
def which[R: AsResult](f: T => R): Matcher[Try[T]] = new TrySuccessCheckedMatcher(f)
55+
56+
def like[R: AsResult](f: PartialFunction[T, R]): Matcher[Try[T]] = new TrySuccessCheckedMatcher(f)
57+
58+
case class TrySuccessCheckedMatcher[T](check: ValueCheck[T]) extends Matcher[Try[T]]:
59+
def apply[S <: Try[T]](value: Expectable[S]): Result = checkSuccess(value, check)
5360

5461
case class TryFailureMatcher[T]()
5562
extends OptionLikeMatcher[Try[T], Throwable]("a Failure", (_: Try[T]).failed.toOption):
@@ -65,3 +72,15 @@ case class TryFailureMatcher[T]()
6572
})
6673
case class TryFailureCheckedMatcher[T](check: ValueCheck[Throwable])
6774
extends OptionLikeCheckedMatcher[Try[T], Throwable]("a Failure", (_: Try[T]).failed.toOption, check)
75+
76+
private def checkSuccess[T](value: Expectable[Try[T]], check: ValueCheck[T]): Result = value.value match
77+
case Success(v) =>
78+
val r = check.check(v)
79+
val koMessage = s"${value.description} is a Success but ${r.message}"
80+
r match
81+
case execute.Failure(_, _, _, details) => Result.result(false, koMessage, details)
82+
case _ => Result.result(r.isSuccess, koMessage)
83+
case Failure(e) =>
84+
execute.Failure(
85+
s"${value.description} is not a Success\n\nFailed with ${e.getMessage}:\n\n${e.getStackTrace.mkString("\n")}"
86+
)

tests/shared/src/test/scala/org/specs2/matcher/TryMatchersSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class TryMatchersSpec extends Spec with TryMatchers with ResultMatchers {
1818
${(Succeeded(1) must beASuccessfulTry.which(_ > 0))}
1919
${(Succeeded(1) must beASuccessfulTry
2020
.which(_ < 0)) returns "Success(1) is a Success but the function returns 'false' on '1'"}
21-
${(Failed[I](e) must beASuccessfulTry.which(_ > 0)) returns "Failure(boom) is not a Success"}
21+
${(Failed[I](e) must beASuccessfulTry.which(_ > 0)) returns "Failure(boom) is not a Success\n\nFailed with boom:\n\n"}
2222

2323
${Succeeded(1) must beASuccessfulTry[Int].like { case a if a > 0 => ok }}
2424
${(Succeeded(1) must not(beASuccessfulTry[Int].like { case a => a must be_>=(0) })) returns "Expectation failed: 'Success(1) is a Success but 1 is strictly less than 0'"}
2525
${Succeeded(1) must not(beASuccessfulTry.withValue(2))}
2626
${Failed[I](e) must not(beASuccessfulTry)}
2727
${Failed[I](e) must not(beASuccessfulTry.withValue(2))}
28-
${(Failed[I](e) must beSuccessfulTry) returns "Failure(boom) is not a Success"}
28+
${(Failed[I](e) must beSuccessfulTry) returns "Failure(boom) is not a Success\n\nFailed with boom:\n\n"}
2929
${(Succeeded(1) must beSuccessfulTry.withValue(2)) returns "Success(1) is a Success but 1 != 2"}
3030

3131
beAFailure checks if an element is Failure(_)

0 commit comments

Comments
 (0)