-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonParserTest.kt
77 lines (59 loc) · 1.99 KB
/
JsonParserTest.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
package com.gokhanaliccii.jsonparser
import com.gokhanaliccii.jsonparser.annotation.JsonList
import com.gokhanaliccii.jsonparser.annotation.JsonObject
import org.junit.Assert.*
import org.junit.Test
class JsonParserTest {
@Test
fun shouldParsedObjectNotNull() {
val input = "{\"name\":\"gokhan\",\"age\":1}"
val person = input.jsonTo(NamedPerson::class.java)
assertNotNull(person)
}
@Test
fun shouldParseStringFieldCorrectly() {
val expectedName = "gokhan"
val input = "{\"name\":\"gokhan\",\"age\":1}"
val person = input.jsonTo(NamedPerson::class.java)
assertTrue(expectedName == person?.name)
}
@Test
fun shouldParseJsonCorrectly() {
val expectedFrientCount = 1
val input = "{\"name\":\"gokhan\",\"age\":1,\"friends\":[{\"name\":\"ahmet\"}]}"
val student = input.jsonTo(Student::class.java)
assertTrue(expectedFrientCount == student?.friends?.size)
}
@Test
fun shouldParseInnerObjectCorrectly() {
val expectedFriendName = "ahmet"
val input = "{\"name\":\"gokhan\",\"age\":1,\"friend\":{\"name\":\"ahmet\"}}"
val student = input.jsonTo(SingleStudent::class.java)
assertTrue(expectedFriendName == student?.friend?.name)
}
@Test
fun shouldParseListObjectCorrectly() {
val expectedFriendName = "gokhan"
val input = "[{\"name\":\"gokhan\",\"age\":1}]"
val student = input.jsonToList(NamedPerson::class.java)
assertTrue(expectedFriendName == student?.first().name)
}
class Friend {
lateinit var name: String
}
class Student {
var age: Int = 0
lateinit var name: String
@JsonList(Friend::class)
lateinit var friends: List<Friend>
}
class SingleStudent {
var age: Int = 0
lateinit var name: String
@JsonObject(Friend::class)
lateinit var friend: Friend
}
class NamedPerson {
lateinit var name: String
}
}