Skip to content

Commit

Permalink
addscala 3 enumeration decoding/encoding test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThijsBroersen committed Feb 21, 2024
1 parent 9dee499 commit 008e5b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ object DerivedDecoderSpec extends ZIOSpecDefault {
"""
})(isRight(anything))
},
test("Derives for a sum type") {
test("Derives for a sum Enumeration type") {
enum Foo derives JsonDecoder:
case Bar
case Baz
case Qux

val result = "\"Qux\"".fromJson[Foo]

assertTrue(result == Right(Foo.Qux))
},
test("Derives for a sum ADT type") {
assertZIO(typeCheck {
"""
enum Foo derives JsonDecoder:
Expand Down
28 changes: 18 additions & 10 deletions zio-json/shared/src/test/scala-3/zio/json/DerivedEncoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ object DerivedEncoderSpec extends ZIOSpecDefault {
"""
})(isRight(anything))
},
test("Derives for a sum type") {
assertZIO(typeCheck {
"""
enum Foo derives JsonEncoder:
case Bar
case Baz(baz: String)
case Qux(foo: Foo)
test("Derives for a sum Enumeration type") {
enum Foo derives JsonEncoder:
case Bar
case Baz
case Qux

(Foo.Qux(Foo.Bar): Foo).toJson
"""
})(isRight(anything))
val json = (Foo.Qux: Foo).toJson

assertTrue(json == """"Qux"""")
},
test("Derives for a sum ADT type") {
enum Foo derives JsonEncoder:
case Bar
case Baz(baz: String)
case Qux(foo: Foo)

val json = (Foo.Qux(Foo.Bar): Foo).toJson

assertTrue(json == """{"Qux":{"foo":{"Bar":{}}}}""")
}
)
}

0 comments on commit 008e5b1

Please sign in to comment.