From dcb9d2c719a4d629708dc535b7fd6ee6c1cdf7f5 Mon Sep 17 00:00:00 2001 From: Lukas Krecan <lukas@krecan.net> Date: Thu, 14 Sep 2023 13:16:19 +0200 Subject: [PATCH] KDoc --- .../javacrumbs/jsonunit/kotest/Matchers.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/json-unit-kotest/src/main/kotlin/net/javacrumbs/jsonunit/kotest/Matchers.kt b/json-unit-kotest/src/main/kotlin/net/javacrumbs/jsonunit/kotest/Matchers.kt index 716abc3a..0345f514 100644 --- a/json-unit-kotest/src/main/kotlin/net/javacrumbs/jsonunit/kotest/Matchers.kt +++ b/json-unit-kotest/src/main/kotlin/net/javacrumbs/jsonunit/kotest/Matchers.kt @@ -77,10 +77,13 @@ fun beJsonNumber(): Matcher<Any?> = beType(NodeType.NUMBER) fun beJsonBoolean(): Matcher<Any?> = beType(NodeType.BOOLEAN) /** - * Returns matcher that asserts that given JSON node is null. + * Returns matcher that asserts that given JSON node is present and null. */ fun beJsonNull(): Matcher<Any?> = beType(NodeType.NULL) +/** + * Returns matcher that asserts that given JSON node is present. + */ fun bePresent(): Matcher<Any?> = Matcher { actual -> val node = getNode(actual) MatcherResult( @@ -101,25 +104,41 @@ private fun beType(expectedType: NodeType): Matcher<Any?> = bePresent() and Matc private fun getNode(actual: Any?): Node = JsonUtils.getNode(actual, "") +/** + * Asserts that JSON node is present, is a number and returns the value as [BigDecimal]. + */ fun Any?.shouldBeJsonNumber(): BigDecimal { this should beJsonNumber() return getNode(this).decimalValue() } +/** + * Asserts that JSON node is present, is a string and returns the value as [String]. + */ fun Any?.shouldBeJsonString(): String { this should beJsonString() return getNode(this).asText() } +/** + * Asserts that JSON node is present, is a boolean and returns the value as [Boolean]. + */ fun Any?.shouldBeJsonBoolean(): Boolean { this should beJsonBoolean() return getNode(this).asBoolean() } +/** + * Asserts that JSON node is present, is an array and returns the value as [List]. + */ fun Any?.shouldBeJsonArray(): List<*> { this should beJsonArray() return getNode(this).value as List<*> } + +/** + * Asserts that JSON node is present, is an object and returns the value as [Map]. + */ fun Any?.shouldBeJsonObject(): Map<String, *> { this should beJsonObject() @Suppress("UNCHECKED_CAST")