Skip to content

Commit

Permalink
Merge branch 'master' into fix/ByteArray-assertion-order
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusAmshove committed Oct 8, 2022
2 parents 0da09bd + 7a0d8f1 commit eafe5cf
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -44,3 +44,4 @@
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. Sami Samhuri: [@samsonjs](https://github.com/samsonjs) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=samsonjs))
41. 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) } }
}
}

0 comments on commit eafe5cf

Please sign in to comment.