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

Added value class support #522

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ repositories {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib"
compileOnly 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.9.20"

api "org.mockito:mockito-core:5.7.0"

Expand Down
22 changes: 22 additions & 0 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.mockito.ArgumentMatcher
import org.mockito.ArgumentMatchers
import org.mockito.kotlin.internal.createInstance
import kotlin.reflect.KClass
import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType.Object
LarsSven marked this conversation as resolved.
Show resolved Hide resolved

/** Object argument that is equal to the given value. */
fun <T> eq(value: T): T {
Expand All @@ -42,11 +43,17 @@ fun <T> same(value: T): T {

/** Matches any object, excluding nulls. */
inline fun <reified T : Any> any(): T {
if(T::class.isValue)
return anyValueClass()

return ArgumentMatchers.any(T::class.java) ?: createInstance()
}

/** Matches anything, including nulls. */
inline fun <reified T : Any> anyOrNull(): T {
if(T::class.isValue)
return anyValueClass()

return ArgumentMatchers.any<T>() ?: createInstance()
}

Expand All @@ -71,6 +78,21 @@ inline fun <reified T : Any?> anyArray(): Array<T> {
return ArgumentMatchers.any(Array<T>::class.java) ?: arrayOf()
}

/** Matches any Kotlin value class with the same boxed type by taking its boxed type. */
inline fun <reified T > anyValueClass(): T {
require(T::class.isValue) {
"${T::class.qualifiedName} is not a value class."
}

val boxImpls = T::class.java.declaredMethods.filter { it.name == "box-impl" && it.parameterCount == 1 }
require(boxImpls.size == 1) // Sanity check

val boxImpl = boxImpls.first()
val boxedType = boxImpl.parameters[0].type

return boxImpl.invoke(null, ArgumentMatchers.any(boxedType)) as T
}

/**
* Creates a custom argument matcher.
* `null` values will never evaluate to `true`.
Expand Down
5 changes: 5 additions & 0 deletions tests/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ interface Methods {
fun argAndVararg(s: String, vararg a: String)

fun nonDefaultReturnType(): ExtraInterface

fun valueClass(v: ValueClass)
}

@JvmInline
value class ValueClass(private val content: String)

interface ExtraInterface

abstract class ThrowingConstructor {
Expand Down
26 changes: 26 additions & 0 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,32 @@ class MatchersTest : TestBase() {
}
}

@Test
fun any_forValueClass() {
mock<Methods>().apply {
valueClass(ValueClass("Content"))
verify(this).valueClass(any())
}
}

@Test
fun anyValueClass_withValueClass() {
mock<Methods>().apply {
valueClass(ValueClass("Content"))
verify(this).valueClass(anyValueClass())
}
}

@Test
fun anyValueClass_withNonValueClass() {
expectErrorWithMessage("kotlin.Float is not a value class.") on {
mock<Methods>().apply {
float(10f)
verify(this).float(anyValueClass())
}
}
}

/**
* a VarargMatcher implementation for varargs of type [T] that will answer with type [R] if any of the var args
* matched. Needs to keep state between matching invocations.
Expand Down
Loading