Eric
07/05/2023, 1:05 PMsingle
on a Map
every once in a while, like there is for Collection
Might be worth dropping in your std assertions
fun <T : Map<K, V>, K, V> Assertion.Builder<T>.single(): Assertion.Builder<Pair<K, V>> =
assert("has only one entry") {
if (it.size == 1) pass(it.size) else fail(it.size)
}.get("single entry %s") { entries.first().toPair() }
Eric
07/05/2023, 1:17 PMfun <T : Map<K, V>, K, V> Assertion.Builder<T>.single(): Assertion.Builder<T> =
assert("has only one entry") {
if (it.size == 1) pass(it.size) else fail(it.size)
}
difference in usage:
get { qux }.single().isEqualTo(" asdf " to "1234")
vs
get { qux }.single().hasEntry(" asdf ", "1234")
Jakub Gwóźdź
07/05/2023, 2:23 PMfun <K, V> DescribeableBuilder<Map<K, V>>.single() = hasSize(1).get { toList() }.single()
(I’m trying to figure out potential problems with such approach)Jakub Gwóźdź
07/05/2023, 2:24 PMEric
07/05/2023, 2:28 PMfun <T : Map<K, V>, K, V> Assertion.Builder<T>.single(): Assertion.Builder<Pair<K, V>> = get { toList() }.single()
Eric
07/05/2023, 2:30 PMsingle
just isn't needed:
get { qux }.isEqualTo(mapOf(" asdf " to "1234"))
Jakub Gwóźdź
07/05/2023, 2:35 PM