From b6803b2400388a6fde0f7c766a56dcf20e4d7c99 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 13 Jun 2023 18:25:01 +0200 Subject: [PATCH] [JS] Migrate web demo examples --- .../JsEnvironmentConfigurator.kt | 7 +- .../kotlin/js/test/AbstractJsTest.kt | 82 ------------------- .../kotlin/js/test/ir/AbstractJsIrTest.kt | 21 ++++- .../kotlin/js/testOld/JsTestChecker.kt | 25 +++++- .../WebDemoExamplesTestGenerated.java | 4 +- 5 files changed, 50 insertions(+), 89 deletions(-) delete mode 100644 js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsTest.kt rename js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/{ => ir}/WebDemoExamplesTestGenerated.java (97%) diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt index 876f363ad77..cf2c7ac822e 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt @@ -173,8 +173,11 @@ class JsEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigu } fun getMainCallParametersForModule(module: TestModule): MainCallParameters { - return when (JsEnvironmentConfigurationDirectives.CALL_MAIN) { - in module.directives -> MainCallParameters.mainWithArguments(listOf()) + return when { + JsEnvironmentConfigurationDirectives.CALL_MAIN in module.directives -> MainCallParameters.mainWithArguments(listOf()) + JsEnvironmentConfigurationDirectives.MAIN_ARGS in module.directives -> { + MainCallParameters.mainWithArguments(module.directives[JsEnvironmentConfigurationDirectives.MAIN_ARGS].single()) + } else -> MainCallParameters.noCall() } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsTest.kt deleted file mode 100644 index c83dd69ca27..00000000000 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/AbstractJsTest.kt +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.js.test - -import org.jetbrains.kotlin.js.test.converters.ClassicJsBackendFacade -import org.jetbrains.kotlin.js.test.converters.incremental.RecompileModuleJsBackendFacade -import org.jetbrains.kotlin.js.test.handlers.* -import org.jetbrains.kotlin.js.test.ir.AbstractJsBlackBoxCodegenTestBase -import org.jetbrains.kotlin.js.test.ir.AbstractJsIrTest -import org.jetbrains.kotlin.js.test.utils.JsIncrementalEnvironmentConfigurator -import org.jetbrains.kotlin.test.Constructor -import org.jetbrains.kotlin.test.TargetBackend -import org.jetbrains.kotlin.test.backend.classic.ClassicBackendInput -import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder -import org.jetbrains.kotlin.test.builders.configureJsArtifactsHandlersStep -import org.jetbrains.kotlin.test.directives.JsEnvironmentConfigurationDirectives -import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2ClassicBackendConverter -import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendFacade -import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendOutputArtifact -import org.jetbrains.kotlin.test.model.* -import org.jetbrains.kotlin.test.services.configuration.JsEnvironmentConfigurator - -abstract class AbstractJsTest( - pathToTestDir: String, - testGroupOutputDirPrefix: String, -) : AbstractJsBlackBoxCodegenTestBase( - FrontendKinds.ClassicFrontend, TargetBackend.JS, pathToTestDir, testGroupOutputDirPrefix -) { - override val frontendFacade: Constructor> - get() = ::ClassicFrontendFacade - - override val frontendToBackendConverter: Constructor> - get() = ::ClassicFrontend2ClassicBackendConverter - - override val backendFacade: Constructor> - get() = ::ClassicJsBackendFacade - - override val afterBackendFacade: Constructor>? - get() = null - - override val recompileFacade: Constructor> - get() = { RecompileModuleJsBackendFacade(it, frontendFacade, frontendToBackendConverter) } - - override fun configure(builder: TestConfigurationBuilder) { - super.configure(builder) - with(builder) { - useConfigurators( - ::JsIncrementalEnvironmentConfigurator - ) - - configureJsArtifactsHandlersStep { - useHandlers( - ::JsTranslationResultHandler, - ::JsSourceMapHandler, - ::JsRecompiledArtifactsIdentityHandler, - ) - } - } - } -} - -open class AbstractWebDemoExamplesTest : AbstractJsTest( - pathToTestDir = "${JsEnvironmentConfigurator.TEST_DATA_DIR_PATH}/webDemoExamples/", - testGroupOutputDirPrefix = "webDemoExamples/" -) { - override fun configure(builder: TestConfigurationBuilder) { - super.configure(builder) - with(builder) { - defaultDirectives { - -JsEnvironmentConfigurationDirectives.GENERATE_NODE_JS_RUNNER - JsEnvironmentConfigurationDirectives.DONT_RUN_GENERATED_CODE.with("JS") - } - - configureJsArtifactsHandlersStep { - useHandlers(::MainCallWithArgumentsHandler) - } - } - } -} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsIrTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsIrTest.kt index f7a3bb7b38b..b87b6cffba5 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsIrTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsIrTest.kt @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.js.test.ir -import org.jetbrains.kotlin.js.test.AbstractJsTest import org.jetbrains.kotlin.js.test.JsSteppingTestAdditionalSourceProvider import org.jetbrains.kotlin.js.test.converters.JsIrBackendFacade import org.jetbrains.kotlin.js.test.converters.JsKlibBackendFacade @@ -163,6 +162,26 @@ open class AbstractMultiModuleOrderTest : AbstractJsIrTest( } } +open class AbstractWebDemoExamplesTest : AbstractJsIrTest( + pathToTestDir = "${JsEnvironmentConfigurator.TEST_DATA_DIR_PATH}/webDemoExamples/", + testGroupOutputDirPrefix = "webDemoExamples/" +) { + override fun configure(builder: TestConfigurationBuilder) { + super.configure(builder) + with(builder) { + defaultDirectives { + +JsEnvironmentConfigurationDirectives.KJS_WITH_FULL_RUNTIME + -JsEnvironmentConfigurationDirectives.GENERATE_NODE_JS_RUNNER + JsEnvironmentConfigurationDirectives.DONT_RUN_GENERATED_CODE.with("JS_IR") + } + + configureJsArtifactsHandlersStep { + useHandlers(::MainCallWithArgumentsHandler) + } + } + } +} + private fun configureJsIrLineNumberTest(builder: TestConfigurationBuilder) { with(builder) { defaultDirectives { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/JsTestChecker.kt b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/JsTestChecker.kt index a75376148aa..1724f228ab9 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/testOld/JsTestChecker.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/testOld/JsTestChecker.kt @@ -10,7 +10,10 @@ import org.jetbrains.kotlin.js.engine.ScriptEngine import org.jetbrains.kotlin.js.engine.ScriptEngineNashorn import org.jetbrains.kotlin.js.engine.ScriptEngineV8 import org.jetbrains.kotlin.js.engine.loadFiles +import org.jetbrains.kotlin.test.utils.withExtension +import org.jetbrains.kotlin.test.utils.withSuffixAndExtension import org.junit.Assert +import java.io.File internal const val TEST_DATA_DIR_PATH = "js/js.translator/testData/" private const val DIST_DIR_JS_PATH = "dist/js/" @@ -104,7 +107,20 @@ abstract class AbstractJsTestChecker { } fun checkStdout(files: List, expectedResult: String) { - run(files) { + val newFiles = files + .mapIndexed { index, s -> + if (index == files.size - 1) { + val file = File(s) + val lines = file.readText().lines().toMutableList() + lines.add(lines.size - 6, JS_IR_OUTPUT_REWRITE) + val newFile = file.withSuffixAndExtension("_modified", "js") + newFile.writeText(lines.joinToString("\n")) + newFile.absolutePath + } else { + s + } + } + run(newFiles) { val actualResult = eval(GET_KOTLIN_OUTPUT) Assert.assertEquals(expectedResult, actualResult.normalize()) "" @@ -165,7 +181,12 @@ abstract class AbstractNashornJsTestChecker : AbstractJsTestChecker() { } const val SETUP_KOTLIN_OUTPUT = "kotlin.kotlin.io.output = new kotlin.kotlin.io.BufferedOutput();" -const val GET_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.buffer;" +const val GET_KOTLIN_OUTPUT = "main.get_output().buffer_1" + +private val JS_IR_OUTPUT_REWRITE = """ + set_output(new BufferedOutput()) + _.get_output = get_output +""".trimIndent() object NashornJsTestChecker : AbstractNashornJsTestChecker() { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/WebDemoExamplesTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/WebDemoExamplesTestGenerated.java similarity index 97% rename from js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/WebDemoExamplesTestGenerated.java rename to js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/WebDemoExamplesTestGenerated.java index 6d777c84ef5..b230a58b66e 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/WebDemoExamplesTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/WebDemoExamplesTestGenerated.java @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -package org.jetbrains.kotlin.js.test; +package org.jetbrains.kotlin.js.test.ir; import com.intellij.testFramework.TestDataPath; import org.jetbrains.kotlin.test.util.KtTestUtil; @@ -22,7 +22,7 @@ import java.util.regex.Pattern; public class WebDemoExamplesTestGenerated extends AbstractWebDemoExamplesTest { @Test public void testAllFilesPresentInWebDemoExamples() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/webDemoExamples"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/webDemoExamples"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } @Test