Skip to content

Commit

Permalink
Add Setter law test for optic conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Feb 2, 2018
1 parent 9745952 commit 74bc110
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
51 changes: 51 additions & 0 deletions arrow-optics/src/test/kotlin/arrow/optics/SetterTest.kt
@@ -1,6 +1,7 @@
package arrow.optics

import arrow.core.getOrElse
import arrow.data.k
import io.kotlintest.KTestJUnitRunner
import io.kotlintest.properties.Gen
import io.kotlintest.properties.forAll
Expand All @@ -11,12 +12,14 @@ import arrow.test.generators.genFunctionAToB
import arrow.test.generators.genOption
import arrow.syntax.either.left
import arrow.syntax.either.right
import arrow.syntax.foldable.combineAll
import org.junit.runner.RunWith

@RunWith(KTestJUnitRunner::class)
class SetterTest : UnitSpec() {

init {

testLaws(SetterLaws.laws(
setter = Setter.id(),
aGen = Gen.int(),
Expand All @@ -41,6 +44,54 @@ class SetterTest : UnitSpec() {
EQA = Eq.any()
))

"asFold should behave as valid Fold: size" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().size(sum) == sumPrism.getOption(sum).map { 1 }.getOrElse { 0 }
}
}

"asFold should behave as valid Fold: nonEmpty" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().nonEmpty(sum) == sumPrism.getOption(sum).nonEmpty()
}
}

"asFold should behave as valid Fold: isEmpty" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().isEmpty(sum) == sumPrism.getOption(sum).isEmpty()
}
}

"asFold should behave as valid Fold: getAll" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().getAll(sum) == sumPrism.getOption(sum).toList().k()
}
}

"asFold should behave as valid Fold: combineAll" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().combineAll(sum) == sumPrism.getOption(sum).combineAll()
}
}

"asFold should behave as valid Fold: fold" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().fold(sum) == sumPrism.getOption(sum).combineAll()
}
}

"asFold should behave as valid Fold: headOption" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().headOption(sum) == sumPrism.getOption(sum)
}
}

"asFold should behave as valid Fold: lastOption" {
forAll(SumGen) { sum: SumType ->
sumPrism.asFold().lastOption(sum) == sumPrism.getOption(sum)
}
}

"Joining two lenses together with same target should yield same result" {
val userTokenStringSetter = userSetter compose tokenSetter
val joinedSetter = tokenSetter.choice(userTokenStringSetter)
Expand Down
3 changes: 2 additions & 1 deletion arrow-test/src/main/kotlin/arrow/test/laws/SetterLaws.kt
Expand Up @@ -6,10 +6,11 @@ import arrow.core.identity
import io.kotlintest.properties.Gen
import io.kotlintest.properties.forAll
import arrow.optics.Setter
import arrow.typeclasses.eq

object SetterLaws {

inline fun <reified A, reified B> laws(setter: Setter<A, B>, aGen: Gen<A>, bGen: Gen<B>, funcGen: Gen<(B) -> B>, EQA: Eq<A>) = listOf(
inline fun <reified A, reified B> laws(setter: Setter<A, B>, aGen: Gen<A>, bGen: Gen<B>, funcGen: Gen<(B) -> B>, EQA: Eq<A> = eq()) = listOf(
Law("Setter law: set is idempotent", { setIdempotent(setter, aGen, bGen, EQA) }),
Law("Setter law: modify identity", { modifyIdentity(setter, aGen, EQA) }),
Law("Setter law: compose modify", { composeModify(setter, aGen, EQA, funcGen) }),
Expand Down

0 comments on commit 74bc110

Please sign in to comment.