Matcher composition doesn't work without braces when Context (scope) is used

54 views
Skip to first unread message

AlexVa

unread,
Oct 25, 2015, 9:37:05 AM10/25/15
to specs2-users
Hi,

I'm using custom matchers and composite them with "and" in my test - "data must haveA and haveB".
This doesn't work correctly in all cases, that is - when context is used and matchers are created in some way (highlighted with yellow colour in the example below) and the matchers are not enclosed with braces, the test passes even when it should not.

Is that a known issue? 

Here is the code example (the highlighted code is the only one not working properly, other tests behave as expected):

import org.specs2.matcher.{Expectable, MatchResult, Matcher}
import org.specs2.mutable.SpecificationWithJUnit
import org.specs2.specification.Scope


class PersonTest extends SpecificationWithJUnit {

  def beNamedJohn: Matcher[Person] = ((_: Person).name) ^^ be_==("John")
  def beYoungerThan30: Matcher[Person] = ((_: Person).age) ^^ be_<(30)

  def beNamedJohn_ByClass: Matcher[Person] = new BeNamedJohnMatcher
  def beYoungerThan30_ByClass: Matcher[Person] = new BeYoungerThanMatcher(30)

  class BeNamedJohnMatcher extends Matcher[Person] {
    override def apply[S <: Person](t: Expectable[S]): MatchResult[S] = {
      result(t.value.name == "John", "ok", "ko", t)
    }
  }

  class BeYoungerThanMatcher(age: Int) extends Matcher[Person] {
    override def apply[S <: Person](t: Expectable[S]): MatchResult[S] = {
      result(t.value.age < age, "ok", "ko", t)
    }
  }

  def beNamedJohn_ByFunc: Matcher[Person] = ((_: Person).name.equals("John"), "Bad name")
  def beYoungerThan30_ByFunc: Matcher[Person] = ((p: Person) => p.age < 30, "Bad age")

  class Context extends Scope {
    val person = Person("Bob", 40)
  }

  "Foo" should {
    "fail (but PASSES when Context used)" in new Context {
      person must beNamedJohn and beYoungerThan30
    }

    "fail (without Context)" in {
      Person("Bob", 40) must beNamedJohn and beYoungerThan30
    }

    "fail (inheritance with Context)" in new Context {
      person must beNamedJohn_ByClass and beYoungerThan30_ByClass
    }

    "fail (braces with Context)" in new Context {
      person must (beNamedJohn and beYoungerThan30)
    }

    "fail (by function with Context)" in new Context {
      person must beNamedJohn_ByFunc and beYoungerThan30_ByFunc
    }
  }
}

case class Person(name: String, age: Int)



Thanks,
Alex

etorreborre

unread,
Oct 25, 2015, 7:31:21 PM10/25/15
to specs2-users
Hi Alex,

Thanks for reporting this issue. It is now fixed in 3.6.5-20151025231639-a7fdf0e.

Eric

etorreborre

unread,
Oct 25, 2015, 11:23:21 PM10/25/15
to specs2-users
Sorry, use this version instead: 3.6.5-20151026032022-1930bd6 (I had an issue with the Scalaz-stream dependency trying to test for Scala 2.12.0-M3).

Thanks.

AlexVa

unread,
Oct 26, 2015, 4:12:36 AM10/26/15
to specs2-users
Cool! 
Thank you for the quick response! 
Reply all
Reply to author
Forward
0 new messages