Skip to content

Commit 2a51e2f

Browse files
committed
Restructure tests
1 parent 78e3ec1 commit 2a51e2f

File tree

3 files changed

+52
-68
lines changed

3 files changed

+52
-68
lines changed

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
}

tests/src/test/kotlin/test/VarArgMatcherTest.kt

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)