Skip to content

Commit

Permalink
- Updated extension functions for Collection and Iterable assertions …
Browse files Browse the repository at this point in the history
…to use bounded types

- Improved test coverage
  • Loading branch information
hanszt committed Apr 2, 2024
1 parent 92091b8 commit b6974f3
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should

infix fun <T : Comparable<T>> Iterable<T>.shouldHaveUpperBound(t: T): Iterable<T> {
infix fun <T : Comparable<T>, I : Iterable<T>> I.shouldHaveUpperBound(t: T): I {
toList().shouldHaveUpperBound(t)
return this
}
Expand All @@ -30,7 +30,7 @@ fun <T : Comparable<T>, C : Collection<T>> haveUpperBound(t: T) = object : Match
}
}

infix fun <T : Comparable<T>> Iterable<T>.shouldHaveLowerBound(t: T): Iterable<T> {
infix fun <T : Comparable<T>, I : Iterable<T>> I.shouldHaveLowerBound(t: T): I {
toList().shouldHaveLowerBound(t)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import io.kotest.matchers.shouldNot
import io.kotest.similarity.possibleMatchesDescription

// Infix
infix fun <T> Iterable<T>.shouldNotContain(t: T): Iterable<T> = shouldNotContain(t, Equality.default())
infix fun <T, I : Iterable<T>> I.shouldNotContain(t: T): I = shouldNotContain(t, Equality.default())
infix fun <T> Array<T>.shouldNotContain(t: T): Array<T> = shouldNotContain(t, Equality.default())
infix fun <T> Iterable<T>.shouldContain(t: T): Iterable<T> = shouldContain(t, Equality.default())
infix fun <T, I : Iterable<T>> I.shouldContain(t: T): I = shouldContain(t, Equality.default())
infix fun <T> Array<T>.shouldContain(t: T): Array<T> = shouldContain(t, Equality.default())

// Should not
fun <T> Iterable<T>.shouldNotContain(t: T, comparator: Equality<T>): Iterable<T> = apply {
fun <T, I : Iterable<T>> I.shouldNotContain(t: T, comparator: Equality<T>): I = apply {
toList() shouldNot contain(t, comparator)
}

Expand All @@ -24,7 +24,7 @@ fun <T> Array<T>.shouldNotContain(t: T, comparator: Equality<T>): Array<T> = app
}

// Should
fun <T> Iterable<T>.shouldContain(t: T, comparator: Equality<T>): Iterable<T> = apply {
fun <T, I : Iterable<T>> I.shouldContain(t: T, comparator: Equality<T>): I = apply {
toList() should contain(t, comparator)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot

fun <T : Comparable<T>> Iterable<T>.shouldBeMonotonicallyDecreasing(): Iterable<T> {
fun <T : Comparable<T>, I : Iterable<T>> I.shouldBeMonotonicallyDecreasing(): I {
toList().shouldBeMonotonicallyDecreasing()
return this
}
Expand All @@ -16,7 +16,7 @@ fun <T : Comparable<T>> Array<T>.shouldBeMonotonicallyDecreasing(): Array<T> {
return this
}

fun <T : Comparable<T>> Sequence<T>.shouldBeMonotonicallyDecreasing(): Sequence<T> {
fun <T : Comparable<T>, S : Sequence<T>> S.shouldBeMonotonicallyDecreasing(): S {
asIterable().shouldBeMonotonicallyDecreasing()
return this
}
Expand All @@ -26,7 +26,7 @@ fun <T : Comparable<T>> List<T>.shouldBeMonotonicallyDecreasing(): List<T> {
return this
}

fun <T : Comparable<T>> Iterable<T>.shouldNotBeMonotonicallyDecreasing(): Iterable<T> {
fun <T : Comparable<T>, I : Iterable<T>> I.shouldNotBeMonotonicallyDecreasing(): I {
toList().shouldNotBeMonotonicallyDecreasing()
return this
}
Expand All @@ -36,7 +36,7 @@ fun <T : Comparable<T>> Array<T>.shouldNotBeMonotonicallyDecreasing(): Array<T>
return this
}

fun <T : Comparable<T>> Sequence<T>.shouldNotBeMonotonicallyDecreasing(): Sequence<T> {
fun <T : Comparable<T>, S : Sequence<T>> S.shouldNotBeMonotonicallyDecreasing(): S {
asIterable().shouldNotBeMonotonicallyDecreasing()
return this
}
Expand All @@ -51,7 +51,7 @@ infix fun <T> List<T>.shouldBeMonotonicallyDecreasingWith(comparator: Comparator
return this
}

infix fun <T> Iterable<T>.shouldBeMonotonicallyDecreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldBeMonotonicallyDecreasingWith(comparator: Comparator<in T>): I {
toList().shouldBeMonotonicallyDecreasingWith(comparator)
return this
}
Expand All @@ -71,7 +71,7 @@ infix fun <T> List<T>.shouldNotBeMonotonicallyDecreasingWith(comparator: Compara
return this
}

infix fun <T> Iterable<T>.shouldNotBeMonotonicallyDecreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldNotBeMonotonicallyDecreasingWith(comparator: Comparator<in T>): I {
toList().shouldNotBeMonotonicallyDecreasingWith(comparator)
return this
}
Expand Down Expand Up @@ -101,7 +101,7 @@ infix fun <T> List<T>.shouldBeStrictlyDecreasingWith(comparator: Comparator<in T
return this
}

infix fun <T> Iterable<T>.shouldBeStrictlyDecreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldBeStrictlyDecreasingWith(comparator: Comparator<in T>): I {
toList().shouldBeStrictlyDecreasingWith(comparator)
return this
}
Expand All @@ -121,7 +121,7 @@ infix fun <T> List<T>.shouldNotBeStrictlyDecreasingWith(comparator: Comparator<i
return this
}

infix fun <T> Iterable<T>.shouldNotBeStrictlyDecreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldNotBeStrictlyDecreasingWith(comparator: Comparator<in T>): I {
toList().shouldNotBeStrictlyDecreasingWith(comparator)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot

fun <T> Iterable<T>.shouldContainDuplicates(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldContainDuplicates(): I {
toList().shouldContainDuplicates()
return this
}
Expand All @@ -15,12 +15,12 @@ fun <T> Array<T>.shouldContainDuplicates() {
asList().shouldContainDuplicates()
}

fun <T> Collection<T>.shouldContainDuplicates(): Collection<T> {
fun <T, C : Collection<T>> C.shouldContainDuplicates(): C {
this should containDuplicates()
return this
}

fun <T> Iterable<T>.shouldNotContainDuplicates(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldNotContainDuplicates(): I {
toList().shouldNotContainDuplicates()
return this
}
Expand All @@ -30,7 +30,7 @@ fun <T> Array<T>.shouldNotContainDuplicates(): Array<T> {
return this
}

fun <T> Collection<T>.shouldNotContainDuplicates(): Collection<T> {
fun <T, C : Collection<T>> C.shouldNotContainDuplicates(): C {
this shouldNot containDuplicates()
return this
}
Expand All @@ -47,7 +47,7 @@ fun <T> containDuplicates() = object : Matcher<Collection<T>> {
}
}

internal fun<T> Collection<T>.duplicates(): Collection<T> = this.groupingBy { it }
internal fun<T> Collection<T>.duplicates(): List<T> = this.groupingBy { it }
.eachCount().entries
.filter { it.value > 1 }
.map { it.key }
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.kotest.matchers.invokeMatcher
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot

fun <T> Iterable<T>?.shouldBeEmpty(): Iterable<T> {
fun <T, I : Iterable<T>> I?.shouldBeEmpty(): I {
if (this == null) fail()
toList().shouldBeEmpty()
return this
Expand All @@ -19,13 +19,13 @@ fun <T> Array<T>?.shouldBeEmpty(): Array<T> {
return this
}

fun <T> Collection<T>?.shouldBeEmpty(): Collection<T> {
fun <T, C : Collection<T>> C?.shouldBeEmpty(): C {
if (this == null) fail()
this should beEmpty()
return this
}

fun <T> Iterable<T>?.shouldNotBeEmpty(): Iterable<T> {
fun <T, I : Iterable<T>> I?.shouldNotBeEmpty(): I {
if (this == null) fail()
toList().shouldNotBeEmpty()
return this
Expand All @@ -37,7 +37,7 @@ fun <T> Array<T>?.shouldNotBeEmpty(): Array<T> {
return this
}

fun <T> Collection<T>?.shouldNotBeEmpty(): Collection<T> {
fun <T, C : Collection<T>> C?.shouldNotBeEmpty(): C {
if (this == null) fail()
this shouldNot beEmpty()
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot

fun <T : Comparable<T>> Iterable<T>.shouldBeStrictlyIncreasing(): Iterable<T> {
fun <T : Comparable<T>, I : Iterable<T>> I.shouldBeStrictlyIncreasing(): I {
toList().shouldBeStrictlyIncreasing()
return this
}
Expand All @@ -26,7 +26,7 @@ fun <T : Comparable<T>> List<T>.shouldBeStrictlyIncreasing(): List<T> {
return this
}

fun <T : Comparable<T>> Iterable<T>.shouldNotBeStrictlyIncreasing(): Iterable<T> {
fun <T : Comparable<T>, I : Iterable<T>> I.shouldNotBeStrictlyIncreasing(): I {
toList().shouldNotBeStrictlyIncreasing()
return this
}
Expand All @@ -46,7 +46,7 @@ fun <T : Comparable<T>> List<T>.shouldNotBeStrictlyIncreasing(): List<T> {
return this
}

fun <T : Comparable<T>> Iterable<T>.shouldBeMonotonicallyIncreasing(): Iterable<T> {
fun <T : Comparable<T>, I : Iterable<T>> I.shouldBeMonotonicallyIncreasing(): I {
toList().shouldBeMonotonicallyIncreasing()
return this
}
Expand All @@ -66,7 +66,7 @@ fun <T : Comparable<T>> List<T>.shouldBeMonotonicallyIncreasing(): List<T> {
return this
}

fun <T : Comparable<T>> Iterable<T>.shouldNotBeMonotonicallyIncreasing(): Iterable<T> {
fun <T : Comparable<T>, I : Iterable<T>> I.shouldNotBeMonotonicallyIncreasing(): I {
toList().shouldNotBeMonotonicallyIncreasing()
return this
}
Expand Down Expand Up @@ -96,7 +96,7 @@ infix fun <T> Sequence<T>.shouldBeMonotonicallyIncreasingWith(comparator: Compar
return this
}

infix fun <T> Iterable<T>.shouldBeMonotonicallyIncreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldBeMonotonicallyIncreasingWith(comparator: Comparator<in T>): I {
toList().shouldBeMonotonicallyIncreasingWith(comparator)
return this
}
Expand All @@ -111,7 +111,7 @@ infix fun <T> List<T>.shouldNotBeMonotonicallyIncreasingWith(comparator: Compara
return this
}

infix fun <T> Iterable<T>.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator<in T>): I {
toList().shouldNotBeMonotonicallyIncreasingWith(comparator)
return this
}
Expand All @@ -131,7 +131,7 @@ infix fun <T> List<T>.shouldBeStrictlyIncreasingWith(comparator: Comparator<in T
return this
}

infix fun <T> Iterable<T>.shouldBeStrictlyIncreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldBeStrictlyIncreasingWith(comparator: Comparator<in T>): I {
toList().shouldBeStrictlyIncreasingWith(comparator)
return this
}
Expand All @@ -151,7 +151,7 @@ infix fun <T> List<T>.shouldNotBeStrictlyIncreasingWith(comparator: Comparator<i
return this
}

infix fun <T> Iterable<T>.shouldNotBeStrictlyIncreasingWith(comparator: Comparator<in T>): Iterable<T> {
infix fun <T, I : Iterable<T>> I.shouldNotBeStrictlyIncreasingWith(comparator: Comparator<in T>): I {
toList().shouldNotBeStrictlyIncreasingWith(comparator)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should

infix fun <T, U> Iterable<T>.shouldBeLargerThan(other: Collection<U>): Iterable<T> {
infix fun <T, U, I : Iterable<T>> I.shouldBeLargerThan(other: Collection<U>): I {
toList().shouldBeLargerThan(other)
return this
}

infix fun <T, U> Iterable<T>.shouldBeLargerThan(other: Iterable<U>): Iterable<T> {
infix fun <T, U, I : Iterable<T>> I.shouldBeLargerThan(other: Iterable<U>): I {
toList().shouldBeLargerThan(other.toList())
return this
}
Expand All @@ -24,7 +24,7 @@ infix fun <T, U> Array<T>.shouldBeLargerThan(other: Array<U>): Array<T> {
return this
}

infix fun <T, U> Collection<T>.shouldBeLargerThan(other: Collection<U>): Collection<T> {
infix fun <T, U, C : Collection<T>> C.shouldBeLargerThan(other: Collection<U>): C {
this should beLargerThan(other)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot

fun <T> Iterable<T>.shouldContainOnlyNulls(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldContainOnlyNulls(): I {
toList().shouldContainOnlyNulls()
return this
}
Expand All @@ -15,12 +15,12 @@ fun <T> Array<T>.shouldContainOnlyNulls(): Array<T> {
return this
}

fun <T> Collection<T>.shouldContainOnlyNulls(): Collection<T> {
fun <T, C : Collection<T>> C.shouldContainOnlyNulls(): C {
this should containOnlyNulls()
return this
}

fun <T> Iterable<T>.shouldNotContainOnlyNulls(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldNotContainOnlyNulls(): I {
toList().shouldNotContainOnlyNulls()
return this
}
Expand All @@ -30,7 +30,7 @@ fun <T> Array<T>.shouldNotContainOnlyNulls(): Array<T> {
return this
}

fun <T> Collection<T>.shouldNotContainOnlyNulls(): Collection<T> {
fun <T, C : Collection<T>> C.shouldNotContainOnlyNulls(): C {
this shouldNot containOnlyNulls()
return this
}
Expand All @@ -45,7 +45,7 @@ fun <T> containOnlyNulls() = object : Matcher<Collection<T>> {
})
}

fun <T> Iterable<T>.shouldContainNull(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldContainNull(): I {
toList().shouldContainNull()
return this
}
Expand All @@ -55,12 +55,12 @@ fun <T> Array<T>.shouldContainNull(): Array<T> {
return this
}

fun <T> Collection<T>.shouldContainNull(): Collection<T> {
fun <T, C : Collection<T>> C.shouldContainNull(): C {
this should containNull()
return this
}

fun <T> Iterable<T>.shouldNotContainNull(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldNotContainNull(): I {
toList().shouldNotContainNull()
return this
}
Expand All @@ -70,7 +70,7 @@ fun <T> Array<T>.shouldNotContainNull(): Array<T> {
return this
}

fun <T> Collection<T>.shouldNotContainNull(): Collection<T> {
fun <T, C : Collection<T>> C.shouldNotContainNull(): C {
this shouldNot containNull()
return this
}
Expand All @@ -83,7 +83,7 @@ fun <T> containNull() = object : Matcher<Collection<T>> {
{ "Collection should not contain any nulls" })
}

fun <T> Iterable<T>.shouldContainNoNulls(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldContainNoNulls(): I {
toList().shouldContainNoNulls()
return this
}
Expand All @@ -93,12 +93,12 @@ fun <T> Array<T>.shouldContainNoNulls(): Array<T> {
return this
}

fun <T> Collection<T>.shouldContainNoNulls(): Collection<T> {
fun <T, C : Collection<T>> C.shouldContainNoNulls(): C {
this should containNoNulls()
return this
}

fun <T> Iterable<T>.shouldNotContainNoNulls(): Iterable<T> {
fun <T, I : Iterable<T>> I.shouldNotContainNoNulls(): I {
toList().shouldNotContainNoNulls()
return this
}
Expand All @@ -108,7 +108,7 @@ fun <T> Array<T>.shouldNotContainNoNulls(): Array<T> {
return this
}

fun <T> Collection<T>.shouldNotContainNoNulls(): Collection<T> {
fun <T, C : Collection<T>> C.shouldNotContainNoNulls(): C {
this shouldNot containNoNulls()
return this
}
Expand Down

0 comments on commit b6974f3

Please sign in to comment.