scalascalatestlensesmonocle-scala

How can I use Monocle's built in law implementations to test my own lenses?


I noticed that Monocle has implementations of lens laws that are used to test the libraries internals. They seem to be nicely generalized and modularized. I tried to use them to test my own lenses, but I am lost in the jungle of dependencies. Has anybody tried to do this, and could post an example? The documentation does not seem to talk about laws at all. Thank you.

To elaborate, here is what I am trying to do (fumbling, not sure if I am using the intended way to use the API):

it should "pass the LensLaws" in check {
  forAll {(c: (String,Int), a: String) =>
    new monocle.law.LensLaws(l).setGet(c,a) }   }

where l is the Monocle lens, visible in scope. I am receiving the following error message:

No implicit view available from monocle.internal.IsEq[String] => org.scalacheck.Prop

As far as I can see the setGet law constructs an IsEq object and I was not able to find how to turn it into a Prop (or Boolean).

I can also see that the framework is using a function checkAll to test all LensLaws at the same time, but I could not get this to work in my own code either. Any help appreciated.


Solution

  • The following works for me

    import org.scalatest.FunSuite
    import org.typelevel.discipline.scalatest.Discipline
    
    class LensesSuite extends FunSuite with Discipline {
    
      import scalaz._
      import Scalaz._
    
      checkAll("Lens l", monocle.law.discipline.LensTests(l))
    }
    

    It turns out that the main problem was my relatively short knowledge on scalatest. checkAll is a checker provided by org.typelevel.discipline.scalatest.Discipline, which only works in FunSuite (not in FlatSpec that I was using). It took me ages to figure that out ...

    Still have no idea how to elegantly use this RuleSet (LensTests) into another spec. It seems strange that the choice of RuleSet by Monocle would enforce the Spec style on the project using these tests.