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
|
/*
* Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package utilities
import org.jetbrains.dokka.utilities.parseJson
import org.jetbrains.dokka.utilities.serializeAsCompactJson
import org.jetbrains.dokka.utilities.serializeAsPrettyJson
import kotlin.test.Test
import kotlin.test.assertEquals
class JsonTest {
@Test
fun `should serialize an object as compact json`() {
val testObject = SimpleTestDataClass(
someString = "Foo",
someInt = 42,
someDouble = 42.0
)
val actual = serializeAsCompactJson(testObject)
val expected = "{\"someString\":\"Foo\",\"someInt\":42,\"someIntWithDefaultValue\":42,\"someDouble\":42.0}"
assertEquals(expected, actual)
}
@Test
fun `should serialize an object as pretty json`() {
val testObject = SimpleTestDataClass(
someString = "Foo",
someInt = 42,
someDouble = 42.0
)
val actual = serializeAsPrettyJson(testObject)
val expected = """
{
"someString" : "Foo",
"someInt" : 42,
"someIntWithDefaultValue" : 42,
"someDouble" : 42.0
}""".trimIndent().withSystemLineSeparator()
assertEquals(expected, actual)
}
@Test
fun `should keep order of Set after serialize and deserialize`() {
val testObject = SimpleTestSetDataClass(
someStringSet = setOf("Foo", "Bar", "ABC")
)
val expected = testObject.someStringSet.toList() // ["Foo", "Bar", "ABC"]
val jsonString = serializeAsCompactJson(testObject)
val parsedClass: SimpleTestSetDataClass = parseJson(jsonString)
val actual = parsedClass.someStringSet.toList()
assertEquals(expected, actual)
}
/**
* If the expected output was generated on Linux, but the tests are run under Windows,
* the test might fail when comparing the strings due to different separators.
*/
private fun String.withSystemLineSeparator(): String = this.replace("\n", System.lineSeparator())
}
data class SimpleTestDataClass(
val someString: String,
val someInt: Int,
val someIntWithDefaultValue: Int = 42,
val someDouble: Double
)
data class SimpleTestSetDataClass(
val someStringSet: Set<String>
)
|