Merge pull request #442 from tkech17/shortcut_containsElementsOf

[#425] shortcut Iterable.containsElementsOf implemented
This commit is contained in:
Robert Stoll
2020-04-14 16:29:04 +02:00
committed by GitHub
4 changed files with 111 additions and 0 deletions

View File

@@ -198,6 +198,21 @@ fun <E : Any, T : Iterable<E?>> Expect<T>.containsExactly(
vararg otherAssertionCreatorsOrNulls: (Expect<E>.() -> Unit)?
): Expect<T> = contains.inOrder.only.entries(assertionCreatorOrNull, *otherAssertionCreatorsOrNulls)
/**
* Expects that the subject of the assertion (an [Iterable]) contains all elements of [expectedIterable].
*
* It is a shortcut for `contains.inAnyOrder.atLeast(1).elementsOf(expectedIterable)`
*
* @param expectedIterable The [Iterable] whose elements are expected to be contained within this [Iterable].
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
* @throws IllegalArgumentException in case the given [expectedIterable] does not have elements (is empty).
*
* @since 0.11.0
*/
inline fun <reified E, T : Iterable<E>> Expect<T>.containsElementsOf(expectedIterable: Iterable<E>): Expect<T> =
contains.inAnyOrder.atLeast(1).elementsOf(expectedIterable)
/**
* Expects that the subject of the assertion (an [Iterable]) does not contain the [expected] value

View File

@@ -2,11 +2,14 @@ package ch.tutteli.atrium.api.fluent.en_GB
import ch.tutteli.atrium.api.verbs.internal.expect
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.kbox.glue
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction2
class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
include(BuilderSpec)
include(ShortcutSpec)
describe("contains.inAnyOrder.atLeast(1).elementsOf") {
it("passing an empty iterable throws an IllegalArgumentException") {
@@ -15,6 +18,14 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
}.toThrow<IllegalArgumentException>()
}
}
describe("containsElementsOf") {
it("passing an empty iterable throws an IllegalArgumentException") {
expect {
expect(listOf(1, 2)).containsElementsOf(listOf())
}.toThrow<IllegalArgumentException>()
}
}
}) {
object BuilderSpec : ch.tutteli.atrium.specs.integration.IterableContainsInAnyOrderAtLeast1ValuesAssertionsSpec(
getContainsPair(),
@@ -23,6 +34,13 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
"[Atrium][Builder] "
)
object ShortcutSpec : ch.tutteli.atrium.specs.integration.IterableContainsInAnyOrderAtLeast1ValuesAssertionsSpec(
getContainsShortcutPair(),
getContainsNullableShortcutPair(),
"◆ ",
"[Atrium][Shortcut] "
)
companion object : IterableContainsSpecBase() {
fun getContainsPair() = "$contains.$inAnyOrder.$atLeast(1).$inAnyOrderElementsOf" to Companion::containsValues
@@ -41,5 +59,27 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
aX: Array<out Double?>
): Expect<Iterable<Double?>> = expect.contains.inAnyOrder.atLeast(1).elementsOf(listOf(a, *aX))
private val containsElementsOfShortcutFun: KFunction2<Expect<Iterable<Long>>, Iterable<Long>, Expect<Iterable<Long>>> =
Expect<Iterable<Long>>::containsElementsOf
private fun getContainsShortcutPair() = containsElementsOfShortcutFun.name to Companion::containsInAnyOrderShortcut
private fun containsInAnyOrderShortcut(
expect: Expect<Iterable<Double>>,
a: Double,
aX: Array<out Double>
): Expect<Iterable<Double>> = expect.containsElementsOf(a glue aX)
private val containsElementsOfNullableShortcutFun: KFunction2<Expect<Iterable<Double?>>, Iterable<Double?>, Expect<Iterable<Double?>>> =
Expect<Iterable<Double?>>::containsElementsOf
private fun getContainsNullableShortcutPair() = containsElementsOfNullableShortcutFun.name to Companion::containsInAnyOrderNullableShortcut;
private fun containsInAnyOrderNullableShortcut(
expect: Expect<Iterable<Double?>>,
a: Double?,
aX: Array<out Double?>
): Expect<Iterable<Double?>> = expect.containsElementsOf(a glue aX)
}
}

View File

@@ -233,6 +233,22 @@ infix fun <E : Any, T : Iterable<E?>> Expect<T>.containsExactly(
infix fun <E : Any, T : Iterable<E?>> Expect<T>.containsExactly(entries: Entries<E>): Expect<T> =
it contains o inGiven order and only the entries
/**
* Expects that the subject of the assertion (an [Iterable]) contains all elements of [expectedIterable].
*
* It is a shortcut for `contains.inAnyOrder.atLeast(1).elementsOf(expectedIterable)`
*
* @param expectedIterable The [Iterable] whose elements are expected to be contained within this [Iterable].
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
* @throws IllegalArgumentException in case the given [expectedIterable] does not have elements (is empty).
*
* @since 0.11.0
*/
inline infix fun <reified E, T : Iterable<E>> Expect<T>.containsElementsOf(expectedIterable: Iterable<E>): Expect<T> =
it contains o inAny order atLeast 1 elementsOf expectedIterable
/**
* Expects that the subject of the assertion (an [Iterable]) does not contain the [expected] value.
*

View File

@@ -2,11 +2,14 @@ package ch.tutteli.atrium.api.infix.en_GB
import ch.tutteli.atrium.api.verbs.internal.expect
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.kbox.glue
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.reflect.KFunction2
class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
include(BuilderSpec)
include(ShortcutSpec)
describe("contains o inAny order atLeast 1 elementsOf") {
it("passing an empty iterable throws an IllegalArgumentException") {
@@ -15,6 +18,14 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
}.toThrow<IllegalArgumentException>()
}
}
describe("contains all Elements at Least Once") {
it("passing an empty iterable throws an IllegalArgumentException") {
expect {
expect(listOf(1, 2)) containsElementsOf listOf()
}.toThrow<IllegalArgumentException>()
}
}
}) {
object BuilderSpec : ch.tutteli.atrium.specs.integration.IterableContainsInAnyOrderAtLeast1ValuesAssertionsSpec(
getContainsPair(),
@@ -23,6 +34,13 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
"[Atrium][Builder] "
)
object ShortcutSpec : ch.tutteli.atrium.specs.integration.IterableContainsInAnyOrderAtLeast1ValuesAssertionsSpec(
getContainsShortcutPair(),
getContainsNullableShortcutPair(),
"* ",
"[Atrium][Shortcut] "
)
companion object : IterableContainsSpecBase() {
fun getContainsPair() =
"$contains $filler $inAnyOrder $atLeast 1 $inAnyOrderElementsOf" to Companion::containsValues
@@ -42,5 +60,27 @@ class IterableContainsInAnyOrderAtLeast1ElementsOfAssertionsSpec : Spek({
aX: Array<out Double?>
): Expect<Iterable<Double?>> = expect contains o inAny order atLeast 1 elementsOf listOf(a, *aX)
private val containsElementsOfShortcutFun: KFunction2<Expect<Iterable<Long>>, Iterable<Long>, Expect<Iterable<Long>>> =
Expect<Iterable<Long>>::containsElementsOf
private fun getContainsShortcutPair() = containsElementsOfShortcutFun.name to Companion::containsInAnyOrderShortcut
private fun containsInAnyOrderShortcut(
expect: Expect<Iterable<Double>>,
a: Double,
aX: Array<out Double>
): Expect<Iterable<Double>> = expect containsElementsOf(a glue aX)
private val containsElementsOfNullableShortcutFun: KFunction2<Expect<Iterable<Double?>>, Iterable<Double?>, Expect<Iterable<Double?>>> =
Expect<Iterable<Double?>>::containsElementsOf
private fun getContainsNullableShortcutPair() = containsElementsOfNullableShortcutFun.name to Companion::containsInAnyOrderNullableShortcut;
private fun containsInAnyOrderNullableShortcut(
expect: Expect<Iterable<Double?>>,
a: Double?,
aX: Array<out Double?>
): Expect<Iterable<Double?>> = expect containsElementsOf(a glue aX)
}
}