Skip to content

Commit

Permalink
#474 Fix vararg argument matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Aug 6, 2023
1 parent d92b616 commit 8fa7197
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,18 @@ class KArgumentCaptor<out T : Any?>(

@Suppress("UNCHECKED_CAST")
fun capture(): T {
return captor.capture() ?: createInstance(tClass) as T
// Special handling for arrays to make it work for varargs
// In Kotlin we want have to capture vararg like this `verify(m).methodName(*captor.capture())` to make the types work
// If we return null for array types, the spread `*` operator will fail with NPE
// If we return empty array, it will fail in MatchersBinder.validateMatchers
// In Java, `captor.capture` returns null and so the method is called with `[null]`
// In Kotlin, we have to create `[null]` explicitly.
// This code-path is applied for non-vararg array arguments as well, but it seems to work fine.
return if (tClass.java.isArray) {
captor.capture() ?: java.lang.reflect.Array.newInstance(tClass.java.componentType, 1) as T
} else {
captor.capture() ?: createInstance(tClass) as T
}
}
}

Expand Down
70 changes: 70 additions & 0 deletions tests/src/test/kotlin/test/ArgumentCaptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,74 @@ class ArgumentCaptorTest : TestBase() {
}
}
}

@Test
fun argumentCaptor_vararg() {
/* Given */
val m: Methods = mock()

/* When */
m.varargBooleanResult("a", "b", "c")

/* Then */
val captor = argumentCaptor<Array<String>>()
verify(m).varargBooleanResult(*captor.capture())
expect(captor.firstValue.toList()).toBe(listOf("a", "b", "c"))
}

@Test
fun argumentCaptor_empty_vararg() {
/* Given */
val m: Methods = mock()

/* When */
m.varargBooleanResult()

/* Then */
val captor = argumentCaptor<Array<String>>()
verify(m).varargBooleanResult(*captor.capture())
expect(captor.firstValue.toList()).toBe(listOf())
}

@Test
fun argumentCaptor_arg_vararg() {
/* Given */
val m: Methods = mock()

/* When */
m.argAndVararg("first", "a", "b", "c")

/* Then */
val captor = argumentCaptor<Array<String>>()
verify(m).argAndVararg(any(), *captor.capture())
expect(captor.firstValue.toList()).toBe(listOf("a", "b", "c"))
}

@Test
fun argumentCaptor_array() {
/* Given */
val m: Methods = mock()

/* When */
m.stringArray(arrayOf("a", "b", "c"))

/* Then */
val captor = argumentCaptor<Array<String>>()
verify(m).stringArray(captor.capture())
expect(captor.firstValue.toList()).toBe(listOf("a", "b", "c"))
}

@Test
fun argumentCaptor_empty_array() {
/* Given */
val m: Methods = mock()

/* When */
m.stringArray(arrayOf())

/* Then */
val captor = argumentCaptor<Array<String>>()
verify(m).stringArray(captor.capture())
expect(captor.firstValue.toList()).toBe(listOf())
}
}
2 changes: 2 additions & 0 deletions tests/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ interface Methods {
fun nullableStringResult(): String?
fun builderMethod(): Methods
fun varargBooleanResult(vararg values: String): Boolean
fun stringArray(a: Array<String>)
fun argAndVararg(s: String, vararg a: String)

fun nonDefaultReturnType(): ExtraInterface
}
Expand Down

0 comments on commit 8fa7197

Please sign in to comment.