Skip to content

Commit

Permalink
Merge branch 'sothawo-2.x' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Sep 30, 2018
2 parents 664ddd3 + 2a51e2f commit a67fb80
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package com.nhaarman.mockitokotlin2

import com.nhaarman.mockitokotlin2.internal.createInstance
import org.mockito.ArgumentMatcher
import org.mockito.Mockito

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

/**
* Registers a custom ArgumentMatcher. The original Mockito function registers the matcher and returns null,
* here the required type is returned.
*
* @param matcher The ArgumentMatcher on [T] to be registered.
*/
inline fun <reified T : Any> argThat(matcher: ArgumentMatcher<T>): T {
return Mockito.argThat(matcher) ?: createInstance()
}

/**
* Alias for [argThat].
*
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface Methods {
fun stringResult(s: String): String
fun nullableStringResult(): String?
fun builderMethod(): Methods
fun varargBooleanResult(vararg values: String): Boolean

fun nonDefaultReturnType(): ExtraInterface
}
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 @@ -4,6 +4,10 @@ import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import com.nhaarman.mockitokotlin2.*
import org.junit.Test
import org.mockito.ArgumentMatcher
import org.mockito.internal.matchers.VarargMatcher
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import java.io.IOException

class MatchersTest : TestBase() {
Expand Down Expand Up @@ -195,4 +199,51 @@ class MatchersTest : TestBase() {
verify(this).nullableString(same(null))
}
}

@Test
fun testVarargAnySuccess() {
/* Given */
val t = mock<Methods>()
// a matcher to check if any of the varargs was equals to "b"
val matcher = VarargAnyMatcher<String, Boolean>({ "b" == it }, true, false)

/* When */
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)

/* Then */
expect(t.varargBooleanResult("a", "b", "c")).toBe(true)
}

@Test
fun testVarargAnyFail() {
/* Given */
val t = mock<Methods>()
// a matcher to check if any of the varargs was equals to "d"
val matcher = VarargAnyMatcher<String, Boolean>({ "d" == it }, true, false)

/* When */
whenever(t.varargBooleanResult(argThat(matcher))).thenAnswer(matcher)

/* Then */
expect(t.varargBooleanResult("a", "b", "c")).toBe(false)
}

/**
* 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.
*/
private class VarargAnyMatcher<T, R>(
private val match: ((T) -> Boolean),
private val success: R,
private val failure: R
) : ArgumentMatcher<T>, VarargMatcher, Answer<R> {
private var anyMatched = false

override fun matches(t: T): Boolean {
anyMatched = anyMatched or match(t)
return true
}

override fun answer(i: InvocationOnMock) = if (anyMatched) success else failure
}
}

0 comments on commit a67fb80

Please sign in to comment.