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

Allow Either Values To Be Tested #8133

Merged
merged 2 commits into from
May 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/TestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ object TestSpec extends ZIOBaseSpec {
message <- STM.succeed("Hello from an STM transaction!")
} yield assert(message)(anything)
},
test("either values can be tested") {
for {
message <- Right("Hello from an Either value!")
} yield assert(message)(anything)
},
test("fail-fast assertion by automatic lifting to ZIO") {
for {
ref <- Ref.make(0)
Expand Down
13 changes: 12 additions & 1 deletion test/shared/src/main/scala/zio/test/CheckConstructor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ trait CheckConstructorLowPriority1 extends CheckConstructorLowPriority2 {
}
}

trait CheckConstructorLowPriority2 {
trait CheckConstructorLowPriority2 extends CheckConstructorLowPriority3 {

implicit def AssertZSTMConstructor[R, R1, E, A <: TestResult]
: CheckConstructor.WithOut[R, ZSTM[R1, E, A], R with R1, E] =
Expand All @@ -50,3 +50,14 @@ trait CheckConstructorLowPriority2 {
input.commit
}
}

trait CheckConstructorLowPriority3 {

implicit def AssertEitherConstructor[R, E, A <: TestResult]: CheckConstructor.WithOut[R, Either[E, A], R, E] =
new CheckConstructor[R, Either[E, A]] {
type OutEnvironment = R
type OutError = E
def apply(input: => Either[E, A])(implicit trace: Trace): ZIO[OutEnvironment, OutError, TestResult] =
ZIO.fromEither(input)
}
}
14 changes: 13 additions & 1 deletion test/shared/src/main/scala/zio/test/TestConstructor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trait TestConstructorLowPriority1 extends TestConstructorLowPriority2 {
}
}

trait TestConstructorLowPriority2 {
trait TestConstructorLowPriority2 extends TestConstructorLowPriority3 {

implicit def AssertZSTMConstructor[R, E, A <: TestResult]: TestConstructor.WithOut[R, ZSTM[R, E, A], Spec[R, E]] =
new TestConstructor[R, ZSTM[R, E, A]] {
Expand All @@ -51,3 +51,15 @@ trait TestConstructorLowPriority2 {
test(label)(assertion.commit)
}
}

trait TestConstructorLowPriority3 {

implicit def AssertEitherConstructor[E, A <: TestResult]: TestConstructor.WithOut[Any, Either[E, A], Spec[Any, E]] =
new TestConstructor[Any, Either[E, A]] {
type Out = Spec[Any, E]
def apply(label: String)(
assertion: => Either[E, A]
)(implicit sourceLocation: SourceLocation, trace: Trace): Spec[Any, E] =
test(label)(ZIO.fromEither(assertion))
}
}