kotlin.test.CollectionAssertions.kt Maven / Gradle / Ivy
@file:Suppress("DEPRECATION")
package kotlin.test
import java.util.*
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
class CollectionAssertionSession>(val collection: C)
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
inline fun > assert(collection: C, block: CollectionAssertionSession.() -> Unit) {
CollectionAssertionSession(collection).block()
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun > CollectionAssertionSession<*, C>.sizeShouldBe(expectedSize: Int, message: String? = null) {
assertEquals(expectedSize, collection.size, message ?: "collection should have size $expectedSize but it is ${collection.size}")
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun CollectionAssertionSession.elementAtShouldBe(position: Int, expected: T, message: String? = null) {
assertEquals(expected, collection.elementAt(position), message ?: "element at $position should be $expected")
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun > CollectionAssertionSession.elementAtShouldComply(position: Int, message: String? = null, predicate: (T) -> Boolean) {
assertTrue(message) { predicate(collection.elementAt(position)) }
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun CollectionAssertionSession.lastElementShouldBe(expected: T, message: String? = null) {
assertEquals(expected, collection.last(), message ?: "the last element should be $expected")
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun CollectionAssertionSession.containsAll(vararg elements: T) {
for (e in elements) {
assertTrue(message = "Element $e is missing in the collection") { e in collection }
}
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun > CollectionAssertionSession.shouldBe(expectedElements: Iterable, message: String? = null) {
val actual = collection.iterator()
val expected = expectedElements.iterator()
while (actual.hasNext() && expected.hasNext()) {
assertEquals(expected.next(), actual.next(), message)
}
if (actual.hasNext()) {
fail("Actual collection is longer than expected. Extra elements are: ${actual.remaining()}")
}
if (expected.hasNext()) {
fail("Actual collection is shorter than expected. Missing elements are: ${expected.remaining()}")
}
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun > CollectionAssertionSession.shouldBeSet(other: Set, message: String? = null) {
for (e in other) {
if (e !in collection) {
fail(message ?: "Element $e in not in the collection $collection")
}
}
for (e in collection) {
if (e !in other) {
fail(message ?: "Element $e is not expected")
}
}
}
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.")
fun > CollectionAssertionSession.shouldBeSet(vararg other: T) {
val otherSet = HashSet()
for (e in other) {
otherSet.add(e)
}
shouldBeSet(otherSet)
}
private operator fun Iterable.contains(e: T): Boolean {
if (this is Set) {
return contains(e)
}
for (it in this) {
if (it == e) {
return true
}
}
return false
}
private fun Iterable.last(): T {
if (this is List) {
if (this.isEmpty()) {
throw NoSuchElementException()
}
return this[size - 1]
}
val it = iterator()
var result: T = iterator().next()
while (it.hasNext()) {
result = it.next()
}
return result
}
private fun Iterable.elementAt(position: Int): T {
if (position < 0) {
throw IllegalArgumentException("position shouldn't be negative: $position")
}
if (this is List) {
return this[position]
}
val iterator = iterator()
var idx = 0
do {
if (!iterator.hasNext()) {
throw IndexOutOfBoundsException("index $position is out of the collection bounds [0; $idx)")
}
val result = iterator.next()
if (idx == position) {
return result
}
idx++
} while (true)
throw IllegalStateException()
}
private fun Iterator.remaining(): List {
val result = ArrayList()
while (hasNext()) {
result.add(next())
}
return result
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy