@@ -18,86 +18,29 @@ package retrofit2.keeper
1818import com.google.common.truth.Truth.assertAbout
1919import com.google.testing.compile.JavaFileObjects
2020import com.google.testing.compile.JavaSourceSubjectFactory.javaSource
21+ import com.google.testing.junit.testparameterinjector.TestParameter
22+ import com.google.testing.junit.testparameterinjector.TestParameterInjector
2123import java.nio.charset.StandardCharsets.UTF_8
24+ import java.nio.file.NoSuchFileException
2225import javax.tools.StandardLocation.CLASS_OUTPUT
26+ import kotlin.io.path.readText
27+ import kotlin.io.path.toPath
2328import org.junit.Test
29+ import org.junit.runner.RunWith
2430
31+ @RunWith(TestParameterInjector ::class )
2532class RetrofitResponseTypeKeepProcessorTest {
2633 @Test
27- fun allHttpMethods () {
34+ fun process (
35+ @TestParameter(
36+ " all-http-methods" ,
37+ " nesting" ,
38+ " kotlin-suspend" ,
39+ ) name : String
40+ ) {
2841 val service = JavaFileObjects .forSourceString(
2942 " test.Service" ,
30- """
31- package test;
32- import retrofit2.*;
33- import retrofit2.http.*;
34-
35- class DeleteUser {}
36- class GetUser {}
37- class HeadUser {}
38- class HttpUser {}
39- class OptionsUser {}
40- class PatchUser {}
41- class PostUser {}
42- class PutUser {}
43-
44- interface Service {
45- @DELETE("/") Call<DeleteUser> delete();
46- @GET("/") Call<GetUser> get();
47- @HEAD("/") Call<HeadUser> head();
48- @HTTP(method = "CUSTOM", path = "/") Call<HttpUser> http();
49- @OPTIONS("/") Call<OptionsUser> options();
50- @PATCH("/") Call<PatchUser> patch();
51- @POST("/") Call<PostUser> post();
52- @PUT("/") Call<PutUser> put();
53- }
54- """ .trimIndent(),
55- )
56-
57- assertAbout(javaSource())
58- .that(service)
59- .processedWith(RetrofitResponseTypeKeepProcessor ())
60- .compilesWithoutError()
61- .and ()
62- .generatesFileNamed(
63- CLASS_OUTPUT ,
64- " " ,
65- " META-INF/proguard/retrofit-response-type-keeper-test.Service.pro" ,
66- ).withStringContents(
67- UTF_8 ,
68- """
69- |# test.Service
70- |-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call
71- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.DeleteUser
72- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.GetUser
73- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.HeadUser
74- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.HttpUser
75- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.OptionsUser
76- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PatchUser
77- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PostUser
78- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PutUser
79- |
80- """ .trimMargin(),
81- )
82- }
83-
84- @Test
85- fun nesting () {
86- val service = JavaFileObjects .forSourceString(
87- " test.Service" ,
88- """
89- package test;
90- import retrofit2.*;
91- import retrofit2.http.*;
92-
93- class One<T> {}
94- class Two<T> {}
95- class Three {}
96-
97- interface Service {
98- @GET("/") Call<One<Two<Three>>> get();
99- }
100- """ .trimIndent(),
43+ readResourceAsText(" $name /Service.java" ),
10144 )
10245
10346 assertAbout(javaSource())
@@ -111,52 +54,15 @@ class RetrofitResponseTypeKeepProcessorTest {
11154 " META-INF/proguard/retrofit-response-type-keeper-test.Service.pro" ,
11255 ).withStringContents(
11356 UTF_8 ,
114- """
115- |# test.Service
116- |-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call
117- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.One
118- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Three
119- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Two
120- |
121- """ .trimMargin(),
57+ readResourceAsText(" $name /Service.pro" ),
12258 )
12359 }
12460
125- @Test
126- fun kotlinSuspend () {
127- val service = JavaFileObjects .forSourceString(
128- " test.Service" ,
129- """
130- package test;
131- import kotlin.coroutines.Continuation;
132- import retrofit2.*;
133- import retrofit2.http.*;
134-
135- class Body {}
136-
137- interface Service {
138- @GET("/") Object get(Continuation<? extends Body> c);
139- }
140- """ .trimIndent(),
141- )
142-
143- assertAbout(javaSource())
144- .that(service)
145- .processedWith(RetrofitResponseTypeKeepProcessor ())
146- .compilesWithoutError()
147- .and ()
148- .generatesFileNamed(
149- CLASS_OUTPUT ,
150- " " ,
151- " META-INF/proguard/retrofit-response-type-keeper-test.Service.pro" ,
152- ).withStringContents(
153- UTF_8 ,
154- """
155- |# test.Service
156- |-keep,allowoptimization,allowshrinking,allowobfuscation class java.lang.Object
157- |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Body
158- |
159- """ .trimMargin(),
160- )
61+ private companion object {
62+ fun readResourceAsText (name : String ): String {
63+ val resource = RetrofitResponseTypeKeepProcessorTest ::class .java.classLoader.getResource(name)
64+ ? : throw NoSuchFileException (" Resource $name not found." )
65+ return resource.toURI().toPath().readText()
66+ }
16167 }
16268}
0 commit comments