Avoid generating irrelevant imports in test classes
Now methods can request generating additional imports.
This commit is contained in:
+3
-3
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
public class DelegatingTestClassModel implements TestClassModel {
|
public class DelegatingTestClassModel extends TestClassModel {
|
||||||
private final TestClassModel delegate;
|
private final TestClassModel delegate;
|
||||||
|
|
||||||
public DelegatingTestClassModel(TestClassModel delegate) {
|
public DelegatingTestClassModel(TestClassModel delegate) {
|
||||||
@@ -70,7 +70,7 @@ public class DelegatingTestClassModel implements TestClassModel {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Collection<Class<?>> getImports() {
|
public Collection<Class<?>> getOwnImports() {
|
||||||
return delegate.getImports();
|
return delegate.getOwnImports();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-1
@@ -22,7 +22,7 @@ class RunTestMethodModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun generateBody(p: Printer) {
|
override fun generateBody(p: Printer) {
|
||||||
if (targetBackend == TargetBackend.ANY && additionalRunnerArguments.isEmpty() && testRunnerMethodName == METHOD_NAME) {
|
if (!isWithTargetBackend()) {
|
||||||
p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, this, testDataFilePath);")
|
p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, this, testDataFilePath);")
|
||||||
} else {
|
} else {
|
||||||
val className = TargetBackend::class.java.simpleName
|
val className = TargetBackend::class.java.simpleName
|
||||||
@@ -33,6 +33,14 @@ class RunTestMethodModel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun imports(): Collection<Class<*>> {
|
||||||
|
return super.imports() + if (isWithTargetBackend()) setOf(TargetBackend::class.java) else emptySet()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isWithTargetBackend(): Boolean {
|
||||||
|
return !(targetBackend == TargetBackend.ANY && additionalRunnerArguments.isEmpty() && testRunnerMethodName == METHOD_NAME)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val METHOD_NAME = "runTest"
|
const val METHOD_NAME = "runTest"
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -29,6 +29,10 @@ class RunTestMethodWithPackageReplacementModel(
|
|||||||
p.println("KotlinTestUtils.$testRunnerMethodName(filePath -> $testMethodName(filePath, packageName), $className.$targetBackend, testDataFilePath$additionalArguments);")
|
p.println("KotlinTestUtils.$testRunnerMethodName(filePath -> $testMethodName(filePath, packageName), $className.$targetBackend, testDataFilePath$additionalArguments);")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun imports(): Collection<Class<*>> {
|
||||||
|
return super.imports() + setOf(TargetBackend::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val METHOD_NAME = "runTestWithPackageReplacement"
|
const val METHOD_NAME = "runTestWithPackageReplacement"
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-8
@@ -19,7 +19,7 @@ import java.util.*;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class SimpleTestClassModel implements TestClassModel {
|
public class SimpleTestClassModel extends TestClassModel {
|
||||||
private static final Comparator<TestEntityModel> BY_NAME = Comparator.comparing(TestEntityModel::getName);
|
private static final Comparator<TestEntityModel> BY_NAME = Comparator.comparing(TestEntityModel::getName);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -238,11 +238,11 @@ public class SimpleTestClassModel implements TestClassModel {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Collection<Class<?>> getImports() {
|
public Collection<Class<?>> getOwnImports() {
|
||||||
return annotations.stream().map(AnnotationModel::getAnnotation).collect(Collectors.toSet());
|
return annotations.stream().map(AnnotationModel::getAnnotation).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestAllFilesPresentMethodModel implements TestMethodModel {
|
private class TestAllFilesPresentMethodModel extends TestMethodModel {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -282,11 +282,6 @@ public class SimpleTestClassModel implements TestClassModel {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void generateSignature(@NotNull Printer p) {
|
|
||||||
TestMethodModel.DefaultImpls.generateSignature(this, p);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldBeGenerated() {
|
public boolean shouldBeGenerated() {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+1
-6
@@ -20,7 +20,7 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isIgnoredTarget;
|
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isIgnoredTarget;
|
||||||
|
|
||||||
public class SimpleTestMethodModel implements TestMethodModel {
|
public class SimpleTestMethodModel extends TestMethodModel {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final File rootDir;
|
private final File rootDir;
|
||||||
@@ -98,9 +98,4 @@ public class SimpleTestMethodModel implements TestMethodModel {
|
|||||||
boolean ignored = skipIgnored && isIgnoredTarget(targetBackend, file);
|
boolean ignored = skipIgnored && isIgnoredTarget(targetBackend, file);
|
||||||
return (ignored ? "ignore" : "test") + StringsKt.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
|
return (ignored ? "ignore" : "test") + StringsKt.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void generateSignature(@NotNull Printer p) {
|
|
||||||
TestMethodModel.DefaultImpls.generateSignature(this, p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-8
@@ -33,7 +33,7 @@ import java.util.List;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class SingleClassTestModel implements TestClassModel {
|
public class SingleClassTestModel extends TestClassModel {
|
||||||
@NotNull
|
@NotNull
|
||||||
private final File rootFile;
|
private final File rootFile;
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -148,11 +148,11 @@ public class SingleClassTestModel implements TestClassModel {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Collection<Class<?>> getImports() {
|
public Collection<Class<?>> getOwnImports() {
|
||||||
return annotations.stream().map(AnnotationModel::getAnnotation).collect(Collectors.toSet());
|
return annotations.stream().map(AnnotationModel::getAnnotation).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestAllFilesPresentMethodModel implements TestMethodModel {
|
private class TestAllFilesPresentMethodModel extends TestMethodModel {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -183,11 +183,6 @@ public class SingleClassTestModel implements TestClassModel {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void generateSignature(@NotNull Printer p) {
|
|
||||||
TestMethodModel.DefaultImpls.generateSignature(this, p);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldBeGenerated() {
|
public boolean shouldBeGenerated() {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+10
-7
@@ -68,13 +68,13 @@ class TestGenerator(
|
|||||||
p.println("import ", RUNNER.canonicalName, ";")
|
p.println("import ", RUNNER.canonicalName, ";")
|
||||||
}
|
}
|
||||||
p.println("import " + KotlinTestUtils::class.java.canonicalName + ";")
|
p.println("import " + KotlinTestUtils::class.java.canonicalName + ";")
|
||||||
p.println("import " + TargetBackend::class.java.canonicalName + ";")
|
|
||||||
if (suiteClassPackage != baseTestClassPackage) {
|
for (clazz in testClassModels.flatMapTo(mutableSetOf()) { classModel -> classModel.imports }) {
|
||||||
p.println("import $baseTestClassPackage.$baseTestClassName;")
|
p.println("import ${clazz.name};")
|
||||||
}
|
}
|
||||||
|
|
||||||
for (clazz in testClassModels.flatMap { classModel -> classModel.imports }.toSet()) {
|
if (suiteClassPackage != baseTestClassPackage) {
|
||||||
p.println("import ${clazz.name};")
|
p.println("import $baseTestClassPackage.$baseTestClassName;")
|
||||||
}
|
}
|
||||||
|
|
||||||
p.println("import " + TestMetadata::class.java.canonicalName + ";")
|
p.println("import " + TestMetadata::class.java.canonicalName + ";")
|
||||||
@@ -98,7 +98,7 @@ class TestGenerator(
|
|||||||
get() = suiteClassName
|
get() = suiteClassName
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
model = object : TestClassModel {
|
model = object : TestClassModel() {
|
||||||
override val innerTestClasses: Collection<TestClassModel>
|
override val innerTestClasses: Collection<TestClassModel>
|
||||||
get() = testClassModels
|
get() = testClassModels
|
||||||
|
|
||||||
@@ -120,8 +120,11 @@ class TestGenerator(
|
|||||||
override val annotations: Collection<AnnotationModel>
|
override val annotations: Collection<AnnotationModel>
|
||||||
get() = emptyList()
|
get() = emptyList()
|
||||||
|
|
||||||
override val imports: Collection<Class<*>>
|
override val ownImports: Collection<Class<*>>
|
||||||
get() = emptyList()
|
get() = emptyList()
|
||||||
|
|
||||||
|
override val imports: Collection<Class<*>>
|
||||||
|
get() = super.imports
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-3
@@ -23,22 +23,35 @@ interface TestEntityModel {
|
|||||||
val dataString: String?
|
val dataString: String?
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TestClassModel : TestEntityModel {
|
interface ClassModel : TestEntityModel {
|
||||||
val imports: Collection<Class<*>>
|
val ownImports: Collection<Class<*>>
|
||||||
val innerTestClasses: Collection<TestClassModel>
|
val innerTestClasses: Collection<TestClassModel>
|
||||||
val methods: Collection<MethodModel>
|
val methods: Collection<MethodModel>
|
||||||
val isEmpty: Boolean
|
val isEmpty: Boolean
|
||||||
val dataPathRoot: String?
|
val dataPathRoot: String?
|
||||||
val annotations: Collection<AnnotationModel>
|
val annotations: Collection<AnnotationModel>
|
||||||
|
val imports: Collection<Class<*>>
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class TestClassModel : ClassModel {
|
||||||
|
override val imports: Collection<Class<*>>
|
||||||
|
get() {
|
||||||
|
return mutableListOf<Class<*>>().also { allImports ->
|
||||||
|
allImports.addAll(ownImports)
|
||||||
|
methods.flatMapTo(allImports) { it.imports() }
|
||||||
|
innerTestClasses.flatMapTo(allImports) { it.imports }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MethodModel : TestEntityModel {
|
interface MethodModel : TestEntityModel {
|
||||||
fun shouldBeGenerated(): Boolean = true
|
fun shouldBeGenerated(): Boolean = true
|
||||||
fun generateSignature(p: Printer)
|
fun generateSignature(p: Printer)
|
||||||
fun generateBody(p: Printer)
|
fun generateBody(p: Printer)
|
||||||
|
fun imports(): Collection<Class<*>> = emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TestMethodModel : MethodModel {
|
abstract class TestMethodModel : MethodModel {
|
||||||
override fun generateSignature(p: Printer) {
|
override fun generateSignature(p: Printer) {
|
||||||
p.print("public void $name() throws Exception")
|
p.print("public void $name() throws Exception")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user