Klitos Kyriacou
09/21/2022, 5:09 PMlistOf(1, 1, 1).shouldContainOnly(1) -- passes (if there was such a function as shouldContainOnly)
listOf(1, 1, 2, 2).shouldContainOnly(1, 2) -- passes
listOf(1, 1, 2).shouldContainOnly(1) -- fails
Emil Kantis
09/21/2022, 5:11 PMshouldContainExactly
Emil Kantis
09/21/2022, 5:12 PMlistOf(1, 1, 2, 2).shouldContainExactly(1, 1, 2, 2)
would pass.Klitos Kyriacou
09/21/2022, 5:14 PMEmil Kantis
09/21/2022, 5:15 PMlistOf(1,1,2,2).forAll {
it.shouldBeOneOf(1, 2)
}
Klitos Kyriacou
09/21/2022, 5:19 PMcontainsOnly
function, but this will do.Emil Kantis
09/21/2022, 5:20 PMKlitos Kyriacou
09/21/2022, 5:34 PMDavio
09/22/2022, 8:10 AMshouldContainExactlyInAnyOrder
right?Klitos Kyriacou
09/22/2022, 8:17 AMlistOf(1, 1, 1).shouldContainExactlyInAnyOrder(1)
fails.Klitos Kyriacou
09/22/2022, 8:24 AMshouldContainOnly
Kotest can be used only with workarounds, such as by making changes to the actual value to be tested. Compare AssertJ's containsOnly
with attempts to use Kotest:
package org.example
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.collections.*
import org.junit.jupiter.api.Test
import org.assertj.core.api.Assertions.*
class KotlinPracticeTest {
@Test
fun foo() {
// AssertJ's containsOnly method is what is required
assertThat(listOf(1, 1, 1)).containsOnly(1)
assertThat(listOf(1, 1, 2, 2)).containsOnly(1, 2)
shouldThrow<AssertionError> { assertThat(listOf(1, 1, 2)).containsOnly(1, 2, 3) }
// There is no Kotest method to do the same thing:
// it only works with workarounds such as converting the actual value to be tested
listOf(1, 1, 1).toSet().shouldContainExactlyInAnyOrder(1)
listOf(1, 1, 2, 2).toSet().shouldContainExactlyInAnyOrder(1, 2) //fails
shouldThrow<AssertionError> { listOf(1, 1, 2).toSet().shouldContainExactlyInAnyOrder(1) }
}
}
Davio
09/22/2022, 8:24 AMshouldMatchAll
kind of function that you can use with a predicate