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
28 changes: 23 additions & 5 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import org.mockito.kotlin.internal.createInstance
import kotlin.reflect.KClass

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

return ArgumentMatchers.eq(value) ?: value
}

Expand Down Expand Up @@ -80,15 +83,30 @@ inline fun <reified T > anyValueClass(): T {
"${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 boxImpl =
T::class.java.declaredMethods
.single { it.name == "box-impl" && it.parameterCount == 1 }
val boxedType = boxImpl.parameters[0].type

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

inline fun <reified T > eqValueClass(value: T): T {
require(T::class.isValue) {
"${T::class.qualifiedName} is not a value class."
}

val unboxImpl =
T::class.java.declaredMethods
.single { it.name == "unbox-impl" && it.parameterCount == 0 }
val unboxed = unboxImpl.invoke(value)

val boxImpl =
T::class.java.declaredMethods.single { it.name == "box-impl" && it.parameterCount == 1 }

return boxImpl.invoke(null, ArgumentMatchers.eq(unboxed) ?: unboxed) as T
}

/**
* Creates a custom argument matcher.
* `null` values will never evaluate to `true`.
Expand Down
12 changes: 12 additions & 0 deletions tests/src/test/kotlin/test/EqTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ class EqTest : TestBase() {
expect(result).toBeNull()
}

@Test
fun eqValueClassInstance() {
/* Given */
val valueClass = ValueClass("Content")

/* When */
val result = eq(valueClass)

/* Then */
expect(result).toBe(valueClass)
}

private interface MyInterface
private open class MyClass : MyInterface
class ClosedClass
Expand Down
35 changes: 35 additions & 0 deletions tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,41 @@ class MatchersTest : TestBase() {
}
}

@Test
fun eq_forValueClass() {
val valueClass = ValueClass("Content")
mock<Methods>().apply {
valueClass(valueClass)
verify(this).valueClass(eq(valueClass))
}
}

@Test
fun eq_withNestedValueClass() {
val nestedValueClass = NestedValueClass(ValueClass("Content"))
mock<Methods>().apply {
nestedValueClass(nestedValueClass)
verify(this).nestedValueClass(eq(nestedValueClass))
}
}

@Test
fun eq_withClosedClass() {
val closedClassInstance = Closed()
mock<Methods>().apply {
closed(closedClassInstance)
verify(this).closed(eq(closedClassInstance))
}
}

@Test
fun eq_withInt() {
mock<Methods>().apply {
int(3)
verify(this).int(eq(3))
}
}

/**
* 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