Best Kotest code snippet using io.kotest.matchers.maps.matchers.Map.shouldContainKeys
MapMatchersTest.kt
Source:MapMatchersTest.kt  
1package com.sksamuel.kotest.matchers.maps2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.WordSpec4import io.kotest.matchers.and5import io.kotest.matchers.maps.*6import io.kotest.matchers.should7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldNot9import java.util.LinkedList10class MapMatchersTest : WordSpec() {11   init {12      "haveKey" should {13         "test that a map contains the given key" {14            val map = mapOf(Pair(1, "a"), Pair(2, "b"))15            map should haveKey(1)16            map.shouldContainKey(1)17            map.shouldNotContainKey(4)18            shouldThrow<AssertionError> {19               map should haveKey(3)20            }21            shouldThrow<AssertionError> {22               map.shouldContainKey(5)23            }.message.shouldBe("Map should contain key 5")24            shouldThrow<AssertionError> {25               map.shouldNotContainKey(1)26            }.message.shouldBe("Map should not contain key 1")27         }28         "support maps and mutable maps" {29            val mutable = mutableMapOf("a" to "b")30            mutable.shouldHaveKey("a")31            val map = mapOf("a" to "b")32            map.shouldHaveKey("a")33         }34      }35      "haveValue" should {36         "test that a map contains the given value" {37            val map = mapOf(Pair(1, "a"), Pair(2, "b"))38            map should haveValue("a")39            map.shouldContainValue("a")40            map.shouldNotContainValue("A")41            shouldThrow<AssertionError> {42               map should haveValue("c")43            }44            shouldThrow<AssertionError> {45               map.shouldContainValue("c")46            }.message.shouldBe("Map should contain value c")47         }48      }49      "contain" should {50         "test that a map contains the given pair" {51            val map = mapOf(Pair(1, "a"), Pair(2, "b"))52            map should contain(1, "a")53            map.shouldContain(2, "b")54            map.shouldNotContain(3, "A")55            map shouldContain (1 to "a")56            map shouldNotContain (3 to "A")57            shouldThrow<AssertionError> {58               map.shouldContain(1, "c")59            }.message.shouldBe("Map should contain mapping 1=c but was 1=a")60            shouldThrow<AssertionError> {61               map.shouldContain(4, "e")62            }.message.shouldBe("Map should contain mapping 4=e but was {1=a, 2=b}")63            shouldThrow<AssertionError> {64               map should contain(2, "a")65            }.message.shouldBe("Map should contain mapping 2=a but was 2=b")66         }67      }68      "haveKeys" should {69         "test that a map contains all given keys" {70            val map = mapOf(Pair(1, "a"), Pair(2, "b"), Pair(3, "c"))71            map should haveKeys(1, 3)72            map should haveKeys(1, 2, 3)73            map.shouldContainKeys(1, 3)74            map.shouldContainKeys(1, 2, 3)75            shouldThrow<AssertionError> {76               map should haveKeys(4)77            }78            shouldThrow<AssertionError> {79               map should haveKeys(1, 4)80            }81            shouldThrow<AssertionError> {82               map.shouldContainKeys(1, 4, 5, 6)83            }.message.shouldBe("Map did not contain the keys 4, 5, 6")84         }85      }86      "haveValues" should {87         "test that a map contains all given values" {88            val map = mapOf(Pair(1, "a"), Pair(2, "b"), Pair(3, "c"))89            map should haveValues("b", "c")90            map should haveValues("a", "b", "c")91            map.shouldContainValues("b", "c")92            map.shouldContainValues("a", "b", "c")93            shouldThrow<AssertionError> {94               map should haveValues("a", "d")95            }96            shouldThrow<AssertionError> {97               map should haveValues("d")98            }99            shouldThrow<AssertionError> {100               map.shouldContainValues("d")101            }.message.shouldBe("Map did not contain the values d")102            shouldThrow<AssertionError> {103               map.shouldNotContainValues("a", "b")104            }.message.shouldBe("Map should not contain the values a, b")105         }106      }107      "containAnyKeys" should {108         "test that a map contains any of the given keys"{109            val map = mapOf("a" to 1, "b" to 2, "c" to 3)110            map should containAnyKeys("a", "x", "y")111            map should containAnyKeys("a", "b", "c")112            map should containAnyKeys("a", "b")113            map.shouldContainAnyKeysOf("a", "c")114            shouldThrow<AssertionError> {115               map should containAnyKeys("x", "y", "z")116            }117            shouldThrow<AssertionError> {118               map.shouldContainAnyKeysOf("x", "y")119            }120            shouldThrow<AssertionError> {121               map.shouldNotContainAnyKeysOf("a", "y")122            }123         }124      }125      "containAnyValues" should {126         "test that a map contains any of the given values"{127            val map = mapOf("a" to 1, "b" to 2, "c" to 3)128            map should containAnyValues(1, 23, 24)129            map should containAnyValues(1, 2, 3)130            map should containAnyValues(3, 2)131            map.shouldContainAnyValuesOf(2, 3)132            shouldThrow<AssertionError> {133               map should containAnyValues(9, 8, 7)134            }135            shouldThrow<AssertionError> {136               map.shouldContainAnyValuesOf(4, 5)137            }138            shouldThrow<AssertionError> {139               map.shouldNotContainAnyValuesOf(1,5)140            }141         }142      }143      "containAll" should {144         "test that a map contains all given pairs" {145            val map = mapOf(Pair(1, "a"), Pair(2, "b"), Pair(3, "c"))146            map should containAll(mapOf(1 to "a", 3 to "c"))147            map should containAll(mapOf(3 to "c"))148            map.shouldContainAll(mapOf(1 to "a", 3 to "c"))149            map.shouldNotContainAll(mapOf(1 to "a", 3 to "h"))150         }151         "test empty map" {152            emptyMap<Any, Any>() should containAll(emptyMap<Any, Any>())153            emptyMap<Any, Any>().shouldContainAll(emptyMap<Any, Any>())154         }155         "test assertion that map does not contain entries from the given map" {156            val e = shouldThrow<AssertionError> {157               emptyMap<Any, Any>() should containAll(mapOf<Any, Any>("\$a" to 1))158            }159            e.message shouldBe """160          |161          |Expected:162          |  mapOf()163          |should contain all of:164          |  mapOf("\${'$'}a" to 1)165          |but differs by:166          |  missing keys:167          |    "\${'$'}a"168          |169        """.trimMargin()170         }171         "test when map contains extra entries" {172            mapOf("a" to 1, "b" to 2) should (173               containAll(mapOf("a" to 1)) and containAll(mapOf("b" to 2)))174         }175         "test assertion when map contains different value type" {176            val e = shouldThrow<AssertionError> {177               mapOf("a" to 1) should containAll(mapOf<String, Any>("a" to 1L))178            }179            e.message shouldBe """180          |181          |Expected:182          |  mapOf("a" to 1)183          |should contain all of:184          |  mapOf("a" to 1L)185          |but differs by:186          |  different values:187          |    "a":188          |      expected:189          |        1L190          |      but was:191          |        1192          |193        """.trimMargin()194         }195         "test that a map with nested map contains all entries from the given map" {196            val map = mapOf("a" to mapOf("b" to 2))197            map should containAll(mapOf("a" to mapOf("b" to 2)))198            val e = shouldThrow<AssertionError> {199               map should containAll(mapOf("a" to mapOf("b" to 3)))200            }201            e.message shouldBe """202          |203          |Expected:204          |  mapOf("a" to mapOf("b" to 2))205          |should contain all of:206          |  mapOf("a" to mapOf("b" to 3))207          |but differs by:208          |  different values:209          |    "a":210          |      different values:211          |        "b":212          |          expected:213          |            3214          |          but was:215          |            2216          |217        """.trimMargin()218         }219         "test shouldNot assertion" {220            val e = shouldThrow<AssertionError> {221               mapOf("a" to 1, "b" to 2) shouldNot containAll(mapOf("a" to 1))222            }223            e.message shouldBe """224          |225          |Expected:226          |  mapOf("a" to 1, "b" to 2)227          |should not contain all of:228          |  mapOf("a" to 1)229          |but contains230          |231        """.trimMargin()232         }233      }234      "containExactly" should {235         "test empty map" {236            emptyMap<Any, Any>() should containExactly(emptyMap<Any, Any>())237            emptyMap<Any, Any>().shouldContainExactly(emptyMap<Any, Any>())238         }239         "test assertion that a map contains extra keys" {240            val e = shouldThrow<AssertionError> {241               mapOf("a" to 1) should containExactly(emptyMap<String, Any>())242            }243            e.message shouldBe """244          |245          |Expected:246          |  mapOf("a" to 1)247          |should be equal to:248          |  mapOf()249          |but differs by:250          |  extra keys:251          |    "a"252          |253        """.trimMargin()254         }255         "test shouldNot assertion" {256            val e = shouldThrow<AssertionError> {257               val arrayList: List<Int> = arrayListOf(1)258               val linkedList = LinkedList<Int>()259               linkedList.push(1)260               mapOf("a" to arrayList) shouldNot containExactly<String, List<Int>>(mapOf("a" to linkedList))261               mapOf("a" to arrayList) shouldNot containExactly<String, List<Int>>("a" to linkedList)262            }263            e.message shouldBe """264          |265          |Expected:266          |  mapOf("a" to listOf(1))267          |should not be equal to:268          |  mapOf("a" to listOf(1))269          |but equals270          |271        """.trimMargin()272         }273      }274      "be empty" should {275         "Work on an empty map" {276            emptyMap<String, String>().shouldBeEmpty()277         }278         "Fail on a non empty map" {279            shouldThrow<AssertionError> {280               mapOf("Foo" to "Bar").shouldBeEmpty()281            }282         }283      }284      "Not be empty" should {285         "Fail on an empty map" {286            shouldThrow<AssertionError> {287               emptyMap<String, String>().shouldNotBeEmpty()288            }289         }290         "Pass on a non empty map" {291            mapOf("Foo" to "Bar").shouldNotBeEmpty()292         }293      }294      "haveSize" should {295         "test that a map has given size" {296            val map = mapOf(1 to "a", 2 to "b")297            map should haveSize(2)298            map shouldHaveSize (2)299            shouldThrow<AssertionError> {300               map should haveSize(3)301            }302            shouldThrow<AssertionError> {303               map shouldHaveSize (3)304            }305         }306      }307      "map comparision" should {308         "give formatted error in case of failure assertion error with shouldBeEqualTo" {309            val map1 = mapOf(310               "Key1" to "Val11",311               "Key2" to "Val21",312               "Key3" to "Val31",313               "Key4" to "Val41",314               "Key5" to "Val51",315               "Key6" to "Val61",316               "Key7" to "Val71",317               "Key8" to "Val81",318               "Key9" to "Val91",319               "Key10" to "Val101",320               "Key11" to "Val111",321               "Key12" to "Val121"322            )323            val map2 = mapOf(324               "Key1" to "Val11",325               "Key2" to "Val22",326               "Key3" to "Val32",327               "Key4" to "Val42",328               "Key5" to "Val52",329               "Key6" to "Val62",330               "Key7" to "Val72",331               "Key8" to "Val82",332               "Key9" to "Val92",333               "Key10" to "Val102",334               "Key11" to "Val112",335               "Key12" to "Val122"336            )337            val expectedErrorMessage = """Expected338{339  "Key1" = "Val11",340  "Key2" = "Val22",341  "Key3" = "Val32",342  "Key4" = "Val42",343  "Key5" = "Val52",344  "Key6" = "Val62",345  "Key7" = "Val72",346  "Key8" = "Val82",347  "Key9" = "Val92",348  "Key10" = "Val102",349...350}351to be equal to352{353  "Key1" = "Val11",354  "Key2" = "Val21",355  "Key3" = "Val31",356  "Key4" = "Val41",357  "Key5" = "Val51",358  "Key6" = "Val61",359  "Key7" = "Val71",360  "Key8" = "Val81",361  "Key9" = "Val91",362  "Key10" = "Val101",363...364}365Values differed at keys Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Key10, Key11, ...""".trimMargin()366            val assertionError = shouldThrow<AssertionError> { map1 shouldBe map2 }367            assertionError.message shouldBe """Values differed at keys Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Key10, Key11, ...368expected:<{369  "Key1" = "Val11",370  "Key2" = "Val22",371  "Key3" = "Val32",372  "Key4" = "Val42",373  "Key5" = "Val52",374  "Key6" = "Val62",375  "Key7" = "Val72",376  "Key8" = "Val82",377  "Key9" = "Val92",378  "Key10" = "Val102",379...380}> but was:<{381  "Key1" = "Val11",382  "Key2" = "Val21",383  "Key3" = "Val31",384  "Key4" = "Val41",385  "Key5" = "Val51",386  "Key6" = "Val61",387  "Key7" = "Val71",388  "Key8" = "Val81",389  "Key9" = "Val91",390  "Key10" = "Val101",391...392}>"""393         }394      }395   }396}...AnimatedLEDStripServerTest.kt
Source:AnimatedLEDStripServerTest.kt  
1/*2 * Copyright (c) 2018-2021 AnimatedLEDStrip3 *4 * Permission is hereby granted, free of charge, to any person obtaining a copy5 * of this software and associated documentation files (the "Software"), to deal6 * in the Software without restriction, including without limitation the rights7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell8 * copies of the Software, and to permit persons to whom the Software is9 * furnished to do so, subject to the following conditions:10 *11 * The above copyright notice and this permission notice shall be included in12 * all copies or substantial portions of the Software.13 *14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN20 * THE SOFTWARE.21 */22package animatedledstrip.test23import animatedledstrip.animations.parameters.PercentDistance24import animatedledstrip.colors.ColorContainer25import animatedledstrip.colors.ccpresets.randomColorList26import animatedledstrip.leds.animationmanagement.AnimationToRunParams27import animatedledstrip.leds.stripmanagement.NativeLEDStrip28import animatedledstrip.server.AnimatedLEDStripServer29import io.kotest.assertions.throwables.shouldThrow30import io.kotest.core.spec.style.StringSpec31import io.kotest.matchers.booleans.shouldBeFalse32import io.kotest.matchers.booleans.shouldBeTrue33import io.kotest.matchers.maps.shouldContainKeys34import io.kotest.matchers.paths.shouldExist35import io.kotest.matchers.paths.shouldNotExist36import kotlinx.coroutines.delay37import java.nio.file.Paths38class AnimatedLEDStripServerTest : StringSpec(39    {40        "test start stop" {41            val server = newTestServer()42            server.start()43            server.running.shouldBeTrue()44            server.leds.renderer.isRendering.shouldBeTrue()45            server.stop()46            server.running.shouldBeFalse()47            server.leds.renderer.isRendering.shouldBeFalse()48        }49        "test bad NativeLEDStrip constructor" {50            class BadStrip : NativeLEDStrip {51                override val numLEDs: Int = 052                override fun close() {}53                override fun render() {}54                override fun setPixelColor(pixel: Int, color: Int) {}55            }56            shouldThrow<IllegalArgumentException> {57                AnimatedLEDStripServer(arrayOf(), BadStrip::class)58            }59            class BadStrip2(override val numLEDs: Int) : NativeLEDStrip {60                override fun close() {}61                override fun render() {}62                override fun setPixelColor(pixel: Int, color: Int) {}63            }64            shouldThrow<IllegalArgumentException> {65                AnimatedLEDStripServer(arrayOf(), BadStrip2::class)66            }67        }68        "test save persistent animation".config(enabled = false) {69            val server = newTestServer("--persist", "--persist-dir", "src/jvmTest/resources/persist-save")70            val anim = AnimationToRunParams("Runway Lights", ColorContainer.randomColorList(), id = "test",71                                            doubleParams = mutableMapOf("maximumInfluence" to 3.0, "spacing" to 10.0),72                                            distanceParams = mutableMapOf("offset" to PercentDistance(0.0,73                                                                                                      50.0,74                                                                                                      0.0))).prepare(75                server.leds.sectionManager.getSection(""))76            server.savePersistentAnimation(anim)77            delay(500)78            Paths.get("src/jvmTest/resources/persist-save/.animations/test.json").shouldExist()79        }80        "test delete persistent animation".config(enabled = false) {81            val server = newTestServer("--persist", "--persist-dir", "src/jvmTest/resources/persist-delete")82            val anim = AnimationToRunParams("Runway Lights", ColorContainer.randomColorList(), id = "test2",83                                            doubleParams = mutableMapOf("maximumInfluence" to 3.0, "spacing" to 10.0),84                                            distanceParams = mutableMapOf("offset" to PercentDistance(0.0,85                                                                                                      50.0,86                                                                                                      0.0)))87                .prepare(server.leds.sectionManager.getSection(""))88            server.savePersistentAnimation(anim)89            delay(500)90            Paths.get("src/jvmTest/resources/persist-delete/.animations/test2.json").shouldExist()91            delay(500)92            server.deletePersistentAnimation(anim)93            delay(500)94            Paths.get("src/jvmTest/resources/persist-delete/.animations/test2.json").shouldNotExist()95        }96        "test load persistent animations" {97            val server = newTestServer("--persist", "--persist-dir", "src/jvmTest/resources/persist")98            server.loadPersistentAnimations()99            delay(1000)100            server.leds.animationManager.runningAnimations.shouldContainKeys("23602685",101                                                                             "40202146",102                                                                             "44470329",103                                                                             "51140794",104                                                                             "65067451",105                                                                             "84029121")106        }107    }108)...HelpersTests.kt
Source:HelpersTests.kt  
1package msw.server.core.common2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.booleans.shouldBeFalse5import io.kotest.matchers.booleans.shouldBeTrue6import io.kotest.matchers.maps.shouldContainKeys7import io.kotest.matchers.nulls.shouldBeNull8import io.kotest.matchers.shouldBe9import io.kotest.matchers.types.shouldBeInstanceOf10import java.io.File11class HelpersTests : FunSpec({12    context("String.isNumeric() Tests: ") {13        test("String.isNumeric() should behave according to the contents of the string") {14            "123".isNumeric().shouldBeTrue()15            "abc".isNumeric().shouldBeFalse()16        }17    }18    context("existsOrNull(File) Tests: ") {19        test("File path '$exists' exists, therefore existsOrNull(File(path)) should return File") {20            existsOrNull(File(exists)).shouldBeInstanceOf<File>()21        }22        test("File path '$existsNot' does not exist, therefore existsOrNull(File(path) should return null") {23            existsOrNull(File(existsNot)).shouldBeNull()24        }25    }26    context("String.replaceMultiple(vararg Pair<Char, Char>, Boolean) Tests") {27        test("String.replaceMultiple() should be capabable of transforming 'I love Kotlin' to leetspeak") {28            "I love Kotlin".replaceMultiple(29                "I" to "1",30                "o" to "0",31                "e" to "3",32                "t" to "7",33                ignoreCase = true34            ) shouldBe "1 l0v3 K07l1n"35        }36    }37    context("String.escape(vararg Char, Boolean) / String.unescape(vararg Char, Boolean) Tests") {38        test("String.escape() should escape a string containing the special characters '~', '/', '+', '-'") {39            val escaped = randomString(50, *escapeThese).escape(*escapeThese)40            for (i in escaped.indices) {41                (escaped[i] !in escapeThese || (i > 0 && escaped[i - 1] == '\\')).shouldBeTrue()42            }43        }44        test("String.unescape() should unescape a string containing the escaped characters '~', '/', '+', '-'") {45            val unescaped = randomString(50, *escapeThese).escape(*escapeThese).unescape(*escapeThese)46            for (i in unescaped.indices) {47                (unescaped[i] !in escapeThese || i < 1 || unescaped[i - 1] != '\\').shouldBeTrue()48            }49        }50    }51    context("xxxJoin(Map<K, V>, Map<K, U>) variations Tests") {52        test("innerJoin() should only contain KV-Pairs present in both maps") {53            innerJoin(map1, map2).shouldContainKeys("Hello", "MSW", "KXS")54        }55        test("leftOuterJoin() should contain all keys from the first map") {56            leftOuterJoin(map1, map2).shouldContainKeys(*map1.keys.toTypedArray())57        }58        test("rightOuterJoin() should contain all keys from the second map") {59            rightOuterJoin(map1, map2).shouldContainKeys(*map2.keys.toTypedArray())60        }61        test("fullOuterJoin() should contain all keys from both maps") {62            val allKeys = map1.keys.toMutableSet().apply { addAll(map2.keys) }.toTypedArray()63            fullOuterJoin(map1, map2).shouldContainKeys(*allKeys)64        }65        test("errorJoin() should not fulfill its preconditions and throw an IllegalArgumentException") {66            shouldThrow<IllegalArgumentException> {67                errorJoin(map1, map2)68            }69        }70    }71})...matchers.kt
Source:matchers.kt  
1package io.kotest.matchers.maps2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6fun <K, V> mapcontain(key: K, v: V) = object : Matcher<Map<K, V>> {7  override fun test(value: Map<K, V>) = MatcherResult(8     value[key] == v,9     "Map should contain mapping $key=$v but was ${buildActualValue(value)}",10     "Map should not contain mapping $key=$v but was $value"11  )12  private fun buildActualValue(map: Map<K, V>) = map[key]?.let { "$key=$it" } ?: map13}14fun <K, V> Map<K, V>.shouldContain(key: K, value: V) = this should mapcontain(key, value)15fun <K, V> Map<K, V>.shouldNotContain(key: K, value: V) = this shouldNot mapcontain(key, value)16infix fun <K, V> Map<K, V>.shouldContain(entry: Pair<K, V>) =17  this should mapcontain(entry.first, entry.second)18infix fun <K, V> Map<K, V>.shouldNotContain(entry: Pair<K, V>) =19  this shouldNot mapcontain(entry.first, entry.second)20infix fun <K, V> Map<K, V>.shouldContainExactly(expected: Map<K, V>) =21  this should containExactly(expected)22infix fun <K, V> Map<K, V>.shouldNotContainExactly(expected: Map<K, V>) =23  this shouldNot containExactly(expected)24infix fun <K, V> Map<K, V>.shouldContainAll(expected: Map<K, V>) = this should containAll(expected)25infix fun <K, V> Map<K, V>.shouldNotContainAll(expected: Map<K, V>) =26  this shouldNot containAll(expected)27infix fun <K, V : Any> Map<K, V>.shouldHaveKey(key: K) = this should haveKey(key)28infix fun <K, V : Any> Map<K, V>.shouldContainKey(key: K) = this should haveKey(key)29infix fun <K, V : Any> Map<K, V>.shouldNotHaveKey(key: K) = this shouldNot haveKey(key)30infix fun <K, V : Any> Map<K, V>.shouldNotContainKey(key: K) = this shouldNot haveKey(key)31infix fun <K, V> Map<K, V>.shouldContainValue(value: V) = this should haveValue<V>(value)32infix fun <K, V> Map<K, V>.shouldNotContainValue(value: V) = this shouldNot haveValue<V>(value)33infix fun <K, V> Map<K, V>.shouldHaveSize(size: Int) = this should haveSize(size)34fun <K, V> Map<K, V>.shouldHaveKeys(vararg keys: K) = this should haveKeys(*keys)35fun <K, V> Map<K, V>.shouldContainKeys(vararg keys: K) = this should haveKeys(*keys)36fun <K, V> Map<K, V>.shouldNotHaveKeys(vararg keys: K) = this shouldNot haveKeys(*keys)37fun <K, V> Map<K, V>.shouldNotContainKeys(vararg keys: K) = this shouldNot haveKeys(*keys)38fun <K, V> Map<K, V>.shouldHaveValues(vararg values: V) = this should haveValues(*values)39fun <K, V> Map<K, V>.shouldContainValues(vararg values: V) = this should haveValues(*values)40fun <K, V> Map<K, V>.shouldNotHaveValues(vararg values: V) = this shouldNot haveValues(*values)41fun <K, V> Map<K, V>.shouldNotContainValues(vararg values: V) = this shouldNot haveValues(*values)42fun <K, V> Map<K, V>.shouldBeEmpty() = this should beEmpty()43fun <K, V> Map<K, V>.shouldNotBeEmpty() = this shouldNot beEmpty()44fun beEmpty() = object : Matcher<Map<*, *>> {45  override fun test(value: Map<*, *>): MatcherResult {46    return MatcherResult(47       value.isEmpty(),48       { "Map should be empty, but was $value." },49       { "Map should not be empty, but was." }50    )51  }52}...OrtConfigPackageCurationProviderFunTest.kt
Source:OrtConfigPackageCurationProviderFunTest.kt  
1/*2 * Copyright (C) 2022 Bosch.IO GmbH3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 *     https://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 * SPDX-License-Identifier: Apache-2.017 * License-Filename: LICENSE18 */19package org.ossreviewtoolkit.analyzer.curation20import io.kotest.core.spec.style.StringSpec21import io.kotest.matchers.collections.beEmpty22import io.kotest.matchers.maps.beEmpty as beEmptyMap23import io.kotest.matchers.maps.shouldContainKeys24import io.kotest.matchers.should25import io.kotest.matchers.shouldNot26import org.ossreviewtoolkit.model.Identifier27class OrtConfigPackageCurationProviderFunTest : StringSpec() {28    init {29        "provider can load curations from the ort-config repository" {30            val azureCore = Identifier("NuGet::Azure.Core:1.22.0")31            val azureCoreAmqp = Identifier("NuGet::Azure.Core.Amqp:1.2.0")32            val curations = OrtConfigPackageCurationProvider().getCurationsFor(listOf(azureCore, azureCoreAmqp))33            curations.shouldContainKeys(azureCore, azureCoreAmqp)34            curations.getValue(azureCore) shouldNot beEmpty()35            curations.getValue(azureCoreAmqp) shouldNot beEmpty()36        }37        "provider does not fail for packages which have no curations" {38            val id = Identifier("Some:Bogus:Package:Id")39            val curations = OrtConfigPackageCurationProvider().getCurationsFor(listOf(id))40            curations should beEmptyMap()41        }42    }43}...JsonUtilTests.kt
Source:JsonUtilTests.kt  
1package com.ancientlightstudios.simplegen2import io.kotest.core.spec.style.BehaviorSpec3import io.kotest.matchers.collections.shouldContainAll4import io.kotest.matchers.maps.shouldContainKeys5class JsonUtilTests : BehaviorSpec({6    Given("i have two simple maps") {7        val node1 = mapOf(8                "narf" to mapOf<String, Any>(9                        "narf" to "barf"10                ),11                "narf3" to "arf"12        )13        val node2 = mapOf<String, Any>(14                "narf" to mapOf<String, Any>(15                        "narf2" to "barf"16                ),17                "narf2" to mapOf<String, Any>(18                        "narf" to "barf",19                        "narf2" to "barf"20                )21        )22        When("merging these maps") {23            val result = JsonUtil.merge(node1, node2)24            Then("it merges the top level objects correctly") {25                result.shouldContainKeys("narf", "narf2", "narf3")26            }27            Then("it merges second level objects correctly") {28                @Suppress("UNCHECKED_CAST")29                (result["narf"] as Map<String, Any>).shouldContainKeys("narf2")30                @Suppress("UNCHECKED_CAST")31                (result["narf2"] as Map<String, Any>).shouldContainKeys("narf")32            }33        }34    }35    Given("i have some maps with nested lists") {36        val node1 = mapOf<String, Any>(37                "narf" to listOf("lorem", "ipsum")38        )39        val node2 = mapOf<String, Any>(40                "narf" to listOf("dolor", "sit")41        )42        When("merging those lists") {43            val result = JsonUtil.merge(node1, node2)44            val finalList = result["narf"] as List<*>45            Then("merges the lists") {46                finalList.shouldContainAll("lorem", "ipsum", "dolor", "sit")47            }48        }49    }50})...Map.shouldContainKeys
Using AI Code Generation
1import io.kotest.matchers.maps.shouldContainKeys2val map = mapOf("a" to 1, "b" to 2, "c" to 3)3import io.kotest.matchers.maps.shouldContainAllKeys4val map = mapOf("a" to 1, "b" to 2, "c" to 3)5map shouldContainAllKeys listOf("a", "b", "c")6import io.kotest.matchers.maps.shouldContainOnlyKeys7val map = mapOf("a" to 1, "b" to 2, "c" to 3)8map shouldContainOnlyKeys listOf("a", "b", "c")9import io.kotest.matchers.maps.shouldContainExactlyKeys10val map = mapOf("a" to 1, "b" to 2, "c" to 3)11map shouldContainExactlyKeys listOf("a", "b", "c")12import io.kotest.matchers.maps.shouldContainExactlyInAnyOrderKeys13val map = mapOf("a" to 1, "b" to 2, "c" to 3)14map shouldContainExactlyInAnyOrderKeys listOf("a", "b", "c")15import io.kotest.matchers.maps.shouldContainNoneKeys16val map = mapOf("a" to 1, "b" to 2, "c" to 3)17map shouldContainNoneKeys listOf("d", "e", "f")18import io.kotest.matchers.maps.shouldContainKey19val map = mapOf("a" to 1, "b" to 2, "c" to 3Map.shouldContainKeys
Using AI Code Generation
1val map = mapOf(1 to "one", 2 to "two")2map shouldContainKeys setOf(1, 2)3val map = mapOf(1 to "one", 2 to "two")4map shouldNotContainKeys setOf(1, 3)5val map = mapOf(1 to "one", 2 to "two")6map shouldContainValue listOf("one", "two")7val map = mapOf(1 to "one", 2 to "two")8map shouldNotContainValue listOf("one", "three")9val map = mapOf(1 to "one", 2 to "two")10map shouldContainEntry listOf(1 to "one", 2 to "two")11val map = mapOf(1 to "one", 2 to "two")12map shouldNotContainEntry listOf(1 to "one", 3 to "three")13val map = mapOf(1 to "one", 2 to "two")14map shouldContainExactly mapOf(1 to "one", 2 to "two")15map shouldContainExactly listOf(1 to "one", 2 to "two")Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
