1
votes

Is it possible, in ScalaTest, to express assertions on arbitrary properties that do more than simple equality comparisons on the values of those properties?

For example, instead of:

 val list = List(1,2,3,4,5)
 list.length should be < 4             // Fails with the unhelpful error message "5 is not less than 4"

 case class Example(field: String)
 val obj = Example("TEST")
 obj.field should be allLowerCase      // Given "allLowerCase", this fails without any information about obj.

I'd like to do something like the following:

  val list = List(1,2,3,4,5)
  list should have length < 4         // Fails with an error message containing the list

  obj should have ('field (allLowerCase))  // Fails with an error message containing obj.

The goal is to have the relevant context about the object with the failing property in the error message if the matcher fails.

1

1 Answers

0
votes
obj.field must beAllLowerCase

Where beAllLowerCase is your matcher.