Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't count optional parameters when trying to find an easy constructor #101

Merged
merged 1 commit into from
Oct 26, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Modifier
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KType
import kotlin.reflect.defaultType
import kotlin.reflect.*
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.jvmName
Expand Down Expand Up @@ -84,12 +81,12 @@ fun <T : Any> createInstance(kClass: KClass<T>): T {
*/
private fun <T : Any> KClass<T>.easiestConstructor(): KFunction<T> {
return constructors
.sortedBy { it.parameters.size }
.sortedBy { it.parameters.withoutOptionalParameters().size }
.withoutParametersOfType(this.defaultType)
.withoutArrayParameters()
.firstOrNull() ?: constructors.sortedBy { it.parameters.size }
.withoutParametersOfType(this.defaultType)
.first()
.firstOrNull() ?: constructors.sortedBy { it.parameters.withoutOptionalParameters().size }
.withoutParametersOfType(this.defaultType)
.first()
}

private fun <T> List<KFunction<T>>.withoutArrayParameters() = filter {
Expand All @@ -101,10 +98,12 @@ private fun <T> List<KFunction<T>>.withoutArrayParameters() = filter {
* This is especially useful to avoid infinite loops where constructors
* accepting a parameter of their own type, e.g. 'copy constructors'.
*/
private fun <T: Any> List<KFunction<T>>.withoutParametersOfType(type: KType) = filter {
private fun <T : Any> List<KFunction<T>>.withoutParametersOfType(type: KType) = filter {
it.parameters.filter { it.type == type }.isEmpty()
}

private fun List<KParameter>.withoutOptionalParameters() = filterNot { it.isOptional }

@Suppress("SENSELESS_COMPARISON")
private fun KClass<*>.hasObjectInstance() = objectInstance != null

Expand Down Expand Up @@ -166,7 +165,7 @@ private fun <T : Any> KClass<T>.toClassObject(): T {
private fun <T : Any> KFunction<T>.newInstance(): T {
try {
isAccessible = true
return callBy(parameters.associate {
return callBy(parameters.withoutOptionalParameters().associate {
it to it.type.createNullableInstance<T>()
})
} catch(e: InvocationTargetException) {
Expand Down
32 changes: 31 additions & 1 deletion mockito-kotlin/src/test/kotlin/CreateInstanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,25 @@ class CreateInstanceTest {
expect(result).toNotBeNull()
}

@Test
fun optionalParametersAreSkippedWhenSorting() {
/* When */
val result = createInstance(WithDefaultParameters::class)

/* Then */
expect(result).toNotBeNull()
}

@Test
fun defaultValuesAreUsedWithOptionalParameters() {
/* When */
val result = createInstance(WithDefaultParameters::class)

/* Then */
expect(result.first).toBe(1)
expect(result.second).toBe(2)
}

private class PrivateClass private constructor(val data: String)

class ClosedClass
Expand Down Expand Up @@ -503,7 +522,18 @@ class CreateInstanceTest {
*/
data class WithCopyConstructor private constructor(val x: String,
val y: String) {
constructor(other: WithCopyConstructor): this(other.x, other.y)
constructor(other: WithCopyConstructor) : this(other.x, other.y)
}

/**
* A class that uses default parameters, but with a constructor without parameters that fails.
* This is to make sure default parameters are not counted when sorting by parameter size.
*/
class WithDefaultParameters constructor(val first: Int = 1, val second: Int = 2) {

constructor(first: Int) : this() {
error("Should not be called")
}
}

enum class MyEnum { VALUE, ANOTHER_VALUE }
Expand Down