Rich assertion system with over 50 built-in assertions, logical combinators, and detailed failure reporting.
Basic assertion functions for testing values and effects.
/**
* Checks that a value satisfies an assertion
* @param value - The value to test (lazily evaluated)
* @param assertion - The assertion to check
* @returns TestResult indicating success or failure
*/
def assert[A](value: => A)(assertion: Assertion[A]): TestResult
/**
* Checks that an effect produces a value satisfying an assertion
* @param effect - ZIO effect to execute and test
* @param assertion - The assertion to check against the effect result
* @returns ZIO effect producing TestResult
*/
def assertM[R, E, A](effect: ZIO[R, E, A])(assertion: AssertionM[A]): ZIO[R, E, TestResult]
/**
* Checks that all provided boolean expressions are true
* @param exprs - Variable number of boolean expressions
* @returns TestResult for all expressions
*/
def assertTrue(exprs: Boolean*): TestResult
/**
* Assertion that a test completes successfully
*/
val assertCompletes: TestResult
/**
* Effectful assertion that a test completes successfully
*/
val assertCompletesM: UIO[TestResult]Core assertion type with logical combinators.
/**
* An assertion that can be checked against a value of type A
* @tparam A - The type of value this assertion can check
*/
case class Assertion[A](arrow: TestArrow[A, Boolean]) {
/** Evaluates the assertion against a value */
def run(actual: A): AssertResult
/** Logical AND - both assertions must pass */
def &&(that: Assertion[A]): Assertion[A]
/** Logical OR - either assertion must pass */
def ||(that: Assertion[A]): Assertion[A]
/** Logical NOT - assertion must fail */
def unary_!: Assertion[A]
/** Adds a descriptive label to the assertion */
def label(string: String): Assertion[A]
}Fundamental assertions for common testing scenarios.
/** Assertion that always succeeds */
val anything: Assertion[Any]
/** Assertion that always fails */
val nothing: Assertion[Any]
/** Checks that value is null */
val isNull: Assertion[Any]
/** Checks that value is not null */
val isNonNull: Assertion[Any]
/** Checks that value equals expected value */
def equalTo[A](expected: A): Assertion[A]
/** Negates another assertion */
def not[A](assertion: Assertion[A]): Assertion[A]
/** Checks that value is the unit value */
val isUnit: Assertion[Unit]Specific assertions for boolean values.
/** Checks that boolean value is true */
val isTrue: Assertion[Boolean]
/** Checks that boolean value is false */
val isFalse: Assertion[Boolean]Assertions for comparing numeric values.
/** Checks that value is greater than reference */
def isGreaterThan[A: Ordering](reference: A): Assertion[A]
/** Checks that value is greater than or equal to reference */
def isGreaterThanEqualTo[A: Ordering](reference: A): Assertion[A]
/** Checks that value is less than reference */
def isLessThan[A: Ordering](reference: A): Assertion[A]
/** Checks that value is less than or equal to reference */
def isLessThanEqualTo[A: Ordering](reference: A): Assertion[A]
/** Checks that value is within tolerance of reference */
def isWithin[A: Numeric](reference: A, tolerance: A): Assertion[A]
/** Checks that value approximately equals reference within tolerance */
def approximatelyEquals[A: Numeric](reference: A, tolerance: A): Assertion[A]
/** Checks that numeric value is positive */
def isPositive[A: Numeric]: Assertion[A]
/** Checks that numeric value is negative */
def isNegative[A: Numeric]: Assertion[A]
/** Checks that numeric value is zero */
def isZero[A: Numeric]: Assertion[A]
/** Checks that numeric value is not zero */
def isNonZero[A: Numeric]: Assertion[A]Specialized assertions for string values.
/** Case-insensitive string equality */
def equalToIgnoringCase(expected: String): Assertion[String]
/** Checks that string starts with prefix */
def startsWithString(prefix: String): Assertion[String]
/** Checks that string ends with suffix */
def endsWithString(suffix: String): Assertion[String]
/** Checks that string contains substring */
def containsString(element: String): Assertion[String]
/** Checks that string matches regular expression */
def matchesRegex(regex: String): Assertion[String]
/** Checks that string is empty */
val isEmptyString: Assertion[String]
/** Checks that string is not empty */
val isNonEmptyString: Assertion[String]Assertions for testing collections and iterables.
/** Checks that collection is empty */
val isEmpty: Assertion[Iterable[Any]]
/** Checks that collection is not empty */
val isNonEmpty: Assertion[Iterable[Any]]
/** Checks that collection size satisfies assertion */
def hasSize[A](assertion: Assertion[Int]): Assertion[Iterable[A]]
/** Checks that collection contains element */
def contains[A](element: A): Assertion[Iterable[A]]
/** Checks that collection contains all specified elements */
def containsAll[A](elements: A*): Assertion[Iterable[A]]
/** Checks that at least one element satisfies assertion */
def exists[A](assertion: Assertion[A]): Assertion[Iterable[A]]
/** Checks that all elements satisfy assertion */
def forall[A](assertion: Assertion[A]): Assertion[Iterable[A]]
/** Checks that collection has same elements as other (order independent) */
def hasSameElements[A](other: Iterable[A]): Assertion[Iterable[A]]
/** Checks that collection is subset of another */
def isSubsetOf[A](other: Iterable[A]): Assertion[Iterable[A]]
/** Checks that collection is superset of another */
def isSupersetOf[A](other: Iterable[A]): Assertion[Iterable[A]]
/** Checks that all elements in collection are distinct */
val isDistinct: Assertion[Iterable[Any]]
/** Checks that list is sorted according to ordering */
def isSorted[A: Ordering]: Assertion[List[A]]Assertions for testing Option values.
/** Checks that Option is Some and its value satisfies assertion */
def isSome[A](assertion: Assertion[A]): Assertion[Option[A]]
/** Checks that Option is None */
val isNone: Assertion[Option[Any]]
/** Checks that Option is defined (Some) */
val isDefined: Assertion[Option[Any]]Assertions for testing Either values.
/** Checks that Either is Left and its value satisfies assertion */
def isLeft[A](assertion: Assertion[A]): Assertion[Either[A, Any]]
/** Checks that Either is Right and its value satisfies assertion */
def isRight[A](assertion: Assertion[A]): Assertion[Either[Any, A]]Assertions for testing exceptions and ZIO effects.
/** Checks that expression throws exception satisfying assertion */
def throws[A](assertion: Assertion[Throwable]): Assertion[A]
/** Checks that expression throws specific exception type */
def throwsA[E <: Throwable]: Assertion[Any]
/** Checks that ZIO effect dies with throwable satisfying assertion */
def dies(assertion: Assertion[Throwable]): Assertion[ZIO[Any, Any, Any]]
/** Checks that ZIO effect fails with error satisfying assertion */
def fails[E](assertion: Assertion[E]): Assertion[ZIO[Any, E, Any]]
/** Checks that ZIO effect succeeds with value satisfying assertion */
def succeeds[A](assertion: Assertion[A]): Assertion[ZIO[Any, Any, A]]
/** Checks that ZIO effect is interrupted */
val isInterrupted: Assertion[ZIO[Any, Any, Any]]import zio.test._
import zio.test.Assertion._
// Basic assertions
test("equality") {
assert(2 + 2)(equalTo(4))
}
test("numeric comparisons") {
assert(42)(isGreaterThan(40) && isLessThan(50))
}
test("string operations") {
assert("Hello World")(
startsWithString("Hello") &&
endsWithString("World") &&
containsString(" ")
)
}
test("collection testing") {
assert(List(1, 2, 3, 2))(
hasSize(equalTo(4)) &&
contains(2) &&
!isDistinct
)
}
test("option testing") {
assert(Some(42))(isSome(isGreaterThan(40)))
assert(None)(isNone)
}
test("either testing") {
assert(Right(42))(isRight(equalTo(42)))
assert(Left("error"))(isLeft(equalToIgnoringCase("ERROR")))
}
testM("effect assertions") {
for {
_ <- assertM(ZIO.succeed(42))(equalTo(42))
_ <- assertM(ZIO.fail("boom"))(anything)
} yield assertCompletes
}