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 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
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
18 changes: 18 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 @@ -42,6 +42,9 @@ 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()
}

Expand Down Expand Up @@ -71,6 +74,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
9 changes: 9 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,17 @@ interface Methods {
fun argAndVararg(s: String, vararg a: String)

fun nonDefaultReturnType(): ExtraInterface

fun valueClass(v: ValueClass?)
fun nestedValueClass(v: NestedValueClass)
}

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

@JvmInline
value class NestedValueClass(val value: ValueClass)

interface ExtraInterface

abstract class ThrowingConstructor {
Expand Down
51 changes: 51 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,57 @@ class MatchersTest : TestBase() {
}
}

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

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

@Test
fun anyOrNull_forValueClass_withNull() {
mock<Methods>().apply {
valueClass(null)
verify(this).valueClass(anyOrNull())
}
}

@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)
// Should throw an error because Float is not a value class
float(anyValueClass())
}
}
}

@Test
fun anyValueClass_withNestedValueClass() {
mock<Methods>().apply {
nestedValueClass(NestedValueClass(ValueClass("Content")))
verify(this).nestedValueClass(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