-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
140 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
ktorfit-ksp/src/test/kotlin/de/jensklingenberg/ktorfit/InheritanceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package de.jensklingenberg.ktorfit | ||
|
||
import com.tschuchort.compiletesting.KotlinCompilation | ||
import com.tschuchort.compiletesting.SourceFile | ||
import com.tschuchort.compiletesting.kspSourcesDir | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class InheritanceTest { | ||
@Test | ||
fun `when Interface Without Ktorfit Annotations Is Extended Then dont add delegation`() { | ||
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 | ||
interface SuperTestService<T>{ | ||
suspend fun test(): T | ||
} | ||
interface TestService : SuperTestService<String> { | ||
@GET("posts") | ||
override suspend fun test(): T | ||
@GET("posts") | ||
suspend fun test(): String | ||
} | ||
""", | ||
) | ||
|
||
val expectedHeadersArgumentText = | ||
"public class _TestServiceImpl(\n" + | ||
" private val _ktorfit: Ktorfit,\n" + | ||
") : TestService {" | ||
|
||
val compilation = getCompilation(listOf(source)) | ||
val result = compilation.compile() | ||
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode) | ||
val generatedSourcesDir = compilation.kspSourcesDir | ||
val generatedFile = | ||
File( | ||
generatedSourcesDir, | ||
"/kotlin/com/example/api/_TestServiceImpl.kt", | ||
) | ||
|
||
val actualSource = generatedFile.readText() | ||
assertTrue(actualSource.contains(expectedHeadersArgumentText)) | ||
} | ||
|
||
@Test | ||
fun `when Interface with Ktorfit Annotations Is Extended Then add delegation`() { | ||
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 | ||
interface SuperTestService{ | ||
@GET("posts") | ||
suspend fun test(): String | ||
} | ||
interface TestService : SuperTestService { | ||
@GET("posts") | ||
override suspend fun test(): T | ||
@GET("posts") | ||
suspend fun test(): String | ||
} | ||
""", | ||
) | ||
|
||
val expectedHeadersArgumentText = | ||
buildString { | ||
append("public class _TestServiceImpl(\n") | ||
append(" private val _ktorfit: Ktorfit,\n") | ||
append(") : TestService,\n") | ||
append(" SuperTestService by com.example.api._SuperTestServiceImpl(_ktorfit) {") | ||
} | ||
|
||
val compilation = getCompilation(listOf(source)) | ||
val result = compilation.compile() | ||
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode) | ||
val generatedSourcesDir = compilation.kspSourcesDir | ||
val generatedFile = | ||
File( | ||
generatedSourcesDir, | ||
"/kotlin/com/example/api/_TestServiceImpl.kt", | ||
) | ||
|
||
val actualSource = generatedFile.readText() | ||
assertTrue(actualSource.contains(expectedHeadersArgumentText)) | ||
} | ||
} |