Use TargetBackend.ANY as default parameter and migrate to more generic runTest method

This commit is contained in:
Nikolay Krasko
2019-10-18 13:37:59 +03:00
parent 3891f9fcaa
commit 98ac2af402
4 changed files with 70 additions and 15 deletions
@@ -707,6 +707,11 @@ public class KotlinTestUtils {
void invoke(@NotNull String filePath) throws Exception; void invoke(@NotNull String filePath) throws Exception;
} }
@SuppressWarnings("unused")
public static void runTest(@NotNull DoTest test, @NotNull TestCase testCase, @TestDataFile String testDataFile) throws Exception {
runTest(test, TargetBackend.ANY, testDataFile);
}
// In this test runner version the `testDataFile` parameter is annotated by `TestDataFile`. // In this test runner version the `testDataFile` parameter is annotated by `TestDataFile`.
// So only file paths passed to this parameter will be used in navigation actions, like "Navigate to testdata" and "Related Symbol..." // So only file paths passed to this parameter will be used in navigation actions, like "Navigate to testdata" and "Related Symbol..."
public static void runTest(DoTest test, TargetBackend targetBackend, @TestDataFile String testDataFile) throws Exception { public static void runTest(DoTest test, TargetBackend targetBackend, @TestDataFile String testDataFile) throws Exception {
@@ -828,6 +833,23 @@ public class KotlinTestUtils {
} }
} }
public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern,
boolean recursive,
@NotNull String... excludeDirs
) {
assertAllTestsPresentByMetadata(
testCaseClass,
testDataDir,
filenamePattern,
TargetBackend.ANY,
recursive,
excludeDirs
);
}
public static void assertAllTestsPresentByMetadata( public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull File testDataDir, @NotNull File testDataDir,
@@ -856,6 +878,14 @@ public class KotlinTestUtils {
} }
} }
public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@NotNull Pattern filenamePattern
) {
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, TargetBackend.ANY);
}
public static void assertAllTestsPresentInSingleGeneratedClass( public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull File testDataDir, @NotNull File testDataDir,
@@ -22,12 +22,16 @@ class RunTestMethodModel(
} }
override fun generateBody(p: Printer) { override fun generateBody(p: Printer) {
if (targetBackend == TargetBackend.ANY && additionalRunnerArguments.isEmpty() && testRunnerMethodName == METHOD_NAME) {
p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, this, testDataFilePath);")
} else {
val className = TargetBackend::class.java.simpleName val className = TargetBackend::class.java.simpleName
val additionalArguments = if (additionalRunnerArguments.isNotEmpty()) val additionalArguments = if (additionalRunnerArguments.isNotEmpty())
additionalRunnerArguments.joinToString(separator = ", ", prefix = ", ") additionalRunnerArguments.joinToString(separator = ", ", prefix = ", ")
else "" else ""
p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, $className.$targetBackend, testDataFilePath$additionalArguments);") p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, $className.$targetBackend, testDataFilePath$additionalArguments);")
} }
}
companion object { companion object {
const val METHOD_NAME = "runTest" const val METHOD_NAME = "runTest"
@@ -238,11 +238,23 @@ public class SimpleTestClassModel implements TestClassModel {
exclude.append(StringUtil.escapeStringCharacters(dir)); exclude.append(StringUtil.escapeStringCharacters(dir));
exclude.append("\""); exclude.append("\"");
} }
String assertTestsPresentStr = String.format(
String assertTestsPresentStr;
if (targetBackend == TargetBackend.ANY) {
assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
recursive, exclude
);
} else {
assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s, %s%s);", "KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s, %s%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
); );
}
p.println(assertTestsPresentStr); p.println(assertTestsPresentStr);
} }
@@ -143,11 +143,20 @@ public class SingleClassTestModel implements TestClassModel {
@Override @Override
public void generateBody(@NotNull Printer p) { public void generateBody(@NotNull Printer p) {
String assertTestsPresentStr = String.format( String assertTestsPresentStr;
if (targetBackend != TargetBackend.ANY) {
assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s);", "KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
TargetBackend.class.getSimpleName(), targetBackend.toString() TargetBackend.class.getSimpleName(), targetBackend.toString()
); );
} else {
assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"));",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern())
);
}
p.println(assertTestsPresentStr); p.println(assertTestsPresentStr);
} }