Skip to content

Commit

Permalink
feature: improve the success messages for numerical matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Aug 10, 2023
1 parent 6ffa6d0 commit b0bfb64
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 20 deletions.
8 changes: 7 additions & 1 deletion common/shared/src/main/scala/org/specs2/execute/Result.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ sealed abstract class Result(val message: String = "", val expected: String = ""
object Result:

/** @return a Success or a Failure */
def result(test: Boolean, successMessage: =>String, failureMessage: =>String, expected: =>String, details: Details): Result =
def result(
test: Boolean,
successMessage: =>String,
failureMessage: =>String,
expected: =>String,
details: Details
): Result =
if test then Success(successMessage.notNull)
else Failure(m = failureMessage.notNull, e = expected, details = details)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ trait ResultLogicalCombinators extends Results:
case f @ Failure(_, _, _, _) =>
o match
case s @ Success(m, exp) =>
if r.message == m then r.addExpectationsNb(s.expectationsNb)
if r.message == m then Success(m, exp, r.expectationsNb + s.expectationsNb)
else Success(r.message + " and " + m, exp, r.expectationsNb + s.expectationsNb)
case Failure(m, e, st, d) =>
Failure(r.message + " and " + m, e, f.stackTrace ::: st, d).addExpectationsNb(r.expectationsNb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ table ${"a" | "b" |>
|[info]
|[info]__+_example_1
|[info]__x_example 2
|[error]__ko_(Result.scala:371)
|[error]__ko_(Result.scala:382)
|[error]
|[info]
|[info]_Total_for_specification_TextPrinterSpec""".stripMargin
Expand Down
4 changes: 2 additions & 2 deletions form/src/test/scala/org/specs2/form/PropSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ Execution
def creation6 = Prop("", 1, be_>(0).mute).execute === Success("")
def creation7 = Prop("", 1, 2, be_>(0).mute).execute === Success("")
def creation8 = {
(Prop[Int]("value", 1, be_>[Int](0)).execute === Success("1 is strictly greater than 0")) and
(Prop[Int]("value", 1, be_<(0)).execute === Failure("1 is greater or equal than 0")) and
(Prop[Int]("value", 1, be_>[Int](0)).execute === Success("1 is strictly greater than 0")) and
(Prop[Int]("value", 1, be_<(0)).execute === Failure("1 is greater or equal than 0")) and
(Prop[Int]("value", 1).must(be_>[Int](0)).execute === Success("1 is strictly greater than 0")) and
(Prop[Int]("value", 1).must(be_<(0)).execute === Failure("1 is greater or equal than 0"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,49 @@ class BeLessThanOrEqualTo[T: Ordering](n: T) extends Matcher[T]:
def apply[S <: T](a: Expectable[S]) =
val value: T = a.value
val r = value <= n
val isEqual = value == n
result(r, a.description + " is strictly greater than " + n.toString)
result(
r,
a.description + " is less or equal than " + n.toString,
a.description + " is strictly greater than " + n.toString,
"",
NoDetails
)

class BeLessThan[T: Ordering](n: T) extends Matcher[T]:
def apply[S <: T](a: Expectable[S]) =
val value: T = a.value
val r = value < n
result(r, a.description + " is greater than " + n.toString)
result(
r,
a.description + " is strictly less than " + n.toString,
a.description + " is greater or equal than " + n.toString,
"",
NoDetails
)

class BeGreaterThanOrEqualTo[T: Ordering](n: T) extends Matcher[T]:
def apply[S <: T](a: Expectable[S]) =
val value: T = a.value
val r = value >= n
val isEqual = value == n
result(r, a.description + " is strictly less than " + n.toString)
result(
r,
a.description + " is greater or equal than " + n.toString,
a.description + " is strictly less than " + n.toString,
"",
NoDetails
)

class BeGreaterThan[T: Ordering](n: T) extends Matcher[T]:
def apply[S <: T](a: Expectable[S]) =
val value: T = a.value
val r = value > n
result(r, a.description + " is less than " + n.toString)
result(
r,
a.description + " is strictly greater than " + n.toString,
a.description + " is less or equal than " + n.toString,
"",
NoDetails
)

class BeCloseTo[T: Numeric](n: T, delta: T) extends Matcher[T]:
def apply[S <: T](x: Expectable[S]) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Messages
def adapt4 =
val result =
new Exception("message") must be_<(2) ^^ ((e: Exception) => e.getMessage.length `aka` "the message size")
result.message must ===("the message size '7' is greater than 2")
result.message must ===("the message size '7' is greater or equal than 2")

def adapt5 =
val result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class EitherMatchersSpec extends Spec with EitherMatchers with ResultMatchers {
${Right(Seq(1)) must beRight(===(Seq(1)))}
${Left(1) must not(beRight(1))}
${Right(1) must beRight.like { case i => i must be_>(0) }}
${(Right(1) must beRight.like { case i => i must be_<(0) }) returns "Right(1) is Right but 1 is greater than 0"}
${(Right(1) must beRight.like { case i =>
i must be_<(0)
}) returns "Right(1) is Right but 1 is greater or equal than 0"}

beLeft checks if an element is Left(_)
${Left(1) must beLeft(1)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ types and more generally with Ordered types.
def e1 = (2 must be_<=(1)) returns "2 is strictly greater than 1"
def e1_1 = (2 `aka` "two" must be_<=(1)) returns "two '2' is strictly greater than 1"

def e2 = (2 must be_<(1)) returns "2 is greater than 1"
def e2_1 = (2 `aka` "two" must be_<(1)) returns "two '2' is greater than 1"
def e2 = (2 must be_<(1)) returns "2 is greater or equal than 1"
def e2_1 = (2 `aka` "two" must be_<(1)) returns "two '2' is greater or equal than 1"

def e3 = (1 must be_>=(2)) returns "1 is strictly less than 2"
def e3_1 = (1 `aka` "one" must be_>=(2)) returns "one '1' is strictly less than 2"

def e4 = (1 must be_>(2)) returns "1 is less than 2"
def e4_1 = (1 `aka` "one" must be_>(2)) returns "one '1' is less than 2"
def e4 = (1 must be_>(2)) returns "1 is less or equal than 2"
def e4_1 = (1 `aka` "one" must be_>(2)) returns "one '1' is less or equal than 2"

def e5 =
(1.1 must beCloseTo(3.2, 0.5)) returns "1.1 is not close to 3.2 +/- 0.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OptionMatchersSpec extends Specification with ResultMatchers {
${(Some(1) must beSome.which(_ > 0))}
${(Some(1) must beSome.which(_ < 0)) returns "Some(1) is Some but the function returns 'false' on '1'"}
${Some(1) must beSome.like { case a if a > 0 => ok }}
${(Some(1) must not(beSome[Int].like { case a => a must be_>=(0) })) returns "Expectation failed: 'Some(1) is Some but 1 is strictly less than 0'"}
${(Some(1) must not(beSome[Int].like { case a => a must be_>=(0) })) returns "Expectation failed: 'Some(1) is Some but 1 is greater or equal than 0'"}
${Some(1) must not(beSome(2))}
${None must not(beSome)}
${None must not(beSome(2))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TryMatchersSpec extends Spec with TryMatchers with ResultMatchers {
${(Failed[I](e) must beASuccessfulTry.which(_ > 0)) returns "Failure(boom) is not a Success\n\nFailed with boom:\n\n"}

${Succeeded(1) must beASuccessfulTry[Int].like { case a if a > 0 => ok }}
${(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'"}
${(Succeeded(1) must not(beASuccessfulTry[Int].like { case a => a must be_>=(0) })) returns "Expectation failed: 'Success(1) is a Success but 1 is greater or equal than 0'"}
${Succeeded(1) must not(beASuccessfulTry.withValue(2))}
${Failed[I](e) must not(beASuccessfulTry)}
${Failed[I](e) must not(beASuccessfulTry.withValue(2))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ScalaCheckMatchersApiSpec extends Specification with ScalaCheck {
returning a result
${prop { (i: Int) => success }}
returning a match result
${prop { (i: Int) => i must (be_>(0) or be_<=(0)) }}
$${prop { (i: Int) => i must (be_>(0) or be_<=(0)) }}
${0 must (be_>(0) or be_<=(0))}
returning a boolean value
${prop { (i: Int) => i > 0 || i <= 0 }}
using an implication and a match result
Expand Down

0 comments on commit b0bfb64

Please sign in to comment.