Skip to content

Commit

Permalink
Add generating private primary constructor in Kotlin (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinia91 authored Dec 18, 2023
1 parent 2c9a154 commit 197b7f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.isAccessible

@API(since = "0.4.0", status = MAINTAINED)
class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector {
Expand All @@ -54,6 +55,7 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector {
val constructor = CONSTRUCTOR_CACHE.computeIfAbsent(type) {
requireNotNull(kotlinClass.primaryConstructor) { "No kotlin primary constructor provided for $kotlinClass" }
}
constructor.isAccessible = true

val arbitrariesByPropertyName = it.mapKeys { map -> map.key.objectProperty.property.name }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.navercorp.fixturemonkey.api.instantiator.Instantiator
import com.navercorp.fixturemonkey.api.instantiator.Instantiator.constructor
import com.navercorp.fixturemonkey.kotlin.KotlinPlugin
import com.navercorp.fixturemonkey.kotlin.giveMeBuilder
import com.navercorp.fixturemonkey.kotlin.giveMeOne
import com.navercorp.fixturemonkey.kotlin.instantiator.instantiateBy
import com.navercorp.fixturemonkey.tests.TestEnvironment.TEST_COUNT
import org.assertj.core.api.BDDAssertions.then
Expand Down Expand Up @@ -446,6 +447,16 @@ class InstantiatorTest {
then(actual).isNotNull()
}

@RepeatedTest(TEST_COUNT)
fun instantiatePrivateConstructorWithoutInstantiate() {
class PrivateConstructorObject private constructor(val value: String)

val actual = SUT.giveMeOne<PrivateConstructorObject>()
.value

then(actual).isNotNull()
}

@RepeatedTest(TEST_COUNT)
fun instantiateDefaultArgumentConstructor() {
class ConstructorObject(val value: String = "default")
Expand Down

0 comments on commit 197b7f5

Please sign in to comment.