-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #499 from RustedBones/tc-serialization-test-scal3
[scala3] Add test for serializable generated type-class
- Loading branch information
Showing
4 changed files
with
87 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
test/src/test/scalajvm/magnolia1/tests/SerializationTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package magnolia1.tests | ||
|
||
import magnolia1.* | ||
import magnolia1.examples.* | ||
|
||
import java.io.* | ||
class SerializationTests extends munit.FunSuite: | ||
import SerializationTests.* | ||
|
||
private def serializeToByteArray(value: Serializable): Array[Byte] = | ||
val buffer = new ByteArrayOutputStream() | ||
val oos = new ObjectOutputStream(buffer) | ||
oos.writeObject(value) | ||
buffer.toByteArray | ||
|
||
private def deserializeFromByteArray(encodedValue: Array[Byte]): AnyRef = | ||
val ois = new ObjectInputStream(new ByteArrayInputStream(encodedValue)) | ||
ois.readObject() | ||
|
||
def ensureSerializable[T <: Serializable](value: T): T = | ||
deserializeFromByteArray(serializeToByteArray(value)).asInstanceOf[T] | ||
|
||
test("generate serializable type-classes") { | ||
ensureSerializable(new Outer().showAddress) | ||
ensureSerializable(new Outer().showColor) | ||
} | ||
|
||
object SerializationTests: | ||
sealed trait Entity | ||
case class Company(name: String) extends Entity | ||
case class Person(name: String, age: Int) extends Entity | ||
case class Address(line1: String, occupant: Person) | ||
|
||
sealed trait Color | ||
case object Red extends Color | ||
case object Green extends Color | ||
case object Blue extends Color | ||
case object Orange extends Color | ||
case object Pink extends Color | ||
class Outer: | ||
val showAddress: Show[String, Address] = summon[Show[String, Address]] | ||
val showColor: Show[String, Color] = summon[Show[String, Color]] |