Skip to content

Commit

Permalink
Test factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhamilton committed Dec 30, 2024
1 parent cabc062 commit 5fc5d91
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.single
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
import dev.drewhamilton.poko.test.isSynthetic
import dev.drewhamilton.poko.test.name
import dev.drewhamilton.poko.test.parameters
import dev.drewhamilton.poko.test.returnType
import dev.drewhamilton.poko.test.type
Expand Down Expand Up @@ -190,6 +193,7 @@ class PokoCompilerPluginTest(
}
}

//region builder
@Test fun `no builder annotation generates no builder class`() {
testCompilation("api/Primitives") { result ->
assertThat(result.messages)
Expand Down Expand Up @@ -258,6 +262,36 @@ class PokoCompilerPluginTest(
}
}

@Test fun `builder annotation generates factory function`() {
assumeTrue(k2) // FIR only works in K2

testCompilation(
"api/Buildable",
"dev/drewhamilton/poko/PokoBuilder",
) { result ->
assertThat(result.messages).all {
contains("Buildable.kt:5:1")
contains("The Poko Builder feature is incomplete, experimental, and private; your generated builder will not work")
}

val factoryParentClass = result.classLoader.tryLoadClass("api.__GENERATED_DECLARATIONS__Kt")!!
assertAll {
val methods = factoryParentClass.methods
assertThat(methods.filter { it.name == "Buildable" }).all {
single().all {
isSynthetic().isTrue()
parameters().all {
hasSize(1)
index(0).type().isEqualTo(Function1::class.java)
}
returnType().name().isEqualTo("api.Buildable")
}
}
}
}
}
//endregion

private inline fun testCompilation(
vararg sourceFileNames: String,
pokoAnnotationName: String = "dev/drewhamilton/poko/Poko",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import assertk.assertions.prop
import java.lang.reflect.Method
import java.lang.reflect.Parameter

internal fun Assert<Class<*>>.name() = prop("name") { it.name }

internal fun Assert<Method>.isSynthetic() = prop("isSynthetic") { it.isSynthetic }

internal fun Assert<Method>.parameters() = prop("parameters") { it.parameters }

internal fun Assert<Method>.returnType() = prop("returnType") { it.returnType }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ class SampleBuilderTest {
.isNotNull()
.contains("is null")
}

@Test fun `factory function succeeds`() {
val sample = Sample {
int = 100
requiredString = "required"
optionalString = null
}

assertThat(sample).isEqualTo(
expected = Sample(
int = 100,
requiredString = "required",
optionalString = null,
)
)
}
}

0 comments on commit 5fc5d91

Please sign in to comment.