scalatest-circe


License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.stephenn
ArtifactId

ArtifactId

scalatest-circe_2.11
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

scalatest-circe
scalatest-circe
Project URL

Project URL

https://github.com/stephennancekivell/scalatest-json
Project Organization

Project Organization

com.stephenn
Source Code Management

Source Code Management

https://github.com/stephennancekivell/scalatest-json

Download scalatest-circe_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.stephenn/scalatest-circe_2.11/ -->
<dependency>
    <groupId>com.stephenn</groupId>
    <artifactId>scalatest-circe_2.11</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.stephenn/scalatest-circe_2.11/
implementation 'com.stephenn:scalatest-circe_2.11:0.0.1'
// https://jarcasting.com/artifacts/com.stephenn/scalatest-circe_2.11/
implementation ("com.stephenn:scalatest-circe_2.11:0.0.1")
'com.stephenn:scalatest-circe_2.11:jar:0.0.1'
<dependency org="com.stephenn" name="scalatest-circe_2.11" rev="0.0.1">
  <artifact name="scalatest-circe_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.stephenn', module='scalatest-circe_2.11', version='0.0.1')
)
libraryDependencies += "com.stephenn" % "scalatest-circe_2.11" % "0.0.1"
[com.stephenn/scalatest-circe_2.11 "0.0.1"]

Dependencies

compile (6)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.scalatest : scalatest_2.11 jar 3.0.4
org.gnieh : diffson-circe_2.11 jar 2.2.5
io.circe : circe-core_2.11 jar 0.9.0
io.circe : circe-generic_2.11 jar 0.9.0
io.circe : circe-parser_2.11 jar 0.9.0

Project Modules

There are no modules declared in this project.

Scalatest-json

Build Status Maven Central

Scalatest matchers for Json with appropriate equality and descriptive error messages.

install

Add the dependency you want

libraryDependencies += "com.stephenn" %% "scalatest-json-jsonassert" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-json4s" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-play-json" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-circe" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-argonaut" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-jsoniter-scala" % "0.2.0"

You can also check the latest version on maven central.

usage

Your scalatest spec should extend or import JsonMatchers

class ExampleSpec extends FunSpec with Matchers with JsonMatchers {

  describe("JsonMatchers usage") {
    it("should pass matching json with different spacing and order") {
      val input = """
        |{
        | "some": "valid json",
        | "plus": ["json", "content"]
        |}
      """.stripMargin

      val expected = """
                    |{
                    | "plus": ["json", "content"],
                    |     "some":   "valid json"
                    |}
                  """.stripMargin

      input should matchJson(expected)
    }

    it("should fail on slightly different json explaining why") {
      val input = """
                    |{
                    | "someField": "valid json"
                    |}
                  """.stripMargin

      val expected = """
                       |{
                       | "someField": "different json"
                       |}
                     """.stripMargin

      input should matchJson(expected)
//
//      Fails
//
//      Json did not match {
//      "someField": "valid json"
//      } did not match {
//      "someField": "different json"
//      }
//
//      Json Diff:
//        someField
//      Expected: different json
//        got: valid json

    }
  }
}

For Jsoniter Scala the data structures and corresponding codec shall be defined, eg.:

  import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec
  import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker

  case class Data(some: String, plus: Seq[String])
  
  implicit val codec: JsonValueCodec[Data] = JsonCodecMaker.make

Please find a complete example here.

Golden Test

For the circe module you write a golden test that encodes and decodes a model, checking that it matches a golden example json.

it("can compare json and a model") {
    case class Foo(sameField: String)
    implicit val encoder = Encoder.forProduct1("someField")(Foo.unapply)
    implicit val decoder = Decoder.forProduct1("someField")(Foo.apply)
    
    val json = """
                |{
                | "someField": "valid json"
                |}
              """.stripMargin
    
    val model = Foo("valid json")
    
    model should matchJsonGolden(json)
}

Publishing

  • bump version to non snaphsot
  • commit and tag git tag scalatest-json-0.1.0 git push --tags
  • export GPG_TTY=$(tty)
  • sbt +publishSigned
  • sbt sonatypeReleaseAll
  • bump version to snapshot

Versions

Version
0.0.1