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)) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 22563b0..06569c9 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -844,6 +844,22 @@ 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.any { it.second }) { + 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 +897,22 @@ 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.any { it.second }) { + 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) } } + } +}