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

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

package io.kotest.matchers.collections

import io.kotest.assertions.AssertionsConfig
import io.kotest.assertions.eq.IterableEq
import io.kotest.assertions.eq.eq
import io.kotest.assertions.print.Printed
import io.kotest.assertions.print.print
import io.kotest.matchers.ComparableMatcherResult
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.neverNullMatcher
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot
import kotlin.jvm.JvmName

@JvmName("shouldContainExactly_iterable")
infix fun  Iterable?.shouldContainExactly(expected: Iterable) =
   this?.toList() should containExactly(expected.toList())

@JvmName("shouldContainExactly_array")
infix fun  Array?.shouldContainExactly(expected: Array) =
   this?.asList() should containExactly(*expected)

fun  Iterable?.shouldContainExactly(vararg expected: T) =
   this?.toList() should containExactly(*expected)

fun  Array?.shouldContainExactly(vararg expected: T) =
   this?.asList() should containExactly(*expected)

infix fun > C?.shouldContainExactly(expected: C) = this should containExactly(expected)
fun  Collection?.shouldContainExactly(vararg expected: T) = this should containExactly(*expected)

fun  containExactly(vararg expected: T): Matcher?> = containExactly(expected.asList())

/** Assert that a collection contains exactly the given values and nothing else, in order. */
fun > containExactly(expected: C): Matcher = neverNullMatcher { actual ->
   fun Throwable?.isDisallowedIterableComparisonFailure() =
      this?.message?.startsWith(IterableEq.trigger) == true

   val failureReason = eq(actual, expected, strictNumberEq = true)
   val passed = failureReason == null

   val failureMessage = {

      val missing = expected.filterNot { actual.contains(it) }
      val extra = actual.filterNot { expected.contains(it) }

      val sb = StringBuilder()

      if (failureReason.isDisallowedIterableComparisonFailure()) {
         sb.append(failureReason?.message)
      } else {
         sb.append("Expecting: ${expected.print().value} but was: ${actual.print().value}")
      }

      sb.append("\n")
      if (missing.isNotEmpty()) {
         sb.append("Some elements were missing: ")
         sb.append(missing.print().value)
         if (extra.isNotEmpty()) {
            sb.append(" and some elements were unexpected: ")
            sb.append(extra.print().value)
         }
      } else if (extra.isNotEmpty()) {
         sb.append("Some elements were unexpected: ")
         sb.append(extra.print().value)
      }

      sb.appendLine()
      sb.toString()
   }

   val negatedFailureMessage = { "Collection should not contain exactly ${expected.print().value}" }

   if (
      actual.size <= AssertionsConfig.maxCollectionEnumerateSize &&
      expected.size <= AssertionsConfig.maxCollectionEnumerateSize &&
      !failureReason.isDisallowedIterableComparisonFailure()
   ) {
      ComparableMatcherResult(
         passed,
         failureMessage,
         negatedFailureMessage,
         actual.print().value,
         expected.print().value,
      )
   } else {
      MatcherResult(
         passed,
         failureMessage,
         negatedFailureMessage,
      )
   }
}

@JvmName("shouldNotContainExactly_iterable")
infix fun  Iterable?.shouldNotContainExactly(expected: Iterable) = this?.toList() shouldNot containExactly(expected.toList())

@JvmName("shouldNotContainExactly_array")
infix fun  Array?.shouldNotContainExactly(expected: Array) = this?.asList() shouldNot containExactly(*expected)

fun  Iterable?.shouldNotContainExactly(vararg expected: T) = this?.toList() shouldNot containExactly(*expected)
fun  Array?.shouldNotContainExactly(vararg expected: T) = this?.asList() shouldNot containExactly(*expected)

infix fun > C?.shouldNotContainExactly(expected: C) = this shouldNot containExactly(expected)
fun  Collection?.shouldNotContainExactly(vararg expected: T) = this shouldNot containExactly(*expected)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy