Repository: MarkusAmshove/Kluent
Pull request #160 (2019-10-05 02:19:12)

Add shouldBeInRange for ClosedRanges #119
# Description
Completes open issue #119 by implementing `ClosedRange<T>.shouldBeInRange(ClosedRange<T>)`.

Also adds JVM support for backtick version.
Already a contributor so no addition to the `AUTHORS` file :)

# Checklist
<!--- We'd like to thank you for your help, appreciate them and give you credit for it. Please check the checkboxes below as you complete them -->

- [x] I've added my name to the `AUTHORS` file, if it wasn't already present.




Patch:
diff --git a/AUTHORS.md b/AUTHORS.md
index 58443a4..1ad86e4 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 50568be..24a3f3b 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 <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))
diff --git a/jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt b/jvm/src/main/kotlin/org/amshove/kluent/CollectionsBacktick.kt
index 92b9e2b..62a5b1e 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 <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)
 }


Test patch:
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 0000000..56bbf97
--- /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 0000000..17e07fa
--- /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