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

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

package io.kotest.matchers.collections

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


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

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

fun > List.shouldBeMonotonicallyDecreasing(): List {
   this should beMonotonicallyDecreasing()
   return this
}

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

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

fun > List.shouldNotBeMonotonicallyDecreasing(): List {
   this shouldNot beMonotonicallyDecreasing()
   return this
}

fun  List.shouldBeMonotonicallyDecreasingWith(comparator: Comparator): List {
   this should beMonotonicallyDecreasingWith(comparator)
   return this
}

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

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

fun  List.shouldNotBeMonotonicallyDecreasingWith(comparator: Comparator): List {
   this shouldNot beMonotonicallyDecreasingWith(comparator)
   return this
}

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

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


fun > Iterable.shouldBeStrictlyDecreasing() = toList().shouldBeStrictlyDecreasing()
fun > List.shouldBeStrictlyDecreasing() = this should beStrictlyDecreasing()
fun > Iterable.shouldNotBeStrictlyDecreasing() = toList().shouldNotBeStrictlyDecreasing()
fun > List.shouldNotBeStrictlyDecreasing() = this shouldNot beStrictlyDecreasing()
fun  List.shouldBeStrictlyDecreasingWith(comparator: Comparator) =
   this should beStrictlyDecreasingWith(comparator)

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

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

fun  List.shouldNotBeStrictlyDecreasingWith(comparator: Comparator) =
   this shouldNot beStrictlyDecreasingWith(comparator)

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

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

fun > beMonotonicallyDecreasing(): Matcher> = monotonicallyDecreasing()
fun > monotonicallyDecreasing(): Matcher> = object : Matcher> {
   override fun test(value: List): MatcherResult {
      return testMonotonicallyDecreasingWith(value) { a, b -> a.compareTo(b) }
   }
}

fun  beMonotonicallyDecreasingWith(comparator: Comparator): Matcher> =
   monotonicallyDecreasingWith(comparator)

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

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


fun > beStrictlyDecreasing(): Matcher> = strictlyDecreasing()
fun > strictlyDecreasing(): Matcher> = object : Matcher> {
   override fun test(value: List): MatcherResult {
      return testStrictlyDecreasingWith(value) { a, b -> a.compareTo(b) }
   }
}

fun  beStrictlyDecreasingWith(comparator: Comparator): Matcher> =
   strictlyDecreasingWith(comparator)

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

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





© 2015 - 2025 Weber Informatics LLC | Privacy Policy