Skip to content

Commit 940577d

Browse files
committed
Add test for serializers
1 parent f37f4d8 commit 940577d

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2016-2024 JetBrains s.r.o.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.collections.immutable.serialization
7+
8+
import kotlin.test.Test
9+
import kotlin.test.assertContentEquals
10+
import kotlin.test.assertEquals
11+
import kotlinx.collections.immutable.ImmutableList
12+
import kotlinx.collections.immutable.ImmutableSet
13+
import kotlinx.collections.immutable.PersistentList
14+
import kotlinx.collections.immutable.PersistentSet
15+
import kotlinx.collections.immutable.persistentListOf
16+
import kotlinx.collections.immutable.persistentSetOf
17+
import kotlinx.collections.immutable.serialization.util.JsonConfigurationFactory
18+
import kotlinx.collections.immutable.serialization.util.JsonExt.encodeAndDecode
19+
import kotlinx.serialization.Serializable
20+
21+
class ImmutableCollectionSerializerTest {
22+
23+
@Serializable
24+
private class ImmutableListHolder<T>(
25+
@Serializable(with = ImmutableListSerializer::class)
26+
val immutableList: ImmutableList<T>
27+
)
28+
29+
@Test
30+
fun testImmutableList() {
31+
val json = JsonConfigurationFactory.createJsonConfiguration()
32+
persistentListOf(1, 2, 3)
33+
.let(::ImmutableListHolder)
34+
.let { expectedList -> assertContentEquals(expectedList.immutableList, json.encodeAndDecode(expectedList).immutableList) }
35+
}
36+
37+
@Serializable
38+
private class PersistentListHolder<T>(
39+
@Serializable(with = PersistentListSerializer::class)
40+
val persistentList: PersistentList<T>
41+
)
42+
43+
@Test
44+
fun testPersistentList() {
45+
val json = JsonConfigurationFactory.createJsonConfiguration()
46+
persistentListOf(1, 2, 3)
47+
.let(::PersistentListHolder)
48+
.let { expectedList -> assertContentEquals(expectedList.persistentList, json.encodeAndDecode(expectedList).persistentList) }
49+
}
50+
51+
@Serializable
52+
private class ImmutableSetHolder<T>(
53+
@Serializable(with = ImmutableSetSerializer::class)
54+
val immutableSet: ImmutableSet<T>
55+
)
56+
57+
@Test
58+
fun testImmutableSet() {
59+
val json = JsonConfigurationFactory.createJsonConfiguration()
60+
persistentSetOf(1, 2, 3)
61+
.let(::ImmutableSetHolder)
62+
.let { expectedSet -> assertEquals(expectedSet.immutableSet, json.encodeAndDecode(expectedSet).immutableSet) }
63+
}
64+
65+
@Serializable
66+
private class PersistentSetHolder<T>(
67+
@Serializable(with = PersistentSetSerializer::class)
68+
val persistentSet: PersistentSet<T>
69+
)
70+
71+
@Test
72+
fun testPersistentSet() {
73+
val json = JsonConfigurationFactory.createJsonConfiguration()
74+
persistentSetOf(1, 2, 3)
75+
.let(::PersistentSetHolder)
76+
.let { expectedSet -> assertEquals(expectedSet.persistentSet, json.encodeAndDecode(expectedSet).persistentSet) }
77+
}
78+
79+
@Serializable
80+
private class PersistentHashSetHolder<T>(
81+
@Serializable(with = PersistentHashSetSerializer::class)
82+
val persistentHashSet: PersistentSet<T>
83+
)
84+
85+
@Test
86+
fun testPersistentHashSet() {
87+
val json = JsonConfigurationFactory.createJsonConfiguration()
88+
persistentSetOf(1, 2, 3)
89+
.let(::PersistentHashSetHolder)
90+
.let { expectedSet -> assertEquals(expectedSet.persistentHashSet, json.encodeAndDecode(expectedSet).persistentHashSet) }
91+
}
92+
93+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2016-2024 JetBrains s.r.o.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.collections.immutable.serialization
7+
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
import kotlinx.collections.immutable.ImmutableMap
11+
import kotlinx.collections.immutable.PersistentMap
12+
import kotlinx.collections.immutable.persistentMapOf
13+
import kotlinx.collections.immutable.serialization.util.JsonConfigurationFactory
14+
import kotlinx.collections.immutable.serialization.util.JsonExt.encodeAndDecode
15+
import kotlinx.serialization.Serializable
16+
17+
class ImmutableMapSerializerTest {
18+
19+
@Serializable
20+
private class ImmutableMapHolder<K, V>(
21+
@Serializable(with = ImmutableMapSerializer::class)
22+
val immutableMap: ImmutableMap<K, V>
23+
)
24+
25+
@Test
26+
fun testImmutableList() {
27+
val json = JsonConfigurationFactory.createJsonConfiguration()
28+
persistentMapOf(1 to 1, 2 to 2, 3 to 3)
29+
.let(::ImmutableMapHolder)
30+
.let { expectedList -> assertEquals(expectedList.immutableMap, json.encodeAndDecode(expectedList).immutableMap) }
31+
}
32+
33+
@Serializable
34+
private class PersistentMapHolder<K, V>(
35+
@Serializable(with = PersistentMapSerializer::class)
36+
val persistentMap: PersistentMap<K, V>
37+
)
38+
39+
@Test
40+
fun testPersistentMap() {
41+
val json = JsonConfigurationFactory.createJsonConfiguration()
42+
persistentMapOf(1 to 1, 2 to 2, 3 to 3)
43+
.let(::PersistentMapHolder)
44+
.let { expectedList -> assertEquals(expectedList.persistentMap, json.encodeAndDecode(expectedList).persistentMap) }
45+
}
46+
47+
@Serializable
48+
private class PersistentHashMapHolder<K, V>(
49+
@Serializable(with = PersistentHashMapSerializer::class)
50+
val hashMap: PersistentMap<K, V>
51+
)
52+
53+
@Test
54+
fun testPersistentHashMap() {
55+
val json = JsonConfigurationFactory.createJsonConfiguration()
56+
persistentMapOf(1 to 1, 2 to 2, 3 to 3)
57+
.let(::PersistentHashMapHolder)
58+
.let { expectedList -> assertEquals(expectedList.hashMap, json.encodeAndDecode(expectedList).hashMap) }
59+
}
60+
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2016-2024 JetBrains s.r.o.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.collections.immutable.serialization.util
7+
8+
import kotlinx.serialization.json.Json
9+
10+
internal object JsonConfigurationFactory {
11+
fun createJsonConfiguration() = Json {
12+
isLenient = false
13+
prettyPrint = true
14+
ignoreUnknownKeys = false
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2016-2024 JetBrains s.r.o.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.collections.immutable.serialization.util
7+
8+
import kotlinx.serialization.encodeToString
9+
import kotlinx.serialization.json.Json
10+
11+
object JsonExt {
12+
inline fun <reified T> Json.encodeAndDecode(value: T): T {
13+
val string = encodeToString(value)
14+
return decodeFromString(string)
15+
}
16+
}

0 commit comments

Comments
 (0)