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

commonMain.io.kotest.matchers.maps.matchers.kt Maven / Gradle / Ivy

package io.kotest.matchers.maps

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

fun  mapcontain(key: K, v: V) = object : Matcher> {
   override fun test(value: Map) = MatcherResult(
      value[key] == v,
      { "Map should contain mapping $key=$v but was ${buildActualValue(value)}" },
      {
         "Map should not contain mapping $key=$v but was $value"
      })

   private fun buildActualValue(map: Map) = map[key]?.let { "$key=$it" } ?: map
}

fun  Map.shouldContain(key: K, value: V) = this should mapcontain(key, value)
fun  Map.shouldNotContain(key: K, value: V) = this shouldNot mapcontain(key, value)

infix fun  Map.shouldContain(entry: Pair) = this should mapcontain(entry.first, entry.second)
infix fun  Map.shouldNotContain(entry: Pair) = this shouldNot mapcontain(entry.first, entry.second)

infix fun  Map.shouldContainExactly(expected: Map) = this should containExactly(expected)
infix fun  Map.shouldNotContainExactly(expected: Map) = this shouldNot containExactly(expected)

infix fun  Map.shouldContainAll(expected: Map) = this should containAll(expected)
infix fun  Map.shouldNotContainAll(expected: Map) = this shouldNot containAll(expected)

infix fun  Map.shouldHaveKey(key: K) = this should haveKey(key)
infix fun  Map.shouldContainKey(key: K) = this should haveKey(key)
infix fun  Map.shouldNotHaveKey(key: K) = this shouldNot haveKey(key)
infix fun  Map.shouldNotContainKey(key: K) = this shouldNot haveKey(key)

infix fun  Map.shouldContainValue(value: V) = this should haveValue(value)
infix fun  Map.shouldNotContainValue(value: V) = this shouldNot haveValue(value)

infix fun  Map.shouldHaveSize(size: Int) = this should haveSize(size)

fun  Map.shouldHaveKeys(vararg keys: K) = this should haveKeys(*keys)
fun  Map.shouldContainKeys(vararg keys: K) = this should haveKeys(*keys)
fun  Map.shouldContainAnyKeysOf(vararg keys: K) = this should containAnyKeys(*keys)
fun  Map.shouldNotHaveKeys(vararg keys: K) = this shouldNot haveKeys(*keys)
fun  Map.shouldNotContainKeys(vararg keys: K) = this shouldNot haveKeys(*keys)
fun  Map.shouldNotContainAnyKeysOf(vararg keys: K) = this shouldNot containAnyKeys(*keys)

fun  Map.shouldHaveValues(vararg values: V) = this should haveValues(*values)
fun  Map.shouldContainValues(vararg values: V) = this should haveValues(*values)
fun  Map.shouldContainAnyValuesOf(vararg values: V) = this should containAnyValues(*values)
fun  Map.shouldNotHaveValues(vararg values: V) = this shouldNot haveValues(*values)
fun  Map.shouldNotContainValues(vararg values: V) = this shouldNot haveValues(*values)
fun  Map.shouldNotContainAnyValuesOf(vararg values: V) = this shouldNot containAnyValues(*values)

fun  Map.shouldBeEmpty() = this should beEmpty()
fun  Map.shouldNotBeEmpty() = this shouldNot beEmpty()

fun beEmpty() = object : Matcher> {
   override fun test(value: Map<*, *>): MatcherResult {
      return MatcherResult(
         value.isEmpty(),
         { "Map should be empty, but was $value." },
         { "Map should not be empty, but was." }
      )
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy