Skip to content

Commit

Permalink
Improve testing (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso authored Oct 7, 2023
1 parent 5433245 commit e70c602
Show file tree
Hide file tree
Showing 26 changed files with 248 additions and 227 deletions.
1 change: 0 additions & 1 deletion compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies {
compileOnly(libs.kotlin.compiler.embeddable)
testImplementation(libs.kctfork.core)
testImplementation(libs.junit)
testImplementation(libs.truth)
testImplementation(kotlin("reflect"))

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.jensklingenberg.ktorfit

import com.google.common.truth.Truth

import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.config.JvmTarget
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -54,8 +56,9 @@ fun <T> Ktorfit.create(ktorfitService: KtorfitService = Default()): T {

val result = compile(listOf(source2, source))

Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
Truth.assertThat(result.messages.contains("_TestServiceImpl")).isEqualTo(true)
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)

assertTrue(result.messages.contains("_TestServiceImpl"))

}

Expand Down Expand Up @@ -98,8 +101,8 @@ fun <T> Ktorfit.create(ktorfitService: KtorfitService = Default()): T {
)

val result = compile(listOf(source2, source))
Assert.assertEquals(KotlinCompilation.ExitCode.INTERNAL_ERROR,result.exitCode)
Truth.assertThat(result.messages.contains(CreateFuncTransformer.ERROR_TYPE_ARGUMENT_NOT_INTERFACE("T"))).isEqualTo(true)
assertEquals(KotlinCompilation.ExitCode.INTERNAL_ERROR,result.exitCode)
assertTrue(result.messages.contains(CreateFuncTransformer.ERROR_TYPE_ARGUMENT_NOT_INTERFACE("T")))
}

private fun prepareCompilation(
Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ licensee-gradle-plugin = "app.cash.licensee:licensee-gradle-plugin:1.8.0"
logbackClassic = "ch.qos.logback:logback-classic:1.4.11"
mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockito-kotlin" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
truth = "com.google.truth:truth:1.1.5"

[plugins]
binaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibilityValidator" }
Expand Down
1 change: 0 additions & 1 deletion ktorfit-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ dependencies {

/* TEST */
testImplementation(libs.junit)
testImplementation(libs.truth)
testImplementation(libs.kctfork.core)
testImplementation(libs.kctfork.ksp)
testImplementation(libs.mockito.kotlin)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.jensklingenberg.ktorfit

import com.google.common.truth.Truth

import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspSourcesDir
import de.jensklingenberg.ktorfit.model.KtorfitError
import org.junit.Assert
import org.junit.Assert.*
import org.junit.Test
import java.io.File

Expand All @@ -30,12 +30,11 @@ interface TestService {
"""
)


val compilation = getCompilation(listOf(source))

val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
Assert.assertTrue(result.messages.contains(KtorfitError.NON_BODY_HTTP_METHOD_CANNOT_CONTAIN_BODY))
assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR, result.exitCode)
assertTrue(result.messages.contains(KtorfitError.NON_BODY_HTTP_METHOD_CANNOT_CONTAIN_BODY))
}

@Test
Expand All @@ -60,8 +59,8 @@ interface TestService {
val compilation = getCompilation(listOf(source))

val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
Assert.assertTrue(result.messages.contains(KtorfitError.BODY_PARAMETERS_CANNOT_BE_USED_WITH_FORM_OR_MULTI_PART_ENCODING))
assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR,result.exitCode)
assertTrue(result.messages.contains(KtorfitError.BODY_PARAMETERS_CANNOT_BE_USED_WITH_FORM_OR_MULTI_PART_ENCODING))
}

@Test
Expand All @@ -85,15 +84,14 @@ interface TestService {

val compilation = getCompilation(listOf(source))
val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)

assertEquals(KotlinCompilation.ExitCode.OK,result.exitCode)
val generatedSourcesDir = compilation.kspSourcesDir
val generatedFile = File(
generatedSourcesDir,
"/kotlin/com/example/api/_TestServiceImpl.kt"
)
Truth.assertThat(generatedFile.exists()).isTrue()
Truth.assertThat(generatedFile.readText().contains(expectedBodyDataArgumentText)).isTrue()
assertTrue(generatedFile.exists())
assertTrue(generatedFile.readText().contains(expectedBodyDataArgumentText))
}

@Test
Expand All @@ -117,15 +115,15 @@ interface TestService {
val compilation = getCompilation(listOf(source))

val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)

assertEquals(KotlinCompilation.ExitCode.OK,result.exitCode)
val generatedSourcesDir = compilation.kspSourcesDir
val generatedFile = File(
generatedSourcesDir,
"/kotlin/com/example/api/_TestServiceImpl.kt"
)
Truth.assertThat(generatedFile.exists()).isTrue()
Truth.assertThat(generatedFile.readText().contains(notExpectedBodyDataArgumentText)).isFalse()

assertTrue(generatedFile.exists())
assertFalse(generatedFile.readText().contains(notExpectedBodyDataArgumentText))
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package de.jensklingenberg.ktorfit

import com.google.common.truth.Truth
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import de.jensklingenberg.ktorfit.model.KtorfitError
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class CompilationErrorsTest() {
class CompilationErrorsTest {

@Test
fun whenJavaInterface_ThrowCompilationError() {
Expand All @@ -26,8 +26,8 @@ class CompilationErrorsTest() {

val compilation = getCompilation(listOf(source))
val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
Assert.assertTrue(result.messages.contains(KtorfitError.JAVA_INTERFACES_ARE_NOT_SUPPORTED))
assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR, result.exitCode)
assertTrue(result.messages.contains(KtorfitError.JAVA_INTERFACES_ARE_NOT_SUPPORTED))
}

@Test
Expand Down Expand Up @@ -60,8 +60,8 @@ interface GithubService {
val compilation = getCompilation(listOf(source))

val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
Assert.assertTrue(result.messages.contains(KtorfitError.INTERFACE_NEEDS_TO_HAVE_A_PACKAGE))
assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR, result.exitCode)
assertTrue(result.messages.contains(KtorfitError.INTERFACE_NEEDS_TO_HAVE_A_PACKAGE))
}


Expand Down Expand Up @@ -95,8 +95,8 @@ interface GithubService<T> {
val compilation = getCompilation(listOf(source))

val result = compilation.compile()
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
Assert.assertTrue(result.messages.contains(KtorfitError.TYPE_PARAMETERS_ARE_UNSUPPORTED_ON))
assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR, result.exitCode)
assertTrue(result.messages.contains(KtorfitError.TYPE_PARAMETERS_ARE_UNSUPPORTED_ON))
}

}
Loading

0 comments on commit e70c602

Please sign in to comment.