Skip to content

Commit 7539047

Browse files
committed
Generate DisplayName annotation only for JUnit 5 #576
1 parent af1b2b3 commit 7539047

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
@@ -1614,7 +1614,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16141614
} else {
16151615
setOf(annotation(testFramework.testAnnotationId))
16161616
}
1617-
displayName?.let { testFrameworkManager.addDisplayName(it) }
1617+
1618+
/* Add a short test's description depending on the test framework type:
1619+
DisplayName in case of JUni5, and description argument to Test annotation in case of TestNG.
1620+
*/
1621+
if (displayName != null) {
1622+
when (testFramework) {
1623+
is Junit5 -> {
1624+
displayName.let { testFrameworkManager.addDisplayName(it) }
1625+
}
1626+
is TestNg -> {
1627+
testFrameworkManager.addTestDescription(displayName)
1628+
}
1629+
else -> {
1630+
// nothing
1631+
}
1632+
}
1633+
}
16181634

16191635
val result = currentExecution!!.result
16201636
if (result is UtTimeoutException) {
@@ -1659,12 +1675,19 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16591675
}
16601676
}
16611677

1662-
documentation = CgDocumentationComment(docComment)
1663-
documentation = if (parameterized) {
1678+
val documentationComment = if (parameterized) {
16641679
CgDocumentationComment(text = null)
16651680
} else {
16661681
CgDocumentationComment(docComment)
16671682
}
1683+
documentation.add(documentationComment)
1684+
1685+
/* JUnit4 doesn't have DisplayName annotation and any other suitable for putting short description,
1686+
that's why we add a single line comment below JavaDoc with a test's short description.
1687+
*/
1688+
if (testFramework is Junit4 && displayName != null) {
1689+
documentation.add(CgSingleLineComment(displayName))
1690+
}
16681691
}
16691692
testMethods += testMethod
16701693
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+
collectedTestMethodAnnotations.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+
collectedTestMethodAnnotations += 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-
collectedTestMethodAnnotations += CgCommentedAnnotation(displayName)
208+
collectedTestMethodAnnotations += 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 lateinit var annotations: MutableList<CgAnnotation>
104104
override lateinit var exceptions: MutableSet<ClassId>
105-
override var documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
105+
override var documentation: List<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
@@ -830,7 +830,9 @@ internal abstract class CgAbstractRenderer(val context: CgContext, val printer:
830830
}
831831

832832
private fun renderMethodDocumentation(element: CgMethod) {
833-
element.documentation.accept(this)
833+
for (comment in element.documentation) {
834+
comment.accept(this)
835+
}
834836
}
835837

836838
private fun Byte.toStringConstant() = when {

0 commit comments

Comments
 (0)