Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Two bugs related to Issue 8 #11

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/main/kotlin/com/tyro/oss/arbitrater/InstanceCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ private val wildcardNullableEnumType = Enum::class.createType(arguments = listOf
class InstanceCreator<out T : Any>(private val targetClass: KClass<T>, settings: GeneratorSettings = GeneratorSettings())
: ConfigurableArbitrater(settings, DefaultConfiguration.generators.toMutableMap()) {

private constructor(targetClass: KClass<T>, specificValues: MutableMap<KParameter, Any?>, settings: GeneratorSettings) :this(targetClass, settings) {
this.specificValues.putAll(specificValues)
}

private val specificValues: MutableMap<KParameter, Any?> = mutableMapOf()

fun generateNulls(value: Boolean = true): InstanceCreator<T> = InstanceCreator(targetClass, settings.copy(generateNulls = value))
fun generateNulls(value: Boolean = true): InstanceCreator<T> = InstanceCreator(targetClass, specificValues, settings.copy(generateNulls = value))

fun useDefaultValues(value: Boolean = false): InstanceCreator<T> = InstanceCreator(targetClass, settings.copy(useDefaultValues = value))
fun useDefaultValues(value: Boolean = false): InstanceCreator<T> = InstanceCreator(targetClass, specificValues, settings.copy(useDefaultValues = value))

fun withValue(parameterName: String, value: Any?): InstanceCreator<T> {
targetClass.primaryConstructor?.parameters?.find { it.name == parameterName }?.let {
specificValues[it] = value
} ?: throw IllegalArgumentException("Parameter named $parameterName not found in primary constructor of ${targetClass.simpleName}")

return this
return InstanceCreator(targetClass, specificValues, settings)
}

/**
Expand All @@ -66,10 +70,9 @@ class InstanceCreator<out T : Any>(private val targetClass: KClass<T>, settings:
val primaryConstructor = targetClass.primaryConstructor!!

val constructorArguments = primaryConstructor
.parameters
.filterNot { it.isOptional && settings.useDefaultValues }
.map { it to (if(specificValues.containsKey(it)) specificValues[it] else it.type.randomValue()) }
.toMap()
.parameters
.filterNot { it.isOptional && settings.useDefaultValues && !specificValues.containsKey(it) }
.associateWith { (if (specificValues.containsKey(it)) specificValues[it] else it.type.randomValue()) }

return primaryConstructor.callBy(constructorArguments)
} catch (e: Exception) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/kotlin/com/tyro/oss/arbitrater/InstanceCreatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,40 @@ class InstanceCreatorTest {
val instance = ClassWithoutConstructorProperty::class.arbitrater().withValue("name", "Boy").createInstance()
instance.bigName shouldBe "Big Boy"
}

@Test
fun `withValue should override default value when useDefaults is true`() {
// https://github.com/tyro/arbitrater/issues/8
val instance = DefaultValue::class.arbitrater()
.useDefaultValues(true)
.withValue("int", 11)
.createInstance()

instance.int shouldBe 11
}

@Test
fun `withValue should override default value when useDefaults is false and called BEFORE withValue`() {
// https://github.com/tyro/arbitrater/issues/8
val instance = DefaultValue::class.arbitrater()
.useDefaultValues(false)
.withValue("int", 11)
.createInstance()

instance.int shouldBe 11
}

@Test
fun `withValue should override default value when useDefaults is false and called AFTER withValue`() {
// https://github.com/tyro/arbitrater/issues/8
val instance = DefaultValue::class.arbitrater()
.withValue("int", 11)
.useDefaultValues(false)
.createInstance()

instance.int shouldBe 11
}

}

class TestClass(val property1: String?, val property2: String)
Expand Down