Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Merge pull request #160 from javatarz/range-assertions
Browse files Browse the repository at this point in the history
Add shouldBeInRange for ClosedRanges #119
  • Loading branch information
MarkusAmshove authored Jan 18, 2020
2 parents 9223ce7 + 0c66fbc commit 87ada19
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
1. Christian Ivicevic - [@ChristianIvicevic](https://github.com/ChristianIvicevic) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=ChristianIvicevic))
1. Ivan Mikhnovich - [@Murtaught](https://github.com/Murtaught) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Murtaught))
1. Jc Miñarro - [@JcMinarro](https://github.com/JcMinarro) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=JcMinarro))
1. Kshitij Patil [@Kshitij09](https://github.com/Kshitij09) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Kshitij09))
18 changes: 18 additions & 0 deletions common/src/main/kotlin/org/amshove/kluent/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,24 @@ infix fun <T> Any?.shouldNotBeIn(iterable: Iterable<T>) = apply { if (!iterable.

infix fun <T> Any?.shouldBeIn(array: Array<T>) = apply { if (array.contains(this)) Unit else failExpectedActual("\"$this\" should be in Array", "the value \"$this\" inside the Array", join(array)) }

infix fun <T : Comparable<T>> ClosedRange<T>.shouldBeInRange(input: ClosedRange<T>): ClosedRange<T> = apply {
if (this.contains(input.start) && this.contains(input.endInclusive)) Unit
else failExpectedActual(
"ClosedRange does not contain all elements of \"$input\"",
"the ClosedRange to contain \"$input\"",
"the ClosedRange contains \"$this\""
)
}

infix fun <T : Comparable<T>> ClosedRange<T>.shouldNotBeInRange(input: ClosedRange<T>): ClosedRange<T> = apply {
if (!(input.start <= this.start && input.endInclusive >= this.endInclusive)) Unit
else failExpectedActual(
"ClosedRange contain elements of \"$input\"",
"the ClosedRange should not contain \"$input\"",
"the ClosedRange contains \"$this\""
)
}

fun <E> Iterable<E>.shouldMatchAtLeastOneOf(predicate: (E) -> Boolean): Iterable<E> {
this.forEach {
if (predicate(it))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.amshove.kluent.collections

import org.amshove.kluent.shouldBeInRange
import kotlin.test.Test
import kotlin.test.assertFails

class ShouldBeInRangeShould {

@Test
fun passWhenAllElementsOfTheInputRangeAreInTheTargetRange() {
val targetRange: IntRange = 1..9
val inputRange = 4..5

targetRange.shouldBeInRange(inputRange)
}

@Test
fun failWhenAllElementsOfTheInputRangeAreNotInTheTargetRange() {
val targetRange = 4..5
val inputRange = 1..9

assertFails { targetRange.shouldBeInRange(inputRange) }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.amshove.kluent.collections

import org.amshove.kluent.shouldNotBeInRange
import kotlin.test.Test
import kotlin.test.assertFails

class ShouldNotBeInRangeShould {

@Test
fun passWhenAllElementsOfTheInputRangeAreNotInTheTargetRange() {
val targetRange: IntRange = 1..9
val inputRange = 4..5

targetRange.shouldNotBeInRange(inputRange)
}

@Test
fun failWhenAllElementsOfTheInputRangeAreInTheTargetRange() {
val targetRange = 4..5
val inputRange = 1..9

assertFails { targetRange.shouldNotBeInRange(inputRange) }
}

}
4 changes: 4 additions & 0 deletions jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ infix fun LongArray.`should contain same`(expected: LongArray) = this.shouldCont

infix fun CharArray.`should contain same`(expected: CharArray) = this.shouldContainSame(expected)

infix fun <T : Comparable<T>> ClosedRange<T>.`should be in range`(target: ClosedRange<T>) = this.shouldBeInRange(target)

infix fun <T : Comparable<T>> ClosedRange<T>.`should not be in range`(target: ClosedRange<T>) = this.shouldNotBeInRange(target)

infix fun <E> Array<E>.`should match at least one of`(predicate: (E) -> Boolean): Array<E> {
return shouldMatchAtLeastOneOf(predicate)
}
Expand Down

0 comments on commit 87ada19

Please sign in to comment.