Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MINF-2398 Specify collectionSize for collections #12

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.javaType

private const val DEFAULT_COLLECTION_SIZE = 3

/**
* Generates a test object for the given data class, where each field is given an actual,
* unique value (as opposed to just some default value like empty string or null).
Expand Down Expand Up @@ -66,20 +68,24 @@ import kotlin.reflect.javaType
* specify a consistent value for this field.
* @param useNullForNullableFields If true, then all fields that are nullable will be set to null,
* unless specified in the overrides map.
* @param collectionSize If specified, supported collection types will be set to the specified
* size rather than the default of size 3
* @return The generated object.
*/
fun <T : Any> KClass<T>.generateTestObject(
prefix: String = "",
overrides: Map<Regex, Any?>? = null,
referenceDate: Date? = null,
useNullForNullableFields: Boolean = false,
collectionSize: Int = DEFAULT_COLLECTION_SIZE,
): T {
return generateTestObject(
prefix,
Parameters(
overrides,
referenceDate,
useNullForNullableFields,
collectionSize.takeIf { it > 0 } ?: DEFAULT_COLLECTION_SIZE,
)
)
}
Expand Down Expand Up @@ -142,45 +148,36 @@ private fun generateValueForParameterizedType(
prefix: String,
params: Parameters,
): Any {
val collectionSize = params.collectionSize
runCatching {
when (type.rawType) {
java.util.List::class.java,
ArrayList::class.java ->
return List(3) { index ->
return List(collectionSize) { index ->
generateValueForField(
generateFieldType(type),
"$prefix$index",
params,
)
}
java.util.Set::class.java ->
return setOf(
return List(collectionSize) { index ->
generateValueForField(
generateFieldType(type),
"${prefix}0",
params,
),
generateValueForField(
generateFieldType(type),
"${prefix}1",
params,
),
generateValueForField(
generateFieldType(type),
"${prefix}2",
"$prefix$index",
params,
)
)
}.toSet()
ArrayList::class.java ->
return List(3) { index ->
return List(collectionSize) { index ->
generateValueForField(
generateFieldType(type),
"$prefix$index",
params,
)
}
java.util.Map::class.java ->
return (0..2).associate { index ->
return (0..< collectionSize).associate { index ->
generateValueForField(
generateFieldType(type, 0),
"${prefix}${index}key",
Expand All @@ -194,7 +191,7 @@ private fun generateValueForParameterizedType(
}
java.util.HashMap::class.java ->
return HashMap(
(0..2).associate { index ->
(0..< collectionSize).associate { index ->
generateValueForField(
generateFieldType(type, 0),
"${prefix}${index}key",
Expand Down Expand Up @@ -291,4 +288,5 @@ private data class Parameters(
val overrides: Map<Regex, Any?>?,
val referenceDate: Date?,
val useNullForNullableFields: Boolean,
val collectionSize: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class GenerateTestObjectTest {
val booleanField: Boolean,
val charField: Char,
val listField: List<String>,
val setField: Set<String>,
val mapField: Map<String, String>,
val intArray: IntArray,
val floatArray: FloatArray,
val doubleArray: DoubleArray,
Expand Down Expand Up @@ -50,4 +52,14 @@ class GenerateTestObjectTest {
assertEquals(3, testObject.booleanArray.size)
assertTrue(testObject.booleanArray.all { !it })
}

@Test
fun `test collectionSize`() {
val collectionSize = 5
val testObject = SampleClass::class.generateTestObject(collectionSize = collectionSize)

assertEquals(collectionSize, testObject.listField.size)
assertEquals(collectionSize, testObject.setField.size)
assertEquals(collectionSize, testObject.mapField.size)
}
}