From 2f538ed71a0393a94f71b68f8f518443a4facda3 Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Sun, 30 Jun 2024 17:25:40 +0200 Subject: [PATCH] Tests for JsonCodec for Schema.EnumN (#700) --- .../zio/schema/codec/JsonCodecSpec.scala | 83 ++++++++++++++++++- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala index 8dddf0733..79ded3d3c 100644 --- a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala +++ b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala @@ -417,7 +417,7 @@ object JsonCodecSpec extends ZIOSpecDefault { ) } ), - suite("Generic Record") { + suite("Generic Record")( test("Do not encode transient field") { assertEncodes( RecordExample.schema.annotate(rejectExtraFields()), @@ -427,8 +427,16 @@ object JsonCodecSpec extends ZIOSpecDefault { ) ) } - - } + ), + suite("EnumN")( + test("Respects the case name annotation") { + assertEncodesJson( + Enum23Cases.schema, + Enum23Cases.Case1("foo"), + """{"NumberOne":{"value":"foo"}}""" + ) + } + ) ) private val decoderSuite = suite("decoding")( @@ -447,7 +455,7 @@ object JsonCodecSpec extends ZIOSpecDefault { ) } @@ TestAspect.jvmOnly ), - suite("generic record")( + suite("Generic record")( test("with extra fields") { assertDecodes( recordSchema, @@ -484,6 +492,22 @@ object JsonCodecSpec extends ZIOSpecDefault { ) } ), + suite("EnumN")( + test("Respects the case name annotation") { + assertDecodes( + Enum23Cases.schema, + Enum23Cases.Case1("foo"), + charSequenceToByteChunk("""{"NumberOne":{"value":"foo"}}""") + ) + }, + test("Respects case aliases") { + assertDecodes( + Enum23Cases.schema, + Enum23Cases.Case1("foo"), + charSequenceToByteChunk("""{"One":{"value":"foo"}}""") + ) + } + ), suite("transform")( test("string") { val stringSchema = Schema.Primitive(StandardType.StringType) @@ -1975,4 +1999,55 @@ object JsonCodecSpec extends ZIOSpecDefault { DeriveSchema.gen[RecordExampleWithOptField] } + sealed trait Enum23Cases + + object Enum23Cases { + implicit lazy val schema: Schema[Enum23Cases] = DeriveSchema.gen[Enum23Cases] + + @caseName("NumberOne") @caseNameAliases("One") case class Case1(value: String) extends Enum23Cases + + case class Case2(value: Int) extends Enum23Cases + + case class Case3(value: String) extends Enum23Cases + + case class Case4(value: String) extends Enum23Cases + + case class Case5(value: String) extends Enum23Cases + + case class Case6(value: String) extends Enum23Cases + + case class Case7(value: String) extends Enum23Cases + + case class Case8(value: String) extends Enum23Cases + + case class Case9(value: String) extends Enum23Cases + + case class Case10(value: String) extends Enum23Cases + + case class Case11(value: String) extends Enum23Cases + + case class Case12(value: String) extends Enum23Cases + + case class Case13(value: String) extends Enum23Cases + + case class Case14(value: String) extends Enum23Cases + + case class Case15(value: String) extends Enum23Cases + + case class Case16(value: String) extends Enum23Cases + + case class Case17(value: String) extends Enum23Cases + + case class Case18(value: String) extends Enum23Cases + + case class Case19(value: String) extends Enum23Cases + + case class Case20(value: String) extends Enum23Cases + + case class Case21(value: String) extends Enum23Cases + + case class Case22(value: String) extends Enum23Cases + + case class Case23(value: String) extends Enum23Cases + } }