In package strikt.api

class strikt.api.Assertion.Builder

Used to construct assertions.

Methods

open fun assert(description: String, assert: AtomicAssertion.(T)->Unit): Builder

Evaluates a condition that may pass or fail.

While this method can be used directly in a test but is typically used inside an extension method on Assertion.Builder such as those provided in the strikt.assertions package.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • assert: AtomicAssertion.(T)->Unit

    the assertion implementation that should result in a call to Assertion.pass or Assertion.fail.

abstract fun assert(description: String, expected: Any?, assert: AtomicAssertion.(T)->Unit): Builder

Evaluates a condition that may pass or fail.

While this method can be used directly in a test but is typically used inside an extension method on Assertion.Builder such as those provided in the strikt.assertions package.

Parameters:
  • description: String

    a description for the condition the assertion evaluates. The description may contain a String.format style placeholder for the expected value.

  • expected: Any?

    the expected value of a comparison.

  • assert: AtomicAssertion.(T)->Unit

    the assertion implementation that should result in a call to Assertion.pass or Assertion.fail.

abstract fun compose(description: String, expected: Any?, assertions: Builder.(T)->Unit): CompoundAssertions

Allows an assertion to be composed of multiple sub-assertions such as on fields of an object or elements of a collection.

The results of assertions made inside the assertions block are included under the overall assertion result.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • expected: Any?

    the expected value of a comparison.

  • assertions: Builder.(T)->Unit

    a group of assertions that will be evaluated against the subject.

open fun compose(description: String, assertions: Builder.(T)->Unit): CompoundAssertions

Allows an assertion to be composed of multiple sub-assertions such as on fields of an object or elements of a collection.

The results of assertions made inside the assertions block are included under the overall assertion result.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • assertions: Builder.(T)->Unit

    a group of assertions that will be evaluated against the subject.

open fun passesIf(description: String, assert: (T)->Boolean): Builder

Evaluates a boolean condition. This is useful for implementing the simplest types of assertion function.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • assert: (T)->Boolean

    a function that returns true (the assertion passes) or false (the assertion fails).

open fun passesIf(description: String, expected: Any?, assert: (T)->Boolean): Builder

Evaluates a boolean condition. This is useful for implementing the simplest types of assertion function.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • expected: Any?

    the expected value of a comparison.

  • assert: (T)->Boolean

    a function that returns true (the assertion passes) or false (the assertion fails).

open fun assertThat(description: String, assert: (T)->Boolean): Builder

Evaluates a boolean condition. This is useful for implementing the simplest types of assertion function.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • assert: (T)->Boolean

    a function that returns true (the assertion passes) or false (the assertion fails).

open fun assertThat(description: String, expected: Any?, assert: (T)->Boolean): Builder

Evaluates a boolean condition. This is useful for implementing the simplest types of assertion function.

Parameters:
  • description: String

    a description for the condition the assertion evaluates.

  • expected: Any?

    the expected value of a comparison.

  • assert: (T)->Boolean

    a function that returns true (the assertion passes) or false (the assertion fails).

open fun <R> get(function: T.()->R): DescribeableBuilder

Maps the assertion subject to the result of function. This is useful for chaining to property values or method call results on the subject.

If function is a callable reference, (for example a getter or property reference) the subject description will be automatically determined for the returned assertion builder.

If function is a lambda, Strikt will make a best-effort attempt to determine an appropriate function / property name.

Parameters:
  • function: T.()->R

    a lambda whose receiver is the current assertion subject.

abstract fun <R> get(description: String, function: T.()->R): DescribeableBuilder

Maps the assertion subject to the result of function. This is useful for chaining to property values or method call results on the subject.

Parameters:
  • description: String

    a description of the mapped result.

  • function: T.()->R

    a lambda whose receiver is the current assertion subject.

open fun <R> chain(function: (T)->R): DescribeableBuilder

Deprecated form of get((T) -> R).

Parameters:
  • function: (T)->R
open fun <R> chain(description: String, function: (T)->R): DescribeableBuilder

Deprecated form of get(String, (T) -> R).

Parameters:
  • description: String
  • function: (T)->R
abstract fun not(): Builder

Reverses any assertions chained after this method.

Parameters:
  • No parameters
abstract infix fun and(assertions: Builder.()->Unit): Builder

Evaluates a block of assertions on the current subject.

The main use for this method is after strikt.assertions.isNotNull or strikt.assertions.isA in order that a group of assertions can more conveniently be performed on the narrowed subject type.

This method may be used as an infix function which tends to enhance readability when it directly follows a lambda.

Parameters:

Extensions

fun <T> Builder.isNull(): Builder

Asserts that the subject is null.

Parameters:
  • No parameters
fun <T> Builder.isNotNull(): Builder

Asserts that the subject is not null.

Parameters:
  • No parameters
inline fun <T> Builder.isA(): Builder

Asserts that the subject is an instance of T.

Parameters:
  • No parameters
fun <T> Builder.isEqualTo(expected: T?): Builder

Asserts that the subject is equal to expected according to the standard Kotlin == operator.

Parameters:
  • expected: T?

    the expected value.

fun Builder.isEqualTo(expected: Double, tolerance: Double): Builder

Asserts that the subject is within tolerance of expected.

Parameters:
  • expected: Double
  • tolerance: Double
fun Builder.isEqualTo(expected: Float, tolerance: Double): Builder

Asserts that the subject is within tolerance of expected.

Parameters:
  • expected: Float
  • tolerance: Double
fun <T> Builder.isNotEqualTo(expected: T?): Builder

Asserts that the subject is not equal to expected according to the standard Kotlin == operator.

Parameters:
  • expected: T?

    the expected value.

fun <T> Builder.isSameInstanceAs(expected: Any?): Builder

Asserts that the subject is the same instance as expected according to the standard Kotlin === operator.

Parameters:
  • expected: Any?

    the expected instance.

fun <T> Builder.isNotSameInstanceAs(expected: Any?): Builder

Asserts that the subject is not the same instance as expected according to the standard Kotlin === operator.

Parameters:
  • expected: Any?

    the expected instance.

fun <T : Any> Builder.propertiesAreEqualTo(other: T): Builder

Asserts that all properties of the subject match those of other according to either contentEquals in the case of array properties or isEqualTo in other cases.

Properties are identified using Java beans conventions.

Parameters:
  • other: T
fun <T> Builder.contentEquals(other: Array): Builder

Asserts that the subject's content is equal to that of other according to Array.contentEquals.

Parameters:
  • other: Array
fun Builder.contentEquals(other: BooleanArray): Builder

Asserts that the subject's content is equal to that of other according to BooleanArray.contentEquals.

Parameters:
  • other: BooleanArray
fun Builder.contentEquals(other: ByteArray): Builder

Asserts that the subject's content is equal to that of other according to ByteArray.contentEquals.

Parameters:
  • other: ByteArray
fun Builder.contentEquals(other: ShortArray): Builder

Asserts that the subject's content is equal to that of other according to ShortArray.contentEquals.

Parameters:
  • other: ShortArray
fun Builder.contentEquals(other: IntArray): Builder

Asserts that the subject's content is equal to that of other according to IntArray.contentEquals.

Parameters:
  • other: IntArray
fun Builder.contentEquals(other: LongArray): Builder

Asserts that the subject's content is equal to that of other according to LongArray.contentEquals.

Parameters:
  • other: LongArray
fun Builder.contentEquals(other: FloatArray): Builder

Asserts that the subject's content is equal to that of other according to FloatArray.contentEquals.

Parameters:
  • other: FloatArray
fun Builder.contentEquals(other: DoubleArray): Builder

Asserts that the subject's content is equal to that of other according to DoubleArray.contentEquals.

Parameters:
  • other: DoubleArray
fun Builder.contentEquals(other: CharArray): Builder

Asserts that the subject's content is equal to that of other according to CharArray.contentEquals.

Parameters:
  • other: CharArray
fun <T> Builder.toList(): Builder

Maps an array to a list to make it possible to use the iterable matchers

Parameters:
  • No parameters
fun <T : Boolean?> Builder.isTrue(): Builder

Asserts that the subject is not null and is the boolean value true.

Parameters:
  • No parameters
fun <T : Boolean?> Builder.isFalse(): Builder

Asserts that the subject is not null and is the boolean value false.

Parameters:
  • No parameters
fun <T : CharSequence> Builder.hasLength(expected: Int): Builder

Asserts that the subject has a CharSequence.length of exactly expected.

Parameters:
  • expected: Int
fun <T : CharSequence> Builder.isLowerCase(): Builder

Asserts that the subject is composed of all lower-case characters.

Parameters:
  • No parameters
fun <T : CharSequence> Builder.isUpperCase(): Builder

Asserts that the subject is composed of all upper-case characters.

Parameters:
  • No parameters
fun <T : CharSequence> Builder.startsWith(expected: Char): Builder

Asserts that the subject starts with the expected character.

Parameters:
  • expected: Char
fun <T : CharSequence> Builder.startsWith(expected: CharSequence): Builder

Asserts that the subject starts with the expected string.

Parameters:
  • expected: CharSequence
fun <T : CharSequence> Builder.endsWith(expected: CharSequence): Builder

Asserts that the subject ends with the expected string.

Parameters:
  • expected: CharSequence
fun <T : CharSequence> Builder.matches(expected: ): Builder

Asserts that the subject is a full match for the expected regular expression.

Parameters:
  • expected:
fun <T : CharSequence> Builder.matchesIgnoringCase(expected: ): Builder

Asserts that the subject is a full match for the expected regular expression regardless of case.

Parameters:
  • expected:
fun <T : CharSequence> Builder.contains(expected: ): Builder

Asserts that the subject contains a match for the expected regular expression.

Parameters:
  • expected:
fun <T : CharSequence> Builder.contains(expected: CharSequence): Builder

Asserts that the subject contains the expected substring.

Parameters:
  • expected: CharSequence
fun <T : E, E> Builder.contains(elements: E): Builder

Asserts that all elements are present in the subject. The elements may exist in any order any number of times and the subject may contain further elements that were not specified. If either the subject or elements are empty the assertion always fails.

Parameters:
  • elements: E
fun <T : E, E> Builder.contains(elements: Collection): Builder

Asserts that all elements are present in the subject. The elements may exist in any order any number of times and the subject may contain further elements that were not specified. If either the subject or elements are empty the assertion always fails.

Parameters:
  • elements: Collection
fun <T : CharSequence> Builder.containsIgnoringCase(expected: ): Builder

Asserts that the subject contains a match for the expected regular expression regardless of case.

Parameters:
  • expected:
fun <T : CharSequence> Builder.containsIgnoringCase(expected: CharSequence): Builder

Asserts that the subject contains the expected substring regardless of case.

Parameters:
  • expected: CharSequence
fun <T : CharSequence?> Builder.isNullOrEmpty(): Builder

Asserts that the subject is null or the empty string.

Parameters:
  • No parameters
fun <T : CharSequence?> Builder.isNullOrBlank(): Builder

Asserts that the subject is null, empty, or contains only whitespace characters.

Parameters:
  • No parameters
fun <T : CharSequence> Builder.isEmpty(): Builder

Asserts that the subject is the empty string.

Parameters:
  • No parameters
fun <T : CharSequence> Builder.isBlank(): Builder

Asserts that the subject is empty, or contains only whitespace characters.

Parameters:
  • No parameters
fun <T : CharSequence> Builder.isNotEmpty(): Builder

Asserts that the subject is not the empty string (contains at least one character).

Parameters:
  • No parameters
fun <T : CharSequence> Builder.isNotBlank(): Builder

Asserts that the subject is not blank (contains at least one non-whitespace character).

Parameters:
  • No parameters
fun <T : CharSequence> Builder.trim(): Builder

Trims the subject CharSequence.

Parameters:
  • No parameters
fun Builder.trim(): Builder

Trims the subject string.

Parameters:
  • No parameters
fun <T : E, E> Builder.hasSize(expected: Int): Builder

Asserts that the subject has a Collection.size of exactly expected.

Parameters:
  • expected: Int
fun <T : E, E> Builder.isSorted(comparator: ): Builder

Asserts that the subject collection is sorted according to the Comparator. Empty collections are considered sorted.

Parameters:
  • comparator:
fun <T : T> Builder.isGreaterThan(expected: T): Builder

Asserts that the subject compares greater than expected according to Kotlin's standard > operator.

Parameters:
  • expected: T
fun <T : T> Builder.isLessThan(expected: T): Builder

Asserts that the subject compares less than expected according to Kotlin's standard < operator.

Parameters:
  • expected: T
fun <T : T> Builder.isGreaterThanOrEqualTo(expected: T): Builder

Asserts that the subject compares greater than or equal to expected according to Kotlin's standard >= operator.

Parameters:
  • expected: T
fun <T : T> Builder.isLessThanOrEqualTo(expected: T): Builder

Asserts that the subject compares less than or equal to expected according to Kotlin's standard <= operator.

Parameters:
  • expected: T
fun <T : T> Builder.isIn(expected: ClosedRange): Builder

Asserts that the subject is in the expected range.

Parameters:
  • expected: ClosedRange
fun <T : E, E, R> Builder.map(function: (E)->R): Builder

Applies Iterable.map with function to the subject and returns an assertion builder wrapping the result.

Parameters:
  • function: (E)->R
fun <T : E, E> Builder.first(): Builder

Maps this assertion to an assertion over the first element in the subject iterable.

Parameters:
  • No parameters
fun <T : E, E> Builder.first(predicate: (E)->Boolean): Builder

Maps this assertion to an assertion over the first element in the subject iterable that matches predicate.

Parameters:
  • predicate: (E)->Boolean
fun <T : E, E> Builder.single(): Builder

Maps this assertion to an assertion over the single element in the subject iterable.

Parameters:
  • No parameters
fun <T : E, E> Builder.last(): Builder

Maps this assertion to an assertion over the last element in the subject iterable.

Parameters:
  • No parameters
fun <T : E, E, R> Builder.flatMap(transform: (E)->Iterable): Builder

Maps this assertion to an assertion over a flattened list of the results of transform for each element in the subject iterable.

Parameters:
  • transform: (E)->Iterable
fun <T : E, E> Builder.all(predicate: Builder.()->Unit): Builder

Asserts that all elements of the subject pass the assertions in predicate.

Parameters:
fun <T : E, E> Builder.any(predicate: Builder.()->Unit): Builder

Asserts that at least one element of the subject pass the assertions in predicate.

Parameters:
fun <T : E, E> Builder.none(predicate: Builder.()->Unit): Builder

Asserts that no elements of the subject pass the assertions in predicate.

Parameters:
fun <T : E, E> Builder.doesNotContain(elements: E): Builder

Asserts that none of elements are present in the subject.

If elements is empty the assertion always fails. If the subject is empty the assertion always passe.

Parameters:
  • elements: E
fun <T : E, E> Builder.doesNotContain(elements: Collection): Builder

Asserts that none of elements are present in the subject.

If elements is empty the assertion always fails. If the subject is empty the assertion always passe.

Parameters:
  • elements: Collection
fun <T : E, E> Builder.containsExactly(elements: E): Builder

Asserts that all elements and no others are present in the subject in the specified order.

If the subject has no guaranteed iteration order (for example a Set) this assertion is probably not appropriate and you should use containsExactlyInAnyOrder instead.

Parameters:
  • elements: E
fun <T : E, E> Builder.containsExactly(elements: Collection): Builder

Asserts that all elements and no others are present in the subject in the specified order.

If the subject has no guaranteed iteration order (for example a Set) this assertion is probably not appropriate and you should use containsExactlyInAnyOrder instead.

Parameters:
  • elements: Collection
fun <T : E, E> Builder.containsExactlyInAnyOrder(elements: E): Builder

Asserts that all elements and no others are present in the subject. Order is not evaluated, so an assertion on a List will pass so long as it contains all the same elements with the same cardinality as elements regardless of what order they appear in.

Parameters:
  • elements: E
fun <T : E, E> Builder.containsExactlyInAnyOrder(elements: Collection): Builder

Asserts that all elements and no others are present in the subject. Order is not evaluated, so an assertion on a List will pass so long as it contains all the same elements with the same cardinality as elements regardless of what order they appear in.

Parameters:
  • elements: Collection
operator fun <T : E, E> Builder.get(i: Int): Builder

Maps this assertion to an assertion on the element at index i in the subject list.

Parameters:
  • i: Int
operator fun <T : E, E> Builder.get(range: IntRange): Builder

Maps this assertion to an assertion on the elements at the sub-list represented by range in the subject list.

Parameters:
  • range: IntRange
operator fun <T : Map, K, V> Builder.get(key: K): Builder

Maps this assertion to an assertion on the value indexed by key in the subject list.

Parameters:
  • key: K
fun <T : Map, K, V> Builder.containsKey(key: K): Builder

Asserts that the subject map contains an entry indexed by key. Depending on the map implementation the value associated with key may be null. This assertion just tests for the existence of the key.

Parameters:
  • key: K
fun <T : Map, K, V> Builder.containsKeys(keys: K): Builder

Asserts that the subject map contains entries for all keys.

Parameters:
  • keys: K
fun <T : Map, K, V> Builder.hasEntry(key: K, value: V): Builder

Asserts that the subject map contains an entry indexed by key with a value equal to value.

Parameters:
  • key: K
  • value: V
fun Builder.isEqualToIgnoringCase(expected: String): Builder

Asserts that the subject is equal to the expected value regardless of case.

Parameters:
  • expected: String
inline fun <E : Throwable> Builder.throws(): Builder

Asserts that an exception is an instance of the expected type. The assertion fails if the subject is null or not an instance of E.

This assertion is designed for use with the strikt.api.catching function.

Parameters:
  • No parameters
fun <T : TemporalAccessor> Builder.isBefore(expected: TemporalAccessor): Builder

Asserts that the subject is before expected.

Parameters:
  • expected: TemporalAccessor
fun <T : TemporalAccessor> Builder.isAfter(expected: TemporalAccessor): Builder

Asserts that the subject is after expected.

Parameters:
  • expected: TemporalAccessor
fun <T : TemporalAccessor> Builder.get(field: TemporalField): Builder

Maps an assertion on the subject to an assertion on the value of the specified temporal field.

Parameters:
  • field: TemporalField
fun <T : TemporalAccessor> Builder.getLong(field: TemporalField): Builder

Maps an assertion on the subject to an assertion on the value of the specified temporal field.

Parameters:
  • field: TemporalField
fun Builder.isEmpty()

Asserts that a com.google.protobuf.Any is empty and does not contain an object of any kind.

Parameters:
  • No parameters
inline fun <T> Builder.unpacksTo(): Builder

Asserts that the subject is a message of type T.

Parameters:
  • No parameters
inline fun <T> Builder.unpack(): Builder

Maps an assertion on com.google.protobuf.Any to an assertion on an unpacked message of type T.

Parameters:
  • No parameters