UseSingleAssertion¶
Issue ID UseSingleAssertion ·
Severity Warning ·
Category Productivity ·
Default on
Flags the assertk.all { hasSize(1); first()... } (and the index(0)
equivalent) pattern where assertk's single() should be used.
Why¶
Assert<List<T>>.single() asserts that the collection has exactly one element
and transforms the subject to that element. Doing it by hand —
hasSize(1) plus first()/index(0) inside an all { } block — is
two assertions where one will do, and the chained form reads more naturally.
The detector fires only on the simple two-statement shape inside an
all { } block from assertk:
hasSize(1)paired withfirst().<something>, orhasSize(1)paired withindex(0).<something>.
The order doesn't matter. Anything else — hasSize(2), index(1),
extra statements in the block — is left alone.
Example¶
The first() form is recognised the same way:
Quick fix¶
Yes — the IDE collapses the entire all { } block to
single().<chained assertion>, preserving whatever assertion was applied to
the element, and adds the assertk.assertions.single import.