Skip to content

Commit

Permalink
- fixing capture nullable
Browse files Browse the repository at this point in the history
 - remove synchronized warning
  • Loading branch information
oleksiyp committed Apr 19, 2020
1 parent 449a527 commit e79f1fb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 26 deletions.
4 changes: 2 additions & 2 deletions dsl/common/src/main/kotlin/io/mockk/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ open class MockKMatcherScope(
inline fun <reified T : Any> any(): T = match(ConstantMatcher(true))
inline fun <reified T : Any> capture(lst: MutableList<T>): T = match(CaptureMatcher(lst, T::class))
inline fun <reified T : Any> capture(lst: CapturingSlot<T>): T = match(CapturingSlotMatcher(lst, T::class))
inline fun <reified T : Any> captureNullable(lst: MutableList<T?>): T = match(CaptureNullableMatcher(lst, T::class))
inline fun <reified T : Any> captureNullable(lst: MutableList<T?>): T? = match(CaptureNullableMatcher(lst, T::class))
inline fun <reified T : Comparable<T>> cmpEq(value: T): T = match(ComparingMatcher(value, 0, T::class))
inline fun <reified T : Comparable<T>> more(value: T, andEquals: Boolean = false): T =
match(ComparingMatcher(value, if (andEquals) 2 else 1, T::class))
Expand Down Expand Up @@ -3763,7 +3763,7 @@ data class InvocationMatcher(
}
}

for (i in 0 until invocation.args.size) {
for (i in invocation.args.indices) {
val matcher = args[i]
val arg = invocation.args[i]

Expand Down
12 changes: 10 additions & 2 deletions dsl/common/src/main/kotlin/io/mockk/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ data class CaptureNullableMatcher<T : Any>(
val captureList: MutableList<T?>,
override val argumentType: KClass<*>
) : Matcher<T>, CapturingMatcher, TypedMatcher, EquivalentMatcher {
override fun equivalent(): Matcher<Any> = ConstantMatcher<Any>(true)
override fun equivalent(): Matcher<Any> = ConstantMatcher(true)

@Suppress("UNCHECKED_CAST")
override fun capture(arg: Any?) {
Expand All @@ -114,7 +114,15 @@ data class CaptureNullableMatcher<T : Any>(

override fun match(arg: T?): Boolean = true

override fun toString(): String = "captureNullable<${argumentType.simpleName}>()"
override fun checkType(arg: Any?): Boolean {
if(arg == null) {
return true
}

return super.checkType(arg)
}

override fun toString(): String = "capture<${argumentType.simpleName}?>()"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ expect object InternalPlatform {

fun weakRef(value: Any): WeakRef

fun multiNotifier() : MultiNotifier
fun multiNotifier(): MultiNotifier

inline fun <T> synchronized(obj: Any, block: () -> T): T
}

interface Ref {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CommonInstanceFactoryRegistry : InstanceFactoryRegistry {
private val factories = InternalPlatform.synchronizedMutableList<InstanceFactory>()

val instanceFactories: List<InstanceFactory>
get() = synchronized(factories) {
get() = InternalPlatform.synchronized(factories) {
factories.toList()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.mockk.impl.stub

import io.mockk.*
import io.mockk.impl.InternalPlatform

class AnswerAnsweringOpportunity<T>(
private val matcherStr: () -> String
Expand All @@ -14,7 +15,7 @@ class AnswerAnsweringOpportunity<T>(
override suspend fun coAnswer(call: Call) = getAnswer().answer(call)

override fun provideAnswer(answer: Answer<T>) {
synchronized(this) {
InternalPlatform.synchronized(this) {
val currentAnswer = this.storedAnswer
this.storedAnswer = if (currentAnswer == null) {
notifyFirstAnswerHandlers(answer)
Expand All @@ -30,7 +31,7 @@ class AnswerAnsweringOpportunity<T>(
}

fun onFirstAnswer(handler: (Answer<T>) -> Unit) {
synchronized(this) {
InternalPlatform.synchronized(this) {
firstAnswerHandlers.add(handler)
}
}
Expand Down
20 changes: 10 additions & 10 deletions mockk/common/src/main/kotlin/io/mockk/impl/stub/MockKStub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ open class MockKStub(
}

override fun answer(invocation: Invocation): Any? {
val invocationAndMatcher = synchronized(answers) {
val invocationAndMatcher = InternalPlatform.synchronized(answers) {
answers
.reversed()
.firstOrNull { it.matcher.match(invocation) }
Expand Down Expand Up @@ -112,7 +112,7 @@ open class MockKStub(
if (record) {
recordedCalls.add(invocation)

synchronized(recordedCallsByMethod) {
InternalPlatform.synchronized(recordedCallsByMethod) {
recordedCallsByMethod.getOrPut(invocation.method) { mutableListOf() }
.add(invocation)
}
Expand All @@ -121,18 +121,18 @@ open class MockKStub(
}
}

private fun checkExcluded(invocation: Invocation) = synchronized(exclusions) {
private fun checkExcluded(invocation: Invocation) = InternalPlatform.synchronized(exclusions) {
exclusions.any { it.match(invocation) }
}

override fun allRecordedCalls(): List<Invocation> {
synchronized(recordedCalls) {
InternalPlatform.synchronized(recordedCalls) {
return recordedCalls.toList()
}
}

override fun allRecordedCalls(method: MethodDescription): List<Invocation> {
synchronized(recordedCallsByMethod) {
InternalPlatform.synchronized(recordedCallsByMethod) {
return recordedCallsByMethod[method]?.toList() ?: listOf()
}
}
Expand All @@ -144,7 +144,7 @@ open class MockKStub(
exclusions.add(matcher)

if (params.current) {
synchronized(recordedCalls) {
InternalPlatform.synchronized(recordedCalls) {
val callsToExclude = recordedCalls
.filter(matcher::match)

Expand All @@ -157,15 +157,15 @@ open class MockKStub(
callsToExclude
.forEach { recordedCalls.remove(it) }

synchronized(recordedCallsByMethod) {
InternalPlatform.synchronized(recordedCallsByMethod) {

recordedCallsByMethod[matcher.method]?.apply {
filter(matcher::match)
.forEach { remove(it) }
}
}

synchronized(verifiedCalls) {
InternalPlatform.synchronized(verifiedCalls) {
verifiedCalls
.filter(matcher::match)
.forEach { verifiedCalls.remove(it) }
Expand All @@ -179,15 +179,15 @@ open class MockKStub(
}

override fun verifiedCalls(): List<Invocation> {
synchronized(verifiedCalls) {
InternalPlatform.synchronized(verifiedCalls) {
return verifiedCalls.toList()
}
}

override fun toStr() = "${type.simpleName}($name)"

override fun childMockK(matcher: InvocationMatcher, childType: KClass<*>): Any {
return synchronized(childs) {
return InternalPlatform.synchronized(childs) {
gatewayAccess.safeToString.exec {
childs.customComputeIfAbsent(matcher) {
gatewayAccess.mockFactory!!.mockk(
Expand Down
20 changes: 12 additions & 8 deletions mockk/common/src/test/kotlin/io/mockk/it/AnswersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.assertEquals

class AnswersTest {
class MockCls {
fun op(a: Int, b: Int, c: Int = 10, d: Int = 25) = a + b + c + d
fun op(a: Int, b: Int, c: Int = 10, d: Int = 25, e: Int? = 55) = a + b + c + d

fun lambdaOp(a: Int, b: () -> Int) = a + b()
}
Expand Down Expand Up @@ -39,24 +39,24 @@ class AnswersTest {

@Test
fun answerLastArg() {
every { spy.op(any(), 7, d = any()) } answers { if (lastArg<Int>() == 11) 7 else 8 }
every { spy.op(any(), 7, e = any()) } answers { if (lastArg<Int>() == 11) 7 else 8 }

assertEquals(7, spy.op(2, 7, d = 11))
assertEquals(8, spy.op(3, 7, d = 12))
assertEquals(7, spy.op(2, 7, e = 11))
assertEquals(8, spy.op(3, 7, e = 12))
}

@Test
fun answerNArgs() {
every { spy.op(any(), 1) } answers { nArgs }

assertEquals(4, spy.op(2, 1))
assertEquals(5, spy.op(2, 1))
}

@Test
fun answerParamTypesCount() {
every { spy.op(any(), 2) } answers { method.paramTypes.size }

assertEquals(4, spy.op(2, 2))
assertEquals(5, spy.op(2, 2))
}

@Test
Expand All @@ -70,9 +70,13 @@ class AnswersTest {
@Test
fun answerCaptureListNullable() {
val lst = mutableListOf<Int?>()
every { spy.op(3, 4, d = captureNullable(lst)) } answers { lst.captured()!! }
every { spy.op(3, 4, e = captureNullable(lst)) } answers { lst.captured() ?: 55 }

assertEquals(33, spy.op(3, 4, d = 33))
spy.op(3, 4, e = 1)
spy.op(3, 4, e = null)
spy.op(3, 4, e = 2)
spy.op(3, 4, e = 3)
assertEquals(listOf(1, null, 2, 3), lst)
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions mockk/jvm/src/main/kotlin/io/mockk/impl/InternalPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ actual object InternalPlatform {

actual fun multiNotifier(): MultiNotifier = JvmMultiNotifier()

actual inline fun <T> synchronized(obj: Any, block: () -> T) = kotlin.synchronized(obj, block)

inline fun <reified T : Any> loadPlugin(className: String, msg: String = "") =
try {
T::class.cast(Class.forName(className).newInstance())
Expand Down

0 comments on commit e79f1fb

Please sign in to comment.