Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stubbing by chained calls on collections (#565) #687

Merged
merged 1 commit into from Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
}
}