Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package zio.test

import zio._
import zio.clock.Clock
import zio.test.Assertion.{ equalTo, isGreaterThan, isLessThan }
import zio.test.environment._
import zio.test.Assertion._
import zio.test.ReportingTestUtils._
import zio.test.TestUtils.label
import zio.test.environment._

import scala.concurrent.Future

Expand All @@ -18,7 +18,8 @@ object DefaultTestReporterSpec extends AsyncBaseSpec {
label(reportSuite1, "correctly reports successful test suite"),
label(reportSuite2, "correctly reports failed test suite"),
label(reportSuites, "correctly reports multiple test suites"),
label(simpleAssertion, "correctly reports failure of simple assertion")
label(simpleAssertion, "correctly reports failure of simple assertion"),
label(multipleNestedFailures, "correctly reports multiple nested failures")
)

def makeTest[L](label: L)(assertion: => TestResult): ZSpec[Any, Nothing, L, Unit] =
Expand All @@ -42,14 +43,14 @@ object DefaultTestReporterSpec extends AsyncBaseSpec {

val test3Expected = Vector(
expectedFailure("Value falls within range"),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("equalTo(42)")}\n"),
withOffset(2)(
s"${blue("52")} did not satisfy ${cyan("(" + yellowThenCyan("equalTo(42)") + " || (isGreaterThan(5) && isLessThan(10)))")}\n"
),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("equalTo(42)")}\n"),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("isLessThan(10)")}\n"),
withOffset(2)(
s"${blue("52")} did not satisfy ${cyan("(equalTo(42) || (isGreaterThan(5) && " + yellowThenCyan("isLessThan(10)") + "))")}\n"
),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("isLessThan(10)")}\n")
)
)

val test4 = Spec.test("Failing test", fail(Cause.fail("Fail")))
Expand All @@ -71,6 +72,21 @@ object DefaultTestReporterSpec extends AsyncBaseSpec {
withOffset(2)(s"${blue("2")} did not satisfy ${cyan("equalTo(3)")}\n")
)

val test6 = makeTest("Multiple nested failures") {
assert(Right(Some(3)), isRight(isSome(isGreaterThan(4))))
}

val test6Expected = Vector(
expectedFailure("Multiple nested failures"),
withOffset(2)(s"${blue("3")} did not satisfy ${cyan("isGreaterThan(4)")}\n"),
withOffset(2)(
s"${blue("Some(3)")} did not satisfy ${cyan("isSome(" + yellowThenCyan("isGreaterThan(4)") + ")")}\n"
),
withOffset(2)(
s"${blue("Right(Some(3))")} did not satisfy ${cyan("isRight(" + yellowThenCyan("isSome(isGreaterThan(4))") + ")")}\n"
)
)

val suite1 = suite("Suite1")(test1, test2)

val suite1Expected = Vector(
Expand Down Expand Up @@ -119,6 +135,9 @@ object DefaultTestReporterSpec extends AsyncBaseSpec {
def simpleAssertion =
check(test5, test5Expected :+ reportStats(0, 0, 1))

def multipleNestedFailures =
check(test6, test6Expected :+ reportStats(0, 0, 1))

def check[E](spec: ZSpec[TestEnvironment, String, String, Unit], expected: Vector[String]): Future[Boolean] =
unsafeRunWith(testEnvironmentManaged) { r =>
val zio = for {
Expand Down
12 changes: 6 additions & 6 deletions test-tests/shared/src/main/scala/zio/test/GenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,10 @@ object GenSpec extends AsyncBaseSpec {
val property = checkSome(gen)(100)(test).map { result =>
result.failures.fold(false) {
case BoolAlgebra.Value(failureDetails) =>
failureDetails.fragment.value.toString == "(List(0),List(1))" ||
failureDetails.fragment.value.toString == "(List(1),List(0))" ||
failureDetails.fragment.value.toString == "(List(0),List(-1))" ||
failureDetails.fragment.value.toString == "(List(-1),List(0))"
failureDetails.assertion.head.value.toString == "(List(0),List(1))" ||
failureDetails.assertion.head.value.toString == "(List(1),List(0))" ||
failureDetails.assertion.head.value.toString == "(List(0),List(-1))" ||
failureDetails.assertion.head.value.toString == "(List(-1),List(0))"
case _ => false
}
}
Expand All @@ -433,7 +433,7 @@ object GenSpec extends AsyncBaseSpec {
val property = checkSome(gen)(100)(test).map { result =>
result.failures.fold(false) {
case BoolAlgebra.Value(failureDetails) =>
failureDetails.fragment.value.toString == "List(0)"
failureDetails.assertion.head.value.toString == "List(0)"
case _ => false
}
}
Expand All @@ -449,7 +449,7 @@ object GenSpec extends AsyncBaseSpec {
val property = checkSome(gen)(100)(test).map { result =>
result.failures.fold(false) {
case BoolAlgebra.Value(failureDetails) =>
failureDetails.fragment.value.toString == "1"
failureDetails.assertion.head.value.toString == "1"
case _ => false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ object SummaryBuilderSpec extends AsyncBaseSpec {

val test3Expected = Vector(
expectedFailure("Value falls within range"),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("equalTo(42)")}\n"),
withOffset(2)(
s"${blue("52")} did not satisfy ${cyan("(" + yellowThenCyan("equalTo(42)") + " || (isGreaterThan(5) && isLessThan(10)))")}\n"
),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("equalTo(42)")}\n"),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("isLessThan(10)")}\n"),
withOffset(2)(
s"${blue("52")} did not satisfy ${cyan("(equalTo(42) || (isGreaterThan(5) && " + yellowThenCyan("isLessThan(10)") + "))")}\n"
),
withOffset(2)(s"${blue("52")} did not satisfy ${cyan("isLessThan(10)")}\n")
)
)

val test4 = Spec.test("Failing test", fail(Cause.fail("Fail")))
Expand Down
6 changes: 6 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/AssertionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ object AssertionSpec
test("exists must fail when all elements of iterable do not satisfy specified assertion") {
assert(Seq(1, 42, 5), exists(equalTo(0)))
} @@ failure,
test("exists must fail when iterable is empty") {
assert(Seq(), exists(hasField[String, Int]("length", _.length, isWithin(0, 3))))
} @@ failure,
test("fails must succeed when error value satisfy specified assertion") {
assert(Exit.fail("Some Error"), fails(equalTo("Some Error")))
},
Expand All @@ -59,6 +62,9 @@ object AssertionSpec
test("forall must fail when one element of iterable do not satisfy specified assertion") {
assert(Seq("a", "bb", "dddd"), forall(hasField[String, Int]("length", _.length, isWithin(0, 3))))
} @@ failure,
test("forall must succeed when an iterable is empty") {
assert(Seq(), forall(hasField[String, Int]("length", _.length, isWithin(0, 3))))
},
test("hasField must succeed when field value satisfy specified assertion") {
assert(SampleUser("User", 23), hasField[SampleUser, Int]("age", _.age, isWithin(0, 99)))
},
Expand Down
18 changes: 18 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/BoolAlgebraSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ object BoolAlgebraSpec
val expected = BoolAlgebra.success("first") && BoolAlgebra.failure("first") && BoolAlgebra.failure("second")
assert(actual, equalTo(expected))
},
testM("monad left identity") {
zio.test.check(boolAlgebra) { a =>
assert(a.flatMap(BoolAlgebra.success), equalTo(a))
}
},
testM("monad right identity") {
val genInt = Gen.int(0, 9)
val genFunction = Gen.function[Random with Sized, Int, BoolAlgebra[Int]](boolAlgebra)
zio.test.check(genInt, genFunction) { (a, f) =>
assert(BoolAlgebra.success(a).flatMap(f), equalTo(f(a)))
}
},
testM("monad associativity") {
val genFunction = Gen.function[Random with Sized, Int, BoolAlgebra[Int]](boolAlgebra)
zio.test.check(boolAlgebra, genFunction, genFunction) { (a, f, g) =>
assert(a.flatMap(f).flatMap(g), equalTo(a.flatMap(n => f(n).flatMap(g))))
}
},
testM("or distributes over and") {
zio.test.check(boolAlgebra, boolAlgebra, boolAlgebra) { (a, b, c) =>
val left = a || (b && c)
Expand Down
Loading