@@ -17,6 +17,7 @@ import org.evomaster.core.search.gene.placeholder.CycleObjectGene
1717import org.evomaster.core.search.gene.regex.RegexGene
1818import org.evomaster.core.search.gene.string.Base64StringGene
1919import org.evomaster.core.search.gene.string.StringGene
20+ import org.evomaster.core.search.gene.wrapper.ChoiceGene
2021import org.evomaster.core.search.gene.wrapper.OptionalGene
2122import org.evomaster.core.utils.StringUtils
2223
@@ -37,46 +38,48 @@ class GeneToDto(
3738 }
3839
3940 /* *
40- * @param leafGene to obtain the refType if the component is defined with a name
41+ * @param gene to obtain the refType if the component is defined with a name
4142 * @param fallback to provide a fallback on the DTO named with the action if the component is defined inline
4243 * @param capitalize to determine if the DTO string name must be capitalized for test case writing
4344 *
4445 * @return the DTO name that will be used to instantiate the first variable
4546 */
46- fun getDtoName (leafGene : Gene , fallback : String , capitalize : Boolean ): String {
47- return when (leafGene ) {
48- is ObjectGene -> TestWriterUtils .safeVariableName(leafGene .refType? : fallback)
47+ fun getDtoName (gene : Gene , fallback : String , capitalize : Boolean ): String {
48+ return when (gene ) {
49+ is ObjectGene -> TestWriterUtils .safeVariableName(gene .refType? : fallback)
4950 is ArrayGene <* > -> {
50- val template = leafGene .template
51+ val template = gene .template
5152 if (template is ObjectGene ) {
5253 TestWriterUtils .safeVariableName(template.refType? : fallback)
5354 } else {
5455 // TODO handle arrays of basic data types
5556 return getListType(fallback, template, capitalize)
5657 }
5758 }
58- else -> throw IllegalStateException (" Gene $leafGene is not supported for DTO payloads for action: $fallback " )
59+ is ChoiceGene <* > -> TestWriterUtils .safeVariableName(fallback)
60+ else -> throw IllegalStateException (" Gene $gene is not supported for DTO payloads for action: $fallback " )
5961 }
6062 }
6163
6264 /* *
6365 * @param gene from which to extract the setter calls
6466 * @param dtoName that will be instantiated for payload
65- * @param counter list to provide uniqueness under the same DTO being used in a single test case
67+ * @param counters list to provide uniqueness under the same DTO being used in a single test case
6668 * @param capitalize to determine if the DTO string name must be capitalized for test case writing
6769 *
6870 * @return a [DtoCall] object that can be written to the test case
6971 */
70- fun getDtoCall (gene : Gene , dtoName : String , counter : MutableList <Int >, capitalize : Boolean ): DtoCall {
72+ fun getDtoCall (gene : Gene , dtoName : String , counters : MutableList <Int >, capitalize : Boolean ): DtoCall {
7173 return when (gene) {
72- is ObjectGene -> getObjectDtoCall(gene, dtoName, counter, capitalize)
73- is ArrayGene <* > -> getArrayDtoCall(gene, dtoName, counter, null , capitalize)
74+ is ObjectGene -> getObjectDtoCall(gene, dtoName, counters)
75+ is ArrayGene <* > -> getArrayDtoCall(gene, dtoName, counters, null , capitalize)
76+ is ChoiceGene <* > -> getDtoCall(gene.activeGene(), dtoName, counters, capitalize)
7477 else -> throw RuntimeException (" BUG: Gene $gene (with type ${this ::class .java.simpleName} ) should not be creating DTOs" )
7578 }
7679 }
7780
78- private fun getObjectDtoCall (gene : ObjectGene , dtoName : String , counter : MutableList <Int >, capitalize : Boolean ): DtoCall {
79- val dtoVarName = " dto_${dtoName} _${counter .joinToString(" _" )} "
81+ private fun getObjectDtoCall (gene : ObjectGene , dtoName : String , counters : MutableList <Int >): DtoCall {
82+ val dtoVarName = " dto_${dtoName} _${counters .joinToString(" _" )} "
8083
8184 val result = mutableListOf<String >()
8285 result.add(dtoOutput.getNewObjectStatement(dtoName, dtoVarName))
@@ -90,13 +93,13 @@ class GeneToDto(
9093 val attributeName = it.name
9194 when (leafGene) {
9295 is ObjectGene -> {
93- val childDtoCall = getDtoCall(leafGene, getDtoName(leafGene, attributeName, true ), counter , true )
96+ val childDtoCall = getDtoCall(leafGene, getDtoName(leafGene, attributeName, true ), counters , true )
9497
9598 result.addAll(childDtoCall.objectCalls)
9699 result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, childDtoCall.varName))
97100 }
98101 is ArrayGene <* > -> {
99- val childDtoCall = getArrayDtoCall(leafGene, getDtoName(leafGene, attributeName, true ), counter , attributeName, true )
102+ val childDtoCall = getArrayDtoCall(leafGene, getDtoName(leafGene, attributeName, true ), counters , attributeName, true )
100103
101104 result.addAll(childDtoCall.objectCalls)
102105 result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, childDtoCall.varName))
@@ -110,20 +113,20 @@ class GeneToDto(
110113 return DtoCall (dtoVarName, result)
111114 }
112115
113- private fun getArrayDtoCall (gene : ArrayGene <* >, dtoName : String , counter : MutableList <Int >, targetAttribute : String? , capitalize : Boolean ): DtoCall {
116+ private fun getArrayDtoCall (gene : ArrayGene <* >, dtoName : String , counters : MutableList <Int >, targetAttribute : String? , capitalize : Boolean ): DtoCall {
114117 val result = mutableListOf<String >()
115118 val template = gene.template
116119
117120 val listType = getListType(dtoName,template, capitalize)
118- val listVarName = " list_${targetAttribute? : dtoName} _${counter .joinToString(" _" )} "
121+ val listVarName = " list_${targetAttribute? : dtoName} _${counters .joinToString(" _" )} "
119122 result.add(dtoOutput.getNewListStatement(listType, listVarName))
120123
121124 if (template is ObjectGene ) {
122125 val childDtoName = template.refType? : if (capitalize) StringUtils .capitalization(dtoName) else dtoName
123126 var listCounter = 1
124127 gene.getViewOfElements().forEach {
125128 val childCounter = mutableListOf<Int >()
126- childCounter.addAll(counter )
129+ childCounter.addAll(counters )
127130 childCounter.add(listCounter++ )
128131 val childDtoCall = getDtoCall(it,childDtoName, childCounter, true )
129132 result.addAll(childDtoCall.objectCalls)
0 commit comments