Created
March 8, 2021 22:36
-
-
Save shekibobo/8ad3b19e5e4ced6ad2279e23edd18a0b to your computer and use it in GitHub Desktop.
Testing a Parcelable class implemented with @parcelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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