Skip to content

Commit

Permalink
Rename MokkeryTestsScope to MokkerySuiteScope
Browse files Browse the repository at this point in the history
  • Loading branch information
lupuuss committed Feb 10, 2025
1 parent ce08fda commit 13d25d8
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Mokkery {
val MockMany4 by dev_mokkery.klass
val MockMany5 by dev_mokkery.klass

val MokkeryTestsScope by dev_mokkery.klass
val MokkerySuiteScope by dev_mokkery.klass

val MokkeryCallInterceptor by dev_mokkery_interceptor.klass
val MokkeryInstance by dev_mokkery_internal.klass
Expand Down Expand Up @@ -71,7 +71,7 @@ object Mokkery {
val MokkeryMockInterceptor by dev_mokkery_internal_interceptor.function
val TemplatingScope by dev_mokkery_internal_calls.function
val registerMock by dev_mokkery.function
val MokkeryTestsScope by dev_mokkery.function
val MokkerySuiteScope by dev_mokkery.function
}

object Property {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MokkeryTransformer(compilerPluginScope: CompilerPluginScope) : CoreTransfo
private val internalVerify = getFunction(Mokkery.Function.internalVerify)
private val internalVerifySuspend = getFunction(Mokkery.Function.internalVerifySuspend)
private val globalMokkeryScopeSymbol = getClass(Mokkery.Class.GlobalMokkeryScope).symbol
private val mokkeryTestsClass = getClass(Mokkery.Class.MokkeryTestsScope)
private val mokkerySuiteScopeClass = getClass(Mokkery.Class.MokkerySuiteScope)
private val testsScopeNameClass = getClass(Mokkery.Class.TestsScopeName)


Expand Down Expand Up @@ -110,17 +110,17 @@ class MokkeryTransformer(compilerPluginScope: CompilerPluginScope) : CoreTransfo

private fun overrideMokkeryTestsScopeIfNotOverridden(irClass: IrClass) {
if (!irClass.isClass) return
if (irClass.superTypes.none { it.getClass() == mokkeryTestsClass }) return
if (irClass.superTypes.none { it.getClass() == mokkerySuiteScopeClass }) return
val property = irClass.getProperty("mokkeryContext")
if (!property.isFakeOverride) return
irClass.declarations.remove(property)
val baseProperty = mokkeryTestsClass.getProperty("mokkeryContext")
val baseProperty = mokkerySuiteScopeClass.getProperty("mokkeryContext")
val newProperty = irClass.overridePropertyBackingField(context = pluginContext, property = baseProperty)
val constructor = irClass.primaryConstructor!!
val oldBody = constructor.body
constructor.body = declarationIrBuilder(constructor.symbol) {
irBlockBody {
val testScopeFun = getFunction(Mokkery.Function.MokkeryTestsScope)
val testScopeFun = getFunction(Mokkery.Function.MokkerySuiteScope)
val getContext = irCall(baseProperty.getter!!) {
dispatchReceiver = irCall(testScopeFun) {
val testsScopeName = irCallConstructor(testsScopeNameClass.primaryConstructor!!) {
Expand Down
4 changes: 2 additions & 2 deletions mokkery-runtime/src/commonMain/kotlin/dev/mokkery/Mock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public inline fun <reified T : Any> mock(


/**
* Provides mock implementation of given type [T] and registers it in this [MokkeryTestsScope].
* Provides mock implementation of given type [T] and registers it in this [MokkerySuiteScope].
*
* [T] **must** be provided directly and **cannot** be a generic parameter.
*
Expand All @@ -30,7 +30,7 @@ public inline fun <reified T : Any> mock(
* * Function types
* * Abstract/open classes with all methods/properties open/abstract and no-args constructor.
*/
public inline fun <reified T : Any> MokkeryTestsScope.mock(
public inline fun <reified T : Any> MokkerySuiteScope.mock(
mode: MockMode = MokkeryCompilerDefaults.mockMode,
block: T.() -> Unit = { }
): T = throw MokkeryPluginNotAppliedException()
16 changes: 8 additions & 8 deletions mokkery-runtime/src/commonMain/kotlin/dev/mokkery/MockMany.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,53 +57,53 @@ public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any, reified
): MockMany5<T1, T2, T3, T4, T5> = throw MokkeryPluginNotAppliedException()

/**
* Provides mock implementation of [T1] and [T2]. It is registered in this [MokkeryTestsScope].
* Provides mock implementation of [T1] and [T2]. It is registered in this [MokkerySuiteScope].
*
* Types restrictions:
* * Each type has to satisfy type restriction from [mock].
* * Only one class is allowed
* * No type duplicates
*/
public inline fun <reified T1 : Any, reified T2 : Any> MokkeryTestsScope.mockMany(
public inline fun <reified T1 : Any, reified T2 : Any> MokkerySuiteScope.mockMany(
mode: MockMode = MokkeryCompilerDefaults.mockMode,
block: MockMany2<T1, T2>.() -> Unit = { }
): MockMany2<T1, T2> = throw MokkeryPluginNotAppliedException()

/**
* Provides mock implementation of [T1], [T2] and [T3]. It is registered in this [MokkeryTestsScope].
* Provides mock implementation of [T1], [T2] and [T3]. It is registered in this [MokkerySuiteScope].
*
* Types restrictions:
* * Each type has to satisfy type restriction from [mock].
* * Only one class is allowed
* * No type duplicates
*/
public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any> MokkeryTestsScope.mockMany(
public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any> MokkerySuiteScope.mockMany(
mode: MockMode = MokkeryCompilerDefaults.mockMode,
block: MockMany3<T1, T2, T3>.() -> Unit = { }
): MockMany3<T1, T2, T3> = throw MokkeryPluginNotAppliedException()

/**
* Provides mock implementation of [T1], [T2], [T3] and [T4]. It is registered in this [MokkeryTestsScope].
* Provides mock implementation of [T1], [T2], [T3] and [T4]. It is registered in this [MokkerySuiteScope].
*
* Types restrictions:
* * Each type has to satisfy type restriction from [mock].
* * Only one class is allowed
* * No type duplicates
*/
public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any, reified T4 : Any> MokkeryTestsScope.mockMany(
public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any, reified T4 : Any> MokkerySuiteScope.mockMany(
mode: MockMode = MokkeryCompilerDefaults.mockMode,
block: MockMany4<T1, T2, T3, T4>.() -> Unit = { }
): MockMany4<T1, T2, T3, T4> = throw MokkeryPluginNotAppliedException()

/**
* Provides mock implementation of [T1], [T2], [T3], [T4] and [T5]. It is registered in this [MokkeryTestsScope].
* Provides mock implementation of [T1], [T2], [T3], [T4] and [T5]. It is registered in this [MokkerySuiteScope].
*
* Types restrictions:
* * Each type has to satisfy type restriction from [mock].
* * Only one class is allowed
* * No type duplicates
*/
public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any, reified T4 : Any, reified T5 : Any> MokkeryTestsScope.mockMany(
public inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any, reified T4 : Any, reified T5 : Any> MokkerySuiteScope.mockMany(
mode: MockMode = MokkeryCompilerDefaults.mockMode,
block: MockMany5<T1, T2, T3, T4, T5>.() -> Unit = { }
): MockMany5<T1, T2, T3, T4, T5> = throw MokkeryPluginNotAppliedException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import dev.mokkery.internal.context.MocksRegistry
import dev.mokkery.internal.context.mocksRegistry

/**
* Returns all mocks from this [MokkeryTestsScope].
* Returns all mocks from this [MokkerySuiteScope].
*/
public val MokkeryTestsScope.mocks: Set<Any>
public val MokkerySuiteScope.mocks: Set<Any>
get() = mocksRegistry.mocks

/**
* Registers given [mock] in this [MokkeryTestsScope].
* Registers given [mock] in this [MokkerySuiteScope].
*
* Usually, it should not be necessary to register mocks manually. Use [MokkeryTestsScope.mock],
* [MokkeryTestsScope.mockMany] and [MokkeryTestsScope.spy] overloads to create mocks that are registered automatically.
* Usually, it should not be necessary to register mocks manually. Use [MokkerySuiteScope.mock],
* [MokkerySuiteScope.mockMany] and [MokkerySuiteScope.spy] overloads to create mocks that are registered automatically.
*/
public fun <T : Any> MokkeryTestsScope.registerMock(mock: T): T = mock.also(mocksRegistry::register)
public fun <T : Any> MokkerySuiteScope.registerMock(mock: T): T = mock.also(mocksRegistry::register)

/**
* A scope for tests that use Mokkery mocks, enabling automation and enhanced exhaustiveness checks.
*
* ### How It Works
*
* Every mock involved in your test must be registered in this scope.
* This happens automatically when using [MokkeryTestsScope.mock], [MokkeryTestsScope.mockMany], or
* [MokkeryTestsScope.spy]. Alternatively, you can register mocks manually with [registerMock].
* This happens automatically when using [MokkerySuiteScope.mock], [MokkerySuiteScope.mockMany], or
* [MokkerySuiteScope.spy]. Alternatively, you can register mocks manually with [registerMock].
*
* ### Benefits
*
* Mokkery provides [MokkeryTestsScope.verify] and [MokkeryTestsScope.verifySuspend]. When used with
* Mokkery provides [MokkerySuiteScope.verify] and [MokkerySuiteScope.verifySuspend]. When used with
* an exhaustive [dev.mokkery.verify.VerifyMode], these ensure that **all** registered mocks (not just
* those inside the verify block) are checked for exhaustiveness.
*
* Additionally, you can call [MokkeryTestsScope.verifyNoMoreCalls] to ensure that all mocks in the scope
* Additionally, you can call [MokkerySuiteScope.verifyNoMoreCalls] to ensure that all mocks in the scope
* have had their calls fully verified.
*
* ### Example
Expand All @@ -60,9 +60,9 @@ public fun <T : Any> MokkeryTestsScope.registerMock(mock: T): T = mock.also(mock
* ```
* Normally, this test passes because Mokkery only checks for the exhaustiveness of mocks used inside the verify block.
*
* However, if we mark the test class with [MokkeryTestsScope], the behavior changes:
* However, if we mark the test class with [MokkerySuiteScope], the behavior changes:
* ```kotlin
* class ClassTest : MokkeryTestsScope {
* class ClassTest : MokkerySuiteScope {
*
* private val a = mock<A>()
* private val b = mock<B>()
Expand All @@ -82,10 +82,10 @@ public fun <T : Any> MokkeryTestsScope.registerMock(mock: T): T = mock.also(mock
*
* In this test, [verify] leverages the scope to improve exhaustiveness checks.
*
* If you cannot mark your test class with [MokkeryTestsScope], you can create a scope manually using [MokkeryTestsScope]:
* If you cannot mark your test class with [MokkerySuiteScope], you can create a scope manually using [MokkerySuiteScope]:
*
* ```kotlin
* with(MokkeryTestsScope()) {
* with(MokkerySuiteScope()) {
* val a = mock<A>()
* val b = mock<B>()
* a.call(1)
Expand All @@ -98,21 +98,21 @@ public fun <T : Any> MokkeryTestsScope.registerMock(mock: T): T = mock.also(mock
* }
* ```
*/
public interface MokkeryTestsScope : MokkeryScope {
public interface MokkerySuiteScope : MokkeryScope {

override val mokkeryContext: MokkeryContext
get() = throw MokkeryPluginNotAppliedException()
}

/**
* Creates [MokkeryTestsScope] instance with given extra [context].
* Creates [MokkerySuiteScope] instance with given extra [context].
*
* Check interface documentation for details.
*/
public fun MokkeryTestsScope(context: MokkeryContext = MokkeryContext.Empty): MokkeryTestsScope {
return object : MokkeryTestsScope {
public fun MokkerySuiteScope(context: MokkeryContext = MokkeryContext.Empty): MokkerySuiteScope {
return object : MokkerySuiteScope {
override val mokkeryContext = GlobalMokkeryScope.mokkeryContext + MocksRegistry() + context

override fun toString(): String = "MokkeryTestsScope(mokkeryContext=$mokkeryContext)"
override fun toString(): String = "MokkerySuiteScope(mokkeryContext=$mokkeryContext)"
}
}
4 changes: 2 additions & 2 deletions mokkery-runtime/src/commonMain/kotlin/dev/mokkery/Spy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public inline fun <reified T : Any> spy(


/**
* Returns given [obj] wrapped with a spying implementation of [T] and registers it in this [MokkeryTestsScope].
* Returns given [obj] wrapped with a spying implementation of [T] and registers it in this [MokkerySuiteScope].
*
* [T] **must** be provided directly and **cannot** be a generic parameter.
*
Expand All @@ -29,7 +29,7 @@ public inline fun <reified T : Any> spy(
* * Function types
* * Abstract/open classes with all methods/properties open/abstract and no-args constructor.
*/
public inline fun <reified T : Any> MokkeryTestsScope.spy(
public inline fun <reified T : Any> MokkerySuiteScope.spy(
obj: T,
block: T.() -> Unit = { }
): T = throw MokkeryPluginNotAppliedException()
Expand Down
14 changes: 7 additions & 7 deletions mokkery-runtime/src/commonMain/kotlin/dev/mokkery/Verify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ public fun verifySuspend(
/**
* Asserts that calls sequence defined in [block] satisfies given [mode].
*
* If verify mode is exhaustive, mocks from [MokkeryTestsScope] are also checked.
* If verify mode is exhaustive, mocks from [MokkerySuiteScope] are also checked.
*
* Each verification is performed only on unverified calls. In result repeated verifications may give different results.
*
* Provided [block] **must** be a lambda and all mock calls **must** occur directly inside it. Extracting [block]
* content to functions is prohibited.
*/
public fun MokkeryTestsScope.verify(
public fun MokkerySuiteScope.verify(
mode: VerifyMode = MokkeryCompilerDefaults.verifyMode,
block: ArgMatchersScope.() -> Unit
): Unit = throw MokkeryPluginNotAppliedException()

/**
* Just like [verify], but allows suspendable function calls.
*
* If verify mode is exhaustive, mocks from [MokkeryTestsScope] are also checked.
* If verify mode is exhaustive, mocks from [MokkerySuiteScope] are also checked.
*/
public fun MokkeryTestsScope.verifySuspend(
public fun MokkerySuiteScope.verifySuspend(
mode: VerifyMode = MokkeryCompilerDefaults.verifyMode,
block: suspend ArgMatchersScope.() -> Unit
): Unit = throw MokkeryPluginNotAppliedException()
Expand All @@ -66,13 +66,13 @@ public fun MokkeryTestsScope.verifySuspend(
* Asserts that all given [mocks] have all their registered calls verified with [verify] or [verifySuspend].
*/
public fun verifyNoMoreCalls(vararg mocks: Any) {
MokkeryTestsScope(MocksRegistry(mocks = mocks.toSet())).verifyNoMoreCalls()
MokkerySuiteScope(MocksRegistry(mocks = mocks.toSet())).verifyNoMoreCalls()
}

/**
* Asserts that all mocks from given [MokkeryTestsScope] have no unverified calls.
* Asserts that all mocks from given [MokkerySuiteScope] have no unverified calls.
*/
public fun MokkeryTestsScope.verifyNoMoreCalls() {
public fun MokkerySuiteScope.verifyNoMoreCalls() {
val tools = this.tools
mocks.forEach { mock ->
val tracing = mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dev.mokkery.interceptor.MokkeryCallListener
import dev.mokkery.interceptor.MokkeryCallScope
import dev.mokkery.interceptor.call
import dev.mokkery.interceptor.self
import dev.mokkery.internal.context.testsScopeName
import dev.mokkery.internal.context.suiteName
import dev.mokkery.internal.utils.callToString

/**
Expand All @@ -28,7 +28,7 @@ public class MokkeryCallLogger(
) : MokkeryCallListener {

override fun onIntercept(scope: MokkeryCallScope) {
scope.testsScopeName
scope.suiteName
?.let { "[$it] " }
.orEmpty()
.plus(callToString(scope.self.toString(), scope.call.function.name, scope.call.args))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.mokkery.internal

import dev.mokkery.MokkeryTestsScope
import dev.mokkery.MokkerySuiteScope
import dev.mokkery.context.MokkeryContext
import dev.mokkery.internal.context.IgnoringMocksRegistry
import dev.mokkery.internal.context.MokkeryTools

internal object GlobalMokkeryScope : MokkeryTestsScope {
internal object GlobalMokkeryScope : MokkerySuiteScope {

override val mokkeryContext: MokkeryContext = MokkeryTools.default() + IgnoringMocksRegistry
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package dev.mokkery.internal

import dev.mokkery.MokkeryTestsScope
import dev.mokkery.MokkerySuiteScope
import dev.mokkery.internal.calls.CallTrace
import dev.mokkery.internal.calls.TemplatingScope
import dev.mokkery.internal.context.mocksRegistry
Expand All @@ -12,13 +12,13 @@ import dev.mokkery.internal.utils.runSuspension
import dev.mokkery.matcher.ArgMatchersScope
import dev.mokkery.verify.VerifyMode

internal fun MokkeryTestsScope.internalVerifySuspend(
internal fun MokkerySuiteScope.internalVerifySuspend(
scope: TemplatingScope,
mode: VerifyMode,
block: suspend ArgMatchersScope.() -> Unit
) = internalVerify(scope, mode) { runSuspension { block() } }

internal fun MokkeryTestsScope.internalVerify(
internal fun MokkerySuiteScope.internalVerify(
templating: TemplatingScope,
mode: VerifyMode,
block: ArgMatchersScope.() -> Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.mokkery.internal.context

import dev.mokkery.MokkeryTestsScope
import dev.mokkery.MokkerySuiteScope
import dev.mokkery.context.MokkeryContext
import dev.mokkery.context.require
import kotlinx.atomicfu.locks.ReentrantLock
import kotlinx.atomicfu.locks.withLock

internal val MokkeryTestsScope.mocksRegistry: MocksRegistry
internal val MokkerySuiteScope.mocksRegistry: MocksRegistry
get() = mokkeryContext.require(MocksRegistry)

internal interface MocksRegistry : MokkeryContext.Element {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.mokkery.internal.context

import dev.mokkery.MokkeryScope
import dev.mokkery.context.MokkeryContext

internal val MokkeryScope.suiteName: String?
get() = mokkeryContext[SuteName]?.name

internal data class SuteName(val name: String) : MokkeryContext.Element {

override val key = Key

companion object Key : MokkeryContext.Key<SuteName>
}

This file was deleted.

Loading

0 comments on commit 13d25d8

Please sign in to comment.