From eb536c23d3e710e86878c2cdd5897e975adcb924 Mon Sep 17 00:00:00 2001 From: Erik Eelde Date: Mon, 20 Jun 2022 15:59:39 +0200 Subject: [PATCH] Add failing tests --- .../NetworkLayerClassJsonDetectorTest.kt | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/checks/src/test/java/com/kozaxinan/android/checks/NetworkLayerClassJsonDetectorTest.kt b/checks/src/test/java/com/kozaxinan/android/checks/NetworkLayerClassJsonDetectorTest.kt index 0242877..ca01c04 100644 --- a/checks/src/test/java/com/kozaxinan/android/checks/NetworkLayerClassJsonDetectorTest.kt +++ b/checks/src/test/java/com/kozaxinan/android/checks/NetworkLayerClassJsonDetectorTest.kt @@ -89,6 +89,61 @@ internal class NetworkLayerClassJsonDetectorTest : LintDetectorTest() { .expectClean() } + @Test + fun `test kotlin file with Json that has nested objects`() { + lint() + .files( + retrofit(), + jsonAnnotation(), + jsonClassAnnotation(), + kotlin( + """ + package foo + + import retrofit2.http.GET + + internal interface Api { + + @GET("url") + suspend fun get(): Dto + } + """.trimIndent() + ), + kotlin( + """ + package foo + + import com.squareup.moshi.Json + import com.squareup.moshi.JsonClass + + @JsonClass(generateAdapter = true) + data class Dto constructor( + @Json(name = "totalResults") val totalResults: Int, + @Json(name = "totalNewResults") @Deprecated + val totalNewResults: Int, + @Json(name = "name") val name: String, + @Json(name = "bool") val bools: List?, + @Json(name = "dto") val dtos: List?, + ) { + + companion object { + + val EMPTY = Dto(0, 0, "", false) + + val mapping: Map = mapOf( + "x" to "x", + "y" to "y" + ) + } + } + """.trimIndent() + ) + ) + .issues(*ISSUES_TO_TEST) + .run() + .expectClean() + } + @Test fun `test kotlin enum file with JsonClass`() { lint() @@ -151,6 +206,56 @@ internal class NetworkLayerClassJsonDetectorTest : LintDetectorTest() { ) } + @Test + fun `test suspending list wrapped in a response`() { + lint() + .files( + retrofit(), + jsonAnnotation(), + jsonClassAnnotation(), + kotlin( + """ + package foo + + import retrofit2.http.GET + import retrofit2.Response + + interface Api { + + @GET("url") + suspend fun get(): Response> + } + """.trimIndent() + ), + kotlin( + """ + package foo + + import com.squareup.moshi.JsonClass + + @JsonClass(generateAdapter = true) + data class Dto( + val myEnums: List? + ) + + enum class EnumType { + FIRST, + SECOND + } + + """.trimIndent() + ) + ) + .issues(ISSUE_NETWORK_LAYER_CLASS_JSON_CLASS_RULE) + .run() + .expect( + """src/foo/Api.kt:9: Information: Return type doesn't have @JsonClass annotation for [EnumType] classes [NetworkLayerClassJsonClassRule] + suspend fun get(): Response> + ~~~ +0 errors, 0 warnings""".trimIndent() + ) + } + @Test fun `test kotlin file without SerializedName`() { lint()