• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.serialization
6 
7 import kotlinx.serialization.builtins.*
8 import kotlinx.serialization.descriptors.*
9 import kotlinx.serialization.json.*
10 import kotlinx.serialization.test.assertStringFormAndRestored
11 import kotlin.test.*
12 
13 class TuplesTest : JsonTestBase() {
14     @Serializable
15     data class MyPair<K, V>(val k: K, val v: V)
16 
17     @Serializable
18     data class PairWrapper(val p: Pair<Int, String>)
19 
20     @Serializable
21     data class TripleWrapper(val t: Triple<Int, String, Boolean>)
22 
23     @Test
testCustomPairnull24     fun testCustomPair() = assertStringFormAndRestored(
25         """{"k":42,"v":"foo"}""",
26         MyPair(42, "foo"),
27         MyPair.serializer(
28             Int.serializer(),
29             String.serializer()
30         ),
31         lenient
32     )
33 
34     @Test
35     fun testStandardPair() = assertStringFormAndRestored(
36         """{"p":{"first":42,"second":"foo"}}""",
37         PairWrapper(42 to "foo"),
38         PairWrapper.serializer(),
39         lenient
40     )
41 
42     @Test
43     fun testStandardPairHasCorrectDescriptor() {
44         val desc = PairWrapper.serializer().descriptor.getElementDescriptor(0)
45         assertEquals(desc.serialName, "kotlin.Pair")
46         assertEquals(
47             desc.elementDescriptors.map(SerialDescriptor::kind),
48             listOf(PrimitiveKind.INT, PrimitiveKind.STRING)
49         )
50     }
51 
52     @Test
testStandardTriplenull53     fun testStandardTriple() = assertStringFormAndRestored(
54         """{"t":{"first":42,"second":"foo","third":false}}""",
55         TripleWrapper(Triple(42, "foo", false)),
56         TripleWrapper.serializer(),
57         lenient
58     )
59 
60     @Test
61     fun testStandardTripleHasCorrectDescriptor() {
62         val desc = TripleWrapper.serializer().descriptor.getElementDescriptor(0)
63         assertEquals(desc.serialName, "kotlin.Triple")
64         assertEquals(
65             desc.elementDescriptors.map(SerialDescriptor::kind),
66             listOf(PrimitiveKind.INT, PrimitiveKind.STRING, PrimitiveKind.BOOLEAN)
67         )
68     }
69 }
70