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

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

compare.kt

Source:compare.kt Github

copy

Full Screen

...136 * For example: `"\"11\"".shouldEqualJson("12", compareJsonOptions { typeCoercion = TypeCoercion.Enabled })` will137 * succeed.138 */139 Enabled;140 internal fun isEnabled(): Boolean =141 this == Enabled142}143fun compareJsonOptions(builder: CompareJsonOptions.() -> Unit): CompareJsonOptions =144 CompareJsonOptions().apply(builder)145/**146 * Compares two json trees, returning a detailed error message if they differ.147 */148internal fun compare(149 path: List<String>,150 expected: JsonNode,151 actual: JsonNode,152 options: CompareJsonOptions153): JsonError? {154 return when (expected) {155 is JsonNode.ObjectNode -> when (actual) {156 is JsonNode.ObjectNode -> compareObjects(path, expected, actual, options)157 else -> JsonError.ExpectedObject(path, actual)158 }159 is JsonNode.ArrayNode -> when (actual) {160 is JsonNode.ArrayNode -> compareArrays(path, expected, actual, options)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 }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? {...

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""2 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""3 actualJson.shouldBeJsonEqual(expectedJson)4 }5 fun `should be json equal with custom options`() {6 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""7 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""8 actualJson.shouldBeJsonEqual(expectedJson) {9 }10 }11 fun `should be json equal with custom options and enabled`() {12 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""13 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""14 actualJson.shouldBeJsonEqual(expectedJson) {15 }16 }17 fun `should be json equal with custom options and enabled and disabled`() {18 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""19 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""20 actualJson.shouldBeJsonEqual(expectedJson) {21 }22 actualJson.shouldBeJsonEqual(expectedJson)23 }24 fun `should be json equal with custom options and disabled`() {

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1+import io.kotest.assertions.json.isEnabled2+fun main() {3+ val json = """{"a":1,"b":2}"""4+ val json2 = """{"a":1,"b":2}"""5+ json.shouldBeJson(json2, CompareJsonOptions(isEnabled = false))6+}7+fun main() {8+ val json = """{"a":1,"b":2}"""9+ val json2 = """{"b":2,"a":1}"""10+ json.shoulBeJson(json2 CompareJsonOptions(isStrict = true))11+}12+import i.kotest.assertions.json.isLenient13+fun ain() {14+ val json = """{"a":1,"b":2}"""15+ val json2 = """{"b":2,"a":1}"""16+ json.shouldBeJson(json2, Com(isLenient = true)17+}

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1+import io.kotest.assertions.json.isEnabled2+fun main() {3+ val json = """{"a":1,"b":2}"""4+ val json2 = """{"a":1,"b":2}"""5+ json.shouldBeJson(json2, CompareJsonOptions(isEnabled = false))6+}7+import io.kotest.assertions.json.isStrict8+fun main() {9+ val json = """{"a":1,"b":2}"""10+ val json2 = """{"b":2,"a":1}"""11+ json.shouldBeJson(json2, CompareJsonOptions(isStrict = true))12+}13+import io.kotest.assertions.json.isLenient14+fun main( {15+ val json = """{"a":1,"b":2"""16+ val json2 = """{"b":2,"a":1}""" @Test17+ json.shouldBeJson(json2, CompareJsonOptions(isLenient true))18+}19fun test_02() {20 val json1 = """{"a":1,"b":2}"""21 val json2 = """{"a":1,"b":2}"""22 val options = CompareJsonOptions(compareMode = CompareMode.EQUALS, ignoreExtraKeys = true, ignoreExtraValues = true, ignoreOrder = true, ignoreCase = true, ignoreWhitespace = true, ignoreNull = true, ignoreMissingKeys = true, ignoreMissingValues = true, ignoreArrayOrder = true, ignoreArrayExtras = true, ignoreArrayMissing = true, ignoreArrayNull = true, ignoreArrayWhitespace = true, ignoreArrayCase = true, ignoreArrayExtrasOrder = true, ignoreArrayExtrasValues = true, ignoreArrayExtrasKeys = true, ignoreArrayMissingOrder = true, ignoreArrayMissingValues = true, ignoreArrayMissingKeys = true, ignoreObjectExtras = true, ignoreObjectMissing = true, ignoreObjectNull = true, ignoreObjectWhitespace = true, ignoreObjectCase = true, ignoreObjectExtrasOrder = true, ignoreObjectExtrasKeys = true, ignoreObjectExtrasValues = true, ignoreObjectMissingOrder = true, ignoreObjectMissingKeys = true, ignoreObjectMissingValues = true, ignoreType = true)23 json1.shouldMatchJson(json2, options)24}25fun test_03() {26 val json1 = """{"a":1,"b":2}"""27 val json2 = """{"a":1,"b":2}"""28 val jsonNode1 = json1.parseJson()29 val jsonNode2 = json2.parseJson()30 jsonNode1.isEqualTo(jsonNode2)31}32fun test_04() {33 val json1 = """{"a":1,"b":2}"""34 val json2 = """{"a":1,"b":2}"""35 val jsonNode1 = json1.parseJson()36 val jsonNode2 = json2.parseJson()37 jsonNode1.isEqualTo(jsonNode2)38}39fun test_05() {40 val json1 = """{"a":1,"b":2}"""41 val json2 = """{"a":1,"

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1fun `s=hould be json equal with custom options`() {2 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""3 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""4 actualJson.shouldBeJsonEqual(expectedJson) {5 }6 }7 fun `should be json equal with custom options and enabled`() {8 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""9 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""10 actualJson.shouldBeJsonEqual(expectedJson) {11 }12 }13 fun `should be json equal with custom options and enabled and disabled`() {14 val actualJson = """{"name": "John", "age": 30, "city": "New York"}"""15 val expectedJson = """{"name": "John", "age": 30, "city": "New York"}"""16 actualJson.shouldBeJsonEqual(expectedJson) {17 }18 actualJson.shouldBeJsonEqual(expectedJson)19 }20 fun `should be json equal with custom options and disabled`() {

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1fun main() {2 val actual = """{"name": "John", "age": 30, "address": {"streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100"}}"""3 val expected = """{"name": "John", "age": 30, "address": {"streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100"}}"""4 val compareJsonOptions = CompareJsonOptions(enabled = false)5 actual.shouldBeJson(expected, compareJsonOptions)6}

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1un main() {2 val json1 = """{"a":1,"b":2}"""3 val json2 = """{"b":2,"":1}"""4 printn(CompareJsonOptions.iEnabld(json1, json2))5 val json2 = """{"name":"kotlin"}"""6 json1.shouldBeJson(json2, CompareJsonOptions(isEnabled = true, mode = JsonCompareMode.STRICT))7 }se isEnabled method of io.kotest.assertions.json.CompareJsonOptions clas to comparto json strings8fun main() {9 val json1 = """{"a":1,"b":2}"""10 val json2 = """{"b":2,"a":1}"""11 println(CompareJsonOptions.isEnabled(json1, json2))12}13fun main() {14 val json1 = """{"a":1,"b":2}"""15 val json2 = """{"b":2,"a":1}"""16 println(CompareJsonOptions.json(json1, json2))17}18fun main() {19 val json1 = """{"a":1,"b":2}"""20 val json2 = """{"b":2,"a":1}"""21 println(CompareJsonOptions.json(json1, json2))22}23fun main() {24 val json1 = """{"a":1,"b":2}"""25 val json2 = """{"b":2,"a":1}"""26 println(CompareJsonOptions.json(json1, json2))27}28 fun testCompareJsonOptionsIsEnabledFalse() {29 }30}

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1fun testCompareJsonOptionsIsEnabled() {2 val options = CompareJsonOptions(ignoreExtraKeys = true, ignoreArrayOrder = true, ignoreExtraArrayItems = true)3 options.isEnabled(CompareJsonOptions.ignoreExtraKeys) shouldBe true4 options.isEnabled(CompareJsonOptions.ignoreArrayOrder) shouldBe true5 options.isEnabled(CompareJsonOptions.ignoreExtraArrayItems) shouldBe true6}7fun testCompareJsonOptionsIsDisabled() {8 val options = CompareJsonOptions(ignoreExtraKeys = true, ignoreArrayOrder = true, ignoreExtraArrayItems = true)9 options.isDisabled(CompareJsonOptions.ignoreExtraKeys) shouldBe false10 options.isDisabled(CompareJsonOptions.ignoreArrayOrder) shouldBe false11 options.isDisabled(CompareJsonOptions.ignoreExtraArrayItems) shouldBe false12}13fun testCompareJsonOptionsWithIgnoreExtraKeys() {14 val options = CompareJsonOptions(ignoreExtraKeys = true)15 options.withIgnoreExtraKeys(false).ignoreExtraKeys shouldBe false16 val options = CompareJsonOptions(ignoreArrayOrder = true)17fun testCompareJsonOptionsWithIgnoreExtraArrayItems() {18 val options = CompareJsonOptions(ignoreExtraArrayItems = true)19 options.withIgnoreExtraArrayItems(false).ignoreExtraArrayItems shouldBe false20}21fun testCompareJsonOptionsWithIgnoreExtraKeys2() {22 val options = CompareJsonOptions(ignoreExtraKeys = true)23 options.withIgnoreExtraKeys(false).ignoreExtraKeys shouldBe false24}25fun testCompareJsonOptionsWithIgnoreArrayOrder2() {26 val options = CompareJsonOptions(ignoreArrayOrder = true)27 options.withIgnoreArrayOrder(false).ignoreArrayOrder shouldBe false28}29 val json2 = """{"name":"kotlin"}"""30 json1.shouldBeJson(json2, CompareJsonOptions(isEnabled = false, mode = JsonCompareMode.STRICT))31 }32 fun testCompareJsonOptionsModeLenient() {33 val json1 = """{"name":"kotlin"}"""34 val json2 = """{"name":"kotlin"}"""35 json1.shouldBeJson(json2, CompareJsonOptions(isEnabled = true, mode = JsonCompareMode.LENIENT))36 }37 fun testCompareJsonOptionsModeStrict() {38 val json1 = """{"name":"kotlin"}"""39 val json2 = """{"name":"kotlin"}"""40 json1.shouldBeJson(json2, CompareJsonOptions(isEnabled = true, mode = JsonCompareMode.STRICT))41 }42 fun testCompareJsonOptionsModeStrictOrder() {43 val json1 = """{"name":"kotlin"}"""44 val json2 = """{"name":"kotlin"}"""45 json1.shouldBeJson(json2, CompareJsonOptions(isEnabled = true, mode = JsonCompareMode.STRICT_ORDER))46 }47 fun testCompareJsonOptionsModeStrictOrderArray() {48 val json1 = """{"

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1 val json1 = """{"a":1,"b":2}"""2 val json2 = """{"a":1,"b":2}"""3 val options = CompareJsonOptions()4 json1.shouldMatchJson(json2, options)5 }6}

Full Screen

Full Screen

isEnabled

Using AI Code Generation

copy

Full Screen

1fun testCompareJsonOptionsIsEnabled() {2 val options = CompareJsonOptions(ignoreExtraKeys = true, ignoreArrayOrder = true, ignoreExtraArrayItems = true)3 options.isEnabled(CompareJsonOptions.ignoreExtraKeys) shouldBe true4 options.isEnabled(CompareJsonOptions.ignoreArrayOrder) shouldBe true5 options.isEnabled(CompareJsonOptions.ignoreExtraArrayItems) shouldBe true6}7fun testCompareJsonOptionsIsDisabled() {8 val options = CompareJsonOptions(ignoreExtraKeys = true, ignoreArrayOrder = true, ignoreExtraArrayItems = true)9 options.isDisabled(CompareJsonOptions.ignoreExtraKeys) shouldBe false10 options.isDisabled(CompareJsonOptions.ignoreArrayOrder) shouldBe false11 options.isDisabled(CompareJsonOptions.ignoreExtraArrayItems) shouldBe false12}13fun testCompareJsonOptionsWithIgnoreExtraKeys() {14 val options = CompareJsonOptions(ignoreExtraKeys = true)15 options.withIgnoreExtraKeys(false).ignoreExtraKeys shouldBe false16}17fun testCompareJsonOptionsWithIgnoreArrayOrder() {18 val options = CompareJsonOptions(ignoreArrayOrder = true)19 options.withIgnoreArrayOrder(false).ignoreArrayOrder shouldBe false20}21fun testCompareJsonOptionsWithIgnoreExtraArrayItems() {22 val options = CompareJsonOptions(ignoreExtraArrayItems = true)23 options.withIgnoreExtraArrayItems(false).ignoreExtraArrayItems shouldBe false24}25fun testCompareJsonOptionsWithIgnoreExtraKeys2() {26 val options = CompareJsonOptions(ignoreExtraKeys = true)27 options.withIgnoreExtraKeys(false).ignoreExtraKeys shouldBe false28}29fun testCompareJsonOptionsWithIgnoreArrayOrder2() {30 val options = CompareJsonOptions(ignoreArrayOrder = true)31 options.withIgnoreArrayOrder(false).ignoreArrayOrder shouldBe false32}

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