Skip to content

Commit 82b6140

Browse files
committed
Adjust mokkery-core-tooling make creating new options from separate project easier
1 parent 3aa34e9 commit 82b6140

5 files changed

Lines changed: 30 additions & 26 deletions

File tree

mokkery-core-plugin/src/main/kotlin/dev/mokkery/plugin/core/MokkeryConfigApi.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import dev.mokkery.internal.options.MokkeryOption
44
import org.jetbrains.kotlin.config.CompilerConfiguration
55

66
fun <T> CompilerConfiguration.getSingleOrDefault(option: MokkeryOption<T>): T {
7+
require(!option.allowMultipleOccurrences) { "`getSingleOrDefault` is not supported for `allowMultipleOccurrences = true`" }
78
return get(option.configurationKey)
89
?.singleOrNull()
9-
?: option.defaultValue
10+
?: option.defaultValues.singleOrNull()
1011
?: error("No value for ${option.configurationKey}")
1112
}

mokkery-core-tooling/src/main/kotlin/dev/mokkery/internal/options/MokkeryOption.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public data class MokkeryOption<T>(
99
public val required: Boolean,
1010
public val allowMultipleOccurrences: Boolean,
1111
public val type: MokkeryOptionType<T>,
12-
public val defaultValue: T?,
12+
public val defaultValues: List<T>,
1313
)
1414

1515
@InternalMokkeryApi

mokkery-core-tooling/src/main/kotlin/dev/mokkery/internal/options/MokkeryOptions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import dev.mokkery.internal.options.MokkeryOptionType.Companion.annotationSelect
77
import dev.mokkery.internal.options.MokkeryOptionType.Companion.boolean
88
import dev.mokkery.internal.options.MokkeryOptionType.Companion.mockMode
99
import dev.mokkery.internal.options.MokkeryOptionType.Companion.verifyMode
10+
import dev.mokkery.internal.options.MokkeryOptionsNamespace.Companion.named
11+
import dev.mokkery.internal.options.MokkeryOptionsNamespace.Companion.root
1012
import dev.mokkery.options.AnnotationSelector
1113
import dev.mokkery.options.AnnotationSelector.Companion.all
1214
import dev.mokkery.verify.VerifyMode
@@ -23,8 +25,6 @@ public object MokkeryOptions : MokkeryOptionsContainer() {
2325
@InternalMokkeryApi
2426
public object Core : MokkeryOptionsContainer() {
2527

26-
private val root = MokkeryNamespace.root
27-
2828
public val defaultVerifyMode: MokkeryOption<VerifyMode> by root.defaultSingleOption(
2929
type = verifyMode,
3030
description = "Default VerifyMode for every verify block.",
@@ -56,7 +56,7 @@ public object MokkeryOptions : MokkeryOptionsContainer() {
5656
@InternalMokkeryApi
5757
public object Stubs : MokkeryOptionsContainer() {
5858

59-
private val stubs by MokkeryNamespace.named
59+
private val stubs by named
6060

6161
public val allowClassInheritance: MokkeryOption<Boolean> by stubs.defaultSingleOption(
6262
type = boolean,
@@ -73,7 +73,7 @@ public object MokkeryOptions : MokkeryOptionsContainer() {
7373
@InternalMokkeryApi
7474
public object Annotations : MokkeryOptionsContainer() {
7575

76-
private val annotations by MokkeryNamespace.named
76+
private val annotations by named
7777

7878
public val copyToMock: MokkeryOption<AnnotationSelector> by annotations.defaultSingleOption(
7979
type = annotationSelector,

mokkery-core-tooling/src/main/kotlin/dev/mokkery/internal/options/MokkeryOptionsContainer.kt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import kotlin.properties.ReadOnlyProperty
66

77
@Suppress("UNCHECKED_CAST")
88
@InternalMokkeryApi
9-
public abstract class MokkeryOptionsContainer: Iterable<MokkeryOption<Any?>> {
9+
public abstract class MokkeryOptionsContainer: Iterable<MokkeryOption<Any>> {
1010

11-
private val _options = mutableMapOf<String, MokkeryOption<Any?>>()
11+
private val _options = mutableMapOf<String, MokkeryOption<Any>>()
1212
private val _containers = mutableListOf<MokkeryOptionsContainer>()
1313

1414

15-
public override operator fun iterator(): Iterator<MokkeryOption<Any?>> = _options
15+
public override operator fun iterator(): Iterator<MokkeryOption<Any>> = _options
1616
.values
1717
.asSequence()
1818
.plus(_containers.flatten())
1919
.iterator()
2020

21-
public operator fun get(name: String): MokkeryOption<Any?>? {
21+
public operator fun get(name: String): MokkeryOption<Any>? {
2222
return _options[name] ?: _containers.firstNotNullOfOrNull { it[name] }
2323
}
2424

2525
public operator fun plusAssign(option: MokkeryOption<*>) {
26-
_options += (option.name to option as MokkeryOption<Any?>)
26+
_options += (option.name to option as MokkeryOption<Any>)
2727
}
2828

2929
public operator fun plusAssign(container: MokkeryOptionsContainer) {
@@ -34,24 +34,25 @@ public abstract class MokkeryOptionsContainer: Iterable<MokkeryOption<Any?>> {
3434
}
3535

3636

37-
internal interface MokkeryNamespace {
37+
@InternalMokkeryApi
38+
public interface MokkeryOptionsNamespace {
3839

39-
val name: String
40-
fun createName(name: String): String
40+
public val name: String
41+
public fun createName(name: String): String
4142

42-
companion object {
43+
public companion object {
4344

44-
val root = object : MokkeryNamespace {
45+
public val root: MokkeryOptionsNamespace = object : MokkeryOptionsNamespace {
4546

4647
override val name: String = ""
4748

4849
override fun createName(name: String): String = name
4950
}
5051

51-
val named: PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, MokkeryNamespace>>
52+
public val named: PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, MokkeryOptionsNamespace>>
5253
get() = PropertyDelegateProvider { _, it ->
5354
ReadOnlyProperty { _, _ ->
54-
object : MokkeryNamespace {
55+
object : MokkeryOptionsNamespace {
5556
override val name: String = it.name
5657
override fun createName(name: String): String = "${this.name}.$name"
5758
}
@@ -60,23 +61,24 @@ internal interface MokkeryNamespace {
6061
}
6162
}
6263

63-
internal fun <T> MokkeryNamespace.defaultSingleOption(
64+
@InternalMokkeryApi
65+
public fun <T> MokkeryOptionsNamespace.defaultSingleOption(
6466
type: MokkeryOptionType<T>,
6567
description: String,
6668
defaultValue: T,
6769
): MokkeryOptionPropertyDelegateProvider<T> = option(
6870
type = type,
6971
description = description,
70-
defaultValue = defaultValue,
72+
defaultValues = listOf(defaultValue),
7173
required = false,
7274
allowMultipleOccurrences = false,
7375
)
7476

75-
76-
internal fun <T> MokkeryNamespace.option(
77+
@InternalMokkeryApi
78+
public fun <T> MokkeryOptionsNamespace.option(
7779
type: MokkeryOptionType<T>,
7880
description: String,
79-
defaultValue: T?,
81+
defaultValues: List<T>,
8082
required: Boolean,
8183
allowMultipleOccurrences: Boolean,
8284
): MokkeryOptionPropertyDelegateProvider<T> = PropertyDelegateProvider { container, it ->
@@ -86,13 +88,14 @@ internal fun <T> MokkeryNamespace.option(
8688
required = required,
8789
allowMultipleOccurrences = allowMultipleOccurrences,
8890
type = type,
89-
defaultValue = defaultValue,
91+
defaultValues = defaultValues,
9092
)
9193
container += option
9294
ReadOnlyProperty { _, _ -> option }
9395
}
9496

95-
internal typealias MokkeryOptionPropertyDelegateProvider<T> = PropertyDelegateProvider<
97+
@InternalMokkeryApi
98+
public typealias MokkeryOptionPropertyDelegateProvider<T> = PropertyDelegateProvider<
9699
MokkeryOptionsContainer,
97100
ReadOnlyProperty<Any?, MokkeryOption<T>>
98101
>

mokkery-gradle/src/main/kotlin/dev/mokkery/gradle/MokkeryGradlePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class MokkeryGradlePlugin : KotlinCompilerPluginSupportPlugin {
3535
mokkery.rule.convention(ApplicationRule.AllTests)
3636
val gradleProperty = gradlePropertyProjection(target)
3737
MokkeryOptions.forEach {
38-
it.get(gradleProperty)?.convention(it.defaultValue)
38+
it.get(gradleProperty)?.convention(it.defaultValues.singleOrNull())
3939
}
4040
target.configureDependencies()
4141
super.apply(target)

0 commit comments

Comments
 (0)