All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.kotest.matchers.collections.increasing.kt Maven / Gradle / Ivy

package io.kotest.matchers.collections

import io.kotest.assertions.print.print
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot


fun > Iterable.shouldBeStrictlyIncreasing(): Iterable {
   toList().shouldBeStrictlyIncreasing()
   return this
}

fun > Array.shouldBeStrictlyIncreasing(): Array {
   asList().shouldBeStrictlyIncreasing()
   return this
}

fun > List.shouldBeStrictlyIncreasing(): List {
   this should beStrictlyIncreasing()
   return this
}

fun > Iterable.shouldNotBeStrictlyIncreasing(): Iterable {
   toList().shouldNotBeStrictlyIncreasing()
   return this
}

fun > Array.shouldNotBeStrictlyIncreasing(): Array {
   asList().shouldNotBeStrictlyIncreasing()
   return this
}

fun > List.shouldNotBeStrictlyIncreasing(): List {
   this shouldNot beStrictlyIncreasing()
   return this
}



fun > Iterable.shouldBeMonotonicallyIncreasing(): Iterable {
   toList().shouldBeMonotonicallyIncreasing()
   return this
}

fun > Array.shouldBeMonotonicallyIncreasing(): Array {
   asList().shouldBeMonotonicallyIncreasing()
   return this
}

fun > List.shouldBeMonotonicallyIncreasing(): List {
   this should beMonotonicallyIncreasing()
   return this
}

fun > Iterable.shouldNotBeMonotonicallyIncreasing(): Iterable {
   toList().shouldNotBeMonotonicallyIncreasing()
   return this
}

fun > Array.shouldNotBeMonotonicallyIncreasing(): Array {
   asList().shouldNotBeMonotonicallyIncreasing()
   return this
}

fun > List.shouldNotBeMonotonicallyIncreasing(): List {
   this shouldNot beMonotonicallyIncreasing()
   return this
}

fun  List.shouldBeMonotonicallyIncreasingWith(comparator: Comparator): List {
   this should beMonotonicallyIncreasingWith(comparator)
   return this
}

fun  Iterable.shouldBeMonotonicallyIncreasingWith(comparator: Comparator): Iterable {
   toList().shouldBeMonotonicallyIncreasingWith(comparator)
   return this
}

fun  Array.shouldBeMonotonicallyIncreasingWith(comparator: Comparator): Array {
   asList().shouldBeMonotonicallyIncreasingWith(comparator)
   return this
}

fun  List.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator): List {
   this shouldNot beMonotonicallyIncreasingWith(comparator)
   return this
}

fun  Iterable.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator): Iterable {
   toList().shouldNotBeMonotonicallyIncreasingWith(comparator)
   return this
}


fun  Array.shouldNotBeMonotonicallyIncreasingWith(comparator: Comparator): Array {
   asList().shouldNotBeMonotonicallyIncreasingWith(comparator)
   return this
}


fun  List.shouldBeStrictlyIncreasingWith(comparator: Comparator) =
   this should beStrictlyIncreasingWith(comparator)

fun  Iterable.shouldBeStrictlyIncreasingWith(comparator: Comparator) =
   toList().shouldBeStrictlyIncreasingWith(comparator)

fun  Array.shouldBeStrictlyIncreasingWith(comparator: Comparator) =
   asList().shouldBeStrictlyIncreasingWith(comparator)

fun  List.shouldNotBeStrictlyIncreasingWith(comparator: Comparator) =
   this shouldNot beStrictlyIncreasingWith(comparator)

fun  Iterable.shouldNotBeStrictlyIncreasingWith(comparator: Comparator) =
   toList().shouldNotBeStrictlyIncreasingWith(comparator)

fun  Array.shouldNotBeStrictlyIncreasingWith(comparator: Comparator) =
   asList().shouldNotBeStrictlyIncreasingWith(comparator)


fun > beStrictlyIncreasing(): Matcher> = strictlyIncreasing()
fun > strictlyIncreasing(): Matcher> = object : Matcher> {
   override fun test(value: List): MatcherResult {
      return testStrictlyIncreasingWith(value) { a, b -> a.compareTo(b) }
   }
}

fun  beStrictlyIncreasingWith(comparator: Comparator): Matcher> = strictlyIncreasingWith(comparator)
fun  strictlyIncreasingWith(comparator: Comparator): Matcher> = object : Matcher> {
   override fun test(value: List): MatcherResult {
      return testStrictlyIncreasingWith(value, comparator)
   }
}

private fun  testStrictlyIncreasingWith(value: List, comparator: Comparator): MatcherResult {
   val failure = value.zipWithNext().withIndex().find { (_, pair) -> comparator.compare(pair.first, pair.second) >= 0 }
   val snippet = value.print().value
   val elementMessage = when (failure) {
      null -> ""
      else -> ". Element ${failure.value.second} at index ${failure.index + 1} was not strictly increased from previous element."
   }
   return MatcherResult(
      failure == null,
      { "List [$snippet] should be strictly increasing$elementMessage" },
      { "List [$snippet] should not be strictly increasing" }
   )
}


fun > beMonotonicallyIncreasing(): Matcher> = monotonicallyIncreasing()
fun > monotonicallyIncreasing(): Matcher> = object : Matcher> {
   override fun test(value: List): MatcherResult {
      return testMonotonicallyIncreasingWith(value) { a, b -> a.compareTo(b) }
   }
}

fun  beMonotonicallyIncreasingWith(comparator: Comparator): Matcher> =
   monotonicallyIncreasingWith(comparator)

fun  monotonicallyIncreasingWith(comparator: Comparator): Matcher> = object : Matcher> {
   override fun test(value: List): MatcherResult {
      return testMonotonicallyIncreasingWith(value, comparator)
   }
}

private fun  testMonotonicallyIncreasingWith(value: List, comparator: Comparator): MatcherResult {
   val failure = value.zipWithNext().withIndex().find { (_, pair) -> comparator.compare(pair.first, pair.second) > 0 }
   val snippet = value.print().value
   val elementMessage = when (failure) {
      null -> ""
      else -> ". Element ${failure.value.second} at index ${failure.index + 1} was not monotonically increased from previous element."
   }
   return MatcherResult(
      failure == null,
      { "List [$snippet] should be monotonically increasing$elementMessage" },
      { "List [$snippet] should not be monotonically increasing" }
   )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy