Skip to content

Commit

Permalink
Add value class support (#522)
Browse files Browse the repository at this point in the history
Co-authored-by: Lars Andringa <l.s.andringa@rug.nl>
  • Loading branch information
LarsSven and Lars Andringa committed Jul 9, 2024
1 parent 6124e10 commit 888e19d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
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.12.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

0 comments on commit 888e19d

Please sign in to comment.