[Test] Avoid importing unused @Nested annotations in generated tests

- Unused `Nested` imports frequently cause unused import warnings in the
  IDE, which are especially annoying in after-commit warning/error
  analysis.
This commit is contained in:
Marco Pennekamp
2024-02-26 17:33:53 +01:00
committed by Space Team
parent 0811a5b77b
commit 708ed81eb2
461 changed files with 14 additions and 462 deletions
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.generators.impl.SingleClassTestModelAllFilesPresente
import org.jetbrains.kotlin.generators.impl.TransformingTestMethodGenerator
import org.jetbrains.kotlin.generators.model.*
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil
import org.jetbrains.kotlin.generators.util.TestGeneratorUtil.getMainClassName
import org.jetbrains.kotlin.test.TestMetadata
import org.jetbrains.kotlin.test.util.KtTestUtil
import org.jetbrains.kotlin.utils.Printer
@@ -147,7 +146,12 @@ class NewTestGeneratorImpl(
}
p.println("import ${TestMetadata::class.java.canonicalName};")
p.println("import ${Nested::class.java.canonicalName};")
// Don't import an unneeded `@Nested` annotation to avoid unused import warnings in the IDE.
if (testClassModels.requiresNestedAnnotation()) {
p.println("import ${Nested::class.java.canonicalName};")
}
p.println("import ${Test::class.java.canonicalName};")
if (testClassModels.any { it.containsTags() }) {
p.println("import ${Tag::class.java.canonicalName};")
@@ -289,6 +293,14 @@ class NewTestGeneratorImpl(
}
}
private fun Collection<TestClassModel>.requiresNestedAnnotation(): Boolean {
// Multiple test class models are generated as inner (nested) test class models of a fake root test class model, see
// `TestGeneratorInstance.generate`.
return size > 1 || singleOrNull()?.requiresNestedAnnotation() == true
}
private fun TestClassModel.requiresNestedAnnotation(): Boolean = innerTestClasses.isNotEmpty()
private fun TestEntityModel.containsTags(): Boolean {
if (this.tags.isNotEmpty()) return true
if (this is TestClassModel) {