Parcelize: Add tests for deprecated (kotlinx.android.parcel) annotations
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
// IGNORE_BACKEND: JVM
|
||||
// See KT-38105
|
||||
// Throws IllegalAccessError, since the code tries to access the private companion field directly from the generated User$Creator class.
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
data class User(val name: String, val age: Int)
|
||||
|
||||
@Parcelize
|
||||
data class UserParcelable(val user: User) : Parcelable {
|
||||
private companion object : Parceler<UserParcelable> {
|
||||
override fun UserParcelable.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(user.name)
|
||||
}
|
||||
|
||||
override fun create(parcel: Parcel) = UserParcelable(User(parcel.readString(), 0))
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val userParcelable = UserParcelable(User("John", 20))
|
||||
userParcelable.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val userParcelable2 = readFromParcel<UserParcelable>(parcel)
|
||||
|
||||
assert(userParcelable.user.name == userParcelable2.user.name)
|
||||
assert(userParcelable2.user.age == 0)
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
object Parceler1 : Parceler<String> {
|
||||
override fun create(parcel: Parcel) = parcel.readInt().toString()
|
||||
|
||||
override fun String.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeInt(length)
|
||||
}
|
||||
}
|
||||
|
||||
typealias Parceler2 = Parceler1
|
||||
|
||||
object Parceler3 : Parceler<String> {
|
||||
override fun create(parcel: Parcel) = parcel.readString().toUpperCase()
|
||||
|
||||
override fun String.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(this)
|
||||
}
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
@TypeParceler<String, Parceler2>
|
||||
data class Test(
|
||||
val a: String,
|
||||
@TypeParceler<String, Parceler1> val b: String,
|
||||
@TypeParceler<String, Parceler3> val c: CharSequence,
|
||||
val d: @WriteWith<Parceler3> String
|
||||
) : Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = Test("Abc", "Abc", "Abc", "Abc")
|
||||
test.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val test2 = readFromParcel<Test>(parcel)
|
||||
|
||||
assert(test.a == "Abc" && test.b == "Abc" && test.c == "Abc" && test.d == "Abc")
|
||||
assert(test2.a == "3" && test2.b == "3" && test2.c == "Abc" && test2.d == "ABC")
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// IGNORE_BACKEND: JVM
|
||||
// See KT-38107
|
||||
// The JVM backend is missing support for custom parcelers in List<String>
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
object Parceler1 : Parceler<String> {
|
||||
override fun create(parcel: Parcel) = parcel.readInt().toString()
|
||||
|
||||
override fun String.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeInt(length)
|
||||
}
|
||||
}
|
||||
|
||||
object Parceler2 : Parceler<List<String>> {
|
||||
override fun create(parcel: Parcel) = listOf(parcel.readString())
|
||||
|
||||
override fun List<String>.write(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(this.joinToString(","))
|
||||
}
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
data class Test(
|
||||
val a: String,
|
||||
val b: @WriteWith<Parceler1> String,
|
||||
val c: List<@WriteWith<Parceler1> String>,
|
||||
val d: @WriteWith<Parceler2> List<String>,
|
||||
val e: @WriteWith<Parceler2> List<@WriteWith<Parceler1> String>
|
||||
) : Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = Test("Abc", "Abc", listOf("A", "bc"), listOf("A", "bc"), listOf("A", "bc"))
|
||||
test.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val test2 = readFromParcel<Test>(parcel)
|
||||
|
||||
with (test) {
|
||||
assert(a == "Abc" && b == "Abc" && c == listOf("A", "bc") && d == listOf("A", "bc") && e == listOf("A", "bc"))
|
||||
}
|
||||
|
||||
with (test2) {
|
||||
assert(a == "Abc" && b == "3" && c == listOf("1", "2") && d == listOf("A,bc") && e == listOf("A,bc"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import java.io.Serializable
|
||||
|
||||
class JHelp(var j1: String) {
|
||||
val j2 = 9
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
class J(val j: @RawValue JHelp) : Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val test = J(JHelp("A"))
|
||||
|
||||
var exceptionCaught = false
|
||||
try {
|
||||
test.writeToParcel(parcel, 0)
|
||||
} catch (e: RuntimeException) {
|
||||
if (e.message!!.contains("Parcel: unable to marshal value test.JHelp")) {
|
||||
exceptionCaught = true
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
if (!exceptionCaught) {
|
||||
error("Exception should be thrown")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.android.parcel.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
annotation class SerializableLike
|
||||
|
||||
@Parcelize @SerializableLike
|
||||
data class User(val firstName: String, val secondName: String, val age: Int) : Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val user = User("John", "Smith", 20)
|
||||
user.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val user2 = readFromParcel<User>(parcel)
|
||||
assert(user == user2)
|
||||
}
|
||||
Reference in New Issue
Block a user