Skip to content

Instantly share code, notes, and snippets.

@shekibobo
Created March 8, 2021 22:36
Show Gist options
  • Save shekibobo/8ad3b19e5e4ced6ad2279e23edd18a0b to your computer and use it in GitHub Desktop.
Save shekibobo/8ad3b19e5e4ced6ad2279e23edd18a0b to your computer and use it in GitHub Desktop.
Testing a Parcelable class implemented with @parcelize
inline fun <reified T> T.forceParcel(): T? where T : Parcelable {
val bytes = Parcel.obtain().use {
writeParcelable(this@forceParcel, 0)
marshall()
}
return Parcel.obtain().use {
unmarshall(bytes, 0, bytes.size)
setDataPosition(0)
readParcelable(T::class.java.classLoader)
}
}
inline fun <reified T> Parcel.use(action: Parcel.() -> T): T {
return try {
action()
} finally {
recycle()
}
}
@Test
fun testParcelableImplementation() {
val thing = Thing()
assertThat(thing.forceParcel()).isEqualTo(thing)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment