Skip to content

Commit

Permalink
fixes issue with InvocationAnswer.hashCode()
Browse files Browse the repository at this point in the history
  • Loading branch information
PHaroZ committed May 20, 2022
1 parent a8d23ed commit 3de3ad7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 5 additions & 9 deletions mockk/common/src/main/kotlin/io/mockk/impl/stub/MockKStub.kt
Expand Up @@ -18,7 +18,6 @@ open class MockKStub(
val log = gatewayAccess.safeToString(Logger<MockKStub>())

private val answers = InternalPlatform.synchronizedMutableList<InvocationAnswer>()
private val answerUsages = InternalPlatform.synchronizedMutableMap<InvocationAnswer, Int>()
private val childs = InternalPlatform.synchronizedMutableMap<InvocationMatcher, Any>()
private val recordedCalls = InternalPlatform.synchronizedMutableList<Invocation>()
private val recordedCallsByMethod =
Expand All @@ -31,17 +30,15 @@ open class MockKStub(
var disposeRoutine: () -> Unit = {}

override fun addAnswer(matcher: InvocationMatcher, answer: Answer<*>) {
val invocationAnswer = InvocationAnswer(matcher, answer)
val invocationAnswer = InvocationAnswer(matcher, answer, 0)
answers.add(invocationAnswer)
answerUsages[invocationAnswer] = 0
}

override fun answer(invocation: Invocation): Any? {
val invocationAndMatcher = InternalPlatform.synchronized(answers) {
answers
.reversed()
.firstOrNull { it.matcher.match(invocation) }
?.also { answerUsages[it] = answerUsages[it]!! + 1 }
.findLast { it.matcher.match(invocation) }
?.also { it.usageCount++ }
} ?: return defaultAnswer(invocation)

return with(invocationAndMatcher) {
Expand Down Expand Up @@ -192,7 +189,7 @@ open class MockKStub(

override fun matcherUsages(): Map<InvocationMatcher, Int> =
InternalPlatform.synchronized(answers) {
answerUsages.mapKeys { it.key.matcher }
answers.associate { it.matcher to it.usageCount }
}

override fun toStr() = "${type.simpleName}($name)"
Expand Down Expand Up @@ -277,7 +274,6 @@ open class MockKStub(
override fun clear(options: MockKGateway.ClearOptions) {
if (options.answers) {
this.answers.clear()
this.answerUsages.clear()
}
if (options.recordedCalls) {
this.recordedCalls.clear()
Expand All @@ -298,7 +294,7 @@ open class MockKStub(
val childOfRegex = Regex("child(\\^(\\d+))? of (.+)")
}

private data class InvocationAnswer(val matcher: InvocationMatcher, val answer: Answer<*>)
private data class InvocationAnswer(val matcher: InvocationMatcher, val answer: Answer<*>, var usageCount: Int)

protected fun Invocation.allEqMatcher() =
InvocationMatcher(
Expand Down
Expand Up @@ -75,6 +75,7 @@ class VerificationAcknowledgeTest {

@Test
fun `checkUnnecessaryStubAll with single call to each stub`() {
clearAllMocks() // ensure that previous tests haven't created unnecessary stubs
doCalls1()
val mock2 = mockk<MockCls>()
every { mock2.op(98) } returns 1
Expand All @@ -84,6 +85,7 @@ class VerificationAcknowledgeTest {

@Test
fun `checkUnnecessaryStubAll with some used and some unused stubs`() {
clearAllMocks() // ensure that previous tests haven't created unnecessary stubs
doCalls1()
val mock2 = mockk<MockCls> { every { op(42) } returns 1 }
assertFailsWith<AssertionError> {
Expand Down

0 comments on commit 3de3ad7

Please sign in to comment.