From 20c4aa6bd56c3ddd027221233c2ea4448dc8e328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ulln=C3=A6ss?= Date: Thu, 10 Feb 2022 18:39:07 +0100 Subject: [PATCH 1/3] Add shouldContainNone and shouldNotContainAny with check lambda --- .../kotlin/org/amshove/kluent/Collections.kt | 36 +++++ .../ShouldContainNoneWithCheckShould.kt | 129 ++++++++++++++++++ .../ShouldNotContainAnyWithCheckShould.kt | 129 ++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldContainNoneWithCheckShould.kt create mode 100644 common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldNotContainAnyWithCheckShould.kt diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 22563b0..1a7d55c 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -844,6 +844,24 @@ infix fun > I.shouldContainNoneIgnoringCase(ex infix fun > I.shouldContainNone(expected: Array): I = apply { assertTrue("Expected Iterable to contain none of \"$expected\"", this.none { expected.contains(it) }) } +infix fun > I.shouldContainNone(check: (T) -> Boolean): I = apply { + val result = this.map { it to check.invoke(it) } + + if (result.none { it.second }) { + Unit + } else { + val failedItems = result + .filterNot { it.second } + .map { it.first } + .joinToString(", ") + failExpectedActual( + "Iterable does contain \"$failedItems\"", + "the Iterable to not contain \"$failedItems\"", + join(this) + ) + } +} + infix fun > I.shouldContainAll(expected: Iterable): I = apply { expected.forEach { shouldContain(it) } } @@ -881,6 +899,24 @@ infix fun > I.shouldNotContainAny(expected: Iterable): I = infix fun > I.shouldNotContainAny(expected: Array): I = apply { expected.forEach { shouldNotContain(it) } } +infix fun > I.shouldNotContainAny(check: (T) -> Boolean): I = apply { + val result = this.map { it to check.invoke(it) } + + if (result.none { it.second }) { + Unit + } else { + val failedItems = result + .filterNot { it.second } + .map { it.first } + .joinToString(", ") + failExpectedActual( + "Iterable does contain \"$failedItems\"", + "the Iterable to not contain \"$failedItems\"", + join(this) + ) + } +} + @Deprecated("Use `shouldBeEqualTo`", ReplaceWith("this.shouldBeEqualTo(expected)")) infix fun > I.shouldEqual(expected: Iterable?): I = shouldBeEqualTo(expected) diff --git a/common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldContainNoneWithCheckShould.kt b/common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldContainNoneWithCheckShould.kt new file mode 100644 index 0000000..1bb476f --- /dev/null +++ b/common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldContainNoneWithCheckShould.kt @@ -0,0 +1,129 @@ +package org.amshove.kluent.tests.collections + +import org.amshove.kluent.shouldContainNone +import kotlin.test.Test +import kotlin.test.assertFails + +class ShouldContainNoneWithCheckShould { + + @Test + fun passWhenTestingAListWithNoMatchingValue() { + val list = listOf("Hello", "World", "Wide", "Web") + list shouldContainNone { it == "Cat" } + } + + @Test + fun failWhenTestingAListWithAtLeastOneMatchingValue() { + val list = listOf("Hello", "World", "Wide", "Web") + assertFails { list shouldContainNone { it == "Hello" } } + } + + @Test + fun passWhenTestingAnIntListWithNoMatchingValue() { + val list = listOf(1, 3, 5) + list shouldContainNone { it == 2 } + list shouldContainNone { it % 2 == 0 } + } + + @Test + fun failWhenTestingAnIntListWithAtLeastOneMatchingValue() { + val list = listOf(1, 3, 5) + assertFails { list shouldContainNone { it == 3 } } + assertFails { list shouldContainNone { it == 5 } } + } + + @Test + fun passWhenTestingABooleanListWithNoMatchingValue() { + val list = listOf(true) + list shouldContainNone { !it } + } + + @Test + fun failWhenTestingABooleanListWithAtLeastOneMatchingValue() { + val list = listOf(false, true) + assertFails { list shouldContainNone { !it } } + assertFails { list shouldContainNone { it } } + } + + @Test + fun passWhenTestingAByteListWithNoMatchingValue() { + val list = listOf(3, 4) + list shouldContainNone { it == 9.toByte() } + } + + @Test + fun failWhenTestingAByteListWithAtLeastOneMatchingValue() { + val list = listOf(5, 7, 8) + assertFails { list shouldContainNone { it == 5.toByte() } } + assertFails { list shouldContainNone { it == 7.toByte() } } + } + + @Test + fun passWhenTestingACharListWithNoMatchingValue() { + val list = listOf('a', 'o', 'e', 'i', 'u') + list shouldContainNone { it == 'b' } + } + + @Test + fun failWhenTestingACharListWithAtLeastOneMatchingValue() { + val list = listOf('a', 'o', 'e', 'i', 'u') + assertFails { list shouldContainNone { it == 'o' } } + assertFails { list shouldContainNone { it in listOf('o', 'u') } } + } + + @Test + fun passWhenTestingADoubleListWithNoMatchingValue() { + val list = listOf(5.6, 7.8, 8.0) + list shouldContainNone { it == 1.2 } + list shouldContainNone { it in listOf(1.2, 3.9) } + } + + @Test + fun failWhenTestingADoubleArrayWithAtLeastOneMatchingValue() { + val list = listOf(8.9, 9.1, 12.3) + assertFails { list shouldContainNone { it == 12.3 } } + assertFails { list shouldContainNone { it in listOf(12.3, 1.0) } } + } + + @Test + fun passWhenTestingAFloatListWithNoMatchingValue() { + val list = listOf(0f, 1f, 2f) + list shouldContainNone { it == 3f } + list shouldContainNone { it in listOf(3f, 4f) } + } + + @Test + fun failWhenTestingAFloatListWithAtLeastOneMatchingValue() { + val list = listOf(2f, 5f, 7f) + assertFails { list shouldContainNone { it == 5f } } + assertFails { list shouldContainNone { it in listOf(5f, 2f, 7f) } } + } + + @Test + fun passWhenTestingALongListWithNoMatchingValue() { + val list = listOf(2L, 100L, 200L) + list shouldContainNone { it == 3L } + list shouldContainNone { it in listOf(3L, 50L, 75L) } + } + + @Test + fun failWhenTestingALongListWithAtLeastOneMatchingValue() { + val list = listOf(1L, 4L) + assertFails { list shouldContainNone { it == 4L } } + assertFails { list shouldContainNone { it in listOf(4L, 5L, 89L) } } + } + + @Test + fun passWhenTestingAShortListWithNoMatchingValue() { + val list = listOf(5, 8, 12) + list shouldContainNone { it == 7.toShort() } + list shouldContainNone { it in listOf(7, 6, -1) } + } + + @Test + fun failWhenTestingAShortListWithAtLeastOneMatchingValue() { + val list = listOf(2, 14, 3) + assertFails { list shouldContainNone { it == 14.toShort() } } + assertFails { list shouldContainNone { it in listOf(14, 1, 7) } } + } +} diff --git a/common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldNotContainAnyWithCheckShould.kt b/common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldNotContainAnyWithCheckShould.kt new file mode 100644 index 0000000..d0913e4 --- /dev/null +++ b/common/src/test/kotlin/org/amshove/kluent/tests/collections/ShouldNotContainAnyWithCheckShould.kt @@ -0,0 +1,129 @@ +package org.amshove.kluent.tests.collections + +import org.amshove.kluent.shouldNotContainAny +import kotlin.test.Test +import kotlin.test.assertFails + +class ShouldNotContainAnyWithCheckShould { + + @Test + fun passWhenTestingAListWithNoMatchingValue() { + val list = listOf("Hello", "World", "Wide", "Web") + list shouldNotContainAny { it == "Cat" } + } + + @Test + fun failWhenTestingAListWithAtLeastOneMatchingValue() { + val list = listOf("Hello", "World", "Wide", "Web") + assertFails { list shouldNotContainAny { it == "Hello" } } + } + + @Test + fun passWhenTestingAnIntListWithNoMatchingValue() { + val list = listOf(1, 3, 5) + list shouldNotContainAny { it == 2 } + list shouldNotContainAny { it % 2 == 0 } + } + + @Test + fun failWhenTestingAnIntListWithAtLeastOneMatchingValue() { + val list = listOf(1, 3, 5) + assertFails { list shouldNotContainAny { it == 3 } } + assertFails { list shouldNotContainAny { it == 5 } } + } + + @Test + fun passWhenTestingABooleanListWithNoMatchingValue() { + val list = listOf(true) + list shouldNotContainAny { !it } + } + + @Test + fun failWhenTestingABooleanListWithAtLeastOneMatchingValue() { + val list = listOf(false, true) + assertFails { list shouldNotContainAny { !it } } + assertFails { list shouldNotContainAny { it } } + } + + @Test + fun passWhenTestingAByteListWithNoMatchingValue() { + val list = listOf(3, 4) + list shouldNotContainAny { it == 9.toByte() } + } + + @Test + fun failWhenTestingAByteListWithAtLeastOneMatchingValue() { + val list = listOf(5, 7, 8) + assertFails { list shouldNotContainAny { it == 5.toByte() } } + assertFails { list shouldNotContainAny { it == 7.toByte() } } + } + + @Test + fun passWhenTestingACharListWithNoMatchingValue() { + val list = listOf('a', 'o', 'e', 'i', 'u') + list shouldNotContainAny { it == 'b' } + } + + @Test + fun failWhenTestingACharListWithAtLeastOneMatchingValue() { + val list = listOf('a', 'o', 'e', 'i', 'u') + assertFails { list shouldNotContainAny { it == 'o' } } + assertFails { list shouldNotContainAny { it in listOf('o', 'u') } } + } + + @Test + fun passWhenTestingADoubleListWithNoMatchingValue() { + val list = listOf(5.6, 7.8, 8.0) + list shouldNotContainAny { it == 1.2 } + list shouldNotContainAny { it in listOf(1.2, 3.9) } + } + + @Test + fun failWhenTestingADoubleArrayWithAtLeastOneMatchingValue() { + val list = listOf(8.9, 9.1, 12.3) + assertFails { list shouldNotContainAny { it == 12.3 } } + assertFails { list shouldNotContainAny { it in listOf(12.3, 1.0) } } + } + + @Test + fun passWhenTestingAFloatListWithNoMatchingValue() { + val list = listOf(0f, 1f, 2f) + list shouldNotContainAny { it == 3f } + list shouldNotContainAny { it in listOf(3f, 4f) } + } + + @Test + fun failWhenTestingAFloatListWithAtLeastOneMatchingValue() { + val list = listOf(2f, 5f, 7f) + assertFails { list shouldNotContainAny { it == 5f } } + assertFails { list shouldNotContainAny { it in listOf(5f, 2f, 7f) } } + } + + @Test + fun passWhenTestingALongListWithNoMatchingValue() { + val list = listOf(2L, 100L, 200L) + list shouldNotContainAny { it == 3L } + list shouldNotContainAny { it in listOf(3L, 50L, 75L) } + } + + @Test + fun failWhenTestingALongListWithAtLeastOneMatchingValue() { + val list = listOf(1L, 4L) + assertFails { list shouldNotContainAny { it == 4L } } + assertFails { list shouldNotContainAny { it in listOf(4L, 5L, 89L) } } + } + + @Test + fun passWhenTestingAShortListWithNoMatchingValue() { + val list = listOf(5, 8, 12) + list shouldNotContainAny { it == 7.toShort() } + list shouldNotContainAny { it in listOf(7, 6, -1) } + } + + @Test + fun failWhenTestingAShortListWithAtLeastOneMatchingValue() { + val list = listOf(2, 14, 3) + assertFails { list shouldNotContainAny { it == 14.toShort() } } + assertFails { list shouldNotContainAny { it in listOf(14, 1, 7) } } + } +} From b898f80ce8fe48744f049488ba51878c52b172ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ulln=C3=A6ss?= Date: Thu, 10 Feb 2022 18:43:34 +0100 Subject: [PATCH 2/3] Add myself to AUTHORS.md --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index 530cf09..02a1bbc 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -43,3 +43,4 @@ 37. Maxim Ivanov: [@drcolombo](https://github.com/drcolombo) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=drcolombo)) 38. Piotr Bakalarski: [@piotrb5e3](https://github.com/piotrb5e3) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=piotrb5e3)) 39. Priyank Gupta: [@priyaaank](https://github.com/priyaaank) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=priyaaank)) +40. Anders Ullnæss: [@andersu](https://github.com/andersu) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=andersu)) From 11bb0f4965e93128b9d57812196988bad7be1cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ulln=C3=A6ss?= Date: Wed, 2 Mar 2022 09:26:40 +0100 Subject: [PATCH 3/3] Simplify check --- common/src/main/kotlin/org/amshove/kluent/Collections.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 1a7d55c..06569c9 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -847,9 +847,7 @@ infix fun > I.shouldContainNone(expected: Array): I = infix fun > I.shouldContainNone(check: (T) -> Boolean): I = apply { val result = this.map { it to check.invoke(it) } - if (result.none { it.second }) { - Unit - } else { + if (result.any { it.second }) { val failedItems = result .filterNot { it.second } .map { it.first } @@ -902,9 +900,7 @@ infix fun > I.shouldNotContainAny(expected: Array): I = infix fun > I.shouldNotContainAny(check: (T) -> Boolean): I = apply { val result = this.map { it to check.invoke(it) } - if (result.none { it.second }) { - Unit - } else { + if (result.any { it.second }) { val failedItems = result .filterNot { it.second } .map { it.first }