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 #1 from Kshitij09/range-assertions
Browse files Browse the repository at this point in the history
Added shouldNotBeInRange for ClosedRange #119
  • Loading branch information
javatarz authored Jan 15, 2020
2 parents 5eae2a4 + f02f190 commit 4494fbd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
1. Jógvan Olsen - [@jeggy](https://github.com/jeggy) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=jeggy))
1. Yang C - [@ychescale9](https://github.com/ychescale9) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=ychescale9))
1. Christian Ivicevic - [@ChristianIvicevic](https://github.com/ChristianIvicevic) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=ChristianIvicevic))
1. Kshitij Patil [@Kshitij09](https://github.com/Kshitij09) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Kshitij09))
9 changes: 9 additions & 0 deletions common/src/main/kotlin/org/amshove/kluent/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,15 @@ infix fun <T : Comparable<T>> ClosedRange<T>.shouldBeInRange(input: ClosedRange<
)
}

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.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) }
}

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

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 4494fbd

Please sign in to comment.