Add assertk List extension containsNoDuplicates to :core:testing
This commit is contained in:
parent
e98ceb70a5
commit
032b1bcc16
3 changed files with 45 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
|||
plugins {
|
||||
id(ThunderbirdPlugins.Library.jvm)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.assertk)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package assertk.assertions
|
||||
|
||||
import assertk.Assert
|
||||
import assertk.assertions.support.expected
|
||||
import assertk.assertions.support.show
|
||||
|
||||
fun <T> Assert<List<T>>.containsNoDuplicates() = given { actual ->
|
||||
val seen: MutableSet<T> = mutableSetOf()
|
||||
val duplicates = actual.filter { !seen.add(it) }
|
||||
if (duplicates.isNotEmpty()) {
|
||||
expected("to contain no duplicates but found: ${show(duplicates)}")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package assertk.assertions
|
||||
|
||||
import assertk.assertThat
|
||||
import kotlin.test.Test
|
||||
|
||||
class ListExtensionsKtTest {
|
||||
|
||||
@Test
|
||||
fun `containsNoDuplicates() should succeed with no duplicates`() {
|
||||
val list = listOf("a", "b", "c")
|
||||
|
||||
assertThat(list).containsNoDuplicates()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `containsNoDuplicates() should fail with duplicates`() {
|
||||
val list = listOf("a", "b", "c", "a", "a")
|
||||
|
||||
assertThat {
|
||||
assertThat(list).containsNoDuplicates()
|
||||
}.isFailure()
|
||||
.hasMessage(
|
||||
"""
|
||||
expected to contain no duplicates but found: <["a", "a"]>
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue