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] = {
}
}
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)