-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to override the optional strategy used (#35)
Fixes #33
- Loading branch information
Showing
16 changed files
with
703 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/ConfigurationDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.config | ||
|
||
@DslMarker | ||
annotation class ConfigurationDsl |
25 changes: 25 additions & 0 deletions
25
.../src/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/AlwaysOptionalStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
|
||
object AlwaysOptionalStrategy : OptionalStrategy { | ||
|
||
override fun Context.generateAsOptional(callingClass: KClass<*>, parameterName: String) = true | ||
} |
25 changes: 25 additions & 0 deletions
25
...e/src/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/NeverOptionalStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
|
||
object NeverOptionalStrategy : OptionalStrategy { | ||
|
||
override fun Context.generateAsOptional(callingClass: KClass<*>, parameterName: String) = false | ||
} |
27 changes: 27 additions & 0 deletions
27
...e/src/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/OptionalConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.config.ConfigurationBuilder | ||
|
||
@Suppress("unused") | ||
fun ConfigurationBuilder.optionalStrategy( | ||
defaultStrategy: OptionalStrategy = RandomlyOptionalStrategy, | ||
init: OptionalStrategyBuilder.() -> Unit = {} | ||
) { | ||
strategies[OptionalStrategy::class] = OptionalStrategyBuilder(defaultStrategy).apply(init).build() | ||
} |
24 changes: 24 additions & 0 deletions
24
fixture/src/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/OptionalStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
|
||
interface OptionalStrategy { | ||
fun Context.generateAsOptional(callingClass: KClass<*>, parameterName: String): Boolean | ||
} |
71 changes: 71 additions & 0 deletions
71
...src/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/OptionalStrategyBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.config.ConfigurationDsl | ||
import com.appmattus.kotlinfixture.toUnmodifiableMap | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KMutableProperty1 | ||
import kotlin.reflect.KParameter | ||
import kotlin.reflect.KProperty1 | ||
|
||
@ConfigurationDsl | ||
class OptionalStrategyBuilder(private val defaultStrategy: OptionalStrategy) { | ||
|
||
private val classes: MutableMap<KClass<*>, OptionalStrategy> = mutableMapOf() | ||
|
||
private val properties: MutableMap<KClass<*>, MutableMap<String, OptionalStrategy>> = mutableMapOf() | ||
|
||
inline fun <reified T> classOverride(strategy: OptionalStrategy) = classOverride(T::class, strategy) | ||
|
||
fun classOverride(superType: KClass<*>, strategy: OptionalStrategy) { | ||
classes[superType] = strategy | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
inline fun <reified T> propertyOverride(propertyName: String, strategy: OptionalStrategy) = | ||
propertyOverride(T::class, propertyName, strategy) | ||
|
||
inline fun <reified T> propertyOverride(property: KProperty1<T, *>, strategy: OptionalStrategy) { | ||
// Only allow read only properties in constructor(s) | ||
if (property !is KMutableProperty1) { | ||
val constructorParams = T::class.constructors.flatMap { | ||
it.parameters.map(KParameter::name) | ||
} | ||
|
||
check(constructorParams.contains(property.name)) { | ||
"No setter available for ${T::class.qualifiedName}.${property.name}" | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return propertyOverride(T::class, property.name, strategy) | ||
} | ||
|
||
fun propertyOverride(clazz: KClass<*>, propertyName: String, strategy: OptionalStrategy) { | ||
val classProperties = properties.getOrElse(clazz) { mutableMapOf() } | ||
classProperties[propertyName] = strategy | ||
|
||
properties[clazz] = classProperties | ||
} | ||
|
||
fun build(): OptionalStrategy = OverrideOptionalStrategy( | ||
defaultStrategy = defaultStrategy, | ||
propertyOverrides = properties.mapValues { it.value.toUnmodifiableMap() }.toUnmodifiableMap(), | ||
classOverrides = classes.toUnmodifiableMap() | ||
) | ||
} |
45 changes: 45 additions & 0 deletions
45
...rc/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/OverrideOptionalStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
|
||
data class OverrideOptionalStrategy( | ||
private val defaultStrategy: OptionalStrategy, | ||
private val propertyOverrides: Map<KClass<*>, Map<String, OptionalStrategy>> = emptyMap(), | ||
private val classOverrides: Map<KClass<*>, OptionalStrategy> = emptyMap() | ||
) : OptionalStrategy { | ||
|
||
@Suppress("ReturnCount") | ||
override fun Context.generateAsOptional(callingClass: KClass<*>, parameterName: String): Boolean { | ||
// first check property overrides | ||
propertyOverrides[callingClass]?.get(parameterName)?.apply { | ||
return generateAsOptional(callingClass, parameterName) | ||
} | ||
|
||
// second check class overrides | ||
classOverrides[callingClass]?.apply { | ||
return generateAsOptional(callingClass, parameterName) | ||
} | ||
|
||
// third use default | ||
with(defaultStrategy) { | ||
return generateAsOptional(callingClass, parameterName) | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rc/main/kotlin/com/appmattus/kotlinfixture/decorator/optional/RandomlyOptionalStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
|
||
object RandomlyOptionalStrategy : OptionalStrategy { | ||
|
||
override fun Context.generateAsOptional(callingClass: KClass<*>, parameterName: String) = random.nextBoolean() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../test/kotlin/com/appmattus/kotlinfixture/decorator/optional/AlwaysOptionalStrategyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2019 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.kotlinfixture.decorator.optional | ||
|
||
import com.appmattus.kotlinfixture.TestContext | ||
import com.appmattus.kotlinfixture.config.ConfigurationBuilder | ||
import com.appmattus.kotlinfixture.resolver.TestResolver | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class AlwaysOptionalStrategyTest { | ||
|
||
private val testContext = TestContext(ConfigurationBuilder().build(), TestResolver()) | ||
|
||
data class DataClass(val optionalValue: String = "hello") | ||
|
||
@Test | ||
fun `Strategy AlwaysOptionalStrategy returns true`(): Unit = with(testContext) { | ||
AlwaysOptionalStrategy.apply { | ||
assertTrue { | ||
generateAsOptional(DataClass::class, "optionalValue") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.