|
| 1 | +package zio.blocks.schema.structural |
| 2 | + |
| 3 | +import zio.blocks.schema._ |
| 4 | +import zio.test._ |
| 5 | + |
| 6 | +/** |
| 7 | + * Tests for sealed trait to structural union type conversion (Scala 3 only). |
| 8 | + * |
| 9 | + * Per issue #517: Sealed traits convert to union types with Tag discriminators. |
| 10 | + * Example: sealed trait Result { Success, Failure } → Schema[{ type Tag = |
| 11 | + * "Success"; def value: Int } | { type Tag = "Failure"; def error: String }] |
| 12 | + */ |
| 13 | +object SealedTraitToUnionSpec extends ZIOSpecDefault { |
| 14 | + |
| 15 | + sealed trait Result |
| 16 | + object Result { |
| 17 | + case class Success(value: Int) extends Result |
| 18 | + case class Failure(error: String) extends Result |
| 19 | + } |
| 20 | + |
| 21 | + sealed trait Status |
| 22 | + object Status { |
| 23 | + case object Active extends Status |
| 24 | + case object Inactive extends Status |
| 25 | + } |
| 26 | + |
| 27 | + sealed trait Animal |
| 28 | + object Animal { |
| 29 | + case class Dog(name: String, breed: String) extends Animal |
| 30 | + case class Cat(name: String, indoor: Boolean) extends Animal |
| 31 | + case class Bird(name: String, canFly: Boolean) extends Animal |
| 32 | + } |
| 33 | + |
| 34 | + def spec = suite("SealedTraitToUnionSpec")( |
| 35 | + suite("Structural Conversion")( |
| 36 | + test("sealed trait converts to structural union") { |
| 37 | + val schema = Schema.derived[Result] |
| 38 | + val structural = schema.structural |
| 39 | + assertTrue(structural != null) |
| 40 | + }, |
| 41 | + test("structural sealed trait schema is a Variant") { |
| 42 | + val schema = Schema.derived[Result] |
| 43 | + val structural = schema.structural |
| 44 | + val isVariant = (structural.reflect: @unchecked) match { |
| 45 | + case _: Reflect.Variant[_, _] => true |
| 46 | + case _ => false |
| 47 | + } |
| 48 | + assertTrue(isVariant) |
| 49 | + }, |
| 50 | + test("structural sealed trait preserves case count") { |
| 51 | + val schema = Schema.derived[Result] |
| 52 | + val structural = schema.structural |
| 53 | + val caseCount = (structural.reflect: @unchecked) match { |
| 54 | + case v: Reflect.Variant[_, _] => v.cases.size |
| 55 | + case _ => -1 |
| 56 | + } |
| 57 | + assertTrue(caseCount == 2) |
| 58 | + }, |
| 59 | + test("structural sealed trait type name contains Tag markers") { |
| 60 | + val schema = Schema.derived[Result] |
| 61 | + val structural = schema.structural |
| 62 | + val typeName = structural.reflect.typeName.name |
| 63 | + assertTrue( |
| 64 | + typeName.contains("|"), |
| 65 | + typeName.contains("Tag:\"Success\""), |
| 66 | + typeName.contains("Tag:\"Failure\""), |
| 67 | + typeName.contains("value:Int"), |
| 68 | + typeName.contains("error:String") |
| 69 | + ) |
| 70 | + }, |
| 71 | + test("sealed trait with case objects converts to structural") { |
| 72 | + val schema = Schema.derived[Status] |
| 73 | + val structural = schema.structural |
| 74 | + val caseCount = (structural.reflect: @unchecked) match { |
| 75 | + case v: Reflect.Variant[_, _] => v.cases.size |
| 76 | + case _ => -1 |
| 77 | + } |
| 78 | + assertTrue(caseCount == 2) |
| 79 | + }, |
| 80 | + test("three variant sealed trait structural preserves all cases") { |
| 81 | + val schema = Schema.derived[Animal] |
| 82 | + val structural = schema.structural |
| 83 | + val caseNames = (structural.reflect: @unchecked) match { |
| 84 | + case v: Reflect.Variant[_, _] => v.cases.map(_.name).toSet |
| 85 | + case _ => Set.empty[String] |
| 86 | + } |
| 87 | + assertTrue( |
| 88 | + caseNames.size == 3, |
| 89 | + caseNames.contains("Dog"), |
| 90 | + caseNames.contains("Cat"), |
| 91 | + caseNames.contains("Bird") |
| 92 | + ) |
| 93 | + } |
| 94 | + ), |
| 95 | + suite("Structural Schema Behavior")( |
| 96 | + test("structural sealed trait encodes via DynamicValue") { |
| 97 | + val schema = Schema.derived[Result] |
| 98 | + val structural = schema.structural |
| 99 | + val value: Result = Result.Success(42) |
| 100 | + |
| 101 | + val structuralAny = structural.asInstanceOf[Schema[Any]] |
| 102 | + val dynamic = structuralAny.toDynamicValue(value) |
| 103 | + |
| 104 | + assertTrue(dynamic match { |
| 105 | + case DynamicValue.Variant("Success", DynamicValue.Record(fields)) => |
| 106 | + val fieldMap = fields.toMap |
| 107 | + fieldMap.get("value").contains(DynamicValue.Primitive(PrimitiveValue.Int(42))) |
| 108 | + case _ => false |
| 109 | + }) |
| 110 | + }, |
| 111 | + test("structural sealed trait decodes from DynamicValue") { |
| 112 | + val schema = Schema.derived[Result] |
| 113 | + val structural = schema.structural |
| 114 | + |
| 115 | + val dynamic = DynamicValue.Variant( |
| 116 | + "Success", |
| 117 | + DynamicValue.Record(Vector("value" -> DynamicValue.Primitive(PrimitiveValue.Int(100)))) |
| 118 | + ) |
| 119 | + |
| 120 | + val structuralAny = structural.asInstanceOf[Schema[Any]] |
| 121 | + val result = structuralAny.fromDynamicValue(dynamic) |
| 122 | + |
| 123 | + assertTrue(result match { |
| 124 | + case Right(recovered) => |
| 125 | + val r = recovered.asInstanceOf[Result] |
| 126 | + r == Result.Success(100) |
| 127 | + case _ => false |
| 128 | + }) |
| 129 | + }, |
| 130 | + test("structural sealed trait round-trips through DynamicValue") { |
| 131 | + val schema = Schema.derived[Result] |
| 132 | + val structural = schema.structural |
| 133 | + val value: Result = Result.Failure("error message") |
| 134 | + |
| 135 | + val structuralAny = structural.asInstanceOf[Schema[Any]] |
| 136 | + val dynamic = structuralAny.toDynamicValue(value) |
| 137 | + val result = structuralAny.fromDynamicValue(dynamic) |
| 138 | + |
| 139 | + assertTrue(result match { |
| 140 | + case Right(recovered) => |
| 141 | + val r = recovered.asInstanceOf[Result] |
| 142 | + r == value |
| 143 | + case _ => false |
| 144 | + }) |
| 145 | + }, |
| 146 | + test("structural case object round-trips correctly") { |
| 147 | + val schema = Schema.derived[Status] |
| 148 | + val structural = schema.structural |
| 149 | + val value: Status = Status.Active |
| 150 | + |
| 151 | + val structuralAny = structural.asInstanceOf[Schema[Any]] |
| 152 | + val dynamic = structuralAny.toDynamicValue(value) |
| 153 | + val result = structuralAny.fromDynamicValue(dynamic) |
| 154 | + |
| 155 | + assertTrue(result match { |
| 156 | + case Right(recovered) => |
| 157 | + val s = recovered.asInstanceOf[Status] |
| 158 | + s == value |
| 159 | + case _ => false |
| 160 | + }) |
| 161 | + }, |
| 162 | + test("all structural animal variants round-trip correctly") { |
| 163 | + val schema = Schema.derived[Animal] |
| 164 | + val structural = schema.structural |
| 165 | + |
| 166 | + val dog: Animal = Animal.Dog("Rex", "German Shepherd") |
| 167 | + val cat: Animal = Animal.Cat("Whiskers", true) |
| 168 | + val bird: Animal = Animal.Bird("Tweety", true) |
| 169 | + |
| 170 | + val structuralAny = structural.asInstanceOf[Schema[Any]] |
| 171 | + |
| 172 | + val dogResult = structuralAny.fromDynamicValue(structuralAny.toDynamicValue(dog)).map(_.asInstanceOf[Animal]) |
| 173 | + val catResult = structuralAny.fromDynamicValue(structuralAny.toDynamicValue(cat)).map(_.asInstanceOf[Animal]) |
| 174 | + val birdResult = structuralAny.fromDynamicValue(structuralAny.toDynamicValue(bird)).map(_.asInstanceOf[Animal]) |
| 175 | + |
| 176 | + assertTrue( |
| 177 | + dogResult == Right(dog), |
| 178 | + catResult == Right(cat), |
| 179 | + birdResult == Right(bird) |
| 180 | + ) |
| 181 | + }, |
| 182 | + test("structural animal variants preserve field information") { |
| 183 | + val schema = Schema.derived[Animal] |
| 184 | + val structural = schema.structural |
| 185 | + |
| 186 | + val dogFields = (structural.reflect: @unchecked) match { |
| 187 | + case v: Reflect.Variant[_, _] => |
| 188 | + v.cases.find(_.name == "Dog").flatMap { c => |
| 189 | + (c.value: @unchecked) match { |
| 190 | + case r: Reflect.Record[_, _] => Some(r.fields.map(_.name).toSet) |
| 191 | + case _ => None |
| 192 | + } |
| 193 | + } |
| 194 | + case _ => None |
| 195 | + } |
| 196 | + assertTrue( |
| 197 | + dogFields.isDefined, |
| 198 | + dogFields.get.contains("name"), |
| 199 | + dogFields.get.contains("breed") |
| 200 | + ) |
| 201 | + } |
| 202 | + ) |
| 203 | + ) |
| 204 | +} |
0 commit comments