-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDeserialization.kt
80 lines (70 loc) · 2.87 KB
/
TestDeserialization.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import dynamic.DeserializationException
import dynamic.deserialize
import dynamic.serialize
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class TestDeserialization {
@Test
fun `Only Primitive and String Properties`() {
data class Student(val name: String, val number: Int)
val student = Student("Afonso", 92494)
assertEquals(student, Student::class.deserialize(student.serialize()))
}
@Test
fun `With Collection Properties`() {
data class Curriculum(val subjects: List<String>)
val curriculum = Curriculum(listOf("PA", "ELP"))
assertEquals(curriculum, Curriculum::class.deserialize(curriculum.serialize()))
}
enum class StudentType { REGULAR, INTERNATIONAL }
@Test
fun `With Enumerator Properties`() {
data class Student(val studentType: StudentType)
val student = Student(StudentType.REGULAR)
assertEquals(student, Student::class.deserialize(student.serialize()))
}
@Test
fun `With Map Properties`() {
data class Student(val grades: Map<String, Int>)
val student = Student(mapOf("AED" to 20, "TC" to 20))
assertEquals(student, Student::class.deserialize(student.serialize()))
}
@Test
fun `With Nested Object Parameters`() {
data class CurricularUnit(val name: String, val ects: Int)
data class Student(val name: String, val favourite: CurricularUnit)
val student = Student("Afonso", CurricularUnit("PA", 6))
assertEquals(student, Student::class.deserialize(student.serialize()))
}
@Test
fun testEverything() {
data class Student(val name: String, val number: Int, val studentType: StudentType, val internalID: Int)
data class Professor(val name: String, val areasOfResearch: List<String>)
data class CurricularUnit(
val internalID: String,
val identifier: String,
val coordinator: Professor,
val faculty: Set<Professor>,
val studentBody: Set<Student>,
val program: List<String>
)
val uc = CurricularUnit(
"M4310",
"PA",
Professor("André Santos", listOf("Pedagogy", "Programming", "Strudel", "Weird Kotlin Things")),
setOf(Professor("Probably ChatGPT", listOf("Everything"))),
setOf(Student("Afonso", 92494, StudentType.INTERNATIONAL, 0)),
listOf("Kotlin", "Reflection", "Visitors")
)
assertEquals(uc, CurricularUnit::class.deserialize(uc.serialize()))
}
@Test
fun `Deserializing to Invalid Type`() {
data class CurricularUnit(val name: String, val ects: Int)
data class Student(val name: String, val number: Int)
assertFailsWith<DeserializationException> {
Student::class.deserialize(CurricularUnit("PA", 6).serialize())
}
}
}