How to use compareNumbers method of io.kotest.assertions.json.CompareJsonOptions class

Best Kotest code snippet using io.kotest.assertions.json.CompareJsonOptions.compareNumbers

compare.kt

Source:compare.kt Github

copy

Full Screen

...161 else -> JsonError.ExpectedArray(path, actual)162 }163 is JsonNode.BooleanNode -> compareBoolean(path, expected, actual, options)164 is JsonNode.StringNode -> compareString(path, expected, actual, options)165 is JsonNode.NumberNode -> compareNumbers(path, expected, actual, options)166 JsonNode.NullNode -> compareNull(path, actual)167 }168}169internal fun compareObjects(170 path: List<String>,171 expected: JsonNode.ObjectNode,172 actual: JsonNode.ObjectNode,173 options: CompareJsonOptions,174): JsonError? {175 if (FieldComparison.Strict == options.fieldComparison) {176 val keys1 = expected.elements.keys177 val keys2 = actual.elements.keys178 if (keys1.size < keys2.size) {179 val missing = keys2 - keys1180 return JsonError.ObjectMissingKeys(path, missing)181 }182 if (keys2.size < keys1.size) {183 val extra = keys1 - keys2184 return JsonError.ObjectExtraKeys(path, extra)185 }186 }187 // when using strict order mode, the order of elements in json matters, normally, we don't care188 when (options.propertyOrder) {189 PropertyOrder.Strict ->190 expected.elements.entries.withIndex().zip(actual.elements.entries).forEach { (e, a) ->191 if (a.key != e.value.key) return JsonError.NameOrderDiff(path, e.index, e.value.key, a.key)192 val error = compare(path + a.key, e.value.value, a.value, options)193 if (error != null) return error194 }195 PropertyOrder.Lenient ->196 expected.elements.entries.forEach { (name, e) ->197 val a = actual.elements[name] ?: return JsonError.ObjectMissingKeys(path, setOf(name))198 val error = compare(path + name, e, a, options)199 if (error != null) return error200 }201 }202 return null203}204internal fun compareArrays(205 path: List<String>,206 expected: JsonNode.ArrayNode,207 actual: JsonNode.ArrayNode,208 options: CompareJsonOptions,209): JsonError? {210 if (expected.elements.size != actual.elements.size)211 return JsonError.UnequalArrayLength(path, expected.elements.size, actual.elements.size)212 when (options.arrayOrder) {213 ArrayOrder.Strict -> {214 expected.elements.withIndex().zip(actual.elements.withIndex()).forEach { (a, b) ->215 val error = compare(path + "[${a.index}]", a.value, b.value, options)216 if (error != null) return error217 }218 }219 /**220 * In [ArrayOrder.Lenient], we try to allow array contents to be out-of-order.221 * We do this by searching for a match for each element in [actual], in the [expected] array,222 * flagging used matches so they can't be used twice. This will probably be slow for very big arrays.223 */224 ArrayOrder.Lenient -> {225 val consumedIndexes = BooleanArray(expected.elements.size) { false }226 fun availableIndexes() = consumedIndexes227 .mapIndexed { index, isConsumed -> if (!isConsumed) index else null }228 .filterNotNull()229 fun findMatchingIndex(element: JsonNode): Int? {230 for (i in availableIndexes()) {231 // Comparison with no error -> matching element232 val isMatch = compare(path + "[$i]", element, expected.elements[i], options) == null233 if (isMatch) {234 return i235 }236 }237 return null238 }239 for ((i, element) in actual.elements.withIndex()) {240 val match = findMatchingIndex(element)241 ?: return JsonError.UnequalArrayContent(path + "[$i]", expected, element)242 consumedIndexes[match] = true243 }244 }245 }246 return null247}248/**249 * When comparing a string, if the [mode] is [CompareMode.Lenient] we can convert the actual node to a string.250 */251internal fun compareString(252 path: List<String>,253 expected: JsonNode.StringNode,254 actual: JsonNode,255 options: CompareJsonOptions256): JsonError? {257 return when {258 actual is JsonNode.StringNode -> compareStrings(path, expected.value, actual.value)259 options.typeCoercion.isEnabled() -> when {260 actual is JsonNode.BooleanNode -> compareStrings(path, expected.value, actual.value.toString())261 actual is JsonNode.NumberNode && expected.contentIsNumber() -> compareNumberNodes(262 path,263 expected.toNumberNode(),264 actual265 )266 else -> JsonError.IncompatibleTypes(path, expected, actual)267 }268 else -> JsonError.IncompatibleTypes(path, expected, actual)269 }270}271internal fun compareStrings(path: List<String>, expected: String, actual: String): JsonError? {272 return when (expected) {273 actual -> null274 else -> JsonError.UnequalStrings(path, expected, actual)275 }276}277/**278 * When comparing a boolean, if the [mode] is [CompareMode.Lenient] and the actual node is a text279 * node with "true" or "false", then we convert.280 */281internal fun compareBoolean(282 path: List<String>,283 expected: JsonNode.BooleanNode,284 actual: JsonNode,285 options: CompareJsonOptions286): JsonError? {287 return when {288 actual is JsonNode.BooleanNode -> compareBooleans(path, expected.value, actual.value)289 options.typeCoercion.isEnabled() && actual is JsonNode.StringNode -> when (actual.value) {290 "true" -> compareBooleans(path, expected.value, true)291 "false" -> compareBooleans(path, expected.value, false)292 else -> JsonError.UnequalValues(path, expected, actual)293 }294 else -> JsonError.IncompatibleTypes(path, expected, actual)295 }296}297internal fun compareBooleans(path: List<String>, expected: Boolean, actual: Boolean): JsonError? {298 return when (expected) {299 actual -> null300 else -> JsonError.UnequalBooleans(path, expected, actual)301 }302}303private fun compareNumbers(304 path: List<String>,305 expected: JsonNode.NumberNode,306 actual: JsonNode,307 options: CompareJsonOptions308): JsonError? {309 return when (actual) {310 is JsonNode.NumberNode -> {311 when (options.numberFormat) {312 NumberFormat.Strict -> {313 if (expected.content != actual.content) JsonError.UnequalValues(path, expected.content, actual.content)314 else null315 }316 NumberFormat.Lenient -> compareNumberNodes(path, expected, actual)317 }...

Full Screen

Full Screen

compareNumbers

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.json.CompareJsonOptions2import io.kotest.assertions.json.compareJson3import io.kotest.assertions.json.JsonCompare4import io.kotest.assertions.json.compareJson5import io.kotest.assertions.json.JsonCompareOptions6import io.kotest.assertions.json.compareJson7import io.kotest.assertions.json.JsonCompareOptionsBuilder8import io.kotest.assertions.json.compareJson9import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion10import io.kotest.assertions.json.compareJson11import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion.compareNumbers12import io.kotest.assertions.json.compareJson13import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion.ignoreExtraElements14import io.kotest.assertions.json.compareJson15import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion.ignoreMissingElements16import io.kotest.assertions.json.compareJson17import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion.ignoreOrder18import io.kotest.assertions.json.compareJson19import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion.ignoreValues20import io.kotest.assertions.json.compareJson21import io.kotest.assertions.json.JsonCompareOptionsBuilder.Companion.ignoreWhitespace

Full Screen

Full Screen

compareNumbers

Using AI Code Generation

copy

Full Screen

1fun main() {2 {3 }4 """.trimIndent()5 {6 }7 """.trimIndent()8 println(result)9}10fun main() {11 {12 }13 """.trimIndent()14 {15 }16 """.trimIndent()17 val compareJsonOptions = CompareJsonOptions(compareNumbers = false)18 println(result)19}20fun main() {21 {22 }23 """.trimIndent()24 {25 }26 """.trimIndent()27 val compareJsonOptions = CompareJsonOptions(compareNumbers = { expected, actual ->28 val expectedNumber = expected.toBigDecimal()29 val actualNumber = actual.toBigDecimal()30 })31 println(result)32}33fun main() {34 {35 }36 """.trimIndent()37 {38 }39 """.trimIndent()40 val compareJsonOptions = CompareJsonOptions(compareNumbers = { expected, actual ->

Full Screen

Full Screen

compareNumbers

Using AI Code Generation

copy

Full Screen

1+val expectedJson = """{"a":1,"b":2}"""2+val actualJson = """{"b":2,"a":1}"""3+expectedJson.shouldBeJson(actualJson, CompareJsonOptions().compareNumbers())4+expectedJson.shouldBeJson(actualJson, CompareJsonOptions().ignoreArrayOrder())5+expectedJson.shouldBeJson(actualJson, CompareJsonOptions().ignoreExtraElements())6+val expectedJson = """{"a":1,"b":2}"""7+val actualJson = """{"a":1,"b":2,"c":3}"""8+expectedJson.shouldBeJson(actualJson, CompareJsonOptions().ignoreExtraFields())9+val expectedJson = """{"a":1,"b":2,"c":3}"""10+val actualJson = """{"a":1,"b":2}"""11+expectedJson.shouldBeJson(actualJson, CompareJsonOptions().ignoreMissingFields())

Full Screen

Full Screen

compareNumbers

Using AI Code Generation

copy

Full Screen

1fun main() {2 val json1 = """{ "name": "John", "age": 30 }"""3 val json2 = """{ "name": "John", "age": 30 }"""4 val json3 = """{ "name": "John", "age": 31 }"""5 val json4 = """{ "name": "John", "age": 30, "salary": 3000 }"""6 val json5 = """{ "name": "John", "age": 30, "salary": 3000.0 }"""7 val json6 = """{ "name": "John", "age": 30, "salary": 3000, "extra": null }"""8 val json7 = """{ "name": "John", "age": 30, "salary": 3000, "extra": { "key": "value" } }"""9 val json8 = """{ "name": "John", "age": 30, "salary": 3000, "extra": { "key": "value", "key2": null } }"""10 val json9 = """{ "name": "John", "age": 30, "salary": 3000, "extra": { "key": "value", "key2": null, "key3": 100 } }"""11 val json10 = """{ "name": "John", "age": 30, "salary": 3000, "extra": { "key": "value", "key2": null, "key3": 100, "key4": [1,2,3] } }"""12 val json11 = """{ "name": "John", "age": 30, "salary": 3000, "extra": { "key": "value", "key2": null, "key3": 100, "key4": [1,2,3], "key5": {"key6": "value6"} } }"""13 val json12 = """{ "name": "John", "age": 30 }"""14 val json13 = """{ "name": "John", "age": 30, "salary": 3000 }"""15 val json14 = """{ "name": "John", "age": 30,

Full Screen

Full Screen

compareNumbers

Using AI Code Generation

copy

Full Screen

1val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS) val result = actual.shouldMatchJson(expected, options)2val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS, ignoreExtraElements = true) val result = actual.shouldMatchJson(expected, options)3val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS, ignoreExtraElements = true, ignoreArrayOrder = true) val result = actual.shouldMatchJson(expected, options)4val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS, ignoreExtraElements = true, ignoreArrayOrder = true, ignoreExtraElements = true) val result = actual.shouldMatchJson(expected, options)5val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS, ignoreExtraElements = true, ignoreArrayOrder = true, ignoreExtraElements = true, ignoreArrayOrder = true) val result = actual.shouldMatchJson(expected, options)6val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS, ignoreExtraElements = true, ignoreArrayOrder = true, ignoreExtraElements = true, ignoreArrayOrder = true, ignoreExtraElements = true) val result = actual.shouldMatchJson(expected, options)7val options = CompareJsonOptions(ignoreArrayOrder = true, ignoreExtraElements = true, compareNumbers = CompareNumbersMode.EQUALS, ignoreExtraElements = true, ignoreArrayOrder = true, ignoreExtraElements = true, ignoreArray

Full Screen

Full Screen

compareNumbers

Using AI Code Generation

copy

Full Screen

1val options = CompareJsonOptions().compareNumbers { a, b -> a == b }2val actual = """{"foo": "bar"}"""3val actual = """{"foo": "bar"}"""4val actual = """{"foo": "bar"}"""5val actual = """{"foo": "bar"}"""6val actual = """{"foo": "bar"}"""7val actual = """{"foo": "bar"}"""

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.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful