i find that i need `single` on a `Map` every once ...
# strikt
e
i find that i need
single
on a
Map
every once in a while, like there is for
Collection
Might be worth dropping in your std assertions
Copy code
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() }
or
Copy code
fun <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:
Copy code
get { qux }.single().isEqualTo(" asdf " to "1234")
vs
Copy code
get { qux }.single().hasEntry(" asdf ", "1234")
j
hmmm. would that be enough?
Copy code
fun <K, V> DescribeableBuilder<Map<K, V>>.single() = hasSize(1).get { toList() }.single()
(I’m trying to figure out potential problems with such approach)
(well, probably too noisy in logs)
e
or
Copy code
fun <T : Map<K, V>, K, V> Assertion.Builder<T>.single(): Assertion.Builder<Pair<K, V>> = get { toList() }.single()
or maybe
single
just isn't needed:
Copy code
get { qux }.isEqualTo(mapOf(" asdf " to "1234"))
j
The first one would be performance hit on maps with thousands of entries. But it was my first approach too, then I added extra hasSize(1) guard 🙂