https://github.com/alejandrohdezma/circe-munit
MUnit assertions for Circe codecs
https://github.com/alejandrohdezma/circe-munit
circe json scala testing
Last synced: about 2 months ago
JSON representation
MUnit assertions for Circe codecs
- Host: GitHub
- URL: https://github.com/alejandrohdezma/circe-munit
- Owner: alejandrohdezma
- License: apache-2.0
- Created: 2024-09-10T06:02:39.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-01-06T07:33:25.000Z (5 months ago)
- Last Synced: 2025-04-12T02:37:33.476Z (about 2 months ago)
- Topics: circe, json, scala, testing
- Language: Scala
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
MUnit assertions for Circe codecs
## Installation
Add the following line to your build.sbt file:
```sbt
libraryDependencies += "com.alejandrohdezma" %% "circe-munit" % "0.1.0" % Test
```## Usage
Create a new test class and extend `munit.CirceSuite`:
```scala
import io.circe._
import io.circe.syntax._class MySuite extends munit.CirceSuite {
// We can asserts codecs, encoders and decoders
checkCodec(List(1, 2, 3)) {
Json.arr(1.asJson, 2.asJson, 3.asJson)
}checkEncoder(List(1, 2, 3)) {
Json.arr(1.asJson, 2.asJson, 3.asJson)
}checkDecoder(List(1, 2, 3)) {
Json.arr(1.asJson, 2.asJson, 3.asJson)
}// We can provide an extra description for the test
// Test name will be "Codec[Map[String, Int]] (some description)"checkCodec("some description")(Map("foo" -> 42, "bar" -> 43)) {
Json.obj("foo" := 42, "bar" := 43)
}// We can use any type with a codec, encoder or decoder
case class Foo(i: Int, s: String)
implicit val FooCodec: Codec[Foo] = Codec.forProduct2("i", "s")(Foo.apply)(foo => (foo.i, foo.s))
checkCodec(Foo(42, "foo")) {
Json.obj("i" := 42, "s" := "foo")
}// If the type has type parameters, they will also appear on the test name
case class Bar[A, B](a: A, b: B)
implicit def BarCodec[A: Encoder: Decoder, B: Encoder: Decoder]: Codec[Bar[A, B]] =
Codec.forProduct2[Bar[A, B], A, B]("a", "b")(Bar(_, _))(bar => (bar.a, bar.b))// Test name for 👇🏼 will be: `Codec[Bar[Foo, Map[String, Int]]]`
checkCodec(Bar(Foo(42, "foo"), Map("bar" -> 43))) {
Json.obj(
"a" := Json.obj("i" := 42, "s" := "foo"),
"b" := Json.obj("bar" := 43)
)
}}
```## Contributors to this project
|
|
| :--: |
| alejandrohdezma |