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

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

compare.kt

Source:compare.kt Github

copy

Full Screen

2package io.kotest.assertions.json3@Deprecated("Json comparison options is now specified with `CompareJsonOptions`", ReplaceWith("TypeCoercion"))4enum class CompareMode {5 /**6 * Types must be identical and compare by value.7 *8 * For example, `"true"` and `true` will not match because one is a string, and the other is a boolean.9 * But `2.99E9` and `299000000` are considered equal as they are the same number, just in a different format.10 * `"100"` and `100` would not match as they are different types (string vs number).11 */12 @Deprecated(13 "Json comparison options is now specified with `CompareJsonOptions`",14 ReplaceWith("TypeCoercion.Disabled")15 )16 Strict,17 /**18 * Compare by value, coercing if possible.19 *20 * For example, "true" and true will match because the string value can be coerced into a valid boolean.21 * Similarly, "100" and 100 will match as the former can be coerced into an int.22 */23 @Deprecated(24 "Json comparison options is now specified with `CompareJsonOptions`",25 ReplaceWith("TypeCoercion.Enabled")26 )27 Lenient,28}29@Deprecated("Json comparison options is now specified with `CompareJsonOptions`", ReplaceWith("PropertyOrder"))30enum class CompareOrder {31 /**32 * All object properties must be in same order as expected.33 *34 * For example, { "x": 14.2, "y": 13.0 }` and `{ "y": 13.0, "x: 14.2 }` will NOT be considered equal.35 */36 @Deprecated(37 "Json comparison options is now specified with `CompareJsonOptions`",38 ReplaceWith("PropertyOrder.Strict")39 )40 Strict,41 @Deprecated(42 "Json comparison options is now specified with `CompareJsonOptions`",43 ReplaceWith("PropertyOrder.Lenient")44 )45 Lenient,46}47/**48 * helper method for bridging old compare options into new49 */50internal fun legacyOptions(mode: CompareMode, order: CompareOrder) =51 compareJsonOptions {52 typeCoercion = when (mode) {53 CompareMode.Strict -> TypeCoercion.Disabled54 CompareMode.Lenient -> TypeCoercion.Enabled55 }56 propertyOrder = when (order) {57 CompareOrder.Strict -> PropertyOrder.Strict58 CompareOrder.Lenient -> PropertyOrder.Lenient59 }60 }61internal val defaultCompareJsonOptions = CompareJsonOptions()62class CompareJsonOptions(63 /**64 * Controls whether property order must be identical65 */66 var propertyOrder: PropertyOrder = PropertyOrder.Lenient,67 /**68 * Controls whether array ordering must be identical.69 */70 var arrayOrder: ArrayOrder = ArrayOrder.Strict,71 /**72 * Controls whether the actual document may contain extra fields or not.73 */74 var fieldComparison: FieldComparison = FieldComparison.Strict,75 /**76 * Controls whether number formatting should be taken into consideration. For instance, comparing 1.00 to 1.0, or77 * 1E2 to 10078 */79 var numberFormat: NumberFormat = NumberFormat.Lenient,80 /**81 * Controls whether types should be coerced when possible. For instance, when strings contain bool or numeric values.82 */83 var typeCoercion: TypeCoercion = TypeCoercion.Disabled84)85enum class PropertyOrder {86 /**87 * Default. Property order in objects does not matter.88 *89 * Example: `"""{ "a": 0, "b": 2 }""".shouldEqualJson("""{ "b": 2, "a": 1 }""", compareJsonOptions { propertyOrder = Lenient })` will pass90 */91 Lenient,92 /**93 * Properties must be in same order. E.g. `{ "a": 0, "b": 2 }` is not considered equal to `{ "b": 2, "a": 1 }`94 */95 Strict96}97enum class ArrayOrder {98 /**99 * Default. Arrays must contain the same elements in the same order.100 */101 Strict,102 /**103 * Arrays are allowed to be shuffled, but must still contain same items.104 */105 Lenient,106}107enum class FieldComparison {108 /**109 * Default. Objects in [expected] and [actual] must contain the same fields.110 */111 Strict,112 /**113 * Objects in the actual document may contain extraneous fields without causing comparison to fail.114 */115 Lenient,116}117enum class NumberFormat {118 /**119 * Default. Numbers will be interpreted before being compared. Meaning we can compare 0E3 to 1000 without fail120 */121 Lenient,122 /**123 * Numbers must also be formatted the same way to be considered equal.124 */125 Strict126}127enum class TypeCoercion {128 /**129 * Default. Types will not be converted. Meaning `"true"` and `true` are considered unequal.130 */131 Disabled,132 /**133 * Types may be coerced. Strings containing numbers will be considered equal to their numbers, and booleans in134 * strings will also be compared.135 *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? {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}...

Full Screen

Full Screen

JsonLiteralsTest.kt

Source:JsonLiteralsTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.tests.json2import io.kotest.assertions.json.NumberFormat3import io.kotest.assertions.json.TypeCoercion4import io.kotest.assertions.json.compareJsonOptions5import io.kotest.assertions.json.shouldEqualJson6import io.kotest.assertions.shouldFail7import io.kotest.assertions.throwables.shouldThrow8import io.kotest.core.spec.style.FunSpec9import io.kotest.matchers.string.shouldContain10import io.kotest.matchers.throwable.shouldHaveMessage11class JsonLiteralsTest : FunSpec(12 {13 test("Unsupported type") {14 shouldThrow<IllegalArgumentException> {15 "0x03" shouldEqualJson "0x03"16 }.shouldHaveMessage("Unsupported kotlinx-serialization type 0x03")17 }18 context("Strict (default) comparisons") {19 test("comparing float and int") {20 shouldFail {21 "3.2" shouldEqualJson "3"22 }.shouldHaveMessage(23 """24 The top level expected 3 but was 3.225 expected:<3> but was:<3.2>26 """.trimIndent()27 )28 }29 test("quoted numbers are treated as strings") {30 shouldFail { "\"1E3\"" shouldEqualJson "1000.0" }31 // Unquoted 1E3 is parsed to double and back due to prettifying output32 shouldFail { "\"1000.0\"" shouldEqualJson "1E3" }.message shouldContain33 "The top level expected number but was string"34 shouldFail { "10.0" shouldEqualJson "\"10.0\"" }.message shouldContain35 "The top level expected string but was number"36 }37 test("comparing exponent-based float with regular float") {38 "1E3" shouldEqualJson "1000.0"39 "1000.0" shouldEqualJson "1E3"40 "1000.0" shouldEqualJson "1000"41 "5E0" shouldEqualJson "5.0"42 "2E-1" shouldEqualJson "0.2"43 shouldFail {44 "1.0E-3" shouldEqualJson "0.0001"45 }46 "1.0E-4" shouldEqualJson "0.0001"47 }48 test("comparing high-precision floating point numbers") {49 // Note: In the middle paragraph of the failure message the expected JSON has been50 // formatted as a JSON tree using KotlinX.serialization which parses the51 // number to a double and back, hence the loss of precision.52 shouldFail {53 "0.12345678912345678" shouldEqualJson "0.123456789123456789"54 }.shouldHaveMessage(55 """56 The top level expected 0.123456789123456789 but was 0.1234567891234567857 expected:<0.123456789123456789> but was:<0.12345678912345678>58 """.trimIndent()59 )60 }61 test("comparing string and boolean") {62 shouldFail {63 "true" shouldEqualJson "\"true\""64 }.shouldHaveMessage(65 """66 The top level expected string but was boolean67 expected:<"true"> but was:<true>68 """.trimIndent()69 )70 }71 }72 context("CompareMode.Exact requires same format for numbers") {73 infix fun String.shouldExactlyEqualJson(expected: String) =74 this.shouldEqualJson(expected, compareJsonOptions { numberFormat = NumberFormat.Strict })75 test("comparing float and exponent") {76 shouldFail {77 "10.0" shouldExactlyEqualJson "1e1"78 }.shouldHaveMessage(79 """80 The top level expected 1e1 but was 10.081 expected:<1e1> but was:<10.0>82 """.trimIndent()83 )84 }85 test("comparing int and exponent") {86 shouldFail {87 "10" shouldExactlyEqualJson "1e1"88 }.shouldHaveMessage(89 """90 The top level expected 1e1 but was 1091 expected:<1e1> but was:<10>92 """.trimIndent()93 )94 }95 test("comparing float and int") {96 shouldFail {97 "10.0" shouldExactlyEqualJson "10"98 }.shouldHaveMessage(99 """100 The top level expected 10 but was 10.0101 expected:<10> but was:<10.0>102 """.trimIndent()103 )104 }105 test("quoted numbers are treated as strings") {106 shouldFail { "\"1E3\"" shouldEqualJson "1000.0" }107 // Unquoted 1E3 is parsed to double and back due to prettifying output108 shouldFail { "\"1000.0\"" shouldEqualJson "1E3" }.message shouldContain109 "The top level expected number but was string"110 shouldFail { "10.0" shouldEqualJson "\"10.0\"" }.message shouldContain111 "The top level expected string but was number"112 }113 }114 context("Lenient type-conversions") {115 infix fun String.lenientShouldEqualJson(expected: String) =116 this.shouldEqualJson(expected, compareJsonOptions { typeCoercion = TypeCoercion.Enabled })117 test("comparing exponent-based float with regular float") {118 "1E3" lenientShouldEqualJson "\"1000.0\""119 "1000.0" lenientShouldEqualJson "\"1E3\""120 "2E-1" lenientShouldEqualJson "0.2"121 "2E-1" lenientShouldEqualJson "\"0.2\""122 "0.2" lenientShouldEqualJson "\"2e-1\""123 "5E0" lenientShouldEqualJson "5.0"124 }125 test("Strings with numbers") {126 shouldFail {127 "\"abc 123\"" lenientShouldEqualJson "123"128 }.shouldHaveMessage(129 """130 The top level expected number but was string...

Full Screen

Full Screen

JsonMatchers.kt

Source:JsonMatchers.kt Github

copy

Full Screen

1package io.kotest.assertions.json2import io.kotest.matchers.ComparableMatcherResult3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.should6import io.kotest.matchers.shouldNot7import kotlinx.serialization.ExperimentalSerializationApi8import kotlinx.serialization.json.Json9import kotlinx.serialization.json.JsonArray10import kotlinx.serialization.json.JsonElement11import kotlinx.serialization.json.JsonObject12import kotlin.reflect.KClass13@OptIn(ExperimentalSerializationApi::class)14internal val pretty by lazy { Json { prettyPrint = true; prettyPrintIndent = " " } }15/**16 * Verifies that the [expected] string is valid json, and that it matches this string.17 *18 * This matcher will consider two json strings matched if they have the same key-values pairs,19 * regardless of order.20 *21 */22infix fun String?.shouldMatchJson(expected: String?) = this should matchJson(expected)23infix fun String?.shouldNotMatchJson(expected: String?) = this shouldNot matchJson(expected)24@OptIn(ExperimentalSerializationApi::class)25fun matchJson(expected: String?) = object : Matcher<String?> {26 override fun test(value: String?): MatcherResult {27 val actualJson = try {28 value?.let(pretty::parseToJsonElement)29 } catch (ex: Exception) {30 return MatcherResult(31 false,32 { "expected: actual json to be valid json: $value" },33 {34 "expected: actual json to be invalid json: $value"35 })36 }37 val expectedJson = try {38 expected?.let(pretty::parseToJsonElement)39 } catch (ex: Exception) {40 return MatcherResult(41 false,42 { "expected: expected json to be valid json: $expected" },43 { "expected: expected json to be invalid json: $expected" }44 )45 }46 return ComparableMatcherResult(47 actualJson == expectedJson,48 { "expected json to match, but they differed\n\n" },49 { "expected not to match with: $expectedJson but match: $actualJson" },50 actualJson.toString(),51 expectedJson.toString()52 )53 }54}55@OptIn(ExperimentalSerializationApi::class)56fun beValidJson() = object : Matcher<String?> {57 override fun test(value: String?): MatcherResult {58 return try {59 value?.let(pretty::parseToJsonElement)60 MatcherResult(61 true,62 { "expected: actual json to be valid json: $value" },63 { "expected: actual json to be invalid json: $value" }64 )65 } catch (ex: Exception) {66 MatcherResult(67 false,68 { "expected: actual json to be valid json: $value" },69 { "expected: actual json to be invalid json: $value" }70 )71 }72 }73}74@OptIn(ExperimentalSerializationApi::class)75fun beJsonType(kClass: KClass<*>) = object : Matcher<String?> {76 override fun test(value: String?): MatcherResult {77 val element = try {78 value?.let(pretty::parseToJsonElement)79 } catch (ex: Exception) {80 return MatcherResult(81 false,82 { "expected: actual json to be valid json: $value" },83 { "expected: actual json to be invalid json: $value" }84 )85 }86 return MatcherResult(87 kClass.isInstance(element),88 { "expected: $value to be valid json of type: ${kClass.simpleName}" },89 { "expected: $value to not be of type: ${kClass.simpleName}" }90 )91 }92}93/**94 * Verifies that the [expected] string is valid json, and that it matches this string.95 *96 * This matcher will consider two json strings matched if they have the same key-values pairs,97 * regardless of order.98 *99 */100fun String.shouldEqualJson(expected: String, mode: CompareMode, order: CompareOrder) =101 this.shouldEqualJson(expected, legacyOptions(mode, order))102fun String.shouldEqualJson(expected: String, options: CompareJsonOptions) {103 val (e, a) = parse(expected, this)104 a should equalJson(e, options)105}106fun String.shouldNotEqualJson(expected: String, mode: CompareMode, order: CompareOrder) =107 this.shouldNotEqualJson(expected, legacyOptions(mode, order))108fun String.shouldNotEqualJson(expected: String, options: CompareJsonOptions) {109 val (e, a) = parse(expected, this)110 a shouldNot equalJson(e, options)111}112fun String.shouldBeEmptyJsonArray(): String {113 this should matchJson("[]")114 return this115}116fun String.shouldBeEmptyJsonObject(): String {117 this should matchJson("{}")118 return this119}120fun String.shouldBeJsonArray(): String {121 this should beJsonType(JsonArray::class)122 return this123}124fun String.shouldNotBeJsonArray(): String {125 this shouldNot beJsonType(JsonArray::class)126 return this127}128fun String.shouldBeJsonObject(): String {129 this should beJsonType(JsonObject::class)130 return this131}132fun String.shouldNotBeJsonObject(): String {133 this shouldNot beJsonType(JsonObject::class)134 return this135}136fun String.shouldBeValidJson(): String {137 this should beValidJson()138 return this139}140fun String.shouldNotBeValidJson(): String {141 this shouldNot beValidJson()142 return this143}144internal fun parse(expected: String, actual: String): Pair<JsonTree, JsonTree> {145 val enode = pretty.parseToJsonElement(expected)146 val anode = pretty.parseToJsonElement(actual)147 val e = toJsonTree(enode)148 val a = toJsonTree(anode)149 return Pair(e, a)150}151internal fun toJsonTree(root: JsonElement) =152 with(root.toJsonNode()) {153 JsonTree(this, prettyPrint(this))154 }...

Full Screen

Full Screen

LenientOrderArrayTest.kt

Source:LenientOrderArrayTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.tests.json2import io.kotest.assertions.json.ArrayOrder3import io.kotest.assertions.json.compareJsonOptions4import io.kotest.assertions.json.shouldEqualJson5import io.kotest.assertions.shouldFail6import io.kotest.core.spec.style.FunSpec7import io.kotest.matchers.throwable.shouldHaveMessage8class LenientOrderArrayTest : FunSpec(9 {10 infix fun String.shouldEqualJsonIgnoringOrder(other: String) =11 this.shouldEqualJson(other, compareJsonOptions { arrayOrder = ArrayOrder.Lenient })12 test("simple") {13 "[1, 2]" shouldEqualJsonIgnoringOrder "[2, 1]"14 }15 test("multiple copies") {16 "[1, 2, 2]" shouldEqualJsonIgnoringOrder "[2, 1, 2]"17 }18 test("duplicates in actual") {19 shouldFail {20 "[1, 2, 2]" shouldEqualJsonIgnoringOrder "[1, 2, 0]"21 }.shouldHaveMessage(22 """23 At '[2]' has extra element '2' not found (or too few) in '[1,2,0]'24 expected:<[25 1,...

Full Screen

Full Screen

matchers.kt

Source:matchers.kt Github

copy

Full Screen

...25 options: CompareJsonOptions26) =27 object : Matcher<JsonTree> {28 override fun test(value: JsonTree): MatcherResult {29 val error = compare(30 path = listOf(),31 expected = expected.root,32 actual = value.root,33 options,34 )?.asString()35 return ComparableMatcherResult(36 error == null,37 { "$error\n\n" },38 { "Expected values to not match\n\n" },39 value.raw,40 expected.raw,41 )42 }43 }44data class JsonTree(val root: JsonNode, val raw: String)45infix fun String.shouldEqualJson(expected: String): Unit =46 this.shouldEqualJson(expected, defaultCompareJsonOptions)47infix fun String.shouldNotEqualJson(expected: String): Unit =48 this.shouldNotEqualJson(expected, defaultCompareJsonOptions)49fun String.shouldEqualJson(expected: String, mode: CompareMode) =50 shouldEqualJson(expected, legacyOptions(mode, CompareOrder.Strict))51fun String.shouldNotEqualJson(expected: String, mode: CompareMode) =52 shouldNotEqualJson(expected, legacyOptions(mode, CompareOrder.Strict))53fun String.shouldEqualJson(expected: String, order: CompareOrder) =54 shouldEqualJson(expected, legacyOptions(CompareMode.Strict, order))55fun String.shouldNotEqualJson(expected: String, order: CompareOrder) =56 shouldNotEqualJson(expected, legacyOptions(CompareMode.Strict, order))57infix fun String.shouldEqualSpecifiedJson(expected: String) =58 shouldEqualJson(expected, compareJsonOptions { fieldComparison = FieldComparison.Lenient })59infix fun String.shouldNotEqualSpecifiedJson(expected: String) =60 shouldNotEqualJson(expected, compareJsonOptions { fieldComparison = FieldComparison.Lenient })

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.json.CompareJsonOptions2import io.kotest.assertions.json.compareJson3import io.kotest.assertions.json.shouldMatchJson4import io.kotest.assertions.json.shouldNotMatchJson5import io.kotest.core.spec.style.StringSpec6import io.kotest.matchers.shouldBe7import io.kotest.matchers.shouldNotBe8class CompareJsonOptionsExample : StringSpec({9"should match json with CompareJsonOptions" {10val expected = """{"name": "John", "age": 30}"""11val actual = """{"name": "John", "age": 30}"""12val options = CompareJsonOptions(ignoreArrayOrder = true)13compareJson(expected, actual, options) shouldBe true14}15"should not match json with CompareJsonOptions" {16val expected = """{"name": "John", "age": 30}"""17val actual = """{"name": "John", "age": 30}"""18val options = CompareJsonOptions(ignoreArrayOrder = false)19compareJson(expected, actual, options) shouldBe false20}21"should match json with CompareJsonOptions using shouldMatchJson" {22val expected = """{"name": "John", "age": 30}"""23val actual = """{"name": "John", "age": 30}"""24val options = CompareJsonOptions(ignoreArrayOrder = true)25}26"should not match json with CompareJsonOptions using shouldNotMatchJson" {27val expected = """{"name": "John", "age": 30}"""28val actual = """{"name": "John", "age": 30}"""29val options = CompareJsonOptions(ignoreArrayOrder = false)30}31})32import io.kotest.assertions.json.CompareJsonOptions33import io.kotest.assertions.json.compareJson34import io.kotest.assertions.json.shouldMatchJson35import io.kotest.assertions.json.shouldNotMatchJson36import io.kotest.core.spec.style.StringSpec37import io.kotest.matchers.shouldBe38import io.kotest.matchers.shouldNotBe39class CompareJsonOptionsExample : StringSpec({40"should match json with CompareJsonOptions" {

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1val expectedJson = """{ "name": "kotest", "version": 4 }"""2val actualJson = """{ "name": "kotest", "version": 5 }"""3CompareJsonOptions().compareJson(expectedJson, actualJson) shouldBe false4val expectedJson = """{ "name": "kotest", "version": 4 }"""5val actualJson = """{ "name": "kotest", "version": 4 }"""6CompareJsonOptions().compareJson(expectedJson, actualJson) shouldBe true7val expectedJson = """{ "name": "kotest", "version": 4 }"""8val actualJson = """{ "name": "kotest", "version": 4 }"""9CompareJsonOptions().compareJson(expectedJson, actualJson) shouldBe true10val expectedJson = """{ "name": "kotest", "version": 4 }"""11val actualJson = """{ "name": "kotest", "version": 4 }"""12CompareJsonOptions().compareJson(expectedJson, actualJson) shouldBe true13val expectedJson = """{ "name": "kotest", "version": 4 }"""14val actualJson = """{ "name": "kotest", "version": 4 }"""15CompareJsonOptions().compareJson(expectedJson, actualJson) shouldBe true16val expectedJson = """{ "name": "kotest", "version": 4 }"""17val actualJson = """{ "name": "kotest", "version": 4 }"""18CompareJsonOptions().compareJson(expectedJson, actualJson) shouldBe true19val expectedJson = """{ "name": "kotest", "version": 4 }"""20val actualJson = """{ "name": "kotest", "version": 4 }"""

Full Screen

Full Screen

compare

Using AI Code Generation

copy

Full Screen

1val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )2val result = json1 . compareJson ( json2 , options )3result . shouldBe ( true )4val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )5val result = json1 . compareJson ( json2 , options )6result . shouldBe ( true )7val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )8val result = json1 . compareJson ( json2 , options )9result . shouldBe ( true )10val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )11val result = json1 . compareJson ( json2 , options )12result . shouldBe ( true )13val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )14val result = json1 . compareJson ( json2 , options )15result . shouldBe ( true )16val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )17val result = json1 . compareJson ( json2 , options )18result . shouldBe ( true )19val options = CompareJsonOptions ( ignoreExtraKeys = true , ignoreExtraValues = true , ignoreOrder = true )20val result = json1 . compareJson ( json2

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