Skip to content

Generate DisplayName annotation only for JUnit 5 #576 #624

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

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1599,7 +1599,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
} else {
setOf(annotation(testFramework.testAnnotationId))
}
displayName?.let { testFrameworkManager.addDisplayName(it) }

/* Add a short test's description depending on the test framework type:
DisplayName in case of JUni5, and description argument to Test annotation in case of TestNG.
*/
if (displayName != null) {
when (testFramework) {
is Junit5 -> {
displayName.let { testFrameworkManager.addDisplayName(it) }
}
is TestNg -> {
testFrameworkManager.addTestDescription(displayName)
}
else -> {
// nothing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to create a ticket to add a custom Java tag for display name as @DisplayName or @summary for JUnit 4

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that we just forget the generated display name :( for JUnit4 but generates it.

No logging in this file at all, it's sad too

}
}
}

val result = currentExecution!!.result
if (result is UtTimeoutException) {
Expand Down Expand Up @@ -1645,12 +1661,19 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
}
}

documentation = CgDocumentationComment(docComment)
documentation = if (parameterized) {
val documentationComment = if (parameterized) {
CgDocumentationComment(text = null)
} else {
CgDocumentationComment(docComment)
}
documentation.add(documentationComment)

/* JUnit4 doesn't have DisplayName annotation and any other suitable for putting short description,
that's why we add a single line comment below JavaDoc with a test's short description.
*/
if (testFramework is Junit4 && displayName != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we don't need this short commented summary under the test, it looks weird and clashes with the JavaDocs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that single-line comment below JavaDoc might look weird, but it contains short useful information about the test. As you suggested in another comment, we can add a custom JavaDoc tag for it in future.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that the comment line does not look good. I am even not sure, that an average user will understand what it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed as discussed, now we don't show display name in case of JUnit4.

documentation.add(CgSingleLineComment(displayName))
}
}
testMethods += testMethod
return testMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
import org.utbot.framework.codegen.model.constructor.util.CgComponents
import org.utbot.framework.codegen.model.constructor.util.classCgClassId
import org.utbot.framework.codegen.model.constructor.util.importIfNeeded
import org.utbot.framework.codegen.model.tree.CgCommentedAnnotation
import org.utbot.framework.codegen.model.tree.CgEnumConstantAccess
import org.utbot.framework.codegen.model.tree.CgExpression
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
Expand Down Expand Up @@ -174,13 +173,39 @@ internal abstract class TestFrameworkManager(val context: CgContext)
}
}

/**
* Supplements TestNG @Test annotation with a description.
* It looks like @Test(description="...")
*
* Should be used only with TestNG.
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
*/
open fun addTestDescription(description: String?) {
if (description == null) return
val testAnnotation =
collectedMethodAnnotations.singleOrNull { it.classId == testFramework.testAnnotationId }

val descriptionArgument = CgNamedAnnotationArgument("description", stringLiteral(description))
if (testAnnotation is CgMultipleArgsAnnotation) {
testAnnotation.arguments += descriptionArgument
} else {
collectedMethodAnnotations += CgMultipleArgsAnnotation(
testFramework.testAnnotationId,
mutableListOf(descriptionArgument)
)
}
}

abstract fun disableTestMethod(reason: String)

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

protected fun ClassId.toExceptionClass(): CgExpression =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface CgMethodBuilder<T : CgMethod> : CgBuilder<T> {
val statements: List<CgStatement>
val exceptions: Set<ClassId>
val annotations: List<CgAnnotation>
val documentation: CgDocumentationComment
val documentation: List<CgComment>
}

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

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

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

override fun build() = CgParameterizedTestDataProviderMethod(name, statements, returnType, annotations, exceptions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ sealed class CgMethod(open val isStatic: Boolean) : CgElement {
abstract val statements: List<CgStatement>
abstract val exceptions: Set<ClassId>
abstract val annotations: List<CgAnnotation>
abstract val documentation: CgDocumentationComment
abstract val documentation: List<CgComment>
abstract val requiredFields: List<CgParameterDeclaration>
}

Expand All @@ -202,14 +202,14 @@ class CgTestMethod(
override val exceptions: Set<ClassId>,
override val annotations: List<CgAnnotation>,
val type: CgTestMethodType,
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList()),
override val documentation: List<CgComment> = emptyList(),
override val requiredFields: List<CgParameterDeclaration> = emptyList(),
) : CgMethod(false)

class CgErrorTestMethod(
override val name: String,
override val statements: List<CgStatement>,
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
override val documentation: List<CgComment> = emptyList()
) : CgMethod(false) {
override val exceptions: Set<ClassId> = emptySet()
override val returnType: ClassId = voidClassId
Expand All @@ -226,7 +226,7 @@ class CgParameterizedTestDataProviderMethod(
override val exceptions: Set<ClassId>,
) : CgMethod(isStatic = true) {
override val parameters: List<CgParameterDeclaration> = emptyList()
override val documentation: CgDocumentationComment = CgDocumentationComment(emptyList())
override val documentation: List<CgDocumentationComment> = emptyList()
override val requiredFields: List<CgParameterDeclaration> = emptyList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,9 @@ internal abstract class CgAbstractRenderer(val context: CgContext, val printer:
}

private fun renderMethodDocumentation(element: CgMethod) {
element.documentation.accept(this)
for (comment in element.documentation) {
comment.accept(this)
}
}

private fun Byte.toStringConstant() = when {
Expand Down