summaryrefslogtreecommitdiff
path: root/core/commonTest/src/kotlinx/serialization/test/CompilerVersions.kt
blob: 7bd35c17bd08749cac2e575906b29484e5d0a783 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.test

import kotlin.test.*

private val currentKotlinVersion = KotlinVersion.CURRENT

private fun String.toKotlinVersion(): KotlinVersion {
    val parts = split(".")
    val intParts = parts.mapNotNull { it.toIntOrNull() }
    if (parts.size != 3 || intParts.size != 3) error("Illegal kotlin version, expected format is 1.2.3")

    return KotlinVersion(intParts[0], intParts[1], intParts[2])
}

internal fun runSince(kotlinVersion: String, test: () -> Unit) {
    if (currentKotlinVersion >= kotlinVersion.toKotlinVersion()) {
        test()
    }
}


internal inline fun <reified T : Throwable> shouldFail(
    sinceKotlin: String? = null,
    beforeKotlin: String? = null,
    onJvm: Boolean = true,
    onJs: Boolean = true,
    onNative: Boolean = true,
    onWasm: Boolean = true,
    test: () -> Unit
) {
    val args = mapOf(
        "since" to sinceKotlin,
        "before" to beforeKotlin,
        "onJvm" to onJvm,
        "onJs" to onJs,
        "onNative" to onNative,
        "onWasm" to onWasm
    )

    val sinceVersion = sinceKotlin?.toKotlinVersion()
    val beforeVersion = beforeKotlin?.toKotlinVersion()

    val version = (sinceVersion != null && currentKotlinVersion >= sinceVersion)
        || (beforeVersion != null && currentKotlinVersion < beforeVersion)

    val platform = (isJvm() && onJvm) || (isJs() && onJs) || (isNative() && onNative) || (isWasm() && onWasm)

    var error: Throwable? = null
    try {
        test()
    } catch (e: Throwable) {
        error = e
    }

    if (version && platform) {
        if (error == null) {
            throw AssertionError("Exception with type '${T::class.simpleName}' expected for $args")
        }
        if (error !is T) throw AssertionError(
            "Illegal exception type, expected '${T::class.simpleName}' actual '${error::class.simpleName}' for $args",
            error
        )
    } else {
        if (error != null) throw AssertionError(
            "Unexpected error for $args",
            error
        )
    }
}

internal class CompilerVersionTest {
    @Test
    fun testSince() {
        var executed = false

        runSince("1.0.0") {
            executed = true
        }
        assertTrue(executed)

        executed = false
        runSince("255.255.255") {
            executed = true
        }
        assertFalse(executed)
    }

    @Test
    fun testFailBefore() {
        // ok if there is no exception if current version greater is before of the specified
        shouldFail<IllegalArgumentException>(beforeKotlin = "0.0.0") {
            // no-op
        }

        // error if there is no exception and if current version is before of the specified
        assertFails {
            shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255") {
                // no-op
            }
        }

        // ok if thrown expected exception if current version is before of the specified
        shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255") {
            throw IllegalArgumentException()
        }

        // ok if thrown unexpected exception if current version is before of the specified
        assertFails {
            shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255") {
                throw Exception()
            }
        }

    }

    @Test
    fun testFailSince() {
        // ok if there is no exception if current version less then specified
        shouldFail<IllegalArgumentException>(sinceKotlin = "255.255.255") {
            // no-op
        }

        // error if there is no exception and if current version is greater or equals specified
        assertFails {
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0") {
                // no-op
            }
        }

        // ok if thrown expected exception if current version is greater or equals specified
        shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0") {
            throw IllegalArgumentException()
        }

        // ok if thrown unexpected exception if current version is greater or equals specified
        assertFails {
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0") {
                throw Exception()
            }
        }
    }

    @Test
    fun testExcludePlatform() {
        if (isJvm()) {
            shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255", onJvm = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", onJvm = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", beforeKotlin = "255.255.255", onJvm = false) {
                // no-op
            }
        } else if (isJs()) {
            shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255", onJs = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", onJs = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", beforeKotlin = "255.255.255", onJs = false) {
                // no-op
            }
        } else if (isWasm()) {
            shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255", onWasm = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", onWasm = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", beforeKotlin = "255.255.255", onWasm = false) {
                // no-op
            }
        } else if (isNative()) {
            shouldFail<IllegalArgumentException>(beforeKotlin = "255.255.255", onNative = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", onNative = false) {
                // no-op
            }
            shouldFail<IllegalArgumentException>(sinceKotlin = "0.0.0", beforeKotlin = "255.255.255", onNative = false) {
                // no-op
            }
        }
    }

}