Skip to content

Commit

Permalink
Add test for opt in marker class deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
dewantawsif committed Feb 10, 2025
1 parent 3cd31c1 commit 6d8e760
Showing 1 changed file with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
@OptIn(ExperimentalCompilerApi::class)
interface TestService {
@Headers(value = ["x:y","a:b"])
@GET("posts")
@OptIn(ExperimentalCompilerApi::class)
suspend fun test(@Header("testHeader") testParameterNonNullable: String?, @Header("testHeader") testParameterNullable: String?, @HeaderMap("testHeaderMap") testParameter2: Map<String,String>): String
@Headers(value = ["x:y","a:b"])
@GET("posts")
@OptIn(ExperimentalCompilerApi::class)
suspend fun test(@Header("testHeader") testParameterNonNullable: String?, @Header("testHeader") testParameterNullable: String?, @HeaderMap("testHeaderMap") testParameter2: Map<String,String>): String
}
""",
)
Expand All @@ -51,4 +51,49 @@ public class _TestServiceImpl(
val actualSource = generatedFile.readText()
assertTrue(actualSource.contains(expectedHeadersArgumentText))
}

@Test
fun `when OptIn annotation are add to the implementation class do not repeat marker classes`() {
val source =
SourceFile.kotlin(
"Source.kt",
"""package com.example.api
import de.jensklingenberg.ktorfit.http.GET
import de.jensklingenberg.ktorfit.http.Headers
import de.jensklingenberg.ktorfit.http.Header
import de.jensklingenberg.ktorfit.http.HeaderMap
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
@OptIn(ExperimentalCompilerApi::class)
interface TestService {
@GET("posts")
@OptIn(ExperimentalCompilerApi::class)
suspend fun test1()
@GET("posts")
@OptIn(ExperimentalCompilerApi::class)
suspend fun test2()
}
""",
)

val expectedHeadersArgumentText =
"""@OptIn(ExperimentalCompilerApi::class, InternalKtorfitApi::class)
public class _TestServiceImpl(
private val _ktorfit: Ktorfit,
) : TestService {"""

val compilation = getCompilation(listOf(source))

val generatedSourcesDir = compilation.apply { compile() }.kspSourcesDir
val generatedFile =
File(
generatedSourcesDir,
"/kotlin/com/example/api/_TestServiceImpl.kt",
)

val actualSource = generatedFile.readText()
println(actualSource)
assertTrue(actualSource.contains(expectedHeadersArgumentText))
}
}

0 comments on commit 6d8e760

Please sign in to comment.