mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
* added test for transient delegate in kotlin code gen (#922) * cleanup spaces Co-Authored-By: Zac Sweers <pandanomic@gmail.com>
This commit is contained in:
committed by
Zac Sweers
parent
7804d74318
commit
f51de08caf
@@ -35,6 +35,7 @@ import org.junit.Assert.fail
|
||||
import org.junit.Ignore
|
||||
import org.junit.Test
|
||||
import java.util.Locale
|
||||
import kotlin.properties.Delegates
|
||||
import kotlin.reflect.full.memberProperties
|
||||
|
||||
@ExperimentalStdlibApi
|
||||
@@ -563,6 +564,38 @@ class GeneratedAdaptersTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun transientDelegateProperty() {
|
||||
val jsonAdapter = moshi.adapter<TransientDelegateProperty>()
|
||||
|
||||
val encoded = TransientDelegateProperty()
|
||||
encoded.a = 3
|
||||
encoded.setB(4)
|
||||
encoded.c = 5
|
||||
assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"c":5}""")
|
||||
|
||||
val decoded = jsonAdapter.fromJson("""{"a":4,"b":5,"c":6}""")!!
|
||||
assertThat(decoded.a).isEqualTo(-1)
|
||||
assertThat(decoded.getB()).isEqualTo(-1)
|
||||
assertThat(decoded.c).isEqualTo(6)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class TransientDelegateProperty {
|
||||
|
||||
private fun <T>delegate(initial: T) = Delegates.observable(initial) { _, _, _-> }
|
||||
|
||||
@delegate:Transient var a: Int by delegate(-1)
|
||||
@delegate:Transient private var b: Int by delegate(-1)
|
||||
var c: Int by delegate(-1)
|
||||
|
||||
@JvmName("getBPublic")
|
||||
fun getB() = b
|
||||
@JvmName("setBPublic")
|
||||
fun setB(b: Int) {
|
||||
this.b = b
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun manyProperties32() {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val jsonAdapter = moshi.adapter<ManyProperties32>()
|
||||
|
Reference in New Issue
Block a user