From 6532108f1b581eb3acfcebd470f4d37abdb3987e Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 11:34:55 +0800 Subject: [PATCH 01/11] Add PublishedApi to BaseRetrofitHelper --- .../io/goooler/demoapp/base/network/BaseRetrofitHelper.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/base/src/main/kotlin/io/goooler/demoapp/base/network/BaseRetrofitHelper.kt b/base/src/main/kotlin/io/goooler/demoapp/base/network/BaseRetrofitHelper.kt index f76f5b48a..5908d625c 100644 --- a/base/src/main/kotlin/io/goooler/demoapp/base/network/BaseRetrofitHelper.kt +++ b/base/src/main/kotlin/io/goooler/demoapp/base/network/BaseRetrofitHelper.kt @@ -10,10 +10,10 @@ import okhttp3.OkHttpClient import retrofit2.Converter import retrofit2.Retrofit -@Suppress("unused") abstract class BaseRetrofitHelper { - private val retrofit by lazy { + @PublishedApi + internal val retrofit by lazy { Retrofit.Builder() .baseUrl(baseUrl) .client(buildOkHttpClient()) @@ -32,9 +32,7 @@ abstract class BaseRetrofitHelper { } } - fun create(service: Class): T = retrofit.create(service) - - inline fun create(): T = create(T::class.java) + inline fun create(): T = retrofit.create(T::class.java) protected abstract fun OkHttpClient.Builder.addInterceptors(): OkHttpClient.Builder From 850312765e72c5d6a7647c58ab2698e870145d0e Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 11:38:33 +0800 Subject: [PATCH 02/11] Add JsonUtil to JsonUtil --- .../goooler/demoapp/common/util/JsonUtil.kt | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt index e57701f9e..1d6e59f5a 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt @@ -4,46 +4,39 @@ package io.goooler.demoapp.common.util import com.squareup.moshi.Moshi import com.squareup.moshi.Types -import java.lang.reflect.Type object JsonUtil { + @PublishedApi internal val moshi: Moshi = Moshi.Builder().build() - fun fromJson(json: String, clazz: Class): T? = try { - moshi.adapter(clazz).fromJson(json) + inline fun fromJson(json: String): T? = try { + moshi.adapter(T::class.java).fromJson(json) } catch (e: Exception) { e.printStackTrace() null } - fun fromJson(json: String, typeOfT: Type): T? = try { - moshi.adapter(typeOfT).fromJson(json) + inline fun fromJson( + json: String, + rawType: Class<*>, + vararg typeArguments: Class<*> + ): T? = try { + moshi.adapter(Types.newParameterizedType(rawType, *typeArguments)).fromJson(json) } catch (e: Exception) { e.printStackTrace() null } - fun fromJson(json: String, rawType: Class<*>, vararg typeArguments: Class<*>): T? = try { - fromJson(json, Types.newParameterizedType(rawType, *typeArguments)) + inline fun toJson(o: T?): String? = try { + moshi.adapter(T::class.java).toJson(o) } catch (e: Exception) { e.printStackTrace() null } - - fun toJson(o: T?, clazz: Class): String? = try { - moshi.adapter(clazz).toJson(o) - } catch (e: Exception) { - e.printStackTrace() - null - } - - inline fun toJson(o: T?): String? = toJson(o, T::class.java) } inline fun String.fromJson(): T? = JsonUtil.fromJson(this, T::class.java) -inline fun String.fromJson(typeOfT: Type): T? = JsonUtil.fromJson(this, typeOfT) - inline fun String.fromJson(rawType: Class<*>, vararg typeArguments: Class<*>): T? = JsonUtil.fromJson(this, rawType, *typeArguments) From b093e5740e60d65fad327f369a94f1fad9dba38d Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 13:32:07 +0800 Subject: [PATCH 03/11] Add JsonUtilTest --- .../io/goooler/demoapp/common/JsonUtilTest.kt | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt new file mode 100644 index 000000000..ec6637ef1 --- /dev/null +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -0,0 +1,58 @@ +package io.goooler.demoapp.common + +import com.squareup.moshi.JsonClass +import io.goooler.demoapp.common.util.JsonUtil +import org.junit.Test + +class JsonUtilTest { + @Test + fun `jsonUtil's fromJson(String)`() { + assert(JsonUtil.fromJson(firstStr) == firstBean) + assert(JsonUtil.fromJson(secondStr) == secondBean) + } + + @JsonClass(generateAdapter = true) + internal data class Repo(val id: Long, val name: String, val owner: Owner) { + @JsonClass(generateAdapter = true) + data class Owner(val login: String) + + override fun equals(other: Any?): Boolean = if (other is Repo) { + this.id == other.id && this.name == other.name && this.owner.login == other.owner.login + } else { + false + } + + override fun hashCode(): Int { + return id.hashCode() + } + } + + companion object { + private val firstStr = """ + { + "id": 126987864, + "name": "1024_hosts", + "owner": { + "login": "Goooler" + } + } + """.trimIndent() + + private val secondStr = """ + { + "id": 374913489, + "name": "AndroidUiDemo", + "owner": { + "login": "Goooler" + } + } + """.trimIndent() + + // https://api.github.com/users/goooler/repos?&page=1&per_page=2 + private val strArray = "[$firstStr,$secondStr]" + + private val firstBean = Repo(126987864, "1024_hosts", Repo.Owner("Goooler")) + private val secondBean = Repo(374913489, "AndroidUiDemo", Repo.Owner("Goooler")) + private val beanArray = arrayOf(firstBean, secondBean) + } +} From f9985702e4b1badf1c3ddaa0c47cc49d797d8b16 Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 13:43:32 +0800 Subject: [PATCH 04/11] Add test cases --- .../goooler/demoapp/common/util/JsonUtil.kt | 14 +++---- .../io/goooler/demoapp/common/JsonUtilTest.kt | 41 +++++++++++++++++-- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt index 1d6e59f5a..4b389ab18 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt @@ -9,26 +9,26 @@ object JsonUtil { @PublishedApi internal val moshi: Moshi = Moshi.Builder().build() - inline fun fromJson(json: String): T? = try { - moshi.adapter(T::class.java).fromJson(json) + inline fun fromJson(string: String): T? = try { + moshi.adapter(T::class.java).fromJson(string) } catch (e: Exception) { e.printStackTrace() null } - inline fun fromJson( - json: String, + fun fromJson( + string: String, rawType: Class<*>, vararg typeArguments: Class<*> ): T? = try { - moshi.adapter(Types.newParameterizedType(rawType, *typeArguments)).fromJson(json) + moshi.adapter(Types.newParameterizedType(rawType, *typeArguments)).fromJson(string) } catch (e: Exception) { e.printStackTrace() null } - inline fun toJson(o: T?): String? = try { - moshi.adapter(T::class.java).toJson(o) + inline fun toJson(value: T?): String? = try { + moshi.adapter(T::class.java).toJson(value) } catch (e: Exception) { e.printStackTrace() null diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt index ec6637ef1..b7ba7b014 100644 --- a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -2,6 +2,8 @@ package io.goooler.demoapp.common import com.squareup.moshi.JsonClass import io.goooler.demoapp.common.util.JsonUtil +import io.goooler.demoapp.common.util.fromJson +import io.goooler.demoapp.common.util.toJson import org.junit.Test class JsonUtilTest { @@ -11,6 +13,40 @@ class JsonUtilTest { assert(JsonUtil.fromJson(secondStr) == secondBean) } + @Test + fun `jsonUtil's fromJson(String, Class, Class)`() { + val array: Array = JsonUtil.fromJson(strArray, Array::class.java, Repo::class.java) + ?: throw Exception("Parse json error") + assert(array.first() == firstBean) + assert(array[1] == secondBean) + } + + @Test + fun `jsonUtil's toJson(T)`() { + assert(JsonUtil.toJson(firstBean) == firstStr) + assert(JsonUtil.toJson(secondBean) == secondStr) + } + + @Test + fun `string's fromJson()`() { + assert(firstStr.fromJson() == firstBean) + assert(secondStr.fromJson() == secondBean) + } + + @Test + fun `string's fromJson(Class, Class)`() { + val array: Array = strArray.fromJson(Array::class.java, Repo::class.java) + ?: throw Exception("Parse json error") + assert(array.first() == firstBean) + assert(array[1] == secondBean) + } + + @Test + fun `any's toJson(T)`() { + assert(firstBean.toJson() == firstStr) + assert(secondBean.toJson() == secondStr) + } + @JsonClass(generateAdapter = true) internal data class Repo(val id: Long, val name: String, val owner: Owner) { @JsonClass(generateAdapter = true) @@ -22,9 +58,7 @@ class JsonUtilTest { false } - override fun hashCode(): Int { - return id.hashCode() - } + override fun hashCode(): Int = id.hashCode() } companion object { @@ -53,6 +87,5 @@ class JsonUtilTest { private val firstBean = Repo(126987864, "1024_hosts", Repo.Owner("Goooler")) private val secondBean = Repo(374913489, "AndroidUiDemo", Repo.Owner("Goooler")) - private val beanArray = arrayOf(firstBean, secondBean) } } From 7e63dbf63fb3c11423a2b7dd2a680d29efb6c6c9 Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 14:08:45 +0800 Subject: [PATCH 05/11] Rename --- .../kotlin/io/goooler/demoapp/common/JsonUtilTest.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt index b7ba7b014..3fb01bfd6 100644 --- a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -8,13 +8,13 @@ import org.junit.Test class JsonUtilTest { @Test - fun `jsonUtil's fromJson(String)`() { + fun `JsonUtil fromJson(String)`() { assert(JsonUtil.fromJson(firstStr) == firstBean) assert(JsonUtil.fromJson(secondStr) == secondBean) } @Test - fun `jsonUtil's fromJson(String, Class, Class)`() { + fun `JsonUtil fromJson(String, Class, Class)`() { val array: Array = JsonUtil.fromJson(strArray, Array::class.java, Repo::class.java) ?: throw Exception("Parse json error") assert(array.first() == firstBean) @@ -22,19 +22,19 @@ class JsonUtilTest { } @Test - fun `jsonUtil's toJson(T)`() { + fun `JsonUtil toJson(T)`() { assert(JsonUtil.toJson(firstBean) == firstStr) assert(JsonUtil.toJson(secondBean) == secondStr) } @Test - fun `string's fromJson()`() { + fun `String fromJson()`() { assert(firstStr.fromJson() == firstBean) assert(secondStr.fromJson() == secondBean) } @Test - fun `string's fromJson(Class, Class)`() { + fun `String fromJson(Class, Class)`() { val array: Array = strArray.fromJson(Array::class.java, Repo::class.java) ?: throw Exception("Parse json error") assert(array.first() == firstBean) @@ -42,7 +42,7 @@ class JsonUtilTest { } @Test - fun `any's toJson(T)`() { + fun `Any? toJson(T)`() { assert(firstBean.toJson() == firstStr) assert(secondBean.toJson() == secondStr) } From 4d6b3b1f6a83fe7de5946c09cdb770ab63f35cfa Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 14:10:25 +0800 Subject: [PATCH 06/11] Replace assert with Assert.assertTrue --- .../io/goooler/demoapp/common/JsonUtilTest.kt | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt index 3fb01bfd6..fe02d1bdc 100644 --- a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -4,47 +4,48 @@ import com.squareup.moshi.JsonClass import io.goooler.demoapp.common.util.JsonUtil import io.goooler.demoapp.common.util.fromJson import io.goooler.demoapp.common.util.toJson +import org.junit.Assert.assertTrue import org.junit.Test class JsonUtilTest { @Test fun `JsonUtil fromJson(String)`() { - assert(JsonUtil.fromJson(firstStr) == firstBean) - assert(JsonUtil.fromJson(secondStr) == secondBean) + assertTrue(JsonUtil.fromJson(firstStr) == firstBean) + assertTrue(JsonUtil.fromJson(secondStr) == secondBean) } @Test fun `JsonUtil fromJson(String, Class, Class)`() { val array: Array = JsonUtil.fromJson(strArray, Array::class.java, Repo::class.java) ?: throw Exception("Parse json error") - assert(array.first() == firstBean) - assert(array[1] == secondBean) + assertTrue(array.first() == firstBean) + assertTrue(array[1] == secondBean) } @Test fun `JsonUtil toJson(T)`() { - assert(JsonUtil.toJson(firstBean) == firstStr) - assert(JsonUtil.toJson(secondBean) == secondStr) + assertTrue(JsonUtil.toJson(firstBean) == firstStr) + assertTrue(JsonUtil.toJson(secondBean) == secondStr) } @Test fun `String fromJson()`() { - assert(firstStr.fromJson() == firstBean) - assert(secondStr.fromJson() == secondBean) + assertTrue(firstStr.fromJson() == firstBean) + assertTrue(secondStr.fromJson() == secondBean) } @Test fun `String fromJson(Class, Class)`() { val array: Array = strArray.fromJson(Array::class.java, Repo::class.java) ?: throw Exception("Parse json error") - assert(array.first() == firstBean) - assert(array[1] == secondBean) + assertTrue(array.first() == firstBean) + assertTrue(array[1] == secondBean) } @Test fun `Any? toJson(T)`() { - assert(firstBean.toJson() == firstStr) - assert(secondBean.toJson() == secondStr) + assertTrue(firstBean.toJson() == firstStr) + assertTrue(secondBean.toJson() == secondStr) } @JsonClass(generateAdapter = true) From 95b2fbf04bbe8a6fa841cc25735e5cefadbc307d Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 14:25:42 +0800 Subject: [PATCH 07/11] Update unit-tests job --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd61adbf6..36e201c83 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,7 +63,7 @@ jobs: ~/.gradle/wrapper key: ${{ runner.os }}-gradle-${{ hashFiles('**/**.gradle', '**/**.gradle.kts', '**/gradle/wrapper/gradle-wrapper.properties', '**/buildSrc/src/main/kotlin/**.kt') }} - name: Unit tests - run: ./gradlew testDebugUnitTest + run: ./gradlew test instrumentation-tests: name: Instrumentation tests From 932972b7e9ef30c9762372bf401aace37457022b Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 21:11:46 +0800 Subject: [PATCH 08/11] kaptTest moshiCompiler --- buildSrc/src/main/kotlin/Extensions.kt | 3 +++ common/build.gradle.kts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/buildSrc/src/main/kotlin/Extensions.kt b/buildSrc/src/main/kotlin/Extensions.kt index 52827afd3..a7dbe6e52 100644 --- a/buildSrc/src/main/kotlin/Extensions.kt +++ b/buildSrc/src/main/kotlin/Extensions.kt @@ -57,6 +57,9 @@ fun DependencyHandler.androidTestImplementations(vararg names: Any): Array = config("testImplementation", *names) +fun DependencyHandler.kaptTests(vararg names: Any): Array = + config("kaptTest", *names) + fun PluginAware.applyPlugins(vararg names: String) { apply { names.forEach(::plugin) } } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 3e84b47d6..3a63a4d08 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -29,4 +29,6 @@ dependencies { implementations(*Libs.coil) debugImplementations(Libs.chuckerDebug) releaseImplementations(Libs.chuckerRelease) + + kaptTests(Libs.moshiCompiler) } From a55725e87311258f040bca53cd8a9176c002c7d3 Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 18:23:46 +0800 Subject: [PATCH 09/11] Add @Language annotations --- .../main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt | 7 +++++-- .../test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt index 4b389ab18..7e0f98966 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt @@ -4,12 +4,13 @@ package io.goooler.demoapp.common.util import com.squareup.moshi.Moshi import com.squareup.moshi.Types +import org.intellij.lang.annotations.Language object JsonUtil { @PublishedApi internal val moshi: Moshi = Moshi.Builder().build() - inline fun fromJson(string: String): T? = try { + inline fun fromJson(@Language("JSON") string: String): T? = try { moshi.adapter(T::class.java).fromJson(string) } catch (e: Exception) { e.printStackTrace() @@ -17,7 +18,7 @@ object JsonUtil { } fun fromJson( - string: String, + @Language("JSON") string: String, rawType: Class<*>, vararg typeArguments: Class<*> ): T? = try { @@ -27,6 +28,7 @@ object JsonUtil { null } + @Language("JSON") inline fun toJson(value: T?): String? = try { moshi.adapter(T::class.java).toJson(value) } catch (e: Exception) { @@ -40,4 +42,5 @@ inline fun String.fromJson(): T? = JsonUtil.fromJson(this, T::class. inline fun String.fromJson(rawType: Class<*>, vararg typeArguments: Class<*>): T? = JsonUtil.fromJson(this, rawType, *typeArguments) +@Language("JSON") fun Any?.toJson(): String? = JsonUtil.toJson(this) diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt index fe02d1bdc..25a97325c 100644 --- a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -4,6 +4,7 @@ import com.squareup.moshi.JsonClass import io.goooler.demoapp.common.util.JsonUtil import io.goooler.demoapp.common.util.fromJson import io.goooler.demoapp.common.util.toJson +import org.intellij.lang.annotations.Language import org.junit.Assert.assertTrue import org.junit.Test @@ -63,6 +64,7 @@ class JsonUtilTest { } companion object { + @Language("JSON") private val firstStr = """ { "id": 126987864, @@ -73,6 +75,7 @@ class JsonUtilTest { } """.trimIndent() + @Language("JSON") private val secondStr = """ { "id": 374913489, @@ -84,6 +87,7 @@ class JsonUtilTest { """.trimIndent() // https://api.github.com/users/goooler/repos?&page=1&per_page=2 + @Language("JSON") private val strArray = "[$firstStr,$secondStr]" private val firstBean = Repo(126987864, "1024_hosts", Repo.Owner("Goooler")) From 5f7e50d6949d9757ee8a6996a3a4a89344bff843 Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 21:25:17 +0800 Subject: [PATCH 10/11] Fix tests --- .../goooler/demoapp/common/util/JsonUtil.kt | 2 +- .../io/goooler/demoapp/common/JsonUtilTest.kt | 20 ++++--------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt index 7e0f98966..7719dddf0 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt @@ -37,7 +37,7 @@ object JsonUtil { } } -inline fun String.fromJson(): T? = JsonUtil.fromJson(this, T::class.java) +inline fun String.fromJson(): T? = JsonUtil.fromJson(this) inline fun String.fromJson(rawType: Class<*>, vararg typeArguments: Class<*>): T? = JsonUtil.fromJson(this, rawType, *typeArguments) diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt index 25a97325c..64090f04a 100644 --- a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -17,7 +17,7 @@ class JsonUtilTest { @Test fun `JsonUtil fromJson(String, Class, Class)`() { - val array: Array = JsonUtil.fromJson(strArray, Array::class.java, Repo::class.java) + val array: List = JsonUtil.fromJson(strArray, List::class.java, Repo::class.java) ?: throw Exception("Parse json error") assertTrue(array.first() == firstBean) assertTrue(array[1] == secondBean) @@ -37,7 +37,7 @@ class JsonUtilTest { @Test fun `String fromJson(Class, Class)`() { - val array: Array = strArray.fromJson(Array::class.java, Repo::class.java) + val array: List = strArray.fromJson(List::class.java, Repo::class.java) ?: throw Exception("Parse json error") assertTrue(array.first() == firstBean) assertTrue(array[1] == secondBean) @@ -66,24 +66,12 @@ class JsonUtilTest { companion object { @Language("JSON") private val firstStr = """ - { - "id": 126987864, - "name": "1024_hosts", - "owner": { - "login": "Goooler" - } - } + {"id":126987864,"name":"1024_hosts","owner":{"login":"Goooler"}} """.trimIndent() @Language("JSON") private val secondStr = """ - { - "id": 374913489, - "name": "AndroidUiDemo", - "owner": { - "login": "Goooler" - } - } + {"id":374913489,"name":"AndroidUiDemo","owner":{"login":"Goooler"}} """.trimIndent() // https://api.github.com/users/goooler/repos?&page=1&per_page=2 From 47a0b02a1fa4e916d8a74ecd3ab23dfdca09d667 Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 28 Feb 2022 21:49:10 +0800 Subject: [PATCH 11/11] Cleanup --- .../io/goooler/demoapp/common/util/JsonUtil.kt | 2 +- .../io/goooler/demoapp/common/JsonUtilTest.kt | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt index 7719dddf0..5634601ca 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/util/JsonUtil.kt @@ -17,7 +17,7 @@ object JsonUtil { null } - fun fromJson( + inline fun fromJson( @Language("JSON") string: String, rawType: Class<*>, vararg typeArguments: Class<*> diff --git a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt index 64090f04a..0872bcf0a 100644 --- a/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt +++ b/common/src/test/kotlin/io/goooler/demoapp/common/JsonUtilTest.kt @@ -1,6 +1,7 @@ package io.goooler.demoapp.common import com.squareup.moshi.JsonClass +import io.goooler.demoapp.base.util.secondOrNull import io.goooler.demoapp.common.util.JsonUtil import io.goooler.demoapp.common.util.fromJson import io.goooler.demoapp.common.util.toJson @@ -17,10 +18,10 @@ class JsonUtilTest { @Test fun `JsonUtil fromJson(String, Class, Class)`() { - val array: List = JsonUtil.fromJson(strArray, List::class.java, Repo::class.java) + val list: List = JsonUtil.fromJson(strArray, List::class.java, Repo::class.java) ?: throw Exception("Parse json error") - assertTrue(array.first() == firstBean) - assertTrue(array[1] == secondBean) + assertTrue(list.firstOrNull() == firstBean) + assertTrue(list.secondOrNull() == secondBean) } @Test @@ -37,14 +38,14 @@ class JsonUtilTest { @Test fun `String fromJson(Class, Class)`() { - val array: List = strArray.fromJson(List::class.java, Repo::class.java) + val list: List = strArray.fromJson(List::class.java, Repo::class.java) ?: throw Exception("Parse json error") - assertTrue(array.first() == firstBean) - assertTrue(array[1] == secondBean) + assertTrue(list.first() == firstBean) + assertTrue(list[1] == secondBean) } @Test - fun `Any? toJson(T)`() { + fun `Any toJson(T)`() { assertTrue(firstBean.toJson() == firstStr) assertTrue(secondBean.toJson() == secondStr) }