Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "2.7.5"
version = "3.0.8"
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
" [ ] " -> "[]",
"0" -> "0",
"""{"a":0, "b":1}""" -> """{"b":1,"a":0}"""
).foreach {
case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
).foreach { case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
}
}

Expand Down Expand Up @@ -93,19 +92,23 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
matchResult.matches shouldBe false
val expectedMsg = s"""Json did not match.
|{
| "someField": ${red("\"different json\"")}${red(" -> ")}${green(
"\"valid json\""
)},
| "someField": ${red("\"different json\"")}${red(
" -> "
)}${green(
"\"valid json\""
)},
| "otherField": [
| "json",
| ${red("\"stuff\"")}${red(" -> ")}${green(
"\"content\""
)},
"\"content\""
)},
| ${green("\"changes\"")}],
| "third": [
| {
| "a": ${red("2")}${red(" -> ")}${green("1")},
| "${red("b")}": ${red("3")}}]}""".stripMargin
| "${red("b")}": ${red(
"3"
)}}]}""".stripMargin
matchResult.rawFailureMessage shouldBe expectedMsg
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import scala.reflect.ClassTag

trait JsonMatchers {

/**
* Checks if the given json objects are equivalent.
/** Checks if the given json objects are equivalent.
*/
def matchJson(right: String): Matcher[String] = {
Matcher[String] { left =>
Expand All @@ -24,8 +23,7 @@ trait JsonMatchers {
}
}

/**
* Checks if the given json objects are equivalent.
/** Checks if the given json objects are equivalent.
*/
def matchJsonString(right: String): Matcher[Json] = {
Matcher[Json] { left =>
Expand All @@ -38,10 +36,12 @@ trait JsonMatchers {
}
}

private def matchJsonResult(left: String,
right: String,
leftJson: Json,
rightJson: Json) =
private def matchJsonResult(
left: String,
right: String,
leftJson: Json,
rightJson: Json
) =
MatchResult(
matches = leftJson == rightJson,
rawFailureMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import org.json4s.native.JsonMethods._

trait JsonMatchers {

/**
* Checks if the given json objects are equivalent.
/** Checks if the given json objects are equivalent.
*/
def matchJson(right: String): Matcher[String] = {
Matcher[String] { left =>
Expand All @@ -20,8 +19,7 @@ trait JsonMatchers {
}
}

/**
* Checks if the given json objects are equivalent.
/** Checks if the given json objects are equivalent.
*/
def matchJsonString(right: String): Matcher[JValue] = {
Matcher[JValue] { left =>
Expand All @@ -34,10 +32,12 @@ trait JsonMatchers {
}
}

private def matchJsonResult(left: String,
right: String,
leftJson: JValue,
rightJson: JValue) =
private def matchJsonResult(
left: String,
right: String,
leftJson: JValue,
rightJson: JValue
) =
MatchResult(
matches = leftJson == rightJson,
rawFailureMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {

describe("JsonMatchers") {
it("should pass when json is the same") {
Seq("{}" -> "{}", "[]" -> "[]").foreach {
case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
Seq("{}" -> "{}", "[]" -> "[]").foreach { case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
}
}

it("should pass when json is equivalent") {
val matchResult = JsonMatchers.matchJson("{ }").apply(" {} ")
matchResult.matches shouldBe true

Seq("{}" -> " { }",
" [ ] " -> "[]",
"""{"a":0, "b":1}""" -> """{"b":1,"a":0}""").foreach {
case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
Seq(
"{}" -> " { }",
" [ ] " -> "[]",
"""{"a":0, "b":1}""" -> """{"b":1,"a":0}"""
).foreach { case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import org.scalatest.matchers.Matcher

trait JsonMatchers {

/**
* Checks if the given json objects are equivalent.
/** Checks if the given json objects are equivalent.
*/
def matchJson(right: String): Matcher[String] =
Matcher[String] { left =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
val matchResult = JsonMatchers.matchJson("{ }").apply(" {} ")
matchResult.matches shouldBe true

Seq("{}" -> " { }",
" [ ] " -> "[]",
"0" -> "0",
"""{"a":0, "b":1}""" -> """{"b":1,"a":0}""").foreach {
case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
Seq(
"{}" -> " { }",
" [ ] " -> "[]",
"0" -> "0",
"""{"a":0, "b":1}""" -> """{"b":1,"a":0}"""
).foreach { case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
}
}

Expand Down
2 changes: 1 addition & 1 deletion jsoniter-scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name := "scalatest-jsoniter-scala"

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.9",
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.9.1",
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % "2.9.1",
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % "2.9.1" % "provided",
"com.softwaremill.diffx" %% "diffx-core" % "0.3.30"
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,34 @@ import scala.util.{Failure, Success, Try}

trait JsonMatchers {

/**
* Checks if the given JSON objects are equivalent.
/** Checks if the given JSON objects are equivalent.
*/
def matchJson[T: JsonValueCodec : ClassTag](expected: String)(implicit diffInst: Diff[T]): Matcher[String] = {
def matchJson[T: JsonValueCodec: ClassTag](
expected: String
)(implicit diffInst: Diff[T]): Matcher[String] = {
Matcher[String] { actual =>
(parse(actual), parse(expected)) match {
case (Left(error), _) => parseError(actual.trim, error.getMessage)
case (_, Left(error)) => parseError(expected.trim, error.getMessage)
case (Right(actual), Right(expected)) => comparisonResult(actual, expected)
case (Right(actual), Right(expected)) =>
comparisonResult(actual, expected)
}
}
}

private def parse[T: JsonValueCodec : ClassTag](json: String): Either[Throwable, T] = {
private def parse[T: JsonValueCodec: ClassTag](
json: String
): Either[Throwable, T] = {
Try(readFromArray(json.getBytes("UTF-8"))) match {
case Success(parsedData) => Right(parsedData)
case Failure(error) => Left(error)
case Failure(error) => Left(error)
}
}

private def parseError[T: JsonValueCodec : ClassTag](left: String, right: String) = {
private def parseError[T: JsonValueCodec: ClassTag](
left: String,
right: String
) = {
MatchResult(
matches = false,
rawFailureMessage = "Could not parse json {0} error: {1}",
Expand All @@ -38,7 +45,10 @@ trait JsonMatchers {
)
}

private def comparisonResult[T: JsonValueCodec : ClassTag](actual: T, expected: T)(implicit diffInst: Diff[T]) = {
private def comparisonResult[T: JsonValueCodec: ClassTag](
actual: T,
expected: T
)(implicit diffInst: Diff[T]) = {
val diffResult = Diff.compare(actual, expected)(diffInst)
val s = diffResult.show
MatchResult(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.stephenn.scalatest.jsoniterscala

import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec
import com.github.plokhotnyuk.jsoniter_scala.macros.{CodecMakerConfig, JsonCodecMaker}
import com.github.plokhotnyuk.jsoniter_scala.macros.{
CodecMakerConfig,
JsonCodecMaker
}
import com.softwaremill.diffx.{Derived, Diff}
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -24,10 +27,9 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
Seq(
"""{"a":1}""" -> """{"a":1}""",
"""{"a":1,"b": 2}""" -> """{"a":1,"b":2}"""
).foreach {
case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
).foreach { case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
}
}

Expand All @@ -38,16 +40,15 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
"""{"a":1}""" -> """{ "a" : 1, "b" : null }""",
"""{"a":0, "b":1}""" -> """{"b":1,"a":0}""",
"""{"b":1,"a":0}""" -> """{"a":0, "b":1}"""
).foreach {
case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
).foreach { case (left, right) =>
val matchResult = JsonMatchers.matchJson(right).apply(left)
matchResult.matches shouldBe true
}
}

it("should show deviation between two JSON objects") {
val matchResult = JsonMatchers.matchJson(
"""{"b":2,"a":0}""").apply("""{"a":0, "b":1}""")
val matchResult =
JsonMatchers.matchJson("""{"b":2,"a":0}""").apply("""{"a":0, "b":1}""")
matchResult.matches shouldBe false
matchResult.failureMessage shouldBe
s"""
Expand All @@ -59,7 +60,8 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
}

it("should fail when left has extra fields") {
val matchResult = JsonMatchers.matchJson("""{"a":1,"x":1}""").apply("""{"a":1}""")
val matchResult =
JsonMatchers.matchJson("""{"a":1,"x":1}""").apply("""{"a":1}""")
matchResult.matches shouldBe false
matchResult.failureMessage shouldBe
"""
Expand All @@ -73,7 +75,8 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
}

it("should fail when right has extra fields") {
val matchResult = JsonMatchers.matchJson("""{"a":1}""").apply("""{"a":1,"x":1}""")
val matchResult =
JsonMatchers.matchJson("""{"a":1}""").apply("""{"a":1,"x":1}""")
matchResult.matches shouldBe false
matchResult.failureMessage shouldBe
"""
Expand All @@ -87,7 +90,8 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
}

it("should fail on invalid JSON Object") {
val matchResult = JsonMatchers.matchJson("""{"a":1}""").apply("""{"a":nope}""")
val matchResult =
JsonMatchers.matchJson("""{"a":1}""").apply("""{"a":nope}""")
matchResult.matches shouldBe false
matchResult.failureMessage shouldBe
"""Could not parse json "{"a":nope}" error: "illegal number, offset: 0x00000005, buf:
Expand Down Expand Up @@ -119,30 +123,30 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers {
Seq(
"""{"items": [{"a":1}]}""" -> """{"items": [{"a":1}]}""",
"""{ "items": [ { "a":1,"b": 2 } ] }""" -> """{"items": [{"a":1,"b":2}]}"""
).foreach {
case (left, right) =>
// note that explicit [Root] is for binding with particular codec explicitly
val matchResult = JsonMatchers.matchJson[Root](right).apply(left)
matchResult.matches shouldBe true
).foreach { case (left, right) =>
// note that explicit [Root] is for binding with particular codec explicitly
val matchResult = JsonMatchers.matchJson[Root](right).apply(left)
matchResult.matches shouldBe true
}
}

it("should pass even left has extra field, but skipUnexpectedFields config param is disabled") {
it(
"should pass even left has extra field, but skipUnexpectedFields config param is disabled"
) {
// note that explicit [Data] is for binding with particular codec explicitly
val matchResult = JsonMatchers.matchJson[Data](
"""{"a":1,"x":1}""").apply("""{"a":1}""")
val matchResult =
JsonMatchers.matchJson[Data]("""{"a":1,"x":1}""").apply("""{"a":1}""")
matchResult.matches shouldBe true
}

it("should pass when another JSON object is equivalent") {
Seq(
"""[{"a":1}]""" -> """[ { "a" : 1 } ]""",
"""[]""" -> """[ ]"""
).foreach {
case (left, right) =>
// note that explicit [List[Data]] is for binding with particular codec explicitly
val matchResult = JsonMatchers.matchJson[List[Data]](right).apply(left)
matchResult.matches shouldBe true
).foreach { case (left, right) =>
// note that explicit [List[Data]] is for binding with particular codec explicitly
val matchResult = JsonMatchers.matchJson[List[Data]](right).apply(left)
matchResult.matches shouldBe true
}
}

Expand Down
Loading