Skip to content

Commit

Permalink
Add Traversal 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 74bc110 commit c3e1d26
Showing 1 changed file with 71 additions and 14 deletions.
85 changes: 71 additions & 14 deletions arrow-optics/src/test/kotlin/arrow/optics/TraversalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import io.kotlintest.properties.Gen
import io.kotlintest.properties.forAll
import arrow.core.Option
import arrow.core.eq
import arrow.core.getOrElse
import arrow.core.toT
import arrow.data.ListKW
import arrow.data.ListKWHK
import arrow.data.eq
import arrow.data.k
import org.junit.runner.RunWith
import arrow.optics.PTraversal.Companion.fromTraversable
import arrow.syntax.collections.firstOption
import arrow.syntax.foldable.combineAll
import arrow.syntax.option.toOption
import arrow.test.UnitSpec
import arrow.test.generators.genFunctionAToB
import arrow.test.generators.genListKW
import arrow.test.generators.genTuple
import arrow.test.laws.SetterLaws
import arrow.test.laws.TraversalLaws
import arrow.typeclasses.Eq

Expand All @@ -23,26 +29,77 @@ class TraversalTest : UnitSpec() {

init {

testLaws(TraversalLaws.laws(
testLaws(
TraversalLaws.laws(
traversal = fromTraversable(),
aGen = Gen.create { Gen.list(Gen.int()).generate().k() },
aGen = genListKW(Gen.int()),
bGen = Gen.int(),
funcGen = genFunctionAToB(Gen.int()),
EQA = Eq.any(),
EQOptionB = Option.eq(Eq.any()),
EQListB = ListKW.eq(Eq.any())
))
funcGen = genFunctionAToB(Gen.int())
),

SetterLaws.laws(
setter = fromTraversable<ListKWHK, Int, Int>().asSetter(),
aGen = genListKW(Gen.int()),
bGen = Gen.int(),
funcGen = genFunctionAToB(Gen.int())
)
)

testLaws(TraversalLaws.laws(
traversal = Traversal({ it.a }, { it.b }, { a, b, _ -> a toT b }),
aGen = genTuple(Gen.float(), Gen.float()),
bGen = Gen.float(),
funcGen = genFunctionAToB(Gen.float()),
EQA = Eq.any(),
EQOptionB = Option.eq(Eq.any()),
EQListB = ListKW.eq(Eq.any())
traversal = Traversal({ it.a }, { it.b }, { a, b, _ -> a toT b }),
aGen = genTuple(Gen.float(), Gen.float()),
bGen = Gen.float(),
funcGen = genFunctionAToB(Gen.float())
))

"asFold should behave as valid Fold: size" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().size(ints) == ints.size
}
}

"asFold should behave as valid Fold: nonEmpty" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().nonEmpty(ints) == ints.isNotEmpty()
}
}

"asFold should behave as valid Fold: isEmpty" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().isEmpty(ints) == ints.isEmpty()
}
}

"asFold should behave as valid Fold: getAll" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().getAll(ints) == ints.k()
}
}

"asFold should behave as valid Fold: combineAll" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().combineAll(ints) == ints.sum()
}
}

"asFold should behave as valid Fold: fold" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().fold(ints) == ints.sum()
}
}

"asFold should behave as valid Fold: headOption" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().headOption(ints) == ints.firstOption()
}
}

"asFold should behave as valid Fold: lastOption" {
forAll(genListKW(Gen.int())) { ints ->
fromTraversable<ListKWHK, Int, Int>().asFold().lastOption(ints) == ints.lastOrNull()?.toOption()
}
}

"Getting all targets of a traversal" {
forAll(Gen.list(Gen.int()), { ints ->
fromTraversable<ListKWHK, Int, Int>().getAll(ints.k()) == ints.k()
Expand Down

0 comments on commit c3e1d26

Please sign in to comment.