What to use instead of symbols in scalatest?

Viewed 264

In scalatest, you’re supposed to be able to test boolean properties using symbols like this:

iter shouldBe 'traversableAgain

But this notation have been deprecated in the most recent versions of scala, so now you’re supposed to write:

iter shouldBe Symbol("traversableAgain")

Which is a bit ugly. Is there any better alternative?

1 Answers

Consider BePropertyMatcher which provides type-safe predicate matching syntax

iter should be (traversableAgain)

for example

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.{BePropertyMatchResult, BePropertyMatcher}
import org.scalatest.matchers.should.Matchers

trait CustomMatchers {
  val traversableAgain = new BePropertyMatcher[Iterator[_]] {
    def apply(left: Iterator[_]): BePropertyMatchResult = 
      BePropertyMatchResult(left.isTraversableAgain, "isTraversableAgain")
  }
}

class BePropertyMatcherExampleSpec extends AnyFlatSpec with Matchers with CustomMatchers {
  "BePropertyMatcher" should "provide type-safe checking of predicates" in {
    Iterator(42, 11) should be (traversableAgain)
  }
}

There is also a related issue Replacement for using symbols as property matchers for 2.13+ #1679

Related