Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shouldContainNone and shouldNotContainAny with check lambda #210

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -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))
32 changes: 32 additions & 0 deletions common/src/main/kotlin/org/amshove/kluent/Collections.kt
Expand Up @@ -844,6 +844,22 @@ infix fun <T : CharSequence, I : Iterable<T>> I.shouldContainNoneIgnoringCase(ex
infix fun <T, I : Iterable<T>> I.shouldContainNone(expected: Array<T>): I =
apply { assertTrue("Expected Iterable to contain none of \"$expected\"", this.none { expected.contains(it) }) }

infix fun <T, I : Iterable<T>> 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 <T, I : Iterable<T>> I.shouldContainAll(expected: Iterable<T>): I =
apply { expected.forEach { shouldContain(it) } }

Expand Down Expand Up @@ -881,6 +897,22 @@ infix fun <T, I : Iterable<T>> I.shouldNotContainAny(expected: Iterable<T>): I =
infix fun <T, I : Iterable<T>> I.shouldNotContainAny(expected: Array<T>): I =
apply { expected.forEach { shouldNotContain(it) } }

infix fun <T, I : Iterable<T>> 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 <T, I : Iterable<T>> I.shouldEqual(expected: Iterable<T>?): I = shouldBeEqualTo(expected)

Expand Down
@@ -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<Byte>(3, 4)
list shouldContainNone { it == 9.toByte() }
}

@Test
fun failWhenTestingAByteListWithAtLeastOneMatchingValue() {
val list = listOf<Byte>(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<Short>(5, 8, 12)
list shouldContainNone { it == 7.toShort() }
list shouldContainNone { it in listOf<Short>(7, 6, -1) }
}

@Test
fun failWhenTestingAShortListWithAtLeastOneMatchingValue() {
val list = listOf<Short>(2, 14, 3)
assertFails { list shouldContainNone { it == 14.toShort() } }
assertFails { list shouldContainNone { it in listOf<Short>(14, 1, 7) } }
}
}
@@ -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<Byte>(3, 4)
list shouldNotContainAny { it == 9.toByte() }
}

@Test
fun failWhenTestingAByteListWithAtLeastOneMatchingValue() {
val list = listOf<Byte>(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<Short>(5, 8, 12)
list shouldNotContainAny { it == 7.toShort() }
list shouldNotContainAny { it in listOf<Short>(7, 6, -1) }
}

@Test
fun failWhenTestingAShortListWithAtLeastOneMatchingValue() {
val list = listOf<Short>(2, 14, 3)
assertFails { list shouldNotContainAny { it == 14.toShort() } }
assertFails { list shouldNotContainAny { it in listOf<Short>(14, 1, 7) } }
}
}