Add assertk List extension containsNoDuplicates to :core:testing

This commit is contained in:
Wolf Montwé 2023-03-17 17:10:16 +01:00
parent e98ceb70a5
commit 032b1bcc16
No known key found for this signature in database
GPG key ID: 6D45B21512ACBF72
3 changed files with 45 additions and 0 deletions

View file

@ -1,3 +1,7 @@
plugins { plugins {
id(ThunderbirdPlugins.Library.jvm) id(ThunderbirdPlugins.Library.jvm)
} }
dependencies {
implementation(libs.assertk)
}

View file

@ -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)}")
}
}

View file

@ -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(),
)
}
}