diff --git a/AUTHORS.md b/AUTHORS.md index 58443a45..1ad86e44 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -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)) diff --git a/common/src/main/kotlin/org/amshove/kluent/Collections.kt b/common/src/main/kotlin/org/amshove/kluent/Collections.kt index 50568bef..24a3f3b1 100644 --- a/common/src/main/kotlin/org/amshove/kluent/Collections.kt +++ b/common/src/main/kotlin/org/amshove/kluent/Collections.kt @@ -745,6 +745,24 @@ infix fun Any?.shouldNotBeIn(iterable: Iterable) = apply { if (!iterable. infix fun Any?.shouldBeIn(array: Array) = apply { if (array.contains(this)) Unit else failExpectedActual("\"$this\" should be in Array", "the value \"$this\" inside the Array", join(array)) } +infix fun > ClosedRange.shouldBeInRange(input: ClosedRange): ClosedRange = 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 > ClosedRange.shouldNotBeInRange(input: ClosedRange): ClosedRange = 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 Iterable.shouldMatchAtLeastOneOf(predicate: (E) -> Boolean): Iterable { this.forEach { if (predicate(it)) diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeInRangeShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeInRangeShould.kt new file mode 100644 index 00000000..56bbf979 --- /dev/null +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldBeInRangeShould.kt @@ -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) } + } + +} \ No newline at end of file diff --git a/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeInRangeShould.kt b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeInRangeShould.kt new file mode 100644 index 00000000..17e07fa4 --- /dev/null +++ b/common/src/test/kotlin/org/amshove/kluent/collections/ShouldNotBeInRangeShould.kt @@ -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) } + } + +} \ No newline at end of file diff --git a/jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt b/jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt index 92b9e2b8..62a5b1e4 100644 --- a/jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt +++ b/jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt @@ -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 > ClosedRange.`should be in range`(target: ClosedRange) = this.shouldBeInRange(target) + +infix fun > ClosedRange.`should not be in range`(target: ClosedRange) = this.shouldNotBeInRange(target) + infix fun Array.`should match at least one of`(predicate: (E) -> Boolean): Array { return shouldMatchAtLeastOneOf(predicate) }