Skip to content

Commit

Permalink
Merge pull request #687 from AlexKrupa/fix/issue-565-collection-chain…
Browse files Browse the repository at this point in the history
…-stubbing

Fix stubbing by chained calls on collections (#565)
  • Loading branch information
Raibaz committed Aug 31, 2021
2 parents d2ef53e + 86e18f3 commit 613c3b2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
Expand Up @@ -62,16 +62,16 @@ abstract class RecordingState(recorder: CommonCallRecorder) : CallRecordingState
override fun call(invocation: Invocation): Any? {
val retType = recorder.childHinter.nextChildType { invocation.method.returnType }
var isTemporaryMock = false

val retValue =
if (invocation.method.isToString()) {
recorder.stubRepo[invocation.self]?.toStr() ?: ""
} else {
recorder.anyValueGenerator().anyValue(retType, invocation.method.returnTypeNullable) {
isTemporaryMock = true
recorder.mockFactory.temporaryMock(retType)
}
}
val temporaryMock: () -> Any = {
isTemporaryMock = true
recorder.mockFactory.temporaryMock(retType)
}
val retValue = when {
invocation.method.isToString() -> recorder.stubRepo[invocation.self]?.toStr() ?: ""
retType in CollectionTypes -> temporaryMock()
else -> recorder.anyValueGenerator()
.anyValue(retType, invocation.method.returnTypeNullable, orInstantiateVia = temporaryMock)
}

if (retValue == null) {
isTemporaryMock = false
Expand Down Expand Up @@ -191,4 +191,14 @@ abstract class RecordingState(recorder: CommonCallRecorder) : CallRecordingState
private fun builder(): CallRoundBuilder = callRoundBuilder
?: throw MockKException("Call builder is not initialized. Bad state")

private companion object {
private val CollectionTypes = listOf(
List::class,
Map::class,
Set::class,
ArrayList::class,
HashMap::class,
HashSet::class
)
}
}
@@ -0,0 +1,68 @@
package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

// Regression tests for GitHub issue #565.
class CollectionChainStubbingTest {

private interface ReturningCollections {
val list: List<Int>
val map: Map<String, Int>
val set: Set<Int>
val arrayList: ArrayList<Int>
val hashMap: Map<String, Int>
val hashSet: HashSet<Int>
}

@Test
fun list() {
val mock = mockk<ReturningCollections>()
every { mock.list[0] } returns 1

assertEquals(1, mock.list[0])
}

@Test
fun map() {
val mock = mockk<ReturningCollections>()
every { mock.map["foo"] } returns 1

assertEquals(1, mock.map["foo"])
}

@Test
fun set() {
val mock = mockk<ReturningCollections>()
every { mock.set.contains(1) } returns true

assertTrue(mock.set.contains(1))
}

@Test
fun arrayList() {
val mock = mockk<ReturningCollections>()
every { mock.arrayList[0] } returns 1

assertEquals(1, mock.arrayList[0])
}

@Test
fun hashMap() {
val mock = mockk<ReturningCollections>()
every { mock.hashMap["foo"] } returns 1

assertEquals(1, mock.hashMap["foo"])
}

@Test
fun hashSet() {
val mock = mockk<ReturningCollections>()
every { mock.hashSet.contains(0) } returns true

assertTrue(mock.hashSet.contains(0))
}
}

0 comments on commit 613c3b2

Please sign in to comment.