Skip to content

Commit 5df5bc2

Browse files
committed
Add support for class fields rendering with annotations
1 parent 9b22a39 commit 5df5bc2

File tree

10 files changed

+100
-14
lines changed

10 files changed

+100
-14
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@ object TestNg : TestFramework(id = "TestNG",displayName = "TestNG") {
290290
simpleName = "ThrowingRunnable"
291291
)
292292

293+
val beforeMethodAnnotation: String = "@$mainPackage.BeforeMethod"
294+
val beforeMethodAnnotationFqn: String = "$mainPackage.BeforeMethod"
295+
val afterMethodAnnotation: String = "@$mainPackage.AfterMethod"
296+
val afterMethodAnnotationFqn: String = "$mainPackage.AfterMethod"
297+
298+
val beforeMethodAnnotationId: ClassId =
299+
BuiltinClassId(
300+
canonicalName = "$mainPackage.annotations.BeforeMethod",
301+
simpleName = "BeforeMethod"
302+
)
303+
304+
val afterMethodAnnotationId: ClassId =
305+
BuiltinClassId(
306+
canonicalName = "$mainPackage.annotations.AfterMethod",
307+
simpleName = "AfterMethod"
308+
)
309+
293310
val assertThrows = builtinStaticMethodId(
294311
classId = assertionsClass,
295312
name = "assertThrows",
@@ -370,7 +387,7 @@ object Junit4 : TestFramework(id = "JUnit4",displayName = "JUnit 4") {
370387
get() = error("Parametrized tests are not supported for JUnit 4")
371388

372389
override val mainPackage: String = JUNIT4_PACKAGE
373-
override val testAnnotation = "@$mainPackage.Test"
390+
override val testAnnotation: String = "@$mainPackage.Test"
374391
override val testAnnotationFqn: String = "$mainPackage.Test"
375392

376393
override val parameterizedTestAnnotation
@@ -395,6 +412,23 @@ object Junit4 : TestFramework(id = "JUnit4",displayName = "JUnit 4") {
395412
simpleName = "RunWith"
396413
)
397414

415+
val beforeAnnotation: String = "@$mainPackage.Before"
416+
val beforeAnnotationFqn: String = "$mainPackage.Before"
417+
val afterAnnotation: String = "@$mainPackage.After"
418+
val afterAnnotationFqn: String = "$mainPackage.After"
419+
420+
val beforeAnnotationId: ClassId =
421+
BuiltinClassId(
422+
canonicalName = "$JUNIT4_PACKAGE.Before",
423+
simpleName = "Before"
424+
)
425+
426+
val afterAnnotationId: ClassId =
427+
BuiltinClassId(
428+
canonicalName = "$JUNIT4_PACKAGE.After",
429+
simpleName = "After"
430+
)
431+
398432
override val assertionsClass = BuiltinClassId(
399433
canonicalName = JUNIT4_ASSERTIONS,
400434
simpleName = "Assert"
@@ -431,13 +465,30 @@ object Junit4 : TestFramework(id = "JUnit4",displayName = "JUnit 4") {
431465

432466
object Junit5 : TestFramework(id = "JUnit5", displayName = "JUnit 5") {
433467
override val mainPackage: String = JUNIT5_PACKAGE
434-
override val testAnnotation = "@$mainPackage.Test"
468+
override val testAnnotation: String = "@$mainPackage.Test"
435469
override val testAnnotationFqn: String = "$mainPackage.Test"
436-
override val parameterizedTestAnnotation = "$JUNIT5_PARAMETERIZED_PACKAGE.ParameterizedTest"
470+
override val parameterizedTestAnnotation: String = "$JUNIT5_PARAMETERIZED_PACKAGE.ParameterizedTest"
437471
override val parameterizedTestAnnotationFqn: String = "$JUNIT5_PARAMETERIZED_PACKAGE.ParameterizedTest"
438472
override val methodSourceAnnotation: String = "$JUNIT5_PARAMETERIZED_PACKAGE.provider.MethodSource"
439473
override val methodSourceAnnotationFqn: String = "$JUNIT5_PARAMETERIZED_PACKAGE.provider.MethodSource"
440474

475+
val beforeEachAnnotation: String = "@$mainPackage.BeforeEach"
476+
val beforeEachAnnotationFqn: String = "$mainPackage.BeforeEach"
477+
val afterEachAnnotation: String = "@$mainPackage.AfterEach"
478+
val afterEachAnnotationFqn: String = "$mainPackage.AfterEach"
479+
480+
val beforeEachAnnotationId: ClassId =
481+
BuiltinClassId(
482+
canonicalName = "$JUNIT5_PACKAGE.BeforeEach",
483+
simpleName = "BeforeEach"
484+
)
485+
486+
val afterEachAnnotationId: ClassId =
487+
BuiltinClassId(
488+
canonicalName = "$JUNIT5_PACKAGE.AfterEach",
489+
simpleName = "AfterEach"
490+
)
491+
441492
val executableClassId = BuiltinClassId(
442493
canonicalName = "$JUNIT5_PACKAGE.function.Executable",
443494
simpleName = "Executable"

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/builtin/MockitoBuiltins.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ internal val mockitoClassId = BuiltinClassId(
2020
simpleName = "Mockito",
2121
)
2222

23+
internal val mockClassId = BuiltinClassId(
24+
canonicalName = "org.mockito.Mock",
25+
simpleName = "Mock",
26+
)
27+
28+
internal val injectMocksClassId = BuiltinClassId(
29+
canonicalName = "org.mockito.InjectMocks",
30+
simpleName = "InjectMocks",
31+
)
32+
2333
internal val ongoingStubbingClassId = BuiltinClassId(
2434
canonicalName = "org.mockito.stubbing.OngoingStubbing",
2535
simpleName = "OngoingStubbing",

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/CgElement.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ interface CgElement {
8181
is CgBreakStatement -> visit(element)
8282
is CgContinueStatement -> visit(element)
8383
is CgDeclaration -> visit(element)
84+
is CgFieldDeclaration -> visit(element)
8485
is CgAssignment -> visit(element)
8586
is CgTypeCast -> visit(element)
8687
is CgIsInstance -> visit(element)
@@ -155,8 +156,19 @@ class CgClassBody(
155156
val methodRegions: List<CgMethodsCluster>,
156157
val staticDeclarationRegions: List<CgStaticsRegion>,
157158
val nestedClassRegions: List<CgNestedClassesRegion<*>>,
158-
//TODO: use [CgFieldDeclaration] after PR-1788 merge
159-
val fields: List<CgDeclaration> = emptyList(),
159+
val fields: List<CgFieldDeclaration> = emptyList(),
160+
) : CgElement
161+
162+
/**
163+
* Field of a class.
164+
* @property classId [ClassId] of the field owner class.
165+
* @property declaration declaration itself.
166+
* @property annotation optional annotation.
167+
*/
168+
class CgFieldDeclaration(
169+
val classId: ClassId,
170+
val declaration: CgDeclaration,
171+
val annotation: CgAnnotation? = null,
160172
) : CgElement
161173

162174
/**

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/TestClassModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class TestClassModel(
1313
val nestedClasses: List<SimpleTestClassModel>,
1414
)
1515

16-
open class SimpleTestClassModel(
16+
class SimpleTestClassModel(
1717
classUnderTest: ClassId,
1818
methodTestSets: List<CgMethodTestSet>,
1919
nestedClasses: List<SimpleTestClassModel> = listOf(),

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.utbot.framework.codegen.domain.models.CgExecutableCall
4141
import org.utbot.framework.codegen.domain.models.CgMethodsCluster
4242
import org.utbot.framework.codegen.domain.models.CgExpression
4343
import org.utbot.framework.codegen.domain.models.CgFieldAccess
44+
import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
4445
import org.utbot.framework.codegen.domain.models.CgForEachLoop
4546
import org.utbot.framework.codegen.domain.models.CgForLoop
4647
import org.utbot.framework.codegen.domain.models.CgGreaterThan
@@ -562,6 +563,14 @@ abstract class CgAbstractRenderer(
562563
println(statementEnding)
563564
}
564565

566+
// Class field declaration
567+
568+
override fun visit(element: CgFieldDeclaration) {
569+
element.annotation?.accept(this)
570+
renderVisibility(element.declaration.variableType)
571+
element.declaration.accept(this)
572+
}
573+
565574
// Variable assignment
566575

567576
override fun visit(element: CgAssignment) {
@@ -878,7 +887,7 @@ abstract class CgAbstractRenderer(
878887
}
879888
}
880889

881-
protected abstract fun renderClassVisibility(classId: ClassId)
890+
protected abstract fun renderVisibility(classId: ClassId)
882891

883892
protected abstract fun renderClassModality(aClass: CgClass)
884893

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgJavaRenderer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal class CgJavaRenderer(context: CgRendererContext, printer: CgPrinter = C
6969
annotation.accept(this)
7070
}
7171

72-
renderClassVisibility(element.id)
72+
renderVisibility(element.id)
7373
renderClassModality(element)
7474
if (element.isStatic) {
7575
print("static ")
@@ -376,7 +376,7 @@ internal class CgJavaRenderer(context: CgRendererContext, printer: CgPrinter = C
376376

377377
override fun escapeNamePossibleKeywordImpl(s: String): String = s
378378

379-
override fun renderClassVisibility(classId: ClassId) {
379+
override fun renderVisibility(classId: ClassId) {
380380
when {
381381
classId.isPublic -> print("public ")
382382
classId.isProtected -> print("protected ")

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgKotlinRenderer.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import org.utbot.framework.codegen.domain.models.CgFormattedString
4242
import org.utbot.framework.codegen.domain.models.CgLiteral
4343
import org.utbot.framework.codegen.domain.models.CgTestMethod
4444
import org.utbot.framework.codegen.domain.models.CgTypeCast
45-
import org.utbot.framework.codegen.domain.models.CgValue
4645
import org.utbot.framework.codegen.domain.models.CgVariable
4746
import org.utbot.framework.codegen.util.nullLiteral
4847
import org.utbot.framework.plugin.api.BuiltinClassId
@@ -84,7 +83,7 @@ internal class CgKotlinRenderer(context: CgRendererContext, printer: CgPrinter =
8483
annotation.accept(this)
8584
}
8685

87-
renderClassVisibility(element.id)
86+
renderVisibility(element.id)
8887
renderClassModality(element)
8988
if (!element.isStatic && element.isNested) {
9089
print("inner ")
@@ -571,7 +570,7 @@ internal class CgKotlinRenderer(context: CgRendererContext, printer: CgPrinter =
571570
override fun escapeNamePossibleKeywordImpl(s: String): String =
572571
if (isLanguageKeyword(s, context.cgLanguageAssistant)) "`$s`" else s
573572

574-
override fun renderClassVisibility(classId: ClassId) {
573+
override fun renderVisibility(classId: ClassId) {
575574
when {
576575
// Kotlin classes are public by default
577576
classId.isPublic -> Unit

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/renderer/CgVisitor.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import org.utbot.framework.codegen.domain.models.CgSwitchCaseLabel
7676
import org.utbot.framework.codegen.domain.models.CgClass
7777
import org.utbot.framework.codegen.domain.models.CgClassBody
7878
import org.utbot.framework.codegen.domain.models.CgDocRegularLineStmt
79+
import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
7980
import org.utbot.framework.codegen.domain.models.CgFormattedString
8081
import org.utbot.framework.codegen.domain.models.CgNestedClassesRegion
8182
import org.utbot.framework.codegen.domain.models.CgTestMethod
@@ -183,6 +184,9 @@ interface CgVisitor<R> {
183184
// Variable declaration
184185
fun visit(element: CgDeclaration): R
185186

187+
// Field declaration
188+
fun visit(element: CgFieldDeclaration): R
189+
186190
// Variable assignment
187191
fun visit(element: CgAssignment): R
188192

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.utbot.framework.codegen.domain.models.CgElement
1313
import org.utbot.framework.codegen.domain.models.CgErrorTestMethod
1414
import org.utbot.framework.codegen.domain.models.CgExceptionHandler
1515
import org.utbot.framework.codegen.domain.models.CgExpression
16+
import org.utbot.framework.codegen.domain.models.CgFieldDeclaration
1617
import org.utbot.framework.codegen.domain.models.CgForEachLoop
1718
import org.utbot.framework.codegen.domain.models.CgForLoop
1819
import org.utbot.framework.codegen.domain.models.CgLoop
@@ -66,7 +67,7 @@ class CgClassBodyBuilder(val classId: ClassId) : CgBuilder<CgClassBody> {
6667
val methodRegions: MutableList<CgMethodsCluster> = mutableListOf()
6768
val staticDeclarationRegions: MutableList<CgStaticsRegion> = mutableListOf()
6869
val nestedClassRegions: MutableList<CgNestedClassesRegion<*>> = mutableListOf()
69-
val fields: MutableList<CgDeclaration> = mutableListOf()
70+
val fields: MutableList<CgFieldDeclaration> = mutableListOf()
7071

7172
override fun build() = CgClassBody(classId, methodRegions, staticDeclarationRegions, nestedClassRegions, fields)
7273
}

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/visitor/CgPythonRenderer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ internal class CgPythonRenderer(
415415
}
416416

417417
override fun escapeNamePossibleKeywordImpl(s: String): String = s
418-
override fun renderClassVisibility(classId: ClassId) {
418+
override fun renderVisibility(classId: ClassId) {
419419
throw UnsupportedOperationException()
420420
}
421421

0 commit comments

Comments
 (0)