Testing monoids

We know that monoids have to satisfy some specific laws. Our examples are simple enough to see that the laws are actually in place, but sometimes it might not be that obvious. In Scalaz, you can actually test your monoids:

import org.scalacheck.Arbitraryimport org.scalatest.prop.Checkersimport org.scalatest.{FlatSpec, Matchers}import scalaz._import scalaz.scalacheck.ScalazProperties._class MonoidsTest extends FlatSpec with Matchers with Checkers {  implicit def arbString(implicit ev: Arbitrary[String]):   Arbitrary[String] =    Arbitrary { ev.arbitrary.map(identity) }    "stringConcatenation monoid" should "satisfy the identity rule." in {    monoid.laws[String](stringConcatenation, Equal.equalA[String],    arbString).check()  }}

In order ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.