-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #49
- Loading branch information
Showing
15 changed files
with
655 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
...in/com/appmattus/kotlinfixture/decorator/constructor/ArrayFavouringConstructorStrategy.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,55 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.starProjectedType | ||
|
||
/** | ||
* Order constructors, selecting those with the most parameters of Array<*> before any other. Any other constructors | ||
* are returned with the most modest constructors first. | ||
*/ | ||
object ArrayFavouringConstructorStrategy : ConstructorStrategy { | ||
|
||
@Suppress("EXPERIMENTAL_API_USAGE") | ||
private val arrayTypes = listOf( | ||
Array<Any>::class, | ||
|
||
BooleanArray::class, | ||
ByteArray::class, | ||
DoubleArray::class, | ||
FloatArray::class, | ||
IntArray::class, | ||
LongArray::class, | ||
ShortArray::class, | ||
CharArray::class, | ||
UByteArray::class, | ||
UIntArray::class, | ||
ULongArray::class, | ||
UShortArray::class | ||
).map { it.starProjectedType } | ||
|
||
override fun constructors(context: Context, obj: KClass<*>): Collection<KFunction<*>> { | ||
return ModestConstructorStrategy.constructors(context, obj).sortedByDescending { | ||
it.parameters.count { parameter -> | ||
(parameter.type.classifier as KClass<*>).starProjectedType in arrayTypes | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...main/kotlin/com/appmattus/kotlinfixture/decorator/constructor/ConstructorConfiguration.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,28 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.config.ConfigurationBuilder | ||
|
||
/** | ||
* Set the ordering strategy used to try class constructors when generating an instance. | ||
* Default: [RandomConstructorStrategy] | ||
*/ | ||
@Suppress("unused") | ||
fun ConfigurationBuilder.constructorStrategy(strategy: ConstructorStrategy) { | ||
strategies[ConstructorStrategy::class] = strategy | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/main/kotlin/com/appmattus/kotlinfixture/decorator/constructor/ConstructorStrategy.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,31 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
|
||
/** | ||
* Strategy used to determine order to try class constructors when generating an instance. | ||
*/ | ||
interface ConstructorStrategy { | ||
/** | ||
* Returns [obj] constructors in the order to try when generating an instance. | ||
*/ | ||
fun constructors(context: Context, obj: KClass<*>): Collection<KFunction<*>> | ||
} |
33 changes: 33 additions & 0 deletions
33
...ain/kotlin/com/appmattus/kotlinfixture/decorator/constructor/GreedyConstructorStrategy.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,33 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
|
||
/** | ||
* Order constructors by the most greedy constructor first, i.e. greater parameters returned first. This means that if a | ||
* default constructor exists, it will be the last one returned. | ||
* | ||
* In case of two constructors with an equal number of parameters, the ordering is unspecified. | ||
*/ | ||
object GreedyConstructorStrategy : ConstructorStrategy { | ||
override fun constructors(context: Context, obj: KClass<*>): Collection<KFunction<*>> { | ||
return obj.constructors.sortedByDescending { it.parameters.size } | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...lin/com/appmattus/kotlinfixture/decorator/constructor/ListFavouringConstructorStrategy.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,36 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.starProjectedType | ||
|
||
/** | ||
* Order constructors, selecting those with the most parameters matching List<*> before any other. Any other | ||
* constructors are returned with the most modest constructors first. | ||
*/ | ||
object ListFavouringConstructorStrategy : ConstructorStrategy { | ||
override fun constructors(context: Context, obj: KClass<*>): Collection<KFunction<*>> { | ||
return ModestConstructorStrategy.constructors(context, obj).sortedByDescending { | ||
it.parameters.count { parameter -> | ||
(parameter.type.classifier as KClass<*>).starProjectedType == List::class.starProjectedType | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ain/kotlin/com/appmattus/kotlinfixture/decorator/constructor/ModestConstructorStrategy.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,33 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
|
||
/** | ||
* Order constructors by the most modest constructor first, i.e. fewer parameters returned first. This means that if a | ||
* default constructor exists, it will be the first one returned. | ||
* | ||
* In case of two constructors with an equal number of parameters, the ordering is unspecified. | ||
*/ | ||
object ModestConstructorStrategy : ConstructorStrategy { | ||
override fun constructors(context: Context, obj: KClass<*>): Collection<KFunction<*>> { | ||
return obj.constructors.sortedBy { it.parameters.size } | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ain/kotlin/com/appmattus/kotlinfixture/decorator/constructor/RandomConstructorStrategy.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,30 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.Context | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
|
||
/** | ||
* Order constructors at random. | ||
*/ | ||
object RandomConstructorStrategy : ConstructorStrategy { | ||
override fun constructors(context: Context, obj: KClass<*>): Collection<KFunction<*>> { | ||
return obj.constructors.shuffled(context.random) | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...om/appmattus/kotlinfixture/decorator/constructor/ArrayFavouringConstructorStrategyTest.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,65 @@ | ||
/* | ||
* Copyright 2020 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.constructor | ||
|
||
import com.appmattus.kotlinfixture.ContextImpl | ||
import com.appmattus.kotlinfixture.config.Configuration | ||
import com.nhaarman.mockitokotlin2.doReturn | ||
import com.nhaarman.mockitokotlin2.mock | ||
import kotlin.reflect.KClass | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ArrayFavouringConstructorStrategyTest { | ||
|
||
@Test | ||
fun `Order constructors with greatest array parameter count first followed by remaining modest first`() { | ||
val context = ContextImpl(Configuration()) | ||
|
||
val shuffledConstructors = mock<KClass<MultipleConstructors>> { | ||
on { constructors } doReturn MultipleConstructors::class.constructors.shuffled() | ||
} | ||
|
||
val result = ArrayFavouringConstructorStrategy.constructors(context, shuffledConstructors).map { | ||
val emptyParameters = List<Any?>(it.parameters.size) { null } | ||
(it.call(*emptyParameters.toTypedArray()) as MultipleConstructors).constructorCalled | ||
} | ||
|
||
assertEquals(listOf("array-2", "array-1", "primary", "string"), result) | ||
} | ||
|
||
@Suppress("unused", "UNUSED_PARAMETER") | ||
class MultipleConstructors { | ||
val constructorCalled: String | ||
|
||
constructor() { | ||
constructorCalled = "primary" | ||
} | ||
|
||
constructor(array: Array<String>?) { | ||
constructorCalled = "array-1" | ||
} | ||
|
||
constructor(value: String?) { | ||
constructorCalled = "string" | ||
} | ||
|
||
constructor(array1: Array<String>?, array2: BooleanArray?) { | ||
constructorCalled = "array-2" | ||
} | ||
} | ||
} |
Oops, something went wrong.