Skip to content

Commit

Permalink
wip Fix LensExercises
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRaFo committed Mar 27, 2017
1 parent 62c7f86 commit 78d8b5a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/monocle/IsoExercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ object IsoExercises extends FlatSpec with Matchers with Section {
def exerciseLaws(res0 : Boolean, res1 : Boolean) =
{
personToTuple.get(Person("Zoe", 25))
def roundTripOneWay[S, A](i: Iso[Person, (String, Int)], s: Person): Boolean =
def roundTripOneWay[S, A](i: Iso[S, A], s: S): Boolean =
i.reverseGet(i.get(s)) == s

def roundTripOtherWay[S, A](i: Iso[Person, (String, Int)], a: (String,Int)): Boolean =
def roundTripOtherWay[S, A](i: Iso[S, A], a: A): Boolean =
i.get(i.reverseGet(a)) == a

roundTripOneWay(personToTuple, Person("Zoey",25)) should be res0
Expand Down
27 changes: 16 additions & 11 deletions src/main/scala/monocle/LensExercises.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package monocleex
package monocle


import monocle.{Iso, Lens}
import monocle.IsoHelper.{Person, personToTuple}
import org.scalatest._
import org.scalaexercises.definitions._
import monocle.macros.GenLens
Expand Down Expand Up @@ -144,18 +146,21 @@ object LensExercises extends FlatSpec with Matchers with Section {
*
* In particular, a Lens must respect the `getSet` law which states that if you get a value `A` from `S` and set it back in, the result is an object identical to the original one. A side effect of this law is that set must only update the `A` it points to, for example it cannot increment a counter or modify another value.
*
* {{{
* def getSet[S, A](l: Lens[S, A], s: S): Boolean =
* l.set(l.get(s))(s) == s
* }}}
*
* On the other hand, the `setGet` law states that if you `set` a `value`, you always `get` the same value back. This law guarantees that `set` is actually updating a value `A` inside of `S`.
*
* {{{
* def setGet[S, A](l: Lens[S, A], s: S, a: A): Boolean =
* l.get(l.set(a)(s)) == a
* }}}
*/
def conclusion(): Unit = ()
def exerciseLaws(res0 : Boolean, res1 : Boolean) =
{
val streetNumber = Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))

def getSet[S, A](l: Lens[S, A], s: S): Boolean =
l.set(l.get(s))(s) == s

def setGet[S, A](l: Lens[S, A], s: S, a: A): Boolean =
l.get(l.set(a)(s)) == a

getSet(streetNumber) should be res0

getSet(streetNumber) should be res1
}
}
1 change: 1 addition & 0 deletions src/test/scala/monocle/IsoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class IsoSpec extends FunSuite with Checkers {
)
)
}

test("exercise laws") {
check(
Test.testSuccess(
Expand Down
8 changes: 8 additions & 0 deletions src/test/scala/monocle/LensSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ class LensSpec extends FunSuite with Checkers {
)
}

test("exercise laws") {
check(
Test.testSuccess(
IsoExercises.exerciseLaws _,
true :: true:: HNil
)
)
}
}

0 comments on commit 78d8b5a

Please sign in to comment.