From 7e77f65003be747157172c10b11a759f55b82429 Mon Sep 17 00:00:00 2001 From: Rob Fletcher Date: Tue, 5 Jun 2018 04:40:50 -0700 Subject: [PATCH] Map entry mapping function Fixes #45 --- strikt-core/src/main/kotlin/strikt/assertions/Map.kt | 5 ++++- strikt-core/src/test/kotlin/strikt/Mapping.kt | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/strikt-core/src/main/kotlin/strikt/assertions/Map.kt b/strikt-core/src/main/kotlin/strikt/assertions/Map.kt index d8ae2467..6f6aed85 100644 --- a/strikt-core/src/main/kotlin/strikt/assertions/Map.kt +++ b/strikt-core/src/main/kotlin/strikt/assertions/Map.kt @@ -3,4 +3,7 @@ package strikt.assertions import strikt.api.Assertion fun , K, V> Assertion.isEmpty() = - passesIf("is empty") { isEmpty() } \ No newline at end of file + passesIf("is empty") { isEmpty() } + +operator fun , K, V> Assertion.get(key: K): Assertion = + map("entry [$key] %s") { get(key) } diff --git a/strikt-core/src/test/kotlin/strikt/Mapping.kt b/strikt-core/src/test/kotlin/strikt/Mapping.kt index 3fecd2f1..092540ae 100644 --- a/strikt-core/src/test/kotlin/strikt/Mapping.kt +++ b/strikt-core/src/test/kotlin/strikt/Mapping.kt @@ -9,6 +9,8 @@ import strikt.assertions.containsExactly import strikt.assertions.first import strikt.assertions.get import strikt.assertions.isEqualTo +import strikt.assertions.isNotNull +import strikt.assertions.isNull import strikt.assertions.last import java.time.LocalDate @@ -37,6 +39,14 @@ internal object Mapping : Spek({ expect(subject)[1..2].containsExactly("rubberplant", "marzipan") } } + + describe("mapping assertions on ${Map::class.java.simpleName}") { + it("maps to a value element using [key]") { + val subject = mapOf("foo" to "bar") + expect(subject)["foo"].isNotNull().isEqualTo("bar") + expect(subject)["bar"].isNull() + } + } } describe("custom mapping") {