mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Inline mask generation and constructor invocation (#908)
* Differentiate local naming from properties with constructor defaults * Remove constructor invocation and mask creation methods We're inlining all this * Add explanatory comment for why we use the default primitive value * Inline mask generation and constructor invocation to generated adapters * Remove unused argument Co-Authored-By: Jake Wharton <jakew@google.com> * Compute masks directly during code gen * Opportunistic: remove extraneous double space from control flow * Just mask and invert directly
This commit is contained in:
@@ -2,45 +2,11 @@ package com.squareup.moshi.kotlin
|
||||
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.internal.Util
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultConstructorTest {
|
||||
|
||||
@Test fun minimal() {
|
||||
val expected = TestClass("requiredClass")
|
||||
val args = arrayOf("requiredClass", null, 0, null, 0, 0)
|
||||
val mask = Util.createDefaultValuesParametersMask(true, false, false, false, false, false)
|
||||
val constructor = Util.lookupDefaultsConstructor(TestClass::class.java)
|
||||
val instance = Util.invokeDefaultConstructor(TestClass::class.java, constructor, mask, *args)
|
||||
check(instance == expected) {
|
||||
"No match:\nActual : $instance\nExpected: $expected"
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun allSet() {
|
||||
val expected = TestClass("requiredClass", "customOptional", 4, "setDynamic", 5, 6)
|
||||
val args = arrayOf("requiredClass", "customOptional", 4, "setDynamic", 5, 6)
|
||||
val mask = Util.createDefaultValuesParametersMask(true, true, true, true, true, true)
|
||||
val constructor = Util.lookupDefaultsConstructor(TestClass::class.java)
|
||||
val instance = Util.invokeDefaultConstructor(TestClass::class.java, constructor, mask, *args)
|
||||
check(instance == expected) {
|
||||
"No match:\nActual : $instance\nExpected: $expected"
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun customDynamic() {
|
||||
val expected = TestClass("requiredClass", "customOptional")
|
||||
val args = arrayOf("requiredClass", "customOptional", 0, null, 0, 0)
|
||||
val mask = Util.createDefaultValuesParametersMask(true, true, false, false, false, false)
|
||||
val constructor = Util.lookupDefaultsConstructor(TestClass::class.java)
|
||||
val instance = Util.invokeDefaultConstructor(TestClass::class.java, constructor, mask, *args)
|
||||
check(instance == expected) {
|
||||
"No match:\nActual : $instance\nExpected: $expected"
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun minimal_codeGen() {
|
||||
val expected = TestClass("requiredClass")
|
||||
val json = """{"required":"requiredClass"}"""
|
||||
val instance = Moshi.Builder().build().adapter<TestClass>(TestClass::class.java)
|
||||
@@ -50,7 +16,7 @@ class DefaultConstructorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun allSet_codeGen() {
|
||||
@Test fun allSet() {
|
||||
val expected = TestClass("requiredClass", "customOptional", 4, "setDynamic", 5, 6)
|
||||
val json = """{"required":"requiredClass","optional":"customOptional","optional2":4,"dynamicSelfReferenceOptional":"setDynamic","dynamicOptional":5,"dynamicInlineOptional":6}"""
|
||||
val instance = Moshi.Builder().build().adapter<TestClass>(TestClass::class.java)
|
||||
@@ -60,7 +26,7 @@ class DefaultConstructorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun customDynamic_codeGen() {
|
||||
@Test fun customDynamic() {
|
||||
val expected = TestClass("requiredClass", "customOptional")
|
||||
val json = """{"required":"requiredClass","optional":"customOptional"}"""
|
||||
val instance = Moshi.Builder().build().adapter<TestClass>(TestClass::class.java)
|
||||
|
Reference in New Issue
Block a user