diff --git a/retrofit-response-type-keeper/build.gradle b/retrofit-response-type-keeper/build.gradle index c9015f23f0..00fd0c6d26 100644 --- a/retrofit-response-type-keeper/build.gradle +++ b/retrofit-response-type-keeper/build.gradle @@ -11,5 +11,6 @@ dependencies { testImplementation libs.junit testImplementation libs.compileTesting testImplementation libs.truth + testImplementation libs.testParameterInjector testImplementation projects.retrofit } diff --git a/retrofit-response-type-keeper/src/test/kotlin/retrofit2/keeper/RetrofitResponseTypeKeepProcessorTest.kt b/retrofit-response-type-keeper/src/test/kotlin/retrofit2/keeper/RetrofitResponseTypeKeepProcessorTest.kt index 23cc38bb9c..6c97f063c4 100644 --- a/retrofit-response-type-keeper/src/test/kotlin/retrofit2/keeper/RetrofitResponseTypeKeepProcessorTest.kt +++ b/retrofit-response-type-keeper/src/test/kotlin/retrofit2/keeper/RetrofitResponseTypeKeepProcessorTest.kt @@ -18,86 +18,29 @@ package retrofit2.keeper import com.google.common.truth.Truth.assertAbout import com.google.testing.compile.JavaFileObjects import com.google.testing.compile.JavaSourceSubjectFactory.javaSource +import com.google.testing.junit.testparameterinjector.TestParameter +import com.google.testing.junit.testparameterinjector.TestParameterInjector import java.nio.charset.StandardCharsets.UTF_8 +import java.nio.file.NoSuchFileException import javax.tools.StandardLocation.CLASS_OUTPUT +import kotlin.io.path.readText +import kotlin.io.path.toPath import org.junit.Test +import org.junit.runner.RunWith +@RunWith(TestParameterInjector::class) class RetrofitResponseTypeKeepProcessorTest { @Test - fun allHttpMethods() { + fun process( + @TestParameter( + "all-http-methods", + "nesting", + "kotlin-suspend", + ) name: String + ) { val service = JavaFileObjects.forSourceString( "test.Service", - """ - package test; - import retrofit2.*; - import retrofit2.http.*; - - class DeleteUser {} - class GetUser {} - class HeadUser {} - class HttpUser {} - class OptionsUser {} - class PatchUser {} - class PostUser {} - class PutUser {} - - interface Service { - @DELETE("/") Call delete(); - @GET("/") Call get(); - @HEAD("/") Call head(); - @HTTP(method = "CUSTOM", path = "/") Call http(); - @OPTIONS("/") Call options(); - @PATCH("/") Call patch(); - @POST("/") Call post(); - @PUT("/") Call put(); - } - """.trimIndent(), - ) - - assertAbout(javaSource()) - .that(service) - .processedWith(RetrofitResponseTypeKeepProcessor()) - .compilesWithoutError() - .and() - .generatesFileNamed( - CLASS_OUTPUT, - "", - "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro", - ).withStringContents( - UTF_8, - """ - |# test.Service - |-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.DeleteUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.GetUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.HeadUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.HttpUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.OptionsUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PatchUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PostUser - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PutUser - | - """.trimMargin(), - ) - } - - @Test - fun nesting() { - val service = JavaFileObjects.forSourceString( - "test.Service", - """ - package test; - import retrofit2.*; - import retrofit2.http.*; - - class One {} - class Two {} - class Three {} - - interface Service { - @GET("/") Call>> get(); - } - """.trimIndent(), + readResourceAsText("$name/Service.java"), ) assertAbout(javaSource()) @@ -111,52 +54,15 @@ class RetrofitResponseTypeKeepProcessorTest { "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro", ).withStringContents( UTF_8, - """ - |# test.Service - |-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.One - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Three - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Two - | - """.trimMargin(), + readResourceAsText("$name/Service.pro"), ) } - @Test - fun kotlinSuspend() { - val service = JavaFileObjects.forSourceString( - "test.Service", - """ - package test; - import kotlin.coroutines.Continuation; - import retrofit2.*; - import retrofit2.http.*; - - class Body {} - - interface Service { - @GET("/") Object get(Continuation c); - } - """.trimIndent(), - ) - - assertAbout(javaSource()) - .that(service) - .processedWith(RetrofitResponseTypeKeepProcessor()) - .compilesWithoutError() - .and() - .generatesFileNamed( - CLASS_OUTPUT, - "", - "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro", - ).withStringContents( - UTF_8, - """ - |# test.Service - |-keep,allowoptimization,allowshrinking,allowobfuscation class java.lang.Object - |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Body - | - """.trimMargin(), - ) + private companion object { + fun readResourceAsText(name: String): String { + val resource = this::class.java.classLoader.getResource(name) + ?: throw NoSuchFileException("Resource $name not found.") + return resource.toURI().toPath().readText() + } } } diff --git a/retrofit-response-type-keeper/src/test/resources/all-http-methods/Service.java b/retrofit-response-type-keeper/src/test/resources/all-http-methods/Service.java new file mode 100644 index 0000000000..93ad79b7b5 --- /dev/null +++ b/retrofit-response-type-keeper/src/test/resources/all-http-methods/Service.java @@ -0,0 +1,24 @@ +package test; + +import retrofit2.*; +import retrofit2.http.*; + +class DeleteUser {} +class GetUser {} +class HeadUser {} +class HttpUser {} +class OptionsUser {} +class PatchUser {} +class PostUser {} +class PutUser {} + +interface Service { + @DELETE("/") Call delete(); + @GET("/") Call get(); + @HEAD("/") Call head(); + @HTTP(method = "CUSTOM", path = "/") Call http(); + @OPTIONS("/") Call options(); + @PATCH("/") Call patch(); + @POST("/") Call post(); + @PUT("/") Call put(); +} diff --git a/retrofit-response-type-keeper/src/test/resources/all-http-methods/Service.pro b/retrofit-response-type-keeper/src/test/resources/all-http-methods/Service.pro new file mode 100644 index 0000000000..bc42931349 --- /dev/null +++ b/retrofit-response-type-keeper/src/test/resources/all-http-methods/Service.pro @@ -0,0 +1,10 @@ +# test.Service +-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call +-keep,allowoptimization,allowshrinking,allowobfuscation class test.DeleteUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.GetUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.HeadUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.HttpUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.OptionsUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.PatchUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.PostUser +-keep,allowoptimization,allowshrinking,allowobfuscation class test.PutUser diff --git a/retrofit-response-type-keeper/src/test/resources/kotlin-suspend/Service.java b/retrofit-response-type-keeper/src/test/resources/kotlin-suspend/Service.java new file mode 100644 index 0000000000..6cd8250020 --- /dev/null +++ b/retrofit-response-type-keeper/src/test/resources/kotlin-suspend/Service.java @@ -0,0 +1,11 @@ +package test; + +import kotlin.coroutines.Continuation; +import retrofit2.*; +import retrofit2.http.*; + +class Body {} + +interface Service { + @GET("/") Object get(Continuation c); +} diff --git a/retrofit-response-type-keeper/src/test/resources/kotlin-suspend/Service.pro b/retrofit-response-type-keeper/src/test/resources/kotlin-suspend/Service.pro new file mode 100644 index 0000000000..f16efdb512 --- /dev/null +++ b/retrofit-response-type-keeper/src/test/resources/kotlin-suspend/Service.pro @@ -0,0 +1,3 @@ +# test.Service +-keep,allowoptimization,allowshrinking,allowobfuscation class java.lang.Object +-keep,allowoptimization,allowshrinking,allowobfuscation class test.Body diff --git a/retrofit-response-type-keeper/src/test/resources/nesting/Service.java b/retrofit-response-type-keeper/src/test/resources/nesting/Service.java new file mode 100644 index 0000000000..7c4f2e5959 --- /dev/null +++ b/retrofit-response-type-keeper/src/test/resources/nesting/Service.java @@ -0,0 +1,12 @@ +package test; + +import retrofit2.*; +import retrofit2.http.*; + +class One {} +class Two {} +class Three {} + +interface Service { + @GET("/") Call>> get(); +} diff --git a/retrofit-response-type-keeper/src/test/resources/nesting/Service.pro b/retrofit-response-type-keeper/src/test/resources/nesting/Service.pro new file mode 100644 index 0000000000..331a97c51b --- /dev/null +++ b/retrofit-response-type-keeper/src/test/resources/nesting/Service.pro @@ -0,0 +1,5 @@ +# test.Service +-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call +-keep,allowoptimization,allowshrinking,allowobfuscation class test.One +-keep,allowoptimization,allowshrinking,allowobfuscation class test.Three +-keep,allowoptimization,allowshrinking,allowobfuscation class test.Two