Skip to content

Adapt assertion extensions for Collections and Iterables to use bounded types #3960

@hanszt

Description

@hanszt

Some of the assertions on collections now look like the following:

infix fun <T> Collection<T>.shouldHaveSize(size: Int): Collection<T> {
...
return this
}

I suggest to change this to

infix fun <T, C : Collection<T>> C.shouldHaveSize(size: Int): C {
   ...
   return this
}

The benefit of this is that you then can do the following:

val (_, _, third) = listOf(1, 2, 3) shouldHaveSize 3
third.shouldBe(3)

A second example:

When

fun <T> Iterable<T>.shouldNotContainDuplicates(): Iterable<T> {
   ...
   return this
}

is changed to

fun <T, I : Iterable<T>> I.shouldNotContainDuplicates(): I {
   ...
   return this
}

This will then allow you to write something like the following in your tests.

class Game(val name: String, players: Iterable<String>) : Iterable<String> by players

val game = Game("Risk", listOf("p1", "p2", "p3", "p4")).shouldNotContainDuplicates()
game.name shouldBe "Risk"

These are only a few examples of functions that could be improved in this way


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions