These are chat archives for non/algebra

8th
Jul 2015
FrankRaiser
@FrankRaiser
Jul 08 2015 18:51
how does scalactic's === interact with Eq? I thought an implicit Eq in scope would be used, but it doesn't seem to behave that way
Erik Osheim
@non
Jul 08 2015 19:04
scalactic doesn't have any baked-in Eq support afaik
FrankRaiser
@FrankRaiser
Jul 08 2015 19:09
oh.. so what would be the preferred way to apply Eq for two objects to test for equality?
Erik Osheim
@non
Jul 08 2015 19:10
@FrankRaiser algebra is notable in that it doesn't provide its own syntax implicits (like spire does, for example)
if you just need to do it a few times, Eq.eqv(x, y) is probably easiest.
otherwise you could create your own very simple implicit syntax, e.g.
implicit class EqOps[A](lhs: A)(implicit ev: Eq[A]) {
  def ===(rhs: A): Boolean = ev.eqv(lhs, rhs)
}
(the actual syntax classes spire would use look a lot different, would be specialized, use machinist's macros, etc.)
FrankRaiser
@FrankRaiser
Jul 08 2015 19:12
would that be ok?
test("Eq for case class") {
    val fooEq = Eq.fromAll[Foo](
      (x, y) => x.a == y.a,
      (x, y) => x.b == y.b
    )

    assert(fooEq.eqv(Foo(1, "ab", true), Foo(1, "ab", false)))
  }
argh.. messed it up of course
Erik Osheim
@non
Jul 08 2015 19:13
you can edit for ~15 min
if you go the right side of the broken message
FrankRaiser
@FrankRaiser
Jul 08 2015 19:13
ah.. nice
Erik Osheim
@non
Jul 08 2015 19:14
@FrankRaiser something structured like that seems good. i might try to write it as a property using scalacheck though
have you seen that before?
case class Foo(a: Int, b: String)
test("...") {
  forAll { (x0: Int, x1: Int, s0: String, s1: String) =>
    val foo0 = Foo(x0, s0)
    val foo1 = Foo(x1, s1)
    val fooEq = ...
    fooEq.eqv(foo1, foo2) shouldBe x1 == x2
  }
}
FrankRaiser
@FrankRaiser
Jul 08 2015 19:15
yeah.. as in re-use the Arbitrary I made for Order.fromAll .. something like for all pairs where a and b are equal it holds that eqv is true as well ?
Erik Osheim
@non
Jul 08 2015 19:15
that would be an example.
yours would end up being more complicated if you have 3+ types for Foo and 2+ functions
but i think taht basic idea makes sense.
FrankRaiser
@FrankRaiser
Jul 08 2015 19:16
ah true.. forAll would pull in the base arbitraries anyways
Erik Osheim
@non
Jul 08 2015 19:16
you can also ensure that Foo(x0, s0) is always equal to Foo(x0, s1)
right.
anyway, gotta run but feel free to ping me with more questions.
i prefer to write these tests with properties since i feel like we get more bang-for-the-buck that way, and it helps be clear about the intent in a way that specific examples are not always obvious.
FrankRaiser
@FrankRaiser
Jul 08 2015 19:19
I'm stuck with Java and JUnit at work so I tend to forget I'm in a better place now ;-)
Erik Osheim
@non
Jul 08 2015 19:20
no worries ;)
honestly it took me awhile until i had memorized enough about how scalacheck worked to use it effectively.
at first i was just cargo-culting one particular scalatest + scalacheck example around to all my projects :)
FrankRaiser
@FrankRaiser
Jul 08 2015 19:23
I am quite impressed with the law stuff you made there.. took a bit to wrap my head around that for the Order testing
I'm not really happy with this part though.. maybe you know what to improve there:
import Arbitrary._

  val fooGenerator = for {
    a <- arbitrary[Int] ;
    b <- arbitrary[String] ;
    c <- arbitrary[Boolean]
  } yield Foo(a,b,c)

  implicit val arbitraryFoo = Arbitrary(fooGenerator)
Erik Osheim
@non
Jul 08 2015 19:23
honestly i think that's about the best you can do. you can always just wrap the for-comprehension directly in Arbitrary(_)
FrankRaiser
@FrankRaiser
Jul 08 2015 19:23
it allows me to call laws[OrderLaws, Foo].check(_.order) afterwards like the other tests.. but not sure if there's a nicer way
Erik Osheim
@non
Jul 08 2015 19:27
that seems right to me.
the cats project has actually improved the on the structure of laws a bit.
algebra may want to steal some of it back.
FrankRaiser
@FrankRaiser
Jul 08 2015 19:31
non/algebra#70
ah.. sweet.. does that already run travis on the merged version now?
Erik Osheim
@non
Jul 08 2015 19:35
yup.
FrankRaiser
@FrankRaiser
Jul 08 2015 19:49
yay.. green on the first try.. what a surprise ;-P
Erik Osheim
@non
Jul 08 2015 19:51
nice! i'll try to take a look after work.
FrankRaiser
@FrankRaiser
Jul 08 2015 19:53
I'm off now anyways.. just let me know if there's something to be improved
Erik Osheim
@non
Jul 08 2015 19:53
thanks!