Skip to content

Commit 9db172b

Browse files
committed
Generate DisplayName annotation only for JUnit 5 #576
1 parent dcf607b commit 9db172b

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

+26-3
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
15991599
} else {
16001600
setOf(annotation(testFramework.testAnnotationId))
16011601
}
1602-
displayName?.let { testFrameworkManager.addDisplayName(it) }
1602+
1603+
/* Add a short test's description depending on the test framework type:
1604+
DisplayName in case of JUni5, and description argument to Test annotation in case of TestNG.
1605+
*/
1606+
if (displayName != null) {
1607+
when (testFramework) {
1608+
is Junit5 -> {
1609+
displayName.let { testFrameworkManager.addDisplayName(it) }
1610+
}
1611+
is TestNg -> {
1612+
testFrameworkManager.addTestDescription(displayName)
1613+
}
1614+
else -> {
1615+
// nothing
1616+
}
1617+
}
1618+
}
16031619

16041620
val result = currentExecution!!.result
16051621
if (result is UtTimeoutException) {
@@ -1645,12 +1661,19 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16451661
}
16461662
}
16471663

1648-
documentation = CgDocumentationComment(docComment)
1649-
documentation = if (parameterized) {
1664+
val documentationComment = if (parameterized) {
16501665
CgDocumentationComment(text = null)
16511666
} else {
16521667
CgDocumentationComment(docComment)
16531668
}
1669+
documentation.add(documentationComment)
1670+
1671+
/* JUnit4 doesn't have DisplayName annotation and any other suitable for putting short description,
1672+
that's why we add a single line comment below JavaDoc with a test's short description.
1673+
*/
1674+
if (testFramework is Junit4 && displayName != null) {
1675+
documentation.add(CgSingleLineComment(displayName))
1676+
}
16541677
}
16551678
testMethods += testMethod
16561679
return testMethod

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/TestFrameworkManager.kt

+30-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
1515
import org.utbot.framework.codegen.model.constructor.util.CgComponents
1616
import org.utbot.framework.codegen.model.constructor.util.classCgClassId
1717
import org.utbot.framework.codegen.model.constructor.util.importIfNeeded
18-
import org.utbot.framework.codegen.model.tree.CgCommentedAnnotation
1918
import org.utbot.framework.codegen.model.tree.CgEnumConstantAccess
2019
import org.utbot.framework.codegen.model.tree.CgExpression
2120
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
@@ -174,13 +173,39 @@ internal abstract class TestFrameworkManager(val context: CgContext)
174173
}
175174
}
176175

176+
/**
177+
* Supplements TestNG @Test annotation with a description.
178+
* It looks like @Test(description="...")
179+
*
180+
* Should be used only with TestNG.
181+
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
182+
*/
183+
open fun addTestDescription(description: String?) {
184+
if (description == null) return
185+
val testAnnotation =
186+
collectedMethodAnnotations.singleOrNull { it.classId == testFramework.testAnnotationId }
187+
188+
val descriptionArgument = CgNamedAnnotationArgument("description", stringLiteral(description))
189+
if (testAnnotation is CgMultipleArgsAnnotation) {
190+
testAnnotation.arguments += descriptionArgument
191+
} else {
192+
collectedMethodAnnotations += CgMultipleArgsAnnotation(
193+
testFramework.testAnnotationId,
194+
mutableListOf(descriptionArgument)
195+
)
196+
}
197+
}
198+
177199
abstract fun disableTestMethod(reason: String)
178200

179-
// We add a commented JUnit5 DisplayName annotation here by default,
180-
// because other test frameworks do not support such feature.
201+
/**
202+
* Adds @DisplayName annotation.
203+
*
204+
* Should be used only with JUnit 5.
205+
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
206+
*/
181207
open fun addDisplayName(name: String) {
182-
val displayName = CgSingleArgAnnotation(Junit5.displayNameClassId, stringLiteral(name))
183-
collectedMethodAnnotations += CgCommentedAnnotation(displayName)
208+
collectedMethodAnnotations += CgSingleArgAnnotation(Junit5.displayNameClassId, stringLiteral(name))
184209
}
185210

186211
protected fun ClassId.toExceptionClass(): CgExpression =

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/Builders.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface CgMethodBuilder<T : CgMethod> : CgBuilder<T> {
5353
val statements: List<CgStatement>
5454
val exceptions: Set<ClassId>
5555
val annotations: List<CgAnnotation>
56-
val documentation: CgDocumentationComment
56+
val documentation: List<CgComment>
5757
}
5858

5959
class CgTestMethodBuilder : CgMethodBuilder<CgTestMethod> {
@@ -64,7 +64,7 @@ class CgTestMethodBuilder : CgMethodBuilder<CgTestMethod> {
6464
override val exceptions: MutableSet<ClassId> = mutableSetOf()
6565
override val annotations: MutableList<CgAnnotation> = mutableListOf()
6666
lateinit var methodType: CgTestMethodType
67-
override var documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
67+
override var documentation: MutableList<CgComment> = mutableListOf()
6868

6969
override fun build() = CgTestMethod(
7070
name,
@@ -87,7 +87,7 @@ class CgErrorTestMethodBuilder : CgMethodBuilder<CgErrorTestMethod> {
8787
override lateinit var statements: List<CgStatement>
8888
override val exceptions: Set<ClassId> = emptySet()
8989
override val annotations: List<CgAnnotation> = emptyList()
90-
override var documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
90+
override var documentation: List<CgComment> = emptyList()
9191

9292
override fun build() = CgErrorTestMethod(name, statements, documentation)
9393
}
@@ -102,7 +102,7 @@ class CgParameterizedTestDataProviderBuilder : CgMethodBuilder<CgParameterizedTe
102102
override lateinit var statements: List<CgStatement>
103103
override val annotations: MutableList<CgAnnotation> = mutableListOf()
104104
override val exceptions: MutableSet<ClassId> = mutableSetOf()
105-
override var documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
105+
override var documentation: MutableList<CgDocumentationComment> = mutableListOf()
106106

107107
override fun build() = CgParameterizedTestDataProviderMethod(name, statements, returnType, annotations, exceptions)
108108
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/tree/CgElement.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ sealed class CgMethod(open val isStatic: Boolean) : CgElement {
190190
abstract val statements: List<CgStatement>
191191
abstract val exceptions: Set<ClassId>
192192
abstract val annotations: List<CgAnnotation>
193-
abstract val documentation: CgDocumentationComment
193+
abstract val documentation: List<CgComment>
194194
abstract val requiredFields: List<CgParameterDeclaration>
195195
}
196196

@@ -202,14 +202,14 @@ class CgTestMethod(
202202
override val exceptions: Set<ClassId>,
203203
override val annotations: List<CgAnnotation>,
204204
val type: CgTestMethodType,
205-
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList()),
205+
override val documentation: List<CgComment> = emptyList(),
206206
override val requiredFields: List<CgParameterDeclaration> = emptyList(),
207207
) : CgMethod(false)
208208

209209
class CgErrorTestMethod(
210210
override val name: String,
211211
override val statements: List<CgStatement>,
212-
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
212+
override val documentation: List<CgComment> = emptyList()
213213
) : CgMethod(false) {
214214
override val exceptions: Set<ClassId> = emptySet()
215215
override val returnType: ClassId = voidClassId
@@ -226,7 +226,7 @@ class CgParameterizedTestDataProviderMethod(
226226
override val exceptions: Set<ClassId>,
227227
) : CgMethod(isStatic = true) {
228228
override val parameters: List<CgParameterDeclaration> = emptyList()
229-
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
229+
override val documentation: List<CgDocumentationComment> = emptyList()
230230
override val requiredFields: List<CgParameterDeclaration> = emptyList()
231231
}
232232

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/visitor/CgAbstractRenderer.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,9 @@ internal abstract class CgAbstractRenderer(val context: CgContext, val printer:
818818
}
819819

820820
private fun renderMethodDocumentation(element: CgMethod) {
821-
element.documentation.accept(this)
821+
for (comment in element.documentation) {
822+
comment.accept(this)
823+
}
822824
}
823825

824826
private fun Byte.toStringConstant() = when {

0 commit comments

Comments
 (0)