Skip to content

Commit 63bdcc3

Browse files
authored
Allow repeatCount to be overridden for properties (#70)
1 parent f618632 commit 63bdcc3

File tree

7 files changed

+775
-72
lines changed

7 files changed

+775
-72
lines changed

fixture/configuration-options.adoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,70 @@ repeatCount {
5959
}
6060
----
6161

62+
Additionally we can override `repeatCount` for properties of a class and nested lists and maps.
63+
64+
._Kotlin class example_
65+
[%collapsible%open]
66+
====
67+
68+
Given the following Kotlin class:
69+
70+
[source,kotlin]
71+
----
72+
class KotlinClass(val readOnly: List<String>, private var private: List<String>) {
73+
var member: List<String>? = null
74+
}
75+
----
76+
77+
We can override repeatCount for `KotlinClass` as follows:
78+
79+
[source,kotlin]
80+
----
81+
val fixture = kotlinFixture {
82+
// Public constructor parameters overridden by reference:
83+
repeatCount(KotlinClass::readOnly) { 1 }
84+
85+
// Private constructor parameters are overridden by name:
86+
repeatCount<KotlinClass>("private") { 2 }
87+
88+
// Public member properties overridden by reference:
89+
repeatCount(KotlinClass::member) { 3 }
90+
}
91+
----
92+
====
93+
94+
._Java class example_
95+
[%collapsible]
96+
====
97+
Given the following Java class:
98+
99+
[source,java]
100+
----
101+
public class JavaClass {
102+
private final List<String> constructor;
103+
private List<String> mutable;
104+
105+
public JavaClass(List<String> constructor) { this.constructor = constructor; }
106+
107+
public void setMutable(List<String> mutable) { this.mutable = mutable; }
108+
}
109+
----
110+
111+
We can override repeatCount for `JavaClass` as follows:
112+
113+
[source,kotlin]
114+
----
115+
val fixture = kotlinFixture {
116+
// Setter overridden by reference:
117+
repeatCount(JavaClass::setMutable) { 1 }
118+
119+
// Constructor parameters don't typically retain names and so are
120+
// overridden by a positional 'arg' names:
121+
repeatCount<JavaClass>("arg0") { 2 }
122+
}
123+
----
124+
====
125+
62126
== Resolving abstract superclasses to a chosen subclass with `subType`
63127

64128
Used to always return an instance of a particular subclass for a superclass.

fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Configuration.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import kotlin.reflect.KType
7171
/**
7272
* The [Configuration] for generating the current fixture. This is a combination of all previous configurations.
7373
* @property repeatCount The length used for lists and maps.
74+
* @property propertiesRepeatCount Overrides the length used for lists and maps on constructor parameters and mutable properties when generating instances of generic classes.
7475
* @property properties Overrides for constructor parameters and mutable properties when generating instances of generic classes.
7576
* @property factories Given instances for a particular class using a factory method.
7677
* @property subTypes Superclass to subclass mapping for subtypes.
@@ -82,6 +83,8 @@ import kotlin.reflect.KType
8283
*/
8384
data class Configuration internal constructor(
8485
val repeatCount: () -> Int = defaultRepeatCount,
86+
val propertiesRepeatCount: Map<KClass<*>, Map<String, () -> Int>> =
87+
emptyMap<KClass<*>, Map<String, () -> Int>>().toUnmodifiableMap(),
8588
val properties: Map<KClass<*>, Map<String, GeneratorFun>> =
8689
emptyMap<KClass<*>, Map<String, GeneratorFun>>().toUnmodifiableMap(),
8790
val factories: Map<KType, GeneratorFun> =

0 commit comments

Comments
 (0)