Skip to content

Commit 79adc6f

Browse files
authored
Rename instance as factory to be better reflect what it does (#22)
1 parent 98b503a commit 79adc6f

File tree

11 files changed

+68
-68
lines changed

11 files changed

+68
-68
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Getting started
99

10-
Include the following dependencies in your build.gradle.kts file:
10+
Include the following dependencies in your `build.gradle.kts` file:
1111

1212
```kotlin
1313
testImplementation("com.appmattus.fixture:fixture:<latest-version>")
@@ -63,7 +63,7 @@ val listOfSevenItems = fixture<List<String>> {
6363
}
6464
```
6565

66-
repeatCount is a factory method so can be used to return lists and maps of
66+
`repeatCount` is a factory method so can be used to return lists and maps of
6767
different lengths each execution:
6868

6969
```kotlin
@@ -89,13 +89,14 @@ val alwaysFloat = fixture<Number>() {
8989
}
9090
```
9191

92-
#### instance
92+
#### factory
9393

94-
Used to return the given instance for a particular class.
94+
Used to return the given instance for a particular class using a factory
95+
method.
9596

9697
```kotlin
9798
val fixture = kotlinFixture {
98-
instance<Number> {
99+
factory<Number> {
99100
41
100101
}
101102
}
@@ -104,44 +105,44 @@ val alwaysFortyOne = fixture<Number>()
104105

105106
val alwaysOnePointFive = fixture<Number>() {
106107
// Overrides the parent configuration
107-
instance<Number> {
108+
factory<Number> {
108109
1.5
109110
}
110111
}
111112
```
112113

113-
instance is a factory method so can be used to return different values on every
114+
As `factory` is a factory method you can return different values on every
114115
execution:
115116

116117
```kotlin
117-
instance<Number> {
118+
factory<Number> {
118119
Random.nextInt(10, 50)
119120
}
120121
```
121122

122123
##### Date and Calendar instances
123124

124-
By default Date and Calendar instances pick a date within one year either side
125-
of now.
125+
By default `Date` and `Calendar` instances pick a date within one year either
126+
side of now.
126127

127-
This can be overridden using `instance` which has some built in constructs:
128+
This can be overridden using `factory` which has some built in constructs:
128129

129130
```kotlin
130131
val fixture = kotlinFixture {
131132
// Generate between two dates
132-
instance<Date> { between(startDate, endDate) }
133+
factory<Date> { between(startDate, endDate) }
133134
}
134135

135136
val betweenTwoDates = fixture<Date>()
136137

137138
// You can also override at instance creation
138139

139140
val pastDate = fixture<Date> {
140-
instance<Date> { before(Date()) }
141+
factory<Date> { before(Date()) }
141142
}
142143

143144
val futureDate = fixture<Date> {
144-
instance<Date> { after(Date()) }
145+
factory<Date> { after(Date()) }
145146
}
146147
```
147148

@@ -201,8 +202,8 @@ val fixture = kotlinFixture {
201202

202203
#### random
203204

204-
By default a Random class is used that will generate unique values between runs.
205-
If you want repeatability you can specify a seeded Random instance.
205+
By default a `Random` class is used that will generate unique values between
206+
runs. If you want repeatability you can specify a seeded `Random` instance.
206207

207208
```kotlin
208209
val fixture = kotlinFixture {
@@ -277,7 +278,8 @@ provide.
277278
Including the `fixture-kotlintest` dependency in your project adds extension
278279
functions `assertAll`, `assertNone`, `forAll` and `forNone` to the fixture.
279280
These functions wrap the equivalent functions from KotlinTest while providing
280-
generation of all the classes KotlinFixture supports. For example:
281+
generation of all the classes [KotlinFixture](https://github.com/appmattus/kotlinfixture)
282+
supports. For example:
281283

282284
```kotlin
283285
data class Person(name: String, age: Int)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import com.appmattus.kotlinfixture.resolver.EnumResolver
3232
import com.appmattus.kotlinfixture.resolver.EnumSetResolver
3333
import com.appmattus.kotlinfixture.resolver.FactoryMethodResolver
3434
import com.appmattus.kotlinfixture.resolver.HashtableKTypeResolver
35-
import com.appmattus.kotlinfixture.resolver.InstanceResolver
35+
import com.appmattus.kotlinfixture.resolver.FactoryResolver
3636
import com.appmattus.kotlinfixture.resolver.IterableKTypeResolver
3737
import com.appmattus.kotlinfixture.resolver.KFunctionResolver
3838
import com.appmattus.kotlinfixture.resolver.KTypeResolver
@@ -59,7 +59,7 @@ data class Configuration(
5959
val repeatCount: () -> Int = defaultRepeatCount,
6060
val properties: Map<KClass<*>, Map<String, () -> Any?>> =
6161
emptyMap<KClass<*>, Map<String, () -> Any?>>().toUnmodifiableMap(),
62-
val instances: Map<KType, Generator<Any?>.() -> Any?> =
62+
val factories: Map<KType, Generator<Any?>.() -> Any?> =
6363
emptyMap<KType, Generator<Any?>.() -> Any?>().toUnmodifiableMap(),
6464
val subTypes: Map<KClass<*>, KClass<*>> = emptyMap<KClass<*>, KClass<*>>().toUnmodifiableMap(),
6565
val random: Random = defaultRandom,
@@ -76,7 +76,7 @@ data class Configuration(
7676
private val defaultDecorators = listOf(RecursionDecorator(), LoggingDecorator())
7777

7878
private val defaultResolvers = listOf(
79-
InstanceResolver(),
79+
FactoryResolver(),
8080
SubTypeResolver(),
8181

8282
CharResolver(),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ class ConfigurationBuilder(configuration: Configuration = Configuration()) {
3838
private var repeatCount: () -> Int = configuration.repeatCount
3939
private val properties: MutableMap<KClass<*>, MutableMap<String, () -> Any?>> =
4040
configuration.properties.mapValues { it.value.toMutableMap() }.toMutableMap()
41-
private val instances: MutableMap<KType, Generator<Any?>.() -> Any?> = configuration.instances.toMutableMap()
41+
private val factories: MutableMap<KType, Generator<Any?>.() -> Any?> = configuration.factories.toMutableMap()
4242
private val subTypes: MutableMap<KClass<*>, KClass<*>> = configuration.subTypes.toMutableMap()
4343

4444
internal val strategies: MutableMap<KClass<*>, Any> = configuration.strategies.toMutableMap()
4545

4646
@Suppress("UNCHECKED_CAST")
47-
inline fun <reified T> instance(noinline generator: Generator<T>.() -> T) =
48-
instance(typeOf<T>(), generator as Generator<Any?>.() -> Any?)
47+
inline fun <reified T> factory(noinline generator: Generator<T>.() -> T) =
48+
factory(typeOf<T>(), generator as Generator<Any?>.() -> Any?)
4949

50-
fun instance(type: KType, generator: Generator<Any?>.() -> Any?) {
51-
instances[type] = generator
50+
fun factory(type: KType, generator: Generator<Any?>.() -> Any?) {
51+
factories[type] = generator
5252
}
5353

5454
inline fun <reified T, reified U : T> subType() = subType(T::class, U::class)
@@ -92,7 +92,7 @@ class ConfigurationBuilder(configuration: Configuration = Configuration()) {
9292
fun build() = Configuration(
9393
repeatCount = repeatCount,
9494
properties = properties.mapValues { it.value.toUnmodifiableMap() }.toUnmodifiableMap(),
95-
instances = instances.toUnmodifiableMap(),
95+
factories = factories.toUnmodifiableMap(),
9696
subTypes = subTypes.toUnmodifiableMap(),
9797
random = random,
9898
decorators = decorators.toUnmodifiableList(),

fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/InstanceResolver.kt renamed to fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolver.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ import com.appmattus.kotlinfixture.Unresolved
2121
import com.appmattus.kotlinfixture.config.Generator
2222
import kotlin.reflect.KType
2323

24-
internal class InstanceResolver : Resolver {
24+
internal class FactoryResolver : Resolver {
2525

2626
override fun resolve(context: Context, obj: Any): Any? {
2727

2828
if (obj is KType) {
29-
context.configuration.instances[obj]?.let {
30-
return with(InstanceGenerator(context)) { it() }
29+
context.configuration.factories[obj]?.let {
30+
return with(FactoryGenerator(context)) { it() }
3131
}
3232
}
3333

3434
return Unresolved
3535
}
3636

37-
private class InstanceGenerator(context: Context) : Generator<Any?> {
37+
private class FactoryGenerator(context: Context) : Generator<Any?> {
3838
override val random = context.random
3939
}
4040
}

fixture/src/test/kotlin/com/appmattus/kotlinfixture/FixtureTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class FixtureTest {
168168
@Test
169169
fun `can override instance in initialisation`() {
170170
val fixture = kotlinFixture {
171-
instance<Number> { 10 }
171+
factory<Number> { 10 }
172172
}
173173

174174
repeat(100) {
@@ -179,7 +179,7 @@ class FixtureTest {
179179
@Test
180180
fun `overridden instance in initialisation is random`() {
181181
val fixture = kotlinFixture {
182-
instance<Number> { Random.nextInt(1, 5) }
182+
factory<Number> { Random.nextInt(1, 5) }
183183
}
184184

185185
repeat(100) {
@@ -192,7 +192,7 @@ class FixtureTest {
192192
val fixture = kotlinFixture()
193193

194194
val result = fixture<Number> {
195-
instance<Number> { 20 }
195+
factory<Number> { 20 }
196196
}
197197
assertEquals(20, result)
198198
}
@@ -203,7 +203,7 @@ class FixtureTest {
203203

204204
repeat(100) {
205205
val result = fixture<Number> {
206-
instance<Number> { Random.nextInt(6, 10) }
206+
factory<Number> { Random.nextInt(6, 10) }
207207
}
208208

209209
assertTrue { result in 6..10 }
@@ -213,11 +213,11 @@ class FixtureTest {
213213
@Test
214214
fun `can override instance in creation when overridden in initialisation`() {
215215
val fixture = kotlinFixture {
216-
instance<Number> { 10 }
216+
factory<Number> { 10 }
217217
}
218218

219219
val result = fixture<Number> {
220-
instance<Number> { 30 }
220+
factory<Number> { 30 }
221221
}
222222
assertEquals(30, result)
223223
}

fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/ConfigurationBuilderTest.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ class ConfigurationBuilderTest {
101101
@Suppress("UNCHECKED_CAST")
102102
val configuration = ConfigurationBuilder(
103103
Configuration(
104-
instances = mapOf(Properties::class.starProjectedType to original as Generator<Any?>.() -> Any?)
104+
factories = mapOf(Properties::class.starProjectedType to original as Generator<Any?>.() -> Any?)
105105
)
106106
).apply {
107-
instance<Properties> { Properties("1") }
107+
factory<Properties> { Properties("1") }
108108
}.build()
109109

110110
with(TestGenerator) {
111111
assertEquals(
112112
Properties("1"),
113-
(configuration.instances.getValue(Properties::class.starProjectedType))()
113+
(configuration.factories.getValue(Properties::class.starProjectedType))()
114114
)
115115
}
116116
}
@@ -122,16 +122,16 @@ class ConfigurationBuilderTest {
122122
@Suppress("UNCHECKED_CAST")
123123
val configuration = ConfigurationBuilder(
124124
Configuration(
125-
instances = mapOf(Properties::class.starProjectedType to original as Generator<Any?>.() -> Any?)
125+
factories = mapOf(Properties::class.starProjectedType to original as Generator<Any?>.() -> Any?)
126126
)
127127
).apply {
128-
instance(Properties::class.starProjectedType) { Properties("1") }
128+
factory(Properties::class.starProjectedType) { Properties("1") }
129129
}.build()
130130

131131
with(TestGenerator) {
132132
assertEquals(
133133
Properties("1"),
134-
(configuration.instances.getValue(Properties::class.starProjectedType))()
134+
(configuration.factories.getValue(Properties::class.starProjectedType))()
135135
)
136136
}
137137
}
@@ -142,10 +142,8 @@ class ConfigurationBuilderTest {
142142

143143
assertFailsWith<UnsupportedOperationException> {
144144
@Suppress("UNCHECKED_CAST", "ReplacePutWithAssignment")
145-
(configuration.instances as MutableMap<KType, () -> Any?>).put(Properties::class.starProjectedType) {
146-
Properties(
147-
"1"
148-
)
145+
(configuration.factories as MutableMap<KType, () -> Any?>).put(Properties::class.starProjectedType) {
146+
Properties("1")
149147
}
150148
}
151149
}

fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorDateTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class GeneratorDateTest {
4444
fun `After specification gives date in the future`() {
4545

4646
val fixture = kotlinFixture {
47-
instance<Date> { after(now) }
47+
factory<Date> { after(now) }
4848
}
4949

5050
repeat(100) {
@@ -60,7 +60,7 @@ class GeneratorDateTest {
6060
fun `After specification is random`() {
6161

6262
val fixture = kotlinFixture {
63-
instance<Date> { after(now) }
63+
factory<Date> { after(now) }
6464
}
6565

6666
assertIsRandom {
@@ -71,7 +71,7 @@ class GeneratorDateTest {
7171
@Test
7272
fun `After specification uses seeded random`() {
7373
val fixture = kotlinFixture {
74-
instance<Date> { after(now) }
74+
factory<Date> { after(now) }
7575
}
7676

7777
val value1 = fixture<Date> {
@@ -87,7 +87,7 @@ class GeneratorDateTest {
8787
@Test
8888
fun `Before specification gives date in the past`() {
8989
val fixture = kotlinFixture {
90-
instance<Date> { before(now) }
90+
factory<Date> { before(now) }
9191
}
9292

9393
repeat(100) {
@@ -103,7 +103,7 @@ class GeneratorDateTest {
103103
fun `Before specification is random`() {
104104

105105
val fixture = kotlinFixture {
106-
instance<Date> { before(now) }
106+
factory<Date> { before(now) }
107107
}
108108

109109
assertIsRandom {
@@ -114,7 +114,7 @@ class GeneratorDateTest {
114114
@Test
115115
fun `Before specification uses seeded random`() {
116116
val fixture = kotlinFixture {
117-
instance<Date> { before(now) }
117+
factory<Date> { before(now) }
118118
}
119119

120120
val value1 = fixture<Date> {
@@ -132,7 +132,7 @@ class GeneratorDateTest {
132132
val minTime = now.time - TimeUnit.HOURS.toMillis(1)
133133

134134
val fixture = kotlinFixture {
135-
instance<Date> { between(Date(minTime), now) }
135+
factory<Date> { between(Date(minTime), now) }
136136
}
137137

138138
repeat(100) {
@@ -150,7 +150,7 @@ class GeneratorDateTest {
150150
val minTime = now.time - TimeUnit.HOURS.toMillis(1)
151151

152152
val fixture = kotlinFixture {
153-
instance<Date> { between(Date(minTime), now) }
153+
factory<Date> { between(Date(minTime), now) }
154154
}
155155

156156
assertIsRandom {
@@ -163,7 +163,7 @@ class GeneratorDateTest {
163163
val minTime = now.time - TimeUnit.HOURS.toMillis(1)
164164

165165
val fixture = kotlinFixture {
166-
instance<Date> { between(Date(minTime), now) }
166+
factory<Date> { between(Date(minTime), now) }
167167
}
168168

169169
val value1 = fixture<Date> {

fixture/src/test/kotlin/com/appmattus/kotlinfixture/resolver/CalendarResolverTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class CalendarResolverTest {
3232
private val now = Date()
3333

3434
private val context = TestContext(
35-
ConfigurationBuilder().apply { instance<Date> { before(now) } }.build(),
36-
CompositeResolver(CalendarResolver(), InstanceResolver())
35+
ConfigurationBuilder().apply { factory<Date> { before(now) } }.build(),
36+
CompositeResolver(CalendarResolver(), FactoryResolver())
3737
)
3838

3939
@Test

fixture/src/test/kotlin/com/appmattus/kotlinfixture/resolver/DateResolverTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ class DateResolverTest {
5656
val value1 = context.seedRandom().resolve(Date::class) as Date
5757
val value2 = context.seedRandom().resolve(Date::class) as Date
5858

59-
assertEquals(value1, value2)
59+
assertEquals(value1.time, value2.time)
6060
}
6161
}

0 commit comments

Comments
 (0)