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;
}
@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`.
// 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 {
@@ -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(
@NotNull Class<?> testCaseClass,
@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(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@@ -22,11 +22,15 @@ class RunTestMethodModel(
}
override fun generateBody(p: Printer) {
val className = TargetBackend::class.java.simpleName
val additionalArguments = if (additionalRunnerArguments.isNotEmpty())
additionalRunnerArguments.joinToString(separator = ", ", prefix = ", ")
else ""
p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, $className.$targetBackend, testDataFilePath$additionalArguments);")
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 additionalArguments = if (additionalRunnerArguments.isNotEmpty())
additionalRunnerArguments.joinToString(separator = ", ", prefix = ", ")
else ""
p.println("KotlinTestUtils.$testRunnerMethodName(this::$testMethodName, $className.$targetBackend, testDataFilePath$additionalArguments);")
}
}
companion object {
@@ -238,11 +238,23 @@ public class SimpleTestClassModel implements TestClassModel {
exclude.append(StringUtil.escapeStringCharacters(dir));
exclude.append("\"");
}
String assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s, %s%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
);
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.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
);
}
p.println(assertTestsPresentStr);
}
@@ -143,11 +143,20 @@ public class SingleClassTestModel implements TestClassModel {
@Override
public void generateBody(@NotNull Printer p) {
String assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
TargetBackend.class.getSimpleName(), targetBackend.toString()
);
String assertTestsPresentStr;
if (targetBackend != TargetBackend.ANY) {
assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
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);
}