All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.kotest.matchers.collections.singleton.kt Maven / Gradle / Ivy

package io.kotest.matchers.collections

/**
 * Verifies this collection contains only one element
 *
 * This assertion is an alias to `collection shouldHaveSize 1`. This will pass if the collection have exactly one element
 * (definition of a Singleton Collection)
 *
 * ```
 * listOf(1).shouldBeSingleton()    // Assertion passes
 * listOf(1, 2).shouldBeSingleton() // Assertion fails
 * ```
 *
 * @see [shouldHaveSize]
 * @see [shouldNotBeSingleton]
 * @see [shouldHaveSingleElement]
 */
fun  Collection.shouldBeSingleton(): Collection {
   this shouldHaveSize 1
   return this
}

fun  Iterable.shouldBeSingleton(): Iterable {
   toList().shouldBeSingleton()
   return this
}

fun  Array.shouldBeSingleton(): Array {
   asList().shouldBeSingleton()
   return this
}


/**
 * Verifies this collection doesn't contain only one element
 *
 * This assertion is an alias to `collection shouldNotHaveSize 1`. This will pass if the collection doesn't have exactly one element
 * (definition of a Singleton Collection)
 *
 * ```
 * listOf(1, 2).shouldNotBeSingleton()    // Assertion passes
 * listOf().shouldNotBeSingleton()   // Assertion passes
 * listOf(1).shouldNotBeSingleton()       // Assertion fails
 * ```
 *
 * @see [shouldNotHaveSize]
 * @see [shouldBeSingleton]
 * @see [shouldNotHaveSingleElement]
 */
fun  Collection.shouldNotBeSingleton(): Collection {
   this shouldNotHaveSize 1
   return this
}

fun  Iterable.shouldNotBeSingleton(): Iterable {
   toList().shouldNotBeSingleton()
   return this

}

fun  Array.shouldNotBeSingleton(): Array {
   asList().shouldNotBeSingleton()
   return this
}


/**
 * Verifies this collection contains only one element and executes the given lambda against that element.
 */
inline fun  Collection.shouldBeSingleton(fn: (T) -> Unit): Collection {
   this.shouldBeSingleton()
   fn(this.first())
   return this
}

/**
 * Verifies this collection contains only one element and executes the given lambda against that element.
 */
inline fun  Iterable.shouldBeSingleton(fn: (T) -> Unit): Iterable {
   toList().shouldBeSingleton(fn)
   return this
}

/**
 * Verifies this collection contains only one element and executes the given lambda against that element.
 */
inline fun  Array.shouldBeSingleton(fn: (T) -> Unit): Array {
   asList().shouldBeSingleton(fn)
   return this
}







© 2015 - 2025 Weber Informatics LLC | Privacy Policy