Skip to content

Commit a67fb80

Browse files
committed
Merge branch 'sothawo-2.x' into 2.x
2 parents 664ddd3 + 2a51e2f commit a67fb80

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockitokotlin2/Matchers.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.nhaarman.mockitokotlin2
2727

2828
import com.nhaarman.mockitokotlin2.internal.createInstance
29+
import org.mockito.ArgumentMatcher
2930
import org.mockito.Mockito
3031

3132
/** Object argument that is equal to the given value. */
@@ -70,6 +71,16 @@ inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean): T {
7071
)
7172
}
7273

74+
/**
75+
* Registers a custom ArgumentMatcher. The original Mockito function registers the matcher and returns null,
76+
* here the required type is returned.
77+
*
78+
* @param matcher The ArgumentMatcher on [T] to be registered.
79+
*/
80+
inline fun <reified T : Any> argThat(matcher: ArgumentMatcher<T>): T {
81+
return Mockito.argThat(matcher) ?: createInstance()
82+
}
83+
7384
/**
7485
* Alias for [argThat].
7586
*

tests/src/test/kotlin/test/Classes.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ interface Methods {
6262
fun stringResult(s: String): String
6363
fun nullableStringResult(): String?
6464
fun builderMethod(): Methods
65+
fun varargBooleanResult(vararg values: String): Boolean
6566

6667
fun nonDefaultReturnType(): ExtraInterface
6768
}

tests/src/test/kotlin/test/MatchersTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import com.nhaarman.expect.expect
44
import com.nhaarman.expect.expectErrorWithMessage
55
import com.nhaarman.mockitokotlin2.*
66
import org.junit.Test
7+
import org.mockito.ArgumentMatcher
8+
import org.mockito.internal.matchers.VarargMatcher
9+
import org.mockito.invocation.InvocationOnMock
10+
import org.mockito.stubbing.Answer
711
import java.io.IOException
812

913
class MatchersTest : TestBase() {
@@ -195,4 +199,51 @@ class MatchersTest : TestBase() {
195199
verify(this).nullableString(same(null))
196200
}
197201
}
202+
203+
@Test
204+
fun testVarargAnySuccess() {
205+
/* Given */
206+
val t = mock<Methods>()
207+
// a matcher to check if any of the varargs was equals to "b"
208+
val matcher = VarargAnyMatcher<String, Boolean>({ "b" == it }, true, false)
209+
210+
/* When */
211+
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
212+
213+
/* Then */
214+
expect(t.varargBooleanResult("a", "b", "c")).toBe(true)
215+
}
216+
217+
@Test
218+
fun testVarargAnyFail() {
219+
/* Given */
220+
val t = mock<Methods>()
221+
// a matcher to check if any of the varargs was equals to "d"
222+
val matcher = VarargAnyMatcher<String, Boolean>({ "d" == it }, true, false)
223+
224+
/* When */
225+
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)
226+
227+
/* Then */
228+
expect(t.varargBooleanResult("a", "b", "c")).toBe(false)
229+
}
230+
231+
/**
232+
* a VarargMatcher implementation for varargs of type [T] that will answer with type [R] if any of the var args
233+
* matched. Needs to keep state between matching invocations.
234+
*/
235+
private class VarargAnyMatcher<T, R>(
236+
private val match: ((T) -> Boolean),
237+
private val success: R,
238+
private val failure: R
239+
) : ArgumentMatcher<T>, VarargMatcher, Answer<R> {
240+
private var anyMatched = false
241+
242+
override fun matches(t: T): Boolean {
243+
anyMatched = anyMatched or match(t)
244+
return true
245+
}
246+
247+
override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure
248+
}
198249
}

0 commit comments

Comments
 (0)