Skip to content

Commit

Permalink
Merge pull request #12 from thumbtack/collectionSize
Browse files Browse the repository at this point in the history
MINF-2398 Specify collectionSize for collections
  • Loading branch information
kelvinwong11 authored Jun 26, 2024
2 parents 7322891 + ffaff63 commit cd90d3a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
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)
}
}

0 comments on commit cd90d3a

Please sign in to comment.