[Tests] Introduce runTest with transformer to old testing framework

This commit is contained in:
Evgeniy.Zhelenskiy
2021-12-05 04:39:30 +03:00
parent c27b647ce8
commit af73034235
6 changed files with 19 additions and 29 deletions
@@ -48,6 +48,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.jetbrains.kotlin.cli.common.output.OutputUtilsKt.writeAllTo;
@@ -515,9 +516,13 @@ public abstract class CodegenTestCase extends KotlinBaseTest<KotlinBaseTest.Test
@Override
protected void doTest(@NotNull String filePath) throws Exception {
doTestWithTransformer(filePath, s -> s);
}
protected void doTestWithTransformer(@NotNull String filePath, @NotNull Function<String, String> sourceTransformer) throws Exception {
File file = new File(filePath);
String expectedText = KtTestUtil.doLoadFile(file);
String expectedText = sourceTransformer.apply(KtTestUtil.doLoadFile(file));
List<TestFile> testFiles = createTestFilesFromFile(file, expectedText);
doMultiFileTest(file, testFiles);
@@ -18,15 +18,16 @@ object RunTestMethodGenerator : MethodGenerator<RunTestMethodModel>() {
override fun generateBody(method: RunTestMethodModel, p: Printer) {
with(method) {
val transformerPostfix = if (method.withTransformer) ", transformer" else ""
val modifiedTestMethodName =
if (withTransformer) "path -> ${testMethodName}WithTransformer(path, transformer)" else "this::$testMethodName"
if (!isWithTargetBackend()) {
p.println("KotlinTestUtils.${method.testRunnerMethodName}(this::$testMethodName, this, testDataFilePath$transformerPostfix);")
p.println("KotlinTestUtils.$testRunnerMethodName($modifiedTestMethodName, 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$transformerPostfix);")
p.println("KotlinTestUtils.$testRunnerMethodName($modifiedTestMethodName, $className.$targetBackend, testDataFilePath$additionalArguments);")
}
}
}
@@ -430,11 +430,7 @@ public class KotlinTestUtils {
}
public static void runTest(@NotNull DoTest test, @NotNull TestCase testCase, @TestDataFile String testDataFile) throws Exception {
runTestImpl(testWithCustomIgnoreDirective(test, TargetBackend.ANY, IGNORE_BACKEND_DIRECTIVE_PREFIX), testCase, testDataFile, null);
}
public static void runTest(@NotNull DoTest test, @NotNull TestCase testCase, @TestDataFile String testDataFile, @NotNull Function<String, String> contentTransformer) throws Exception {
runTestImpl(testWithCustomIgnoreDirective(test, TargetBackend.ANY, IGNORE_BACKEND_DIRECTIVE_PREFIX), testCase, testDataFile, contentTransformer);
runTestImpl(testWithCustomIgnoreDirective(test, TargetBackend.ANY, IGNORE_BACKEND_DIRECTIVE_PREFIX), testCase, testDataFile);
}
public static void runTest(@NotNull TestCase testCase, @NotNull Function0<Unit> test) {
@@ -459,15 +455,8 @@ public class KotlinTestUtils {
runTest0(test, targetBackend, testDataFile);
}
public static void runTest(
DoTest test, TargetBackend targetBackend, @TestDataFile String testDataFile,
@NotNull Function<String, String> contentTransformer
) throws Exception {
runTest0(test, targetBackend, testDataFile, contentTransformer);
}
public static void runTestWithCustomIgnoreDirective(DoTest test, TargetBackend targetBackend, @TestDataFile String testDataFile, String ignoreDirective) throws Exception {
runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, ignoreDirective), null, testDataFile, null);
runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, ignoreDirective), null, testDataFile);
}
// In this test runner version, NONE of the parameters are annotated by `TestDataFile`.
@@ -479,16 +468,10 @@ public class KotlinTestUtils {
// * sometimes, for too common/general names, it shows many variants to navigate
// * it adds an additional step for navigation -- you must choose an exact file to navigate
public static void runTest0(DoTest test, TargetBackend targetBackend, String testDataFilePath) throws Exception {
runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, IGNORE_BACKEND_DIRECTIVE_PREFIX), null, testDataFilePath, null);
runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, IGNORE_BACKEND_DIRECTIVE_PREFIX), null, testDataFilePath);
}
public static void runTest0(
DoTest test, TargetBackend targetBackend, String testDataFilePath, @NotNull Function<String, String> contentTransformer
) throws Exception {
runTestImpl(testWithCustomIgnoreDirective(test, targetBackend, IGNORE_BACKEND_DIRECTIVE_PREFIX), null, testDataFilePath, contentTransformer);
}
private static void runTestImpl(@NotNull DoTest test, @Nullable TestCase testCase, String testDataFilePath, @SuppressWarnings("unused") @Nullable Function<String, String> contentTransformer) throws Exception {
private static void runTestImpl(@NotNull DoTest test, @Nullable TestCase testCase, String testDataFilePath) throws Exception {
if (testCase != null && !isRunTestOverridden(testCase)) {
Function0<Unit> wrapWithMuteInDatabase = MuteWithDatabaseKt.wrapWithMuteInDatabase(testCase, () -> {
try {
@@ -15741,7 +15741,7 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
}
private void runTest(String testDataFilePath, java.util.function.Function<String, String> transformer) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath, transformer);
KotlinTestUtils.runTest(path -> doTestWithTransformer(path, transformer), TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInInlineClasses() throws Exception {
@@ -55,11 +55,12 @@ abstract class BasicWasmBoxTest(
TODO("TestWithCoroutinesPackageReplacement are not supported")
}
fun doTest(filePath: String) {
fun doTest(filePath: String) = doTestWithTransformer(filePath) { it }
fun doTestWithTransformer(filePath: String, transformer: java.util.function.Function<String, String>) {
val file = File(filePath)
val outputDir = getOutputDir(file)
val fileContent = KtTestUtil.doLoadFile(file)
val fileContent = transformer.apply(KtTestUtil.doLoadFile(file))
TestFileFactoryImpl().use { testFactory ->
val inputFiles: MutableList<TestFile> = TestFiles.createTestFiles(file.name, fileContent, testFactory, true)
@@ -12423,7 +12423,7 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
}
private void runTest(String testDataFilePath, java.util.function.Function<String, String> transformer) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath, transformer);
KotlinTestUtils.runTest0(path -> doTestWithTransformer(path, transformer), TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInInlineClasses() throws Exception {