Skip to content

Commit 8899b63

Browse files
authored
Named property request and resolver to make interception easier (#46)
1 parent c232e71 commit 8899b63

File tree

11 files changed

+111
-39
lines changed

11 files changed

+111
-39
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import com.appmattus.kotlinfixture.resolver.HashtableKTypeResolver
4444
import com.appmattus.kotlinfixture.resolver.IterableKTypeResolver
4545
import com.appmattus.kotlinfixture.resolver.JodaTimeResolver
4646
import com.appmattus.kotlinfixture.resolver.KFunctionResolver
47+
import com.appmattus.kotlinfixture.resolver.KNamedPropertyResolver
4748
import com.appmattus.kotlinfixture.resolver.KTypeResolver
4849
import com.appmattus.kotlinfixture.resolver.LocaleResolver
4950
import com.appmattus.kotlinfixture.resolver.MapKTypeResolver
@@ -131,6 +132,7 @@ data class Configuration(
131132
MapKTypeResolver(),
132133

133134
KTypeResolver(),
135+
KNamedPropertyResolver(),
134136
KFunctionResolver(),
135137

136138
AbstractClassResolver(),

fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/ClassResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ internal class ClassResolver : Resolver, PopulateInstance {
3030
context = context,
3131
obj = obj,
3232
constructorParameterNames = obj.constructorParameterNames(),
33-
overrides = context.configuration.properties.getOrElse(obj) { emptyMap() }
33+
callingClass = obj
3434
)
3535

3636
val results = obj.constructors.shuffled().map { constructor ->

fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/FactoryMethodResolver.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,10 +42,10 @@ class FactoryMethodResolver : Resolver, PopulateInstance {
4242
if (result !is Unresolved) {
4343
if (result != null) {
4444
val callContext = PopulateInstance.CallContext(
45-
context,
46-
result::class,
45+
context = context,
46+
obj = result::class,
4747
constructorParameterNames = emptySet(),
48-
overrides = context.configuration.properties.getOrElse(obj) { emptyMap() }
48+
callingClass = obj
4949
)
5050

5151
populatePropertiesAndSetters(callContext, result)

fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/KFunctionResolver.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package com.appmattus.kotlinfixture.resolver
1818

1919
import com.appmattus.kotlinfixture.Context
2020
import com.appmattus.kotlinfixture.Unresolved
21-
import com.appmattus.kotlinfixture.config.DefaultGenerator
2221
import com.appmattus.kotlinfixture.createUnresolved
2322
import com.appmattus.kotlinfixture.decorator.optional.OptionalStrategy
2423
import com.appmattus.kotlinfixture.decorator.optional.RandomlyOptionalStrategy
@@ -36,16 +35,16 @@ internal class KFunctionResolver : Resolver {
3635
val overrides = context.configuration.properties.getOrElse(obj.containingClass) { emptyMap() }
3736

3837
val parameters = obj.function.parameters.associateWith {
39-
if (it.kind == KParameter.Kind.VALUE) {
40-
if (it.name in overrides) {
41-
overrides[it.name]?.invoke(DefaultGenerator(context))
42-
} else {
43-
context.resolve(it.type)
38+
when (it.kind) {
39+
KParameter.Kind.VALUE -> {
40+
context.resolve(KNamedPropertyRequest(it.name, obj.containingClass, it.type))
41+
}
42+
KParameter.Kind.INSTANCE -> {
43+
obj.containingClass.companionObjectInstance
44+
}
45+
else -> {
46+
throw IllegalStateException("Unsupported parameter type: $it")
4447
}
45-
} else if (it.kind == KParameter.Kind.INSTANCE) {
46-
obj.containingClass.companionObjectInstance
47-
} else {
48-
throw IllegalStateException("Unsupported parameter type: $it")
4948
}
5049
}.filterKeys {
5150
with(context) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 Appmattus Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.appmattus.kotlinfixture.resolver
18+
19+
import kotlin.reflect.KClass
20+
import kotlin.reflect.KType
21+
22+
internal data class KNamedPropertyRequest(
23+
val name: String?,
24+
val containingClass: KClass<*>,
25+
val type: KType
26+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 Appmattus Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.appmattus.kotlinfixture.resolver
18+
19+
import com.appmattus.kotlinfixture.Context
20+
import com.appmattus.kotlinfixture.Unresolved
21+
import com.appmattus.kotlinfixture.config.DefaultGenerator
22+
23+
internal class KNamedPropertyResolver : Resolver {
24+
override fun resolve(context: Context, obj: Any): Any? {
25+
if (obj is KNamedPropertyRequest) {
26+
val overrides = context.configuration.properties.getOrElse(obj.containingClass) { emptyMap() }
27+
28+
return if (obj.name in overrides) {
29+
overrides[obj.name]?.invoke(DefaultGenerator(context))
30+
} else {
31+
context.resolve(obj.type)
32+
}
33+
}
34+
35+
return Unresolved.Unhandled
36+
}
37+
}

fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/PopulateInstance.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@ package com.appmattus.kotlinfixture.resolver
1818

1919
import com.appmattus.kotlinfixture.Context
2020
import com.appmattus.kotlinfixture.Unresolved
21-
import com.appmattus.kotlinfixture.config.GeneratorFun
22-
import com.appmattus.kotlinfixture.config.DefaultGenerator
2321
import kotlin.reflect.KClass
2422
import kotlin.reflect.KFunction
2523
import kotlin.reflect.KMutableProperty
@@ -34,16 +32,15 @@ internal interface PopulateInstance {
3432
val context: Context,
3533
val obj: KClass<*>,
3634
val constructorParameterNames: Set<String?>,
37-
val overrides: Map<String, GeneratorFun>
35+
val callingClass: KClass<*>
3836
)
3937

4038
fun populatePropertiesAndSetters(
4139
callContext: CallContext,
4240
result: Any?
4341
): Boolean {
4442
return try {
45-
populateKotlinProperties(callContext, result) &&
46-
populateJavaSetters(callContext, result)
43+
populateKotlinProperties(callContext, result) && populateJavaSetters(callContext, result)
4744
} catch (expected: Error) {
4845
// If retrieving setters fails we fail regardless of constructor used
4946
false
@@ -60,11 +57,9 @@ internal interface PopulateInstance {
6057
val name = it.name.removePrefix("set").decapitalize()
6158
callContext.constructorParameterNames.contains(name)
6259
}.forEach {
63-
val propertyResult = if (it.name in callContext.overrides) {
64-
callContext.overrides[it.name]?.invoke(DefaultGenerator(callContext.context))
65-
} else {
66-
callContext.context.resolve(it.valueParameters[0].type)
67-
}
60+
val propertyResult = callContext.context.resolve(
61+
KNamedPropertyRequest(it.name, callContext.callingClass, it.valueParameters[0].type)
62+
)
6863

6964
if (propertyResult is Unresolved) {
7065
return false
@@ -83,11 +78,9 @@ internal interface PopulateInstance {
8378
callContext.obj.settableMutableProperties()
8479
.filterNot { callContext.constructorParameterNames.contains(it.name) }
8580
.forEach { property ->
86-
val propertyResult = if (property.name in callContext.overrides) {
87-
callContext.overrides[property.name]?.invoke(DefaultGenerator(callContext.context))
88-
} else {
89-
callContext.context.resolve(property.returnType)
90-
}
81+
val propertyResult = callContext.context.resolve(
82+
KNamedPropertyRequest(property.name, callContext.callingClass, property.returnType)
83+
)
9184

9285
if (propertyResult is Unresolved) {
9386
return false

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,14 @@ import kotlin.test.assertTrue
3131
class ClassResolverTest {
3232
private val context = TestContext(
3333
Configuration(),
34-
CompositeResolver(PrimitiveResolver(), StringResolver(), KTypeResolver(), ClassResolver(), KFunctionResolver())
34+
CompositeResolver(
35+
PrimitiveResolver(),
36+
StringResolver(),
37+
KTypeResolver(),
38+
ClassResolver(),
39+
KNamedPropertyResolver(),
40+
KFunctionResolver()
41+
)
3542
)
3643

3744
@Test

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ class FactoryMethodResolverTest {
3434
PrimitiveResolver(),
3535
StringResolver(),
3636
KTypeResolver(),
37+
KNamedPropertyResolver(),
3738
FactoryMethodResolver(),
3839
KFunctionResolver()
3940
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,6 +119,7 @@ class IterableKTypeResolverTest {
119119
PrimitiveResolver(),
120120
KTypeResolver(),
121121
KFunctionResolver(),
122+
KNamedPropertyResolver(),
122123
ClassResolver()
123124
)
124125
)

0 commit comments

Comments
 (0)