[Gradle] Lift String, Int and Boolean serializers from testFixtures to main source set

KT-55189
This commit is contained in:
Sebastian Sellmair
2022-11-30 10:50:03 +01:00
committed by Space Team
parent c30b554dc4
commit 8cee0496f3
8 changed files with 134 additions and 23 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.serialize
object IdeaKotlinBooleanExtrasSerializer : IdeaKotlinExtrasSerializer<Boolean> {
override fun serialize(context: IdeaKotlinSerializationContext, value: Boolean): ByteArray {
return byteArrayOf(if (value) 1 else 0)
}
override fun deserialize(context: IdeaKotlinSerializationContext, data: ByteArray): Boolean? {
if (data.isEmpty()) {
context.logger.error("Failed to decode Boolean from empty array")
return null
}
return when (val value = data.first()) {
0.toByte() -> false
1.toByte() -> true
else -> {
context.logger.error("Failed to decode Boolean from value $value")
null
}
}
}
}
@@ -3,13 +3,11 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.testFixtures.serialize
package org.jetbrains.kotlin.gradle.idea.serialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinExtrasSerializer
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinSerializationContext
import java.nio.ByteBuffer
object TestIdeaIntKotlinExtrasSerializer : IdeaKotlinExtrasSerializer<Int> {
object IdeaKotlinIntExtrasSerializer : IdeaKotlinExtrasSerializer<Int> {
override fun serialize(context: IdeaKotlinSerializationContext, value: Int): ByteArray {
return ByteBuffer.allocate(Int.SIZE_BYTES).putInt(value).array()
}
@@ -17,4 +15,4 @@ object TestIdeaIntKotlinExtrasSerializer : IdeaKotlinExtrasSerializer<Int> {
override fun deserialize(context: IdeaKotlinSerializationContext, data: ByteArray): Int {
return ByteBuffer.wrap(data).int
}
}
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.serialize
object IdeaKotlinStringExtrasSerializer : IdeaKotlinExtrasSerializer<String> {
override fun serialize(context: IdeaKotlinSerializationContext, value: String): ByteArray {
return value.encodeToByteArray()
}
override fun deserialize(context: IdeaKotlinSerializationContext, data: ByteArray): String {
return data.decodeToString()
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.serialize
import org.jetbrains.kotlin.gradle.idea.testFixtures.serialize.TestIdeaKotlinSerializationContext
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.fail
class IdeaKotlinBooleanExtrasSerializerTest {
@Test
fun `test - true`() {
val context = TestIdeaKotlinSerializationContext()
val data = IdeaKotlinBooleanExtrasSerializer.serialize(context, true)
assertEquals(true, IdeaKotlinBooleanExtrasSerializer.deserialize(context, data))
if (context.logger.reports.isNotEmpty()) fail("Unexpected reports: ${context.logger.reports}")
}
@Test
fun `test - false`() {
val context = TestIdeaKotlinSerializationContext()
val data = IdeaKotlinBooleanExtrasSerializer.serialize(context, false)
assertEquals(false, IdeaKotlinBooleanExtrasSerializer.deserialize(context, data))
if (context.logger.reports.isNotEmpty()) fail("Unexpected reports: ${context.logger.reports}")
}
@Test
fun `test - bad data`() {
val context = TestIdeaKotlinSerializationContext()
assertNull(IdeaKotlinBooleanExtrasSerializer.deserialize(context, byteArrayOf()))
if (context.logger.reports.size != 1) fail("Expected one report. Found: ${context.logger.reports}")
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.serialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinIntExtrasSerializer.deserialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinIntExtrasSerializer.serialize
import org.jetbrains.kotlin.gradle.idea.testFixtures.serialize.TestIdeaKotlinSerializationContext
import kotlin.test.Test
import kotlin.test.assertEquals
class IdeaKotlinIntExtrasSerializerTest {
@Test
fun `test - values`() {
val context = TestIdeaKotlinSerializationContext()
assertEquals(2411, deserialize(context, serialize(context, 2411)))
assertEquals(-2411, deserialize(context, serialize(context, -2411)))
assertEquals(Int.MAX_VALUE, deserialize(context, serialize(context, Int.MAX_VALUE)))
assertEquals(Int.MIN_VALUE, deserialize(context, serialize(context, Int.MIN_VALUE)))
}
}
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.serialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinStringExtrasSerializer.deserialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinStringExtrasSerializer.serialize
import org.jetbrains.kotlin.gradle.idea.testFixtures.serialize.TestIdeaKotlinSerializationContext
import kotlin.test.Test
import kotlin.test.assertEquals
class IdeaKotlinStringExtrasSerializerTest {
@Test
fun `test - values`() {
val context = TestIdeaKotlinSerializationContext()
assertEquals("Sunny", deserialize(context, serialize(context, "Sunny")))
assertEquals("☀️", deserialize(context, serialize(context, "☀️")))
}
}
@@ -5,8 +5,7 @@
package org.jetbrains.kotlin.gradle.idea.testFixtures.serialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinExtrasSerializationExtension
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinExtrasSerializer
import org.jetbrains.kotlin.gradle.idea.serialize.*
import org.jetbrains.kotlin.tooling.core.Extras
import org.jetbrains.kotlin.tooling.core.extrasKeyOf
import org.jetbrains.kotlin.tooling.core.extrasTypeOf
@@ -20,8 +19,9 @@ object TestIdeaExtrasSerializationExtension : IdeaKotlinExtrasSerializationExten
override fun <T : Any> serializer(key: Extras.Key<T>): IdeaKotlinExtrasSerializer<T>? = when {
key == ignoredStringKey -> null
key == anySerializableKey -> IdeaKotlinExtrasSerializer.javaIoSerializable<Any>()
key.type == extrasTypeOf<String>() -> TestIdeaStringKotlinExtrasSerializer
key.type == extrasTypeOf<Int>() -> TestIdeaIntKotlinExtrasSerializer
key.type == extrasTypeOf<String>() -> IdeaKotlinStringExtrasSerializer
key.type == extrasTypeOf<Int>() -> IdeaKotlinIntExtrasSerializer
key.type == extrasTypeOf<Boolean>() -> IdeaKotlinBooleanExtrasSerializer
else -> null
} as? IdeaKotlinExtrasSerializer<T>
}
@@ -1,14 +0,0 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.idea.testFixtures.serialize
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinExtrasSerializer
import org.jetbrains.kotlin.gradle.idea.serialize.IdeaKotlinSerializationContext
object TestIdeaStringKotlinExtrasSerializer : IdeaKotlinExtrasSerializer<String> {
override fun serialize(context: IdeaKotlinSerializationContext, value: String): ByteArray = value.encodeToByteArray()
override fun deserialize(context: IdeaKotlinSerializationContext, data: ByteArray) = data.decodeToString()
}