Skip to content

Commit 6886e34

Browse files
authored
Setup Kover for validation of each module (#2674)
Also, slightly increased coverage Resolves #2634
1 parent 2e5c66e commit 6886e34

File tree

6 files changed

+183
-15
lines changed

6 files changed

+183
-15
lines changed

build.gradle.kts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
id("org.jetbrains.dokka")
1313
id("benchmark-conventions")
1414
id("publishing-check-conventions")
15+
id("kover-conventions")
1516

1617
alias(libs.plugins.serialization) apply false
1718
}
@@ -136,12 +137,6 @@ subprojects {
136137
apply(plugin = "bom-conventions")
137138
}
138139

139-
// == Kover setup ==
140-
subprojects {
141-
if (project.name in uncoveredProjects) return@subprojects
142-
apply(plugin = "kover-conventions")
143-
}
144-
145140
// == Dokka setup ==
146141
subprojects {
147142
if (name in documentedSubprojects) {
@@ -200,5 +195,3 @@ val documentedSubprojects get() = setOf("kotlinx-serialization-core",
200195
"kotlinx-serialization-properties",
201196
"kotlinx-serialization-hocon",
202197
"kotlinx-serialization-protobuf")
203-
204-
val uncoveredProjects get() = setOf("kotlinx-serialization-bom", "benchmark", "guide", "kotlinx-serialization-json-okio", "kotlinx-serialization-json-io")

buildSrc/src/main/kotlin/kover-conventions.gradle.kts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,43 @@ kover {
1010
disable()
1111
}
1212

13+
currentProject {
14+
projectsForCoverageVerification.forEach { (variantName, _) ->
15+
// copy the `main` variant for each module to check the coverage only in its section
16+
copyVariant(variantName, "main")
17+
}
18+
}
19+
20+
merge {
21+
// collect common coverage for all projects (except excluded) in `main` variant
22+
subprojects { subproject ->
23+
subproject.path !in uncoveredProjects
24+
}
25+
createVariant("main") { add("jvm", optional = true) }
26+
}
27+
1328
reports {
14-
verify {
15-
rule("Minimal line coverage rate in percents") {
29+
total.verify.rule("Total coverage") {
30+
minBound(90)
31+
}
32+
33+
projectsForCoverageVerification.forEach { (variantName, projectPath) ->
34+
variant(variantName) {
35+
filters.includes.projects.add(projectPath)
1636

17-
// Core is mainly uncovered because a lot of serializers are tested with JSON
18-
val minPercentage = if (project.name.contains("core") || project.name.contains("properties") || project.name.contains("json-okio")) 44 else 80
19-
minBound(minPercentage)
20-
// valueType is 'COVERED_LINES_PERCENTAGE' by default
37+
// verify the coverage individually for each module by `check` task
38+
verify {
39+
onCheck = true
40+
rule("Coverage for $projectPath") {
41+
minBound(85)
42+
}
43+
}
2144
}
2245
}
2346
}
2447
}
48+
49+
50+
val uncoveredProjects get() = setOf(":kotlinx-serialization-bom", ":benchmark", ":guide")
51+
// map: variant name -> project path
52+
val projectsForCoverageVerification get() = mapOf("core" to ":kotlinx-serialization-core", "json" to ":kotlinx-serialization-json", "jsonOkio" to ":kotlinx-serialization-json-okio", "cbor" to ":kotlinx-serialization-cbor", "hocon" to ":kotlinx-serialization-hocon", "properties" to ":kotlinx-serialization-properties", "protobuf" to ":kotlinx-serialization-protobuf", "io" to ":kotlinx-serialization-json-io")
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.serialization.json.io
6+
7+
import kotlinx.io.*
8+
import kotlinx.serialization.Serializable
9+
import kotlinx.serialization.json.*
10+
import kotlinx.serialization.json.io.internal.*
11+
import kotlin.test.*
12+
13+
class IoTests {
14+
15+
@Serializable
16+
data class Simple(val i: Int)
17+
18+
@Test
19+
fun testSurrogate() {
20+
val text = "\uD83D\uDE03"
21+
val originalChars = text.toCharArray()
22+
23+
val buffer = Buffer()
24+
buffer.writeString(text)
25+
val reader = IoSerialReader(buffer)
26+
27+
val readArray = CharArray(2)
28+
assertEquals(1, reader.read(readArray, 0, 1) )
29+
assertEquals(1, reader.read(readArray, 1, 1) )
30+
31+
assertContentEquals(originalChars, readArray)
32+
}
33+
34+
35+
@Test
36+
fun testEncodingAndDecoding() {
37+
val json = "{\"i\":42}"
38+
val value = Simple(42)
39+
val buffer = Buffer()
40+
Json.encodeToSink(value, buffer)
41+
val encoded = buffer.readString()
42+
assertEquals(json, encoded)
43+
44+
buffer.writeString(encoded)
45+
val decoded = Json.decodeFromSource<Simple>(buffer)
46+
assertEquals(value, decoded)
47+
48+
assertTrue(buffer.exhausted())
49+
}
50+
51+
@Test
52+
fun testDecodeSequence() {
53+
val json = "{\"i\":1}{\"i\":2}"
54+
val value1 = Simple(1)
55+
val value2 = Simple(2)
56+
val buffer = Buffer()
57+
buffer.writeString(json)
58+
val decoded = Json.decodeSourceToSequence<Simple>(buffer).toList()
59+
60+
assertTrue(buffer.exhausted())
61+
assertEquals(2, decoded.size)
62+
assertEquals(listOf(value1, value2), decoded)
63+
64+
buffer.writeString(json)
65+
val decodedExplicit = Json.decodeSourceToSequence(buffer, Simple.serializer()).toList()
66+
assertTrue(buffer.exhausted())
67+
assertEquals(2, decodedExplicit.size)
68+
assertEquals(listOf(value1, value2), decodedExplicit)
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.serialization.json.okio.internal
6+
7+
import kotlinx.serialization.Serializable
8+
import kotlinx.serialization.json.*
9+
import kotlinx.serialization.json.okio.*
10+
import okio.*
11+
import kotlin.test.*
12+
13+
class OkioTests {
14+
15+
@Serializable
16+
data class Simple(val i: Int)
17+
18+
@Test
19+
fun testSurrogate() {
20+
val text = "\uD83D\uDE03"
21+
val originalChars = text.toCharArray()
22+
23+
val buffer = Buffer()
24+
buffer.writeUtf8(text)
25+
val reader = OkioSerialReader(buffer)
26+
27+
val readArray = CharArray(2)
28+
assertEquals(1, reader.read(readArray, 0, 1) )
29+
assertEquals(1, reader.read(readArray, 1, 1) )
30+
31+
assertContentEquals(originalChars, readArray)
32+
}
33+
34+
35+
@Test
36+
fun testEncodingAndDecoding() {
37+
val json = "{\"i\":42}"
38+
val value = Simple(42)
39+
val buffer = Buffer()
40+
Json.encodeToBufferedSink(value, buffer)
41+
val encoded = buffer.readUtf8()
42+
assertEquals(json, encoded)
43+
44+
buffer.writeUtf8(encoded)
45+
val decoded = Json.decodeFromBufferedSource<Simple>(buffer)
46+
assertEquals(value, decoded)
47+
48+
assertTrue(buffer.exhausted())
49+
}
50+
51+
@Test
52+
fun testDecodeSequence() {
53+
val json = "{\"i\":1}{\"i\":2}"
54+
val value1 = Simple(1)
55+
val value2 = Simple(2)
56+
val buffer = Buffer()
57+
buffer.writeUtf8(json)
58+
val decoded = Json.decodeBufferedSourceToSequence<Simple>(buffer).toList()
59+
60+
assertTrue(buffer.exhausted())
61+
assertEquals(2, decoded.size)
62+
assertEquals(listOf(value1, value2), decoded)
63+
64+
buffer.writeUtf8(json)
65+
val decodedExplicit = Json.decodeBufferedSourceToSequence(buffer, Simple.serializer()).toList()
66+
assertTrue(buffer.exhausted())
67+
assertEquals(2, decodedExplicit.size)
68+
assertEquals(listOf(value1, value2), decodedExplicit)
69+
}
70+
}

formats/properties/commonTest/src/kotlinx/serialization/properties/PropertiesTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kotlinx.serialization.properties
77

88
import kotlinx.serialization.*
99
import kotlinx.serialization.builtins.*
10+
import kotlinx.serialization.modules.*
1011
import kotlin.test.*
1112

1213
class PropertiesTest {
@@ -107,6 +108,12 @@ class PropertiesTest {
107108
assertEquals(emptyMap(), Properties.encodeToMap(Unit.serializer(), Unit))
108109
}
109110

111+
@Test
112+
fun testUnitIsEmptyMapModule() {
113+
val module = SerializersModule {}
114+
assertEquals(emptyMap(), Properties(module).encodeToMap(Unit.serializer(), Unit))
115+
}
116+
110117
@Test
111118
fun testList() {
112119
val data = Data(listOf("element1"), "property")

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
kotlin = "2.0.0"
3-
kover = "0.8.0-Beta2"
3+
kover = "0.8.2"
44
dokka = "1.9.20"
55
knit = "0.5.0"
66
bcv = "0.15.0-Beta.2"

0 commit comments

Comments
 (0)