Skip to content
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
6 changes: 3 additions & 3 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import org.mockito.kotlin.internal.valueClassInnerClass

/** Matches an argument that is equal to the given value. */
inline fun <reified T : Any?> eq(value: T): T {
if (T::class.isValue) return eqValueClass(value)
if (value != null && T::class.isValue) return eqValueClass(value)

return ArgumentMatchers.eq(value) ?: value
}
Expand Down Expand Up @@ -87,10 +87,10 @@ inline fun <reified T> anyValueClass(): T {
}

/** Matches an argument that is equal to the given Kotlin value class value. */
inline fun <reified T> eqValueClass(value: T): T {
inline fun <reified T : Any> eqValueClass(value: T): T {
require(value::class.isValue) { "${value::class.qualifiedName} is not a value class." }

val unboxed = value?.unboxValueClass()
val unboxed = value.unboxValueClass()
val matcher = AdditionalMatchers.or(ArgumentMatchers.eq(value), ArgumentMatchers.eq(unboxed))

return (matcher ?: unboxed).toKotlinType(T::class)
Expand Down
18 changes: 18 additions & 0 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,24 @@ class MatchersTest : TestBase() {
verify(this).nestedValueClass(eq(nestedValueClass))
}
}

@Test
fun eqNullableValueClass_nullValue() {
val valueClass = null as ValueClass?
mock<SynchronousFunctions>().apply {
nullableValueClass(valueClass)
verify(this).nullableValueClass(eq(valueClass))
}
}

@Test
fun eqNullableLongValueClass_nullValue() {
val longValueClass = null as LongValueClass?
mock<SynchronousFunctions>().apply {
nullableLongValueClass(longValueClass)
verify(this).nullableLongValueClass(eq(longValueClass))
}
}
}

class OtherMatchersTest {
Expand Down