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

.kotlin.kotlin-compiler-embeddable.1.1.0.source-code.CollectionAssertions.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@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)
}

private fun  Iterator.remaining(): List {
    val result = ArrayList()
    while (hasNext()) {
        result.add(next())
    }
    return result
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy