Skip to content

Commit

Permalink
Changed output from failing JSON assertions to match intellij format. C…
Browse files Browse the repository at this point in the history
…loses #2620 (#2694)
  • Loading branch information
Kantis committed Nov 29, 2021
1 parent 3fd716b commit ac3896b
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 372 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -45,3 +45,4 @@ documentation/site/
node_modules/

.docusaurus
.DS_Store
@@ -1,10 +1,15 @@
package io.kotest.assertions.json

import io.kotest.assertions.Actual
import io.kotest.assertions.Expected
import io.kotest.assertions.intellijFormatError
import io.kotest.assertions.print.printed
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement

Expand All @@ -20,6 +25,7 @@ internal val pretty by lazy { Json { prettyPrint = true; prettyPrintIndent = "
*/
infix fun String?.shouldMatchJson(expected: String?) = this should matchJson(expected)
infix fun String?.shouldNotMatchJson(expected: String?) = this shouldNot matchJson(expected)
@OptIn(ExperimentalSerializationApi::class)
fun matchJson(expected: String?) = object : Matcher<String?> {
override fun test(value: String?): MatcherResult {
val actualJson = try {
Expand All @@ -46,7 +52,12 @@ fun matchJson(expected: String?) = object : Matcher<String?> {

return MatcherResult(
actualJson == expectedJson,
{ "expected: $expectedJson but was: $actualJson" },
{
intellijFormatError(
Expected(expectedJson.toString().printed()),
Actual(actualJson.toString().printed())
)
},
{
"expected not to match with: $expectedJson but match: $actualJson"
})
Expand Down
@@ -1,5 +1,9 @@
package io.kotest.assertions.json

import io.kotest.assertions.Actual
import io.kotest.assertions.Expected
import io.kotest.assertions.intellijFormatError
import io.kotest.assertions.print.printed
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult

Expand Down Expand Up @@ -35,8 +39,9 @@ fun equalJson(

return MatcherResult(
error == null,
{ "$error\n\nexpected:\n${expected.raw}\n\nactual:\n${value.raw}\n" },
{ "Expected values to not match ${expected.raw}" })
{ "$error\n\n${intellijFormatError(Expected(expected.raw.printed()), Actual(value.raw.printed()))}" },
{ "Expected values to not match ${expected.raw}" }
)
}
}

Expand Down
@@ -1,5 +1,9 @@
package io.kotest.assertions.json

import io.kotest.assertions.Actual
import io.kotest.assertions.Expected
import io.kotest.assertions.intellijFormatError
import io.kotest.assertions.print.printed
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
Expand All @@ -26,7 +30,12 @@ fun matchJsonResource(resource: String) = object : Matcher<String?> {

return MatcherResult(
actualJson == expectedJson,
{ "expected: $expectedJson but was: $actualJson" },
{
intellijFormatError(
Expected(expectedJson.toString().printed()),
Actual(actualJson.toString().printed())
)
},
{
"expected not to match with: $expectedJson but match: $actualJson"
})
Expand Down
Expand Up @@ -32,17 +32,13 @@ class EqualIgnoringUnknownTest : FunSpec(
}.shouldHaveMessage(
"""At 'b' expected 'baz' but was 'bar'

expected:
{
expected:<{
"a": "foo",
"b": "baz"
}

actual:
{
}> but was:<{
"a": "foo",
"b": "bar"
}"""
}>"""
)
}

Expand All @@ -55,16 +51,12 @@ actual:
}.shouldHaveMessage(
"""The top level object was missing expected field(s) [b]

expected:
{
expected:<{
"a": "foo",
"b": "bar"
}

actual:
{
}> but was:<{
"a": "foo"
}"""
}>"""
)
}

Expand All @@ -89,21 +81,17 @@ actual:
}.shouldHaveMessage(
"""At 'wrapper.b' expected 'baz' but was 'bar'

expected:
{
expected:<{
"wrapper": {
"a": "foo",
"b": "baz"
}
}

actual:
{
}> but was:<{
"wrapper": {
"a": "foo",
"b": "bar"
}
}"""
}>"""
)
}

Expand All @@ -116,20 +104,16 @@ actual:
}.shouldHaveMessage(
"""At 'wrapper' object was missing expected field(s) [b]

expected:
{
expected:<{
"wrapper": {
"a": "foo",
"b": "bar"
}
}

actual:
{
}> but was:<{
"wrapper": {
"a": "foo"
}
}"""
}>"""
)
}

Expand All @@ -156,25 +140,21 @@ actual:
}.shouldHaveMessage(
"""At 'wrapper.[0].b' expected 'baz' but was 'bar'

expected:
{
expected:<{
"wrapper": [
{
"a": "foo",
"b": "baz"
}
]
}

actual:
{
}> but was:<{
"wrapper": [
{
"a": "foo",
"b": "bar"
}
]
}"""
}>"""
)
}

Expand All @@ -187,24 +167,20 @@ actual:
}.shouldHaveMessage(
"""At 'wrapper.[0]' object was missing expected field(s) [b]

expected:
{
expected:<{
"wrapper": [
{
"a": "foo",
"b": "bar"
}
]
}

actual:
{
}> but was:<{
"wrapper": [
{
"a": "foo"
}
]
}"""
}>"""
)
}
}
Expand Down

0 comments on commit ac3896b

Please sign in to comment.