From af73034235ce2e0d5f1852c6e4c1068a33efedcc Mon Sep 17 00:00:00 2001 From: "Evgeniy.Zhelenskiy" Date: Sun, 5 Dec 2021 04:39:30 +0300 Subject: [PATCH] [Tests] Introduce runTest with transformer to old testing framework --- .../kotlin/codegen/CodegenTestCase.java | 7 +++++- .../generators/impl/RunTestMethodGenerator.kt | 7 +++--- .../kotlin/test/KotlinTestUtils.java | 25 +++---------------- .../LightAnalysisModeTestGenerated.java | 2 +- .../kotlin/js/testOld/BasicWasmBoxTest.kt | 5 ++-- .../IrCodegenBoxWasmTestGenerated.java | 2 +- 6 files changed, 19 insertions(+), 29 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java index b1f76bc9d9d..d5acc1409b3 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codegen/CodegenTestCase.java @@ -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 s); + } + + protected void doTestWithTransformer(@NotNull String filePath, @NotNull Function sourceTransformer) throws Exception { File file = new File(filePath); - String expectedText = KtTestUtil.doLoadFile(file); + String expectedText = sourceTransformer.apply(KtTestUtil.doLoadFile(file)); List testFiles = createTestFilesFromFile(file, expectedText); doMultiFileTest(file, testFiles); diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/generators/impl/RunTestMethodGenerator.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/generators/impl/RunTestMethodGenerator.kt index 73cf9ee5c81..9476223a443 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/generators/impl/RunTestMethodGenerator.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/generators/impl/RunTestMethodGenerator.kt @@ -18,15 +18,16 @@ object RunTestMethodGenerator : MethodGenerator() { 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);") } } } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java index 2e5520bfaf6..f2badf73976 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -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 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 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 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 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 contentTransformer) throws Exception { + private static void runTestImpl(@NotNull DoTest test, @Nullable TestCase testCase, String testDataFilePath) throws Exception { if (testCase != null && !isRunTestOverridden(testCase)) { Function0 wrapWithMuteInDatabase = MuteWithDatabaseKt.wrapWithMuteInDatabase(testCase, () -> { try { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 4935e2c7dc8..84ba01f6d46 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15741,7 +15741,7 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes } private void runTest(String testDataFilePath, java.util.function.Function 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 { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/BasicWasmBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/BasicWasmBoxTest.kt index 3f75551012b..8e0680d0f0e 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/BasicWasmBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/BasicWasmBoxTest.kt @@ -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) { 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 = TestFiles.createTestFiles(file.name, fileContent, testFactory, true) diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 6abd3186e93..8a6d77eb81d 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -12423,7 +12423,7 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest } private void runTest(String testDataFilePath, java.util.function.Function 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 {