Skip to content

Commit

Permalink
Fix shouldContainAnyOf matchers with varargs (#3733)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshired committed Oct 11, 2023
1 parent ad53f1c commit 85c4c40
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ infix fun <T> List<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = th



fun <T> Iterable<T>.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(ts)
fun <T> Array<T>.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(ts)
fun <T> Iterable<T>.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(*ts)
fun <T> Array<T>.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(*ts)
fun <T> Collection<T>.shouldContainAnyOf(vararg ts: T) = this should containAnyOf(ts.asList())
fun <T> Iterable<T>.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(ts)
fun <T> Array<T>.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(ts)
fun <T> Iterable<T>.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(*ts)
fun <T> Array<T>.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(*ts)
fun <T> Collection<T>.shouldNotContainAnyOf(vararg ts: T) = this shouldNot containAnyOf(ts.asList())
infix fun <T> Iterable<T>.shouldContainAnyOf(ts: Collection<T>) = toList().shouldContainAnyOf(ts)
infix fun <T> Array<T>.shouldContainAnyOf(ts: Collection<T>) = asList().shouldContainAnyOf(ts)
Expand All @@ -122,4 +122,3 @@ fun <T> containAnyOf(ts: Collection<T>) = object : Matcher<Collection<T>> {
internal fun throwEmptyCollectionError(): Nothing {
throw AssertionError("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")
}

Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,14 @@ class CollectionMatchersTest : WordSpec() {
listOf(1, 2, 3).shouldContainAnyOf(1)
}

"Pass when one element is in the iterable" {
listOf(1, 2, 3).asIterable().shouldContainAnyOf(1)
}

"Pass when one element is in the array" {
arrayOf(1, 2, 3).shouldContainAnyOf(1)
}

"Pass when all elements are in the list" {
listOf(1, 2, 3).shouldContainAnyOf(1, 2, 3)
}
Expand All @@ -866,6 +874,18 @@ class CollectionMatchersTest : WordSpec() {
listOf(1, 2, 3).shouldContainAnyOf(4)
}.shouldHaveMessage("Collection [1, 2, 3] should contain any of [4]")
}

"Fail when no element is in the iterable" {
shouldThrow<AssertionError> {
listOf(1, 2, 3).asIterable().shouldContainAnyOf(4)
}.shouldHaveMessage("Collection [1, 2, 3] should contain any of [4]")
}

"Fail when no element is in the array" {
shouldThrow<AssertionError> {
arrayOf(1, 2, 3).shouldContainAnyOf(4)
}.shouldHaveMessage("Collection [1, 2, 3] should contain any of [4]")
}
}

"Contain any (negative)" should {
Expand All @@ -885,6 +905,18 @@ class CollectionMatchersTest : WordSpec() {
}.shouldHaveMessage("Collection [1, 2, 3] should not contain any of [1]")
}

"Fail when one element is in the iterable" {
shouldThrow<AssertionError> {
listOf(1, 2, 3).asIterable().shouldNotContainAnyOf(1)
}.shouldHaveMessage("Collection [1, 2, 3] should not contain any of [1]")
}

"Fail when one element is in the array" {
shouldThrow<AssertionError> {
arrayOf(1, 2, 3).shouldNotContainAnyOf(1)
}.shouldHaveMessage("Collection [1, 2, 3] should not contain any of [1]")
}

"Fail when all elements are in the list" {
shouldThrow<AssertionError> {
listOf(1, 2, 3).shouldNotContainAnyOf(1, 2, 3)
Expand Down

0 comments on commit 85c4c40

Please sign in to comment.