Files
kotlin/plugins/android-extensions/android-extensions-compiler/testData/parcel/box/customSerializerBoxing.kt
Steven Schäfer 9b7a95b05c Parcelize: Fix test code
On Android, we would need to call `setDataPosition(0)` after
unmarshalling a parcel. The reason why the test code is currently
working is purely because the Robolectric test framework is more
permissive.
2020-05-27 02:39:29 +09:00

56 lines
1.4 KiB
Kotlin
Vendored

// WITH_RUNTIME
@file:JvmName("TestKt")
package test
import kotlinx.android.parcel.*
import android.os.Parcel
import android.os.Parcelable
import java.util.Arrays
object Parceler1 : Parceler<Int> {
override fun create(parcel: Parcel) = -parcel.readInt()
override fun Int.write(parcel: Parcel, flags: Int) {
parcel.writeInt(this)
}
}
object Parceler2 : Parceler<Long> {
override fun create(parcel: Parcel) = parcel.readString().length.toLong()
override fun Long.write(parcel: Parcel, flags: Int) {
parcel.writeString("Abc")
}
}
@Parcelize
data class Test(
val a: Int,
@TypeParceler<Int, Parceler1> val b: Int,
@TypeParceler<Long, Parceler2> val c: Long,
@TypeParceler<Int, Parceler1> val d: List<Int>,
@TypeParceler<Long, Parceler2> val e: LongArray
) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test(5, 5, 50L, listOf(1, 2, 3), longArrayOf(3, 2, 1))
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val test2 = readFromParcel<Test>(parcel)
println(test.toString())
println(test2.toString())
with (test) {
assert(a == 5 && b == 5 && c == 50L && d == listOf(1, 2, 3) && Arrays.equals(e, longArrayOf(3, 2, 1)))
}
with (test2) {
assert(a == 5 && b == -5 && c == 3L && d == listOf(-1, -2, -3) && Arrays.equals(e, longArrayOf(3, 3, 3)))
}
}