Permalink
Browse files

wip Fix LensExercises

  • Loading branch information...
AdrianRaFo committed Mar 27, 2017
1 parent 62c7f86 commit 78d8b5aa33db6ca0ff8fcb309422f18452c337bc
@@ -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
@@ -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
@@ -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
}
}
@@ -63,6 +63,7 @@ class IsoSpec extends FunSuite with Checkers {
)
)
}
test("exercise laws") {
check(
Test.testSuccess(
@@ -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.