Skip to content

Commit

Permalink
Add tests for ADTs with custom keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdziuban committed May 16, 2024
1 parent bea7025 commit a66ecef
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions upickle/test/src/upickle/MacroTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ object KeyedPerson {
implicit val rw: RW[KeyedPerson] = upickle.default.macroRW
}

@upickle.implicits.key("customKey")
sealed trait KeyedADT
object KeyedADT {
implicit val rw: RW[KeyedADT] = upickle.default.macroRW

case object Foo extends KeyedADT {
implicit val rw: RW[Foo.type] = upickle.default.macroRW
}
case class Bar(i: Int) extends KeyedADT
object Bar {
implicit val rw: RW[Bar] = upickle.default.macroRW
}
}

@upickle.implicits.key("customKey1")
sealed trait MultiKeyedADT1
@upickle.implicits.key("customKey2")
sealed trait MultiKeyedADT2
case object MultiKeyedObj extends MultiKeyedADT1 with MultiKeyedADT2

@upickle.implicits.key("customKey")
sealed trait SomeMultiKeyedADT1
sealed trait SomeMultiKeyedADT2
case object SomeMultiKeyedObj extends SomeMultiKeyedADT1 with SomeMultiKeyedADT2

object Custom {
trait ThingBase{
val i: Int
Expand Down Expand Up @@ -639,5 +664,23 @@ object MacroTests extends TestSuite {
UnknownKeys.DisallowPickler.read[UnknownKeys.DisAllow]("""{"id":1, "name":"x", "omg": "wtf"}""")
}
}

test("keyedADT") {
val fooJson = "\"upickle.KeyedADT.Foo\""
upickle.default.read[KeyedADT](fooJson) ==> KeyedADT.Foo
upickle.default.write[KeyedADT](KeyedADT.Foo) ==> fooJson

val barJson = """{"customKey":"upickle.KeyedADT.Bar","i":1}"""
upickle.default.read[KeyedADT](barJson) ==> KeyedADT.Bar(1)
upickle.default.write[KeyedADT](KeyedADT.Bar(1)) ==> barJson
}

test("multiKeyedADT") {
compileError("upickle.default.macroRW[upickle.MultiKeyedObj.type]")
.check("", "inherits from multiple parent types with different `@key` annotations")

compileError("upickle.default.macroRW[upickle.SomeMultiKeyedObj.type]")
.check("", "inherits from multiple parent types with different `@key` annotations")
}
}
}

0 comments on commit a66ecef

Please sign in to comment.