Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new BoolAlgebra.all operator #3857

Merged
merged 3 commits into from Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,7 +7,9 @@ object BoolAlgebraSpec extends ZIOBaseSpec {

def spec = suite("BoolAlgebraSpec")(
test("all returns conjunction of values") {
assert(BoolAlgebra.all(List(success1, failure1, failure2)))(isSome(isFailure))
assert(BoolAlgebra.all(List(success1, failure1, failure2)))(isSome(isFailure)) &&
assert(BoolAlgebra.all(success1, failure1, failure2))(isFailure) &&
assert(BoolAlgebra.all(success1, success2))(isSuccess)
},
testM("and distributes over or") {
check(boolAlgebra, boolAlgebra, boolAlgebra)((a, b, c) => assert(a && (b || c))(equalTo((a && b) || (a && c))))
Expand All @@ -19,7 +21,9 @@ object BoolAlgebraSpec extends ZIOBaseSpec {
check(boolAlgebra, boolAlgebra)((a, b) => assert(a && b)(equalTo(b && a)))
},
test("any returns disjunction of values") {
assert(BoolAlgebra.any(List(success1, failure1, failure2)))(isSome(isSuccess))
assert(BoolAlgebra.any(List(success1, failure1, failure2)))(isSome(isSuccess)) &&
assert(BoolAlgebra.any(success1, failure1, failure2))(isSuccess) &&
assert(BoolAlgebra.any(failure1, failure2))(isFailure)
},
test("as maps values to constant value") {
assert((success1 && success2).as("value"))(equalTo(BoolAlgebra.success("value") && BoolAlgebra.success("value")))
Expand Down
12 changes: 12 additions & 0 deletions test/shared/src/main/scala/zio/test/BoolAlgebra.scala
Expand Up @@ -306,6 +306,12 @@ object BoolAlgebra {
def all[A](as: Iterable[BoolAlgebra[A]]): Option[BoolAlgebra[A]] =
if (as.isEmpty) None else Some(as.reduce(_ && _))

/**
* Returns a result that is the logical conjunction of all of the results
*/
def all[A](a: BoolAlgebra[A], as: BoolAlgebra[A]*): BoolAlgebra[A] =
as.foldLeft(a)(_ && _)

/**
* Constructs a result that is the logical conjunction of two results.
*/
Expand All @@ -319,6 +325,12 @@ object BoolAlgebra {
def any[A](as: Iterable[BoolAlgebra[A]]): Option[BoolAlgebra[A]] =
if (as.isEmpty) None else Some(as.reduce(_ || _))

/**
* Returns a result that is the logical disjunction of all of the results
*/
def any[A](a: BoolAlgebra[A], as: BoolAlgebra[A]*): BoolAlgebra[A] =
as.foldLeft(a)(_ || _)

/**
* Combines a collection of results to create a single result that succeeds
* if all of the results succeed.
Expand Down