mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-10 08:31:29 +00:00
Drop deprecated CollectionAssertions.kt
Remove incorrect import from callByWithEmptyVarArg test
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.full.*
|
||||
import kotlin.test.assert
|
||||
|
||||
annotation class Foo(vararg val strings: String)
|
||||
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION_ERROR")
|
||||
package kotlin.test
|
||||
|
||||
import java.util.*
|
||||
|
||||
// TODO: Drop entirely in 1.4
|
||||
|
||||
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.", level = DeprecationLevel.ERROR)
|
||||
class CollectionAssertionSession<E, C: Iterable<E>>(val collection: C)
|
||||
|
||||
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.", level = DeprecationLevel.ERROR)
|
||||
inline fun <E, C: Iterable<E>> assert(collection: C, block: CollectionAssertionSession<E, C>.() -> Unit) {
|
||||
CollectionAssertionSession(collection).block()
|
||||
}
|
||||
|
||||
@Deprecated("This is an experimental part of the API. It may be changed or removed in newer releases.", level = DeprecationLevel.ERROR)
|
||||
fun <C: Collection<*>> 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.", level = DeprecationLevel.ERROR)
|
||||
fun <T> CollectionAssertionSession<T, *>.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.", level = DeprecationLevel.ERROR)
|
||||
fun <T, C: Iterable<T>> CollectionAssertionSession<T, C>.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.", level = DeprecationLevel.ERROR)
|
||||
fun <T> CollectionAssertionSession<T, *>.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.", level = DeprecationLevel.ERROR)
|
||||
fun <T> CollectionAssertionSession<T, *>.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.", level = DeprecationLevel.ERROR)
|
||||
fun <T, C: Iterable<T>> CollectionAssertionSession<T, C>.shouldBe(expectedElements: Iterable<T>, 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.", level = DeprecationLevel.ERROR)
|
||||
fun <T, C: Set<T>> CollectionAssertionSession<T, C>.shouldBeSet(other: Set<T>, 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.", level = DeprecationLevel.ERROR)
|
||||
fun <T, C: Set<T>> CollectionAssertionSession<T, C>.shouldBeSet(vararg other: T) {
|
||||
val otherSet = HashSet<T>()
|
||||
for (e in other) {
|
||||
otherSet.add(e)
|
||||
}
|
||||
|
||||
shouldBeSet(otherSet)
|
||||
}
|
||||
|
||||
private fun <T> Iterator<T>.remaining(): List<T> {
|
||||
val result = ArrayList<T>()
|
||||
while (hasNext()) {
|
||||
result.add(next())
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION_ERROR")
|
||||
|
||||
package kotlin.test.tests
|
||||
|
||||
import org.junit.Assert
|
||||
import java.util.*
|
||||
import kotlin.test.*
|
||||
|
||||
class CollectionAssertionTest {
|
||||
@Test
|
||||
fun testList() {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
sizeShouldBe(3)
|
||||
elementAtShouldBe(0, 1)
|
||||
elementAtShouldComply(0) { it > 0 }
|
||||
lastElementShouldBe(3)
|
||||
containsAll(1, 2)
|
||||
|
||||
shouldBe(listOf(1, 2, 3))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSet() {
|
||||
assert(setOf(1, 2, 3)) {
|
||||
sizeShouldBe(3)
|
||||
elementAtShouldBe(0, 1)
|
||||
elementAtShouldComply(0) { it > 0 }
|
||||
lastElementShouldBe(3)
|
||||
containsAll(1, 2)
|
||||
|
||||
shouldBeSet(setOf(1, 2, 3))
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testSizeShouldBeFails() {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
sizeShouldBe(1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testElementAtShouldBeFail() {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
elementAtShouldBe(0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testElementAtShouldComplyFail() {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
elementAtShouldComply(0) { it < 0 }
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testLastElementFail() {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
lastElementShouldBe(0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException::class)
|
||||
fun testLastElementOnEmptyFail() {
|
||||
assert(listOf<Int>()) {
|
||||
lastElementShouldBe(0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testContainsAll() {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
containsAll(1, 8)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testContainsAllWithSet() {
|
||||
assert(setOf(1, 2, 3)) {
|
||||
containsAll(1, 8)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShouldBeLess() {
|
||||
try {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
shouldBe(listOf(1, 2, 3, 4))
|
||||
}
|
||||
Assert.fail("It shouldn't pass here")
|
||||
} catch (e: AssertionError) {
|
||||
assertTrue { "[4]" in e.message!! }
|
||||
assertTrue { "shorter" in e.message!! }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShouldBeLonger() {
|
||||
try {
|
||||
assert(listOf(1, 2, 3)) {
|
||||
shouldBe(listOf(1, 2))
|
||||
}
|
||||
Assert.fail("It shouldn't pass here")
|
||||
} catch (e: AssertionError) {
|
||||
assertTrue { "[3]" in e.message!! }
|
||||
assertTrue { "longer" in e.message!! }
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testShouldBeSetExtra() {
|
||||
assert(setOf(1, 2, 3)) {
|
||||
shouldBeSet(setOf(1, 2))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShouldBeSetExact() {
|
||||
assert(setOf(1, 2, 3)) {
|
||||
shouldBeSet(setOf(1, 2, 3))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShouldBeSetExactVararg() {
|
||||
assert(setOf(1, 2, 3)) {
|
||||
shouldBeSet(1, 2, 3)
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError::class)
|
||||
fun testShouldBeSetMissing() {
|
||||
assert(setOf(1, 2, 3)) {
|
||||
shouldBeSet(setOf(1, 2, 3, 4))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user