Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions modules/mockk-dsl/api/mockk-dsl.api
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,13 @@ public final class io/mockk/FunctionAnswer : io/mockk/Answer {
}

public final class io/mockk/FunctionMatcher : io/mockk/EquivalentMatcher, io/mockk/Matcher, io/mockk/TypedMatcher {
public fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;)V
public fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;Z)V
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun checkType (Ljava/lang/Object;)Z
public final fun component1 ()Lkotlin/jvm/functions/Function1;
public final fun component2 ()Lkotlin/reflect/KClass;
public final fun copy (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;)Lio/mockk/FunctionMatcher;
public static synthetic fun copy$default (Lio/mockk/FunctionMatcher;Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;ILjava/lang/Object;)Lio/mockk/FunctionMatcher;
public final fun copy (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;Z)Lio/mockk/FunctionMatcher;
public static synthetic fun copy$default (Lio/mockk/FunctionMatcher;Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;ZILjava/lang/Object;)Lio/mockk/FunctionMatcher;
public fun equals (Ljava/lang/Object;)Z
public fun equivalent ()Lio/mockk/Matcher;
public fun getArgumentType ()Lkotlin/reflect/KClass;
Expand All @@ -309,12 +310,13 @@ public final class io/mockk/FunctionMatcher : io/mockk/EquivalentMatcher, io/moc
}

public final class io/mockk/FunctionWithNullableArgMatcher : io/mockk/EquivalentMatcher, io/mockk/Matcher, io/mockk/TypedMatcher {
public fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;)V
public fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;Z)V
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun checkType (Ljava/lang/Object;)Z
public final fun component1 ()Lkotlin/jvm/functions/Function1;
public final fun component2 ()Lkotlin/reflect/KClass;
public final fun copy (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;)Lio/mockk/FunctionWithNullableArgMatcher;
public static synthetic fun copy$default (Lio/mockk/FunctionWithNullableArgMatcher;Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;ILjava/lang/Object;)Lio/mockk/FunctionWithNullableArgMatcher;
public final fun copy (Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;Z)Lio/mockk/FunctionWithNullableArgMatcher;
public static synthetic fun copy$default (Lio/mockk/FunctionWithNullableArgMatcher;Lkotlin/jvm/functions/Function1;Lkotlin/reflect/KClass;ZILjava/lang/Object;)Lio/mockk/FunctionWithNullableArgMatcher;
public fun equals (Ljava/lang/Object;)Z
public fun equivalent ()Lio/mockk/Matcher;
public fun getArgumentType ()Lkotlin/reflect/KClass;
Expand Down
29 changes: 21 additions & 8 deletions modules/mockk-dsl/src/commonMain/kotlin/io/mockk/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2166,16 +2166,29 @@ class MockKVerificationScope(
callRecorder: CallRecorder,
lambda: CapturingSlot<Function<*>>
) : MockKMatcherScope(callRecorder, lambda) {
inline fun <reified T : Any> withArg(noinline captureBlock: MockKAssertScope.(T) -> Unit): T = match {
MockKAssertScope(it).captureBlock(it)
true
}
inline fun <reified T : Any> withArg(noinline captureBlock: MockKAssertScope.(T) -> Unit): T =
match(
FunctionMatcher(
{
MockKAssertScope(it).captureBlock(it)
true
},
T::class,
logAssertionError = true
)
)

inline fun <reified T : Any> withNullableArg(noinline captureBlock: MockKAssertScope.(T?) -> Unit): T =
matchNullable {
MockKAssertScope(it).captureBlock(it)
true
}
match(
FunctionWithNullableArgMatcher(
{
MockKAssertScope(it).captureBlock(it)
true
},
T::class,
logAssertionError = true
)
)

inline fun <reified T : Any> coWithArg(noinline captureBlock: suspend MockKAssertScope.(T) -> Unit): T =
withArg {
Expand Down
18 changes: 15 additions & 3 deletions modules/mockk-dsl/src/commonMain/kotlin/io/mockk/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ data class ConstantMatcher<in T : Any>(val constValue: Boolean) : Matcher<T> {
*/
data class FunctionMatcher<in T : Any>(
val matchingFunc: (T) -> Boolean,
override val argumentType: KClass<*>
override val argumentType: KClass<*>,
private val logAssertionError: Boolean = false,
) : Matcher<T>, TypedMatcher, EquivalentMatcher {
override fun equivalent(): Matcher<Any> = ConstantMatcher(true)

Expand All @@ -62,6 +63,9 @@ data class FunctionMatcher<in T : Any>(
try {
matchingFunc(arg)
} catch (a: AssertionError) {
if (logAssertionError) {
a.printStackTrace()
}
false
}
}
Expand All @@ -72,11 +76,19 @@ data class FunctionMatcher<in T : Any>(

data class FunctionWithNullableArgMatcher<in T : Any>(
val matchingFunc: (T?) -> Boolean,
override val argumentType: KClass<*>
override val argumentType: KClass<*>,
private val logAssertionError: Boolean = false,
) : Matcher<T>, TypedMatcher, EquivalentMatcher {
override fun equivalent(): Matcher<Any> = ConstantMatcher(true)

override fun match(arg: T?): Boolean = matchingFunc(arg)
override fun match(arg: T?): Boolean = try {
matchingFunc(arg)
} catch (a: AssertionError) {
if (logAssertionError) {
a.printStackTrace()
}
false
}

override fun checkType(arg: Any?): Boolean {
if (arg == null) {
Expand Down