-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSerialization.kt
149 lines (137 loc) · 5.28 KB
/
TestSerialization.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import dynamic.DoNotSerialize
import dynamic.SerializeAs
import dynamic.Stringify
import dynamic.serialize
import model.elements.*
import kotlin.test.Test
import kotlin.test.assertEquals
class TestSerialization {
@Test
fun testNull() {
assertEquals(Null(), null.serialize())
}
@Test
fun `Only Primitive and String Properties`() {
data class Student(val name: String, val number: Int)
val serial = Student("Afonso", 92494).serialize()
println(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("name", JSONString("Afonso")),
JSONProperty("number", JSONNumber(92494))
)), serial)
}
@Test
fun `With Collection Properties`() {
data class Curriculum(val subjects: List<String>)
val serial = Curriculum(listOf("PA", "ELP")).serialize()
println(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("subjects", JSONArray(mutableListOf(
JSONString("PA"),
JSONString("ELP")
)))
)), serial)
}
enum class StudentType { REGULAR }
@Test
fun `With Enumerator Properties`() {
data class Student(val studentType: StudentType)
val serial = Student(StudentType.REGULAR).serialize()
println(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("studentType", JSONString("REGULAR"))
)), serial)
}
@Test
fun `With Map Properties`() {
data class Student(val grades: Map<String, Int>)
val serial = Student(mapOf("AED" to 20, "TC" to 20)).serialize()
println(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("grades", JSONObject(mutableListOf(
JSONProperty("AED", JSONNumber(20)),
JSONProperty("TC", JSONNumber(20))
)))
)), serial)
}
@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 serial = Student("Afonso", CurricularUnit("PA", 6)).serialize()
println(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("name", JSONString("Afonso")),
JSONProperty("favourite", JSONObject(mutableListOf(
JSONProperty("name", JSONString("PA")),
JSONProperty("ects", JSONNumber(6))
)))
)), serial)
}
@Test
fun testAnnotations() {
data class CurricularUnit(
@SerializeAs("name") val id: String,
@DoNotSerialize val code: String,
@Stringify val ects: Double
)
val serial = CurricularUnit("PA", "M4310", 6.0).serialize()
println(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("name", JSONString("PA")),
JSONProperty("ects", JSONString("6.0"))
)), serial)
}
@Test
fun testEverythingAllAtOnce() {
data class Student(val name: String, @Stringify val number: Int, @DoNotSerialize val internalID: Int)
data class Professor(val name: String, val areasOfResearch: List<String>)
data class CurricularUnit(
@DoNotSerialize val internalID: String,
@SerializeAs("name") val identifier: String,
val coordinator: Professor,
@SerializeAs("professors") val faculty: Set<Professor>,
@SerializeAs("students") 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, 0)),
listOf("Kotlin", "Reflection", "Visitors")
)
val serial = uc.serialize()
print(serial)
assertEquals(JSONObject(mutableListOf(
JSONProperty("name", JSONString("PA")),
JSONProperty("coordinator", JSONObject(mutableListOf(
JSONProperty("name", JSONString("André Santos")),
JSONProperty("areasOfResearch", JSONArray(mutableListOf(
JSONString("Pedagogy"),
JSONString("Programming"),
JSONString("Strudel"),
JSONString("Weird Kotlin Things")
)))
))),
JSONProperty("professors", JSONArray(mutableListOf(
JSONObject(mutableListOf(
JSONProperty("name", JSONString("Probably ChatGPT")),
JSONProperty("areasOfResearch", JSONArray(mutableListOf(JSONString("Everything"))))
))
))),
JSONProperty("students", JSONArray(mutableListOf(
JSONObject(mutableListOf(
JSONProperty("name", JSONString("Afonso")),
JSONProperty("number", JSONString("92494"))
))
))),
JSONProperty("program", JSONArray(mutableListOf(
JSONString("Kotlin"),
JSONString("Reflection"),
JSONString("Visitors")
)))
)), serial)
}
}