From 85c4c4002178b4bef3fe8181a6f672e335e4958e Mon Sep 17 00:00:00 2001 From: SEONGILKIM Date: Wed, 11 Oct 2023 23:11:45 +0900 Subject: [PATCH] Fix shouldContainAnyOf matchers with varargs (#3733) --- .../kotest/matchers/collections/matchers.kt | 9 +++--- .../collections/CollectionMatchersTest.kt | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/collections/matchers.kt b/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/collections/matchers.kt index ab3540f5ec9..90a09d2096f 100644 --- a/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/collections/matchers.kt +++ b/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/collections/matchers.kt @@ -93,11 +93,11 @@ infix fun List.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = th -fun Iterable.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(ts) -fun Array.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(ts) +fun Iterable.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(*ts) +fun Array.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(*ts) fun Collection.shouldContainAnyOf(vararg ts: T) = this should containAnyOf(ts.asList()) -fun Iterable.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(ts) -fun Array.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(ts) +fun Iterable.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(*ts) +fun Array.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(*ts) fun Collection.shouldNotContainAnyOf(vararg ts: T) = this shouldNot containAnyOf(ts.asList()) infix fun Iterable.shouldContainAnyOf(ts: Collection) = toList().shouldContainAnyOf(ts) infix fun Array.shouldContainAnyOf(ts: Collection) = asList().shouldContainAnyOf(ts) @@ -122,4 +122,3 @@ fun containAnyOf(ts: Collection) = object : Matcher> { internal fun throwEmptyCollectionError(): Nothing { throw AssertionError("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.") } - diff --git a/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/collections/CollectionMatchersTest.kt b/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/collections/CollectionMatchersTest.kt index 70da34a488d..5650b0302fc 100644 --- a/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/collections/CollectionMatchersTest.kt +++ b/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/collections/CollectionMatchersTest.kt @@ -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) } @@ -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 { + 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 { + arrayOf(1, 2, 3).shouldContainAnyOf(4) + }.shouldHaveMessage("Collection [1, 2, 3] should contain any of [4]") + } } "Contain any (negative)" should { @@ -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 { + 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 { + 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 { listOf(1, 2, 3).shouldNotContainAnyOf(1, 2, 3)