Depending on your degree of eagerness to use this construct…
- Only pull it in where the frequency of usage justifies the boilerplate overhead.
- Write some helpers to reduce boilerplate.
- Consider implementing some macro on top (in contrast to the reflection-based approach used by
HavePropertyMatcherGenerator
)
Naive suggestion for 2:
def mkPropEqMatcher[T, V](
name: String, extr: T => V)(
expVal: V
): HavePropertyMatcher[T, V] =
(t: T) =>
HavePropertyMatchResult(extr(t) == expVal, name, expVal, extr(t))
val title = mkPropEqMatcher("title", (_: Book).title) _
val author = mkPropEqMatcher("author", (_: Book).author) _