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+ }
0 commit comments