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.eq.eq
import io.kotest.assertions.show.Printed
import io.kotest.assertions.show.show
import io.kotest.matchers.*
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 ->

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

   val failureMessage = {

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

      val sb = StringBuilder()
      sb.append("Expecting: ${expected.printed().value} but was: ${actual.printed().value}")
      sb.append("\n")
      if (missing.isNotEmpty()) {
         sb.append("Some elements were missing: ")
         sb.append(missing.printed().value)
         if (extra.isNotEmpty()) {
            sb.append(" and some elements were unexpected: ")
            sb.append(extra.printed().value)
         }
      } else if (extra.isNotEmpty()) {
         sb.append("Some elements were unexpected: ")
         sb.append(extra.printed().value)
      }
      sb.toString()
   }

   MatcherResult(
      passed,
      failureMessage
   ) { "Collection should not be exactly ${expected.printed().value}" }
}

@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)

fun > C.printed(): Printed {
   val expectedPrinted = take(20).joinToString(",\n  ", prefix = "[\n  ", postfix = "\n]") { it.show().value }
   val expectedMore = if (size > 20) " ... (plus ${size - 20} more)" else ""
   return Printed("$expectedPrinted$expectedMore")
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy