summaryrefslogtreecommitdiff
path: root/formats/json/commonTest/src/kotlinx/serialization/TuplesTest.kt
blob: 9fd2d5ea11a5cd45f7281931e2a8a42636a49398 (plain)
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
/*
 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization

import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.json.*
import kotlinx.serialization.test.assertStringFormAndRestored
import kotlin.test.*

class TuplesTest : JsonTestBase() {
    @Serializable
    data class MyPair<K, V>(val k: K, val v: V)

    @Serializable
    data class PairWrapper(val p: Pair<Int, String>)

    @Serializable
    data class TripleWrapper(val t: Triple<Int, String, Boolean>)

    @Test
    fun testCustomPair() = assertStringFormAndRestored(
        """{"k":42,"v":"foo"}""",
        MyPair(42, "foo"),
        MyPair.serializer(
            Int.serializer(),
            String.serializer()
        ),
        lenient
    )

    @Test
    fun testStandardPair() = assertStringFormAndRestored(
        """{"p":{"first":42,"second":"foo"}}""",
        PairWrapper(42 to "foo"),
        PairWrapper.serializer(),
        lenient
    )

    @Test
    fun testStandardPairHasCorrectDescriptor() {
        val desc = PairWrapper.serializer().descriptor.getElementDescriptor(0)
        assertEquals(desc.serialName, "kotlin.Pair")
        assertEquals(
            desc.elementDescriptors.map(SerialDescriptor::kind),
            listOf(PrimitiveKind.INT, PrimitiveKind.STRING)
        )
    }

    @Test
    fun testStandardTriple() = assertStringFormAndRestored(
        """{"t":{"first":42,"second":"foo","third":false}}""",
        TripleWrapper(Triple(42, "foo", false)),
        TripleWrapper.serializer(),
        lenient
    )

    @Test
    fun testStandardTripleHasCorrectDescriptor() {
        val desc = TripleWrapper.serializer().descriptor.getElementDescriptor(0)
        assertEquals(desc.serialName, "kotlin.Triple")
        assertEquals(
            desc.elementDescriptors.map(SerialDescriptor::kind),
            listOf(PrimitiveKind.INT, PrimitiveKind.STRING, PrimitiveKind.BOOLEAN)
        )
    }
}