summaryrefslogtreecommitdiff
path: root/core/jvmTest/src/kotlinx/serialization/CachingTest.kt
blob: b146c92053d56ffb094d3b1586a55751534f56b1 (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
/*
 * 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.internal.*
import kotlinx.serialization.modules.*
import org.junit.Test
import kotlin.reflect.*
import kotlin.test.*

class CachingTest {
    @Test
    fun testCache() {
        var factoryCalled = 0

        val cache = createCache {
            factoryCalled += 1
            it.serializerOrNull()
        }

        repeat(10) {
            cache.get(typeOf<String>().kclass())
        }

        assertEquals(1, factoryCalled)
    }

    @Test
    fun testParameterizedCache() {
        var factoryCalled = 0

        val cache = createParametrizedCache { clazz, types ->
            factoryCalled += 1
            val serializers = EmptySerializersModule().serializersForParameters(types, true)!!
            clazz.parametrizedSerializerOrNull(serializers) { types[0].classifier }
        }

        repeat(10) {
            cache.get(typeOf<Map<*, *>>().kclass(), listOf(typeOf<String>(), typeOf<String>()))
        }

        assertEquals(1, factoryCalled)
    }
}