Best Kotest code snippet using io.kotest.assertions.json.CompareJsonOptions.compareNull
compare.kt
Source:compare.kt
...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 }318 }319 is JsonNode.StringNode -> {320 if (options.typeCoercion.isEnabled() && actual.contentIsNumber()) compareNumberNodes(321 path,322 expected,323 actual.toNumberNode()324 )325 else JsonError.IncompatibleTypes(path, expected, actual)326 }327 else -> JsonError.IncompatibleTypes(path, expected, actual)328 }329}330private fun compareNumberNodes(331 path: List<String>,332 expected: JsonNode.NumberNode,333 actual: JsonNode.NumberNode334): JsonError? {335 return when {336 expected.lenientEquals(actual) -> null337 else -> JsonError.UnequalValues(path, expected.content, actual.content)338 }339}340internal fun compareNull(path: List<String>, b: JsonNode): JsonError? {341 return when (b) {342 is JsonNode.NullNode -> null343 else -> JsonError.ExpectedNull(path, b)344 }345}...
compareNull
Using AI Code Generation
1@DisplayName ( "CompareJsonOptionsTest" ) class CompareJsonOptionsTest { @Test fun `test compareNull`() { val options = CompareJsonOptions . compareNull CompareJsonOptions . compareNull . shouldBe ( options ) } @Test fun `test compareNull with value`() { val options = CompareJsonOptions . compareNull ( true ) CompareJsonOptions . compareNull ( true ) . shouldBe ( options ) } }2@DisplayName ( "CompareJsonOptionsTest" ) class CompareJsonOptionsTest { @Test fun `test compareNull`() { val options = CompareJsonOptions . compareNull CompareJsonOptions . compareNull . shouldBe ( options ) } @Test fun `test compareNull with value`() { val options = CompareJsonOptions . compareNull ( true ) CompareJsonOptions . compareNull ( true ) . shouldBe ( options ) } }3@DisplayName("CompareJsonOptionsTest") class CompareJsonOptionsTest { @Test fun `test compareNull`() { val options = CompareJsonOptions.compareNull CompareJsonOptions.compareNull.shouldBe(options) } @Test fun `test compareNull with value`() { val options = CompareJsonOptions.compareNull(true) CompareJsonOptions.compareNull(true).shouldBe(options) } }4@DisplayName("CompareJsonOptionsTest") class CompareJsonOptionsTest { @Test fun `test compareNull`() { val options = CompareJsonOptions.compareNull CompareJsonOptions.compareNull.shouldBe(options) } @Test fun `test compareNull with value`() { val options = CompareJsonOptions.compareNull(true) CompareJsonOptions.compareNull(true).shouldBe(options) } }5@DisplayName("CompareJsonOptionsTest") class CompareJsonOptionsTest { @Test fun `test compareNull`() { val options = CompareJsonOptions.compareNull CompareJsonOptions.compareNull.shouldBe(options) } @Test fun `test compareNull with value`() { val options = CompareJsonOptions.compareNull(true) CompareJsonOptions.compareNull(true).shouldBe(options) } }6@DisplayName("CompareJsonOptionsTest") class CompareJsonOptionsTest { @Test fun `test compareNull`() { val options = CompareJsonOptions.compareNull CompareJsonOptions.compareNull.shouldBe(options) } @Test fun `test
compareNull
Using AI Code Generation
1assertJsonEquals(2 expected = """{ "a": "b" }""",3 actual = """{ "a": null }""",4 compareOptions = CompareJsonOptions(compareNull = true)5assertJsonEquals(6 expected = """{ "a": "b" }""",7 actual = """{ "a": null }""",8 compareOptions = CompareJsonOptions(compareNull = false)9assertJsonEquals(10 expected = """{ "a": "b" }""",11 actual = """{ "a": "b" }""",12 compareOptions = CompareJsonOptions(compareNull = false)13assertJsonEquals(14 expected = """{ "a": "b" }""",15 actual = """{ "a": null }""",16 compareOptions = CompareJsonOptions(compareNull = false)17assertJsonEquals(18 expected = """{ "a": "b" }""",19 actual = """{ "a": "b" }""",20 compareOptions = CompareJsonOptions(compareNull = true)21assertJsonEquals(22 expected = """{ "a": "b" }""",23 actual = """{ "a": null }""",24 compareOptions = CompareJsonOptions(compareNull = true)25assertJsonEquals(26 expected = """{ "a": "b" }""",27 actual = """{ "a": "b" }""",28 compareOptions = CompareJsonOptions(compareNull = false)29assertJsonEquals(30 expected = """{ "a": "b" }""",31 actual = """{ "a": null }""",32 compareOptions = CompareJsonOptions(compareNull = false)33assertJsonEquals(34 expected = """{ "a": "b" }""",35 actual = """{ "a": "b" }""",36 compareOptions = CompareJsonOptions(compareNull = true)37assertJsonEquals(38 expected = """{ "a": "b" }""",39 actual = """{ "a": null }""",40 compareOptions = CompareJsonOptions(compareNull = true)41assertJsonEquals(42 expected = """{ "a": "b" }""",43 actual = """{ "a": "b" }""",44 compareOptions = CompareJsonOptions(compareNull = false)45assertJsonEquals(46 expected = """{ "a": "b" }""",
compareNull
Using AI Code Generation
1val json = """{ "name": "kotest", "age": 5 }"""2json.shouldBeJson("""{ "name": "kotest", "age": null }""",3CompareJsonOptions(4json.shouldBeJson("""{ "name": "kotest", "age": null }""",5JsonOptions(6json.shouldBeJson("""{ "name": "kotest", "age": null }""",7CompareJsonOptions(8json.shouldBeJson("""{ "name": "kotest", "age": null }""",9JsonOptions(10json.shouldBeJson("""{ "name": "kotest", "age": null }""",11CompareJsonOptions(12json.shouldBeJson("""{ "name": "kotest", "age": null }""",13JsonOptions(14json.shouldBeJson("""{ "name": "kotest", "age": null }""",15CompareJsonOptions(16json.shouldBeJson("""{ "name": "kotest", "age": null }""",17JsonOptions(18json.shouldBeJson("""{ "name": "kotest", "age": null
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!!