How to use Map.shouldHaveKeys method of io.kotest.matchers.maps.matchers class

Best Kotest code snippet using io.kotest.matchers.maps.matchers.Map.shouldHaveKeys

ALSHttpClientTest.kt

Source:ALSHttpClientTest.kt Github

copy

Full Screen

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.test.client23import animatedledstrip.animations.groups.AnimationGroup24import animatedledstrip.animations.groups.GroupType25import animatedledstrip.animations.predefined.*26import animatedledstrip.animations.predefinedgroups.sortingAnimations27import animatedledstrip.client.ALSHttpClient28import animatedledstrip.leds.animationmanagement.AnimationToRunParams29import animatedledstrip.leds.animationmanagement.RunningAnimationParams30import animatedledstrip.leds.sectionmanagement.Section31import animatedledstrip.leds.stripmanagement.StripInfo32import io.kotest.assertions.throwables.shouldThrow33import io.kotest.matchers.collections.shouldHaveSize34import io.kotest.matchers.maps.shouldHaveKeys35import io.kotest.matchers.maps.shouldHaveSize36import io.kotest.matchers.shouldBe37import io.ktor.client.engine.mock.MockEngine38import io.ktor.client.engine.mock.MockRequestHandleScope39import io.ktor.client.engine.mock.respond40import io.ktor.client.features.json.JsonFeature41import io.ktor.client.features.json.serializer.KotlinxSerializer42import io.ktor.http.HttpMethod43import io.ktor.http.HttpStatusCode44import io.ktor.http.fullPath45import io.ktor.http.headersOf46import kotlinx.serialization.encodeToString47import kotlin.js.JsName48import kotlin.test.Test49import animatedledstrip.communication.serializer as alsSerializer50class ALSHttpClientTest {51 private inline fun <reified T> MockRequestHandleScope.serializeAndRespond(data: T) =52 respond(alsSerializer.encodeToString(data),53 HttpStatusCode.OK,54 headersOf("Content-Type", "application/json"))55 private val testClient = ALSHttpClient(MockEngine, "testURL") {56 install(JsonFeature) {57 serializer = KotlinxSerializer(alsSerializer)58 }59 engine {60 addHandler { request ->61 when (request.url.fullPath) {62 "/animation/alternate" -> serializeAndRespond(alternate.info)63 "/animations/names" -> serializeAndRespond(listOf("anim1", "anim2", "anim3"))64 "/animations" -> serializeAndRespond(listOf(alternate.info, bounce.info))65 "/animations/map" -> serializeAndRespond(mapOf("Alternate" to alternate.info,66 "Bounce" to bounce.info))67 "/animations/newGroup" -> serializeAndRespond(sortingAnimations.groupInfo)68 "/sections" -> {69 when (request.method) {70 HttpMethod.Get -> serializeAndRespond(listOf(Section("s1", listOf(1, 2, 3, 4), "s0"),71 Section("s2", listOf(5, 8, 1, 3))))72 HttpMethod.Post -> serializeAndRespond(Section("s3", listOf(5, 6, 7, 8), "s2"))73 else -> error("Unsupported method ${request.method}")74 }75 }76 "/sections/map" -> serializeAndRespond(mapOf("s1" to Section("s1", listOf(1, 2, 3, 4), "s0"),77 "s2" to Section("s2", listOf(5, 8, 1, 3))))78 "/section/s1" -> serializeAndRespond(Section("s1", listOf(1, 2, 3, 4), "s0"))79 "/section/s2" -> serializeAndRespond(Section("s2", listOf(5, 8, 1, 3)))80 "/section/fullStrip" -> serializeAndRespond(Section("s0", (0..9).toList()))81 "/running" -> serializeAndRespond(82 mapOf("anim1" to RunningAnimationParams(color,83 "color",84 listOf(),85 "anim1",86 "",87 -1,88 mapOf(),89 mapOf(),90 mapOf(),91 mapOf(),92 mapOf(),93 mapOf(),94 mapOf(),95 AnimationToRunParams()),96 "anim2" to RunningAnimationParams(color,97 "color",98 listOf(),99 "anim2",100 "",101 -1,102 mapOf(),103 mapOf(),104 mapOf(),105 mapOf(),106 mapOf(),107 mapOf(),108 mapOf(),109 AnimationToRunParams())))110 "/running/ids" -> serializeAndRespond(listOf("anim4", "anim5", "anim6"))111 "/running/anim7" -> serializeAndRespond(RunningAnimationParams(color,112 "color",113 listOf(),114 "anim7",115 "",116 -1,117 mapOf(),118 mapOf(),119 mapOf(),120 mapOf(),121 mapOf(),122 mapOf(),123 mapOf(),124 AnimationToRunParams()))125 "/running/anim8" -> serializeAndRespond(RunningAnimationParams(color,126 "color",127 listOf(),128 "anim8",129 "",130 -1,131 mapOf(),132 mapOf(),133 mapOf(),134 mapOf(),135 mapOf(),136 mapOf(),137 mapOf(),138 AnimationToRunParams()))139 "/running/anim9" -> serializeAndRespond(RunningAnimationParams(color,140 "color",141 listOf(),142 "anim9",143 "",144 -1,145 mapOf(),146 mapOf(),147 mapOf(),148 mapOf(),149 mapOf(),150 mapOf(),151 mapOf(),152 AnimationToRunParams()))153 "/start" -> serializeAndRespond(RunningAnimationParams(color,154 "color",155 listOf(),156 "",157 "",158 -1,159 mapOf(),160 mapOf(),161 mapOf(),162 mapOf(),163 mapOf(),164 mapOf(),165 mapOf(),166 AnimationToRunParams()))167 "/strip/info" -> serializeAndRespond(StripInfo(10, 12, is2DSupported = true))168 "/strip/color" -> serializeAndRespond(listOf(0xFF, 0xFF, 0xFFFF, 0xFFFF, 0xFFFFFF, 0xFFFFFF))169 else -> error("Unhandled ${request.url.fullPath}")170 }171 }172 }173 }174 @Test175 @JsName("resolvePath")176 fun `resolve path`() {177 testClient.resolvePath("/testEndpoint") shouldBe "http://testURL:8080/testEndpoint"178 testClient.resolvePath("testEndpoint") shouldBe "http://testURL:8080/testEndpoint"179 }180 @Test181 @JsName("getAnimationInfo")182 fun `get animation info`() = runInCoroutine {183 shouldThrow<IllegalArgumentException> {184 testClient.getAnimationInfo("")185 }186 testClient.getAnimationInfo("alternate") shouldBe alternate.info187 }188 @Test189 @JsName("getSupportedAnimations")190 fun `get supported animations`() = runInCoroutine {191 testClient.getSupportedAnimations() shouldBe listOf(alternate.info, bounce.info)192 }193 @Test194 @JsName("getSupportedAnimationsMap")195 fun `get supported animations map`() = runInCoroutine {196 testClient.getSupportedAnimationsMap() shouldBe mapOf("Alternate" to alternate.info,197 "Bounce" to bounce.info)198 }199 @Test200 @JsName("getSupportedAnimationsNames")201 fun `get supported animations names`() = runInCoroutine {202 testClient.getSupportedAnimationsNames() shouldBe listOf("anim1", "anim2", "anim3")203 }204 @Test205 @JsName("createNewGroup")206 fun `create new group`() = runInCoroutine {207 testClient.createNewGroup(sortingAnimations) shouldBe sortingAnimations.groupInfo208 }209 @Test210 @JsName("getRunningAnimations")211 fun `get running animations`() = runInCoroutine {212 testClient.getRunningAnimations() shouldBe213 mapOf("anim1" to RunningAnimationParams(color,214 "color",215 listOf(),216 "anim1",217 "",218 -1,219 mapOf(),220 mapOf(),221 mapOf(),222 mapOf(),223 mapOf(),224 mapOf(),225 mapOf(),226 AnimationToRunParams()),227 "anim2" to RunningAnimationParams(color,228 "color",229 listOf(),230 "anim2",231 "",232 -1,233 mapOf(),234 mapOf(),235 mapOf(),236 mapOf(),237 mapOf(),238 mapOf(),239 mapOf(),240 AnimationToRunParams()))241 }242 @Test243 @JsName("getRunningAnimationsIds")244 fun `get running animations ids`() = runInCoroutine {245 testClient.getRunningAnimationsIds() shouldBe listOf("anim4", "anim5", "anim6")246 }247 @Test248 @JsName("getRunningAnimationParams")249 fun `get running animation params`() = runInCoroutine {250 testClient.getRunningAnimationParams("anim7") shouldBe251 RunningAnimationParams(color,252 "color",253 listOf(),254 "anim7",255 "",256 -1,257 mapOf(),258 mapOf(),259 mapOf(),260 mapOf(),261 mapOf(),262 mapOf(),263 mapOf(),264 AnimationToRunParams())265 }266 @Test267 @JsName("endAnimation")268 fun `end animation`() = runInCoroutine {269 testClient.endAnimation("anim8") shouldBe270 RunningAnimationParams(color,271 "color",272 listOf(),273 "anim8",274 "",275 -1,276 mapOf(),277 mapOf(),278 mapOf(),279 mapOf(),280 mapOf(),281 mapOf(),282 mapOf(),283 AnimationToRunParams())284 testClient.endAnimation(285 RunningAnimationParams(color,286 "color",287 listOf(),288 "anim9",289 "",290 -1,291 mapOf(),292 mapOf(),293 mapOf(),294 mapOf(),295 mapOf(),296 mapOf(),297 mapOf(),298 AnimationToRunParams())) shouldBe299 RunningAnimationParams(color,300 "color",301 listOf(),302 "anim9",303 "",304 -1,305 mapOf(),306 mapOf(),307 mapOf(),308 mapOf(),309 mapOf(),310 mapOf(),311 mapOf(),312 AnimationToRunParams())313 }314 @Test315 @JsName("getSections")316 fun `get sections`() = runInCoroutine {317 val sections = testClient.getSections()318 sections.shouldHaveSize(2)319 sections[0].name shouldBe "s1"320 sections[0].pixels shouldBe listOf(1, 2, 3, 4)321 sections[0].parentSectionName shouldBe "s0"322 sections[1].name shouldBe "s2"323 sections[1].pixels shouldBe listOf(5, 8, 1, 3)324 sections[1].parentSectionName shouldBe ""325 }326 @Test327 @JsName("getSectionsMap")328 fun `get sections map`() = runInCoroutine {329 val sections = testClient.getSectionsMap()330 sections.shouldHaveSize(2)331 sections.shouldHaveKeys("s1", "s2")332 sections["s1"]!!.name shouldBe "s1"333 sections["s1"]!!.pixels shouldBe listOf(1, 2, 3, 4)334 sections["s1"]!!.parentSectionName shouldBe "s0"335 sections["s2"]!!.name shouldBe "s2"336 sections["s2"]!!.pixels shouldBe listOf(5, 8, 1, 3)337 sections["s2"]!!.parentSectionName shouldBe ""338 }339 @Test340 @JsName("getSection")341 fun `get section`() = runInCoroutine {342 val section1 = testClient.getSection("s1")343 section1.name shouldBe "s1"344 section1.pixels shouldBe listOf(1, 2, 3, 4)345 section1.parentSectionName shouldBe "s0"346 val section2 = testClient.getSection("s2")347 section2.name shouldBe "s2"348 section2.pixels shouldBe listOf(5, 8, 1, 3)349 section2.parentSectionName shouldBe ""350 }351 @Test352 @JsName("getFullStripSection")353 fun `get full strip section`() = runInCoroutine {354 val section1 = testClient.getFullStripSection()355 section1.name shouldBe "s0"356 section1.pixels shouldBe listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)357 section1.parentSectionName shouldBe ""358 val section2 = testClient.getSection("")359 section2.name shouldBe "s0"360 section2.pixels shouldBe listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)361 section2.parentSectionName shouldBe ""362 }363 @Test364 @JsName("createNewSection")365 fun `create new section`() = runInCoroutine {366 val newSection = testClient.createNewSection(Section("s3", listOf(5, 6, 7, 8), "s2"))367 newSection.name shouldBe "s3"368 newSection.pixels shouldBe listOf(5, 6, 7, 8)369 newSection.parentSectionName shouldBe "s2"370 }371 @Test372 @JsName("startAnimation")373 fun `start animation`() = runInCoroutine {374 testClient.startAnimation(AnimationToRunParams("color")) shouldBe375 RunningAnimationParams(color,376 "color",377 listOf(),378 "",379 "",380 -1,381 mapOf(),382 mapOf(),383 mapOf(),384 mapOf(),385 mapOf(),386 mapOf(),387 mapOf(),388 AnimationToRunParams())389 }390 @Test391 @JsName("getStripInfo")392 fun `get strip info`() = runInCoroutine {393 testClient.getStripInfo() shouldBe StripInfo(10, 12, is2DSupported = true)394 }395 @Test396 @JsName("getCurrentStripColor")397 fun `get current strip color`() = runInCoroutine {398 testClient.getCurrentStripColor() shouldBe listOf(0xFF, 0xFF, 0xFFFF, 0xFFFF, 0xFFFFFF, 0xFFFFFF)399 }400}...

Full Screen

Full Screen

matchers.kt

Source:matchers.kt Github

copy

Full Screen

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}...

Full Screen

Full Screen

Map.shouldHaveKeys

Using AI Code Generation

copy

Full Screen

1val map = mapOf("name" to "kotlin", "type" to "programming language")2val map = mapOf("name" to "kotlin", "type" to "programming language")3val map = mapOf("name" to "kotlin", "type" to "programming language")4val map = mapOf("name" to "kotlin", "type" to "programming language")5val map = mapOf("name" to "kotlin", "type" to "programming language")6val map = mapOf("name" to "kotlin", "type" to "programming language")7val map = mapOf("name" to "kotlin", "type" to "programming language")8val map = mapOf("name" to "kotlin", "type" to "programming language")9val map = mapOf("name" to "kotlin", "type" to "programming language")10val map = mapOf("name" to "kotlin", "type" to "programming language")

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful