diff --git a/.scalafmt.conf b/.scalafmt.conf index 010fa6d..7016fbe 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1 +1 @@ -version = "2.7.5" +version = "3.0.8" diff --git a/argonaut/src/test/scala/com/stephenn/scalatest/argonaut/JsonMatchersSpec.scala b/argonaut/src/test/scala/com/stephenn/scalatest/argonaut/JsonMatchersSpec.scala index 2a841b9..86be134 100644 --- a/argonaut/src/test/scala/com/stephenn/scalatest/argonaut/JsonMatchersSpec.scala +++ b/argonaut/src/test/scala/com/stephenn/scalatest/argonaut/JsonMatchersSpec.scala @@ -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 } } @@ -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 } diff --git a/circe/src/main/scala/com/stephenn/scalatest/circe/JsonMatchers.scala b/circe/src/main/scala/com/stephenn/scalatest/circe/JsonMatchers.scala index c2d858b..4677561 100644 --- a/circe/src/main/scala/com/stephenn/scalatest/circe/JsonMatchers.scala +++ b/circe/src/main/scala/com/stephenn/scalatest/circe/JsonMatchers.scala @@ -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 => @@ -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 => @@ -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 = diff --git a/json4s/src/main/scala/com/stephenn/scalatest/json4s/JsonMatchers.scala b/json4s/src/main/scala/com/stephenn/scalatest/json4s/JsonMatchers.scala index 279e080..1576dea 100644 --- a/json4s/src/main/scala/com/stephenn/scalatest/json4s/JsonMatchers.scala +++ b/json4s/src/main/scala/com/stephenn/scalatest/json4s/JsonMatchers.scala @@ -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 => @@ -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 => @@ -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 = diff --git a/json4s/src/test/scala/com/stephenn/scalatest/json4s/JsonMatchersSpec.scala b/json4s/src/test/scala/com/stephenn/scalatest/json4s/JsonMatchersSpec.scala index 81669d7..011095a 100644 --- a/json4s/src/test/scala/com/stephenn/scalatest/json4s/JsonMatchersSpec.scala +++ b/json4s/src/test/scala/com/stephenn/scalatest/json4s/JsonMatchersSpec.scala @@ -7,10 +7,9 @@ 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 } } @@ -18,12 +17,13 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers { 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 } } diff --git a/jsonassert/src/main/scala/com/stephenn/scalatest/jsonassert/JsonMatchers.scala b/jsonassert/src/main/scala/com/stephenn/scalatest/jsonassert/JsonMatchers.scala index 6aef41e..17d7ec8 100644 --- a/jsonassert/src/main/scala/com/stephenn/scalatest/jsonassert/JsonMatchers.scala +++ b/jsonassert/src/main/scala/com/stephenn/scalatest/jsonassert/JsonMatchers.scala @@ -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 => diff --git a/jsonassert/src/test/scala/com/stephenn/scalatest/jsonassert/JsonMatchersSpec.scala b/jsonassert/src/test/scala/com/stephenn/scalatest/jsonassert/JsonMatchersSpec.scala index 322cdf2..ba28170 100644 --- a/jsonassert/src/test/scala/com/stephenn/scalatest/jsonassert/JsonMatchersSpec.scala +++ b/jsonassert/src/test/scala/com/stephenn/scalatest/jsonassert/JsonMatchersSpec.scala @@ -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 } } diff --git a/jsoniter-scala/build.sbt b/jsoniter-scala/build.sbt index f3308d9..14a2afe 100644 --- a/jsoniter-scala/build.sbt +++ b/jsoniter-scala/build.sbt @@ -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" ) diff --git a/jsoniter-scala/src/main/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchers.scala b/jsoniter-scala/src/main/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchers.scala index d3f8275..d84c727 100644 --- a/jsoniter-scala/src/main/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchers.scala +++ b/jsoniter-scala/src/main/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchers.scala @@ -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}", @@ -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( diff --git a/jsoniter-scala/src/test/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchersSpec.scala b/jsoniter-scala/src/test/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchersSpec.scala index b8580f1..6f2a51d 100644 --- a/jsoniter-scala/src/test/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchersSpec.scala +++ b/jsoniter-scala/src/test/scala/com/stephenn/scalatest/jsoniterscala/JsonMatchersSpec.scala @@ -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 @@ -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 } } @@ -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""" @@ -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 """ @@ -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 """ @@ -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: @@ -119,18 +123,19 @@ 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 } @@ -138,11 +143,10 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers { 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 } } diff --git a/play-json/src/main/scala/com/stephenn/scalatest/playjson/JsonMatchers.scala b/play-json/src/main/scala/com/stephenn/scalatest/playjson/JsonMatchers.scala index 9962cc7..b6e57e1 100644 --- a/play-json/src/main/scala/com/stephenn/scalatest/playjson/JsonMatchers.scala +++ b/play-json/src/main/scala/com/stephenn/scalatest/playjson/JsonMatchers.scala @@ -7,8 +7,7 @@ import scala.util.Try 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 => @@ -21,8 +20,7 @@ trait JsonMatchers { } } - /** - * Checks if the given json objects are equivalent. + /** Checks if the given json objects are equivalent. */ def matchJsonString(right: String): Matcher[JsValue] = { Matcher[JsValue] { left => @@ -35,10 +33,12 @@ trait JsonMatchers { } } - private def matchJsonResult(left: String, - right: String, - leftJson: JsValue, - rightJson: JsValue) = + private def matchJsonResult( + left: String, + right: String, + leftJson: JsValue, + rightJson: JsValue + ) = MatchResult( matches = leftJson == rightJson, rawFailureMessage = diff --git a/play-json/src/test/scala/com/stephenn/scalatest/playjson/JsonMatchersSpec.scala b/play-json/src/test/scala/com/stephenn/scalatest/playjson/JsonMatchersSpec.scala index 700efe1..66fda14 100644 --- a/play-json/src/test/scala/com/stephenn/scalatest/playjson/JsonMatchersSpec.scala +++ b/play-json/src/test/scala/com/stephenn/scalatest/playjson/JsonMatchersSpec.scala @@ -7,10 +7,9 @@ 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 } } @@ -22,10 +21,9 @@ class JsonMatchersSpec extends AnyFunSpec with Matchers { "{}" -> " { }", " [ ] " -> "[]", """{"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 } }