Skip to content

Commit

Permalink
Adds tests for KotlinPoet integration
Browse files Browse the repository at this point in the history
Modifies `KotlinSource` to take relative path instead of absolute path
for testing, making it somewhat similar to how `JavaSource` takes
qualified name.
  • Loading branch information
gcvasconcelos-picpay committed Mar 9, 2021
1 parent af2305d commit 91f8f88
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,10 @@ internal class KotlinCompileTestingCompilationResult(
Source.loadJavaSource(sourceFile, qName)
}
sourceFile.name.endsWith(".kt") -> {
Source.loadKotlinSource(sourceFile)
val relativePath = sourceFile.absolutePath.substringAfter(
srcRoot.absolutePath
).dropWhile { it == '/' }
Source.loadKotlinSource(sourceFile, relativePath)
}
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ sealed class Source {
}

fun loadKotlinSource(
file: File
file: File,
relativePath: String
): Source {
check(file.exists() && file.name.endsWith(".kt"))
return kotlin(file.absolutePath, file.readText())
return kotlin(relativePath, file.readText())
}

fun loadJavaSource(
Expand All @@ -142,13 +143,14 @@ sealed class Source {

fun load(
file: File,
qName: String
qName: String,
relativePath: String
): Source {
check(file.exists()) {
"file does not exist: ${file.absolutePath}"
}
return when {
file.name.endsWith(".kt") -> loadKotlinSource(file)
file.name.endsWith(".kt") -> loadKotlinSource(file, relativePath)
file.name.endsWith(".java") -> loadJavaSource(file, qName)
else -> error("invalid file extension ${file.name}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import com.google.common.truth.Truth.assertThat
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeSpec
import com.squareup.kotlinpoet.BOOLEAN
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.TypeSpec as KTypeSpec
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
Expand Down Expand Up @@ -111,4 +114,70 @@ class GeneratedCodeMatchTest internal constructor(
)
assertThat(result.exceptionOrNull()).hasMessageThat().contains(mismatch.toString())
}

@Test
fun successfulGeneratedKotlinCodeMatch() {
// java environment will not generate kotlin files
if (runTest.toString() == "java") return

val file = FileSpec.builder("foo.bar", "Baz")
.addType(KTypeSpec.classBuilder("Baz").build())
.build()
runTest { invocation ->
if (invocation.processingEnv.findTypeElement("foo.bar.Baz") == null) {
invocation.processingEnv.filer.write(file)
}
invocation.assertCompilationResult {
generatedSource(
Source.kotlin("foo/bar/Baz.kt", file.toString())
)
}
}
}

@Test
fun missingGeneratedKotlinCode_mismatch() {
// java environment will not generate kotlin files
if (runTest.toString() == "java") return

val generated = FileSpec.builder("foo.bar", "Baz")
.addType(
KTypeSpec.classBuilder("Baz")
.addProperty("bar", BOOLEAN)
.build()
)
.build()
val expected = FileSpec.builder("foo.bar", "Baz")
.addType(
KTypeSpec.classBuilder("Baz")
.addProperty("foo", BOOLEAN)
.build()
)
.build()

val result = runCatching {
runTest { invocation ->
if (invocation.processingEnv.findTypeElement("foo.bar.Baz") == null) {
invocation.processingEnv.filer.write(generated)
}
invocation.assertCompilationResult {
generatedSource(
Source.kotlin("foo/bar/Baz.kt", expected.toString())
)
}
}
}

val mismatch = SourceFileMismatch(
expected = Line(
pos = 6,
content = "val foo: Boolean"
),
actual = Line(
pos = 6,
content = "val bar: Boolean"
)
)
assertThat(result.exceptionOrNull()).hasMessageThat().contains(mismatch.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fun loadJavaCode(fileName: String, qName: String): JavaFileObject {

fun loadTestSource(fileName: String, qName: String): Source {
val contents = File("src/test/data/$fileName")
return Source.load(contents, qName)
return Source.load(contents, qName, fileName)
}

fun createVerifierFromEntitiesAndViews(invocation: TestInvocation): DatabaseVerifier {
Expand Down

0 comments on commit 91f8f88

Please sign in to comment.