-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
267 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,5 @@ jobs: | |
java-version: "17" | ||
cache: "sbt" | ||
- name: Test Opinions | ||
run: sbt opinions/test | ||
run: sbt test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
test-opinions/src/main/scala/test/opinions/Assertions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.test.* | ||
import opinions.* | ||
|
||
extension [A](a: A) | ||
|
||
/** Wrap a: A in Assertion.equalTo(a) | ||
*/ | ||
def eqTo: Assertion[A] = Assertion.equalTo(a) | ||
|
||
/** Wrap a: A in Assertion.not(Assertion.equalTo(a)) | ||
*/ | ||
def neqTo: Assertion[A] = Assertion.not(a.eqTo) |
14 changes: 14 additions & 0 deletions
14
test-opinions/src/main/scala/test/opinions/Expectations.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.mock.* | ||
|
||
extension [A: Tag](a: A) | ||
|
||
/** Wrap a: A in Expectation.value(a) | ||
*/ | ||
def expected: Result[Any, Nothing, A] = Expectation.value(a) | ||
|
||
/** Wrap a: A in Expectation.failure(a) | ||
*/ | ||
def expectedF: Result[Any, A, Nothing] = Expectation.failure(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package test.opinions | ||
|
||
import zio.test.* | ||
import zio.* | ||
|
||
object GenRandom: | ||
|
||
/** Generate a random List[A] | ||
* @param count | ||
* The number of elements to generate | ||
* @param gen | ||
* The implicit Gen[Any, A] to use. | ||
* @tparam A | ||
*/ | ||
def apply[A](count: Int)(using gen: Gen[Any, A]): UIO[List[A]] = | ||
gen.runCollectN(count) | ||
|
||
/** Generate a random A | ||
* @param gen | ||
* The implicit Gen[Any, A] to use. | ||
* @tparam A | ||
*/ | ||
def apply[A](using gen: Gen[Any, A]): UIO[A] = | ||
GenRandom[A](1).map(_.head) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.mock.* | ||
import zio.test.Assertion | ||
|
||
extension [S, E: Tag, I, O: Tag](serviceMethod: Mock[S]#Effect[I, E, O]) | ||
|
||
/** For a given ZIO Mock Capability (serviceMethod), apply the given values as | ||
* Expectation.value/Assertion.equalTo | ||
* @param o | ||
* Expected output value | ||
* @param i | ||
* Asserted input value | ||
*/ | ||
def expectWhen(o: O, i: I): Expectation[S] = | ||
serviceMethod.apply(i.eqTo, o.expected) | ||
|
||
/** For a given ZIO Mock Capability (serviceMethod), apply the given values as | ||
* Expectation.failure/Assertion.equalTo | ||
* @param e | ||
* Expected output failure value | ||
* @param i | ||
* Asserted input value | ||
*/ | ||
def expectWhenF(e: E, i: I): Expectation[S] = | ||
serviceMethod.apply(i.eqTo, e.expectedF) | ||
|
||
/** For a given ZIO Mock Capability (serviceMethod), apply the given | ||
* Expectation Result/Assertion | ||
* @param o | ||
* The expected output Result/Expectation | ||
* @param i | ||
* The expected input assertion | ||
*/ | ||
def expectWhen(o: Result[Any, E, O], i: Assertion[I]): Expectation[S] = | ||
serviceMethod.apply(i, o) |
16 changes: 16 additions & 0 deletions
16
test-opinions/src/test/scala/test/opinions/AssertionsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.test.* | ||
import opinions.* | ||
|
||
object AssertionsSpec extends ZIOSpecDefault: | ||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("AssertionsSpec")( | ||
test("eqTo") { | ||
assertZIO(42.uio)(42.eqTo) | ||
}, | ||
test("neqTo") { | ||
assertZIO(42.uio)(69.neqTo) | ||
} | ||
) |
17 changes: 17 additions & 0 deletions
17
test-opinions/src/test/scala/test/opinions/ExpectationsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.test.* | ||
import zio.mock.* | ||
|
||
object ExpectationsSpec extends ZIOSpecDefault: | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("ExpectationsSpec")( | ||
test("expected") { | ||
assertZIO(42.expected.io(()))(42.eqTo) | ||
}, | ||
test("expectedF") { | ||
assertZIO(42.expectedF.io(()).flip)(42.eqTo) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.test.* | ||
import opinions.* | ||
import zio.test.magnolia.DeriveGen | ||
|
||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
object GensSpec extends ZIOSpecDefault: | ||
|
||
case class SomeModel(a: Int, b: String, c: Instant, d: UUID) | ||
given Gen[Any, SomeModel] = DeriveGen[SomeModel] | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("GensSpec")( | ||
test("Generate a random List of case class instances") { | ||
for { | ||
someNumber <- Random.nextIntBetween(10, 50) | ||
models <- GenRandom[SomeModel](someNumber) | ||
} yield assertTrue( | ||
models.length == someNumber, | ||
models.distinct.length == someNumber | ||
) | ||
}, | ||
test("Generate a random instance of a case class") { | ||
for { | ||
model <- GenRandom[SomeModel] | ||
} yield assertCompletes | ||
} | ||
) |
71 changes: 71 additions & 0 deletions
71
test-opinions/src/test/scala/test/opinions/MocksSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package test.opinions | ||
|
||
import zio.* | ||
import zio.test.* | ||
import zio.mock.* | ||
import opinions.* | ||
|
||
object MocksSpec extends ZIOSpecDefault: | ||
|
||
trait SomeService: | ||
def get(id: Int): Task[String] | ||
|
||
object SomeMockService extends Mock[SomeService]: | ||
object Get extends Effect[Int, Throwable, String] | ||
|
||
val compose: URLayer[Proxy, SomeService] = | ||
ZLayer { | ||
for { | ||
proxy <- ZIO.service[Proxy] | ||
} yield new SomeService: | ||
override def get(id: RuntimeFlags): Task[String] = proxy(Get, id) | ||
} | ||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("MocksSpec + ")( | ||
test("expectWhen of values") { | ||
for { | ||
result <- ZIO | ||
.serviceWithZIO[SomeService](_.get(42)) | ||
.provide(SomeMockService.Get.expectWhen("forty two", 42)) | ||
_ <- ZIO | ||
.serviceWithZIO[SomeService](_.get(42)) | ||
.provide( | ||
// Comparison without expectWhen extension method | ||
SomeMockService | ||
.Get(Assertion.equalTo(42), Expectation.value("forty two")) | ||
) | ||
} yield assertTrue(result == "forty two") | ||
}, | ||
test("expectWhenF of values") { | ||
for { | ||
error <- | ||
ZIO | ||
.serviceWithZIO[SomeService](_.get(42)) | ||
.flip | ||
.provide( | ||
SomeMockService.Get.expectWhenF(new Exception("boom"), 42) | ||
) | ||
} yield assertTrue(error.getMessage == "boom") | ||
}, | ||
test("expectWhen of assertions") { | ||
for { | ||
result <- | ||
ZIO | ||
.serviceWithZIO[SomeService](_.get(42)) | ||
.provide( | ||
SomeMockService.Get.expectWhen("forty two".expected, 42.eqTo) | ||
) | ||
error <- | ||
ZIO | ||
.serviceWithZIO[SomeService](_.get(42)) | ||
.flip | ||
.provide( | ||
SomeMockService.Get | ||
.expectWhen(new Exception("boom").expectedF, 42.eqTo) | ||
) | ||
} yield assertTrue( | ||
result == "forty two", | ||
error.getMessage == "boom" | ||
) | ||
} | ||
) |