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

Implement clearStaticMockk for KFunction and KProperty #1239

Merged
merged 2 commits into from
Apr 3, 2024
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
2 changes: 2 additions & 0 deletions modules/mockk/api/mockk.api
@@ -1,4 +1,6 @@
public final class io/mockk/JVMMockKKt {
public static final fun clearStaticMockk ([Lkotlin/reflect/KFunction;)V
public static final fun clearStaticMockk ([Lkotlin/reflect/KProperty;)V
public static final fun getDeclaringKotlinFile (Lkotlin/reflect/KFunction;)Lkotlin/reflect/KClass;
public static final fun mockkStatic ([Lkotlin/reflect/KFunction;)V
public static final fun mockkStatic ([Lkotlin/reflect/KFunction;Lkotlin/jvm/functions/Function0;)V
Expand Down
12 changes: 12 additions & 0 deletions modules/mockk/src/jvmMain/kotlin/io/mockk/MockK.kt
Expand Up @@ -44,6 +44,18 @@ fun unmockkStatic(vararg functions: KFunction<*>) =
fun unmockkStatic(vararg functions: KProperty<*>) =
unmockkStatic(*functions.map(KProperty<*>::getter).toTypedArray())

/**
* Clear static mocks.
*/
fun clearStaticMockk(vararg functions: KFunction<*>) =
clearStaticMockk(*functions.map { it.declaringKotlinFile }.toTypedArray())

/**
* Clear static mocks.
*/
fun clearStaticMockk(vararg functions: KProperty<*>) =
clearStaticMockk(*functions.map(KProperty<*>::getter).toTypedArray())

/**
* Builds a static mock and unmocks it after the block has been executed.
*/
Expand Down
27 changes: 27 additions & 0 deletions modules/mockk/src/jvmTest/kotlin/io/mockk/it/StaticMockkTest.kt
@@ -1,5 +1,6 @@
package io.mockk.it

import io.mockk.clearStaticMockk
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
Expand Down Expand Up @@ -73,6 +74,19 @@ class StaticMockkTest {
verify { 5 op 6 }
}

@Test
fun extensionFunctionClearStaticMock() {
mockkStatic(Int::op)

every { 5 op 6 } returns 2

assertEquals(2, 5 op 6)

clearStaticMockk(Int::op)

verify(exactly = 0) { 5 op 6 }
}

@Test
fun extensionPropertyStaticMock() {
mockkStatic(Int::selfOp)
Expand Down Expand Up @@ -106,4 +120,17 @@ class StaticMockkTest {

verify { 5.selfOp }
}

@Test
fun extensionPropertyClearStaticMock() {
mockkStatic(Int::selfOp)

every { 5.selfOp } returns 2

assertEquals(2, 5.selfOp)

clearStaticMockk(Int::selfOp)

verify(exactly = 0) { 5.selfOp }
}
}