[JS FIR] Enable FirJsLineNumberTests for FIR

^KT-64426 Fixed
This commit is contained in:
Alexander Korepanov
2023-12-20 16:19:58 +01:00
committed by Space Team
parent ac30911497
commit 6df0e0c847
58 changed files with 460 additions and 146 deletions
@@ -165,10 +165,9 @@ fun main(args: Array<String>) {
// model("typescript-export/", pattern = "^([^_](.+))\\.kt$")
// }
// see todo on defining class
// testClass<AbstractJsFirLineNumberTest> {
// model("lineNumbers/")
// }
testClass<AbstractFirJsLineNumberTest> {
model("lineNumbers/")
}
}
testGroup("js/js.tests/tests-gen", "compiler/testData", testRunnerMethodName = "runTest0") {
@@ -7,6 +7,7 @@ import org.jetbrains.kotlin.js.test.converters.JsIrBackendFacade
import org.jetbrains.kotlin.js.test.converters.incremental.RecompileModuleJsIrBackendFacade
import org.jetbrains.kotlin.js.test.handlers.JsDebugRunner
import org.jetbrains.kotlin.js.test.handlers.JsIrRecompiledArtifactsIdentityHandler
import org.jetbrains.kotlin.js.test.handlers.createFirJsLineNumberHandler
import org.jetbrains.kotlin.parsing.parseBoolean
import org.jetbrains.kotlin.test.Constructor
import org.jetbrains.kotlin.test.FirParser
@@ -134,16 +135,25 @@ open class AbstractFirJsCodegenInlineTest : AbstractFirJsTest(
// }
//}
// TODO: implement separate expectations for FIR/JS to reuse testdata, disabled for now
//open class AbstractJsFirLineNumberTest : AbstractFirJsTest(
// pathToTestDir = "${JsEnvironmentConfigurator.TEST_DATA_DIR_PATH}/lineNumbers/",
// testGroupOutputDirPrefix = "firLineNumbers/"
//) {
// override fun configure(builder: TestConfigurationBuilder) {
// super.configure(builder)
// configureJsIrLineNumberTest(builder)
// }
//}
open class AbstractFirJsLineNumberTest : AbstractFirJsTest(
pathToTestDir = "${JsEnvironmentConfigurator.TEST_DATA_DIR_PATH}/lineNumbers/",
testGroupOutputDirPrefix = "firLineNumbers/"
) {
override fun configure(builder: TestConfigurationBuilder) {
super.configure(builder)
with(builder) {
defaultDirectives {
+JsEnvironmentConfigurationDirectives.KJS_WITH_FULL_RUNTIME
+JsEnvironmentConfigurationDirectives.NO_COMMON_FILES
-JsEnvironmentConfigurationDirectives.GENERATE_NODE_JS_RUNNER
JsEnvironmentConfigurationDirectives.DONT_RUN_GENERATED_CODE.with(listOf("JS", "JS_IR", "JS_IR_ES6"))
}
configureJsArtifactsHandlersStep {
useHandlers(::createFirJsLineNumberHandler)
}
}
}
}
open class AbstractFirJsSteppingTest : AbstractFirJsTest(
pathToTestDir = "compiler/testData/debug/stepping/",
@@ -9,39 +9,36 @@ import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.CompilationOutputs
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.TranslationMode
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.safeModuleName
import org.jetbrains.kotlin.js.backend.ast.JsProgram
import org.jetbrains.kotlin.js.facade.TranslationResult
import org.jetbrains.kotlin.js.test.utils.LineCollector
import org.jetbrains.kotlin.js.test.utils.LineOutputToStringVisitor
import org.jetbrains.kotlin.js.util.TextOutputImpl
import org.jetbrains.kotlin.test.backend.handlers.JsBinaryArtifactHandler
import org.jetbrains.kotlin.test.model.BinaryArtifacts
import org.jetbrains.kotlin.test.model.FrontendKind
import org.jetbrains.kotlin.test.model.FrontendKinds
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
import org.jetbrains.kotlin.test.services.configuration.JsEnvironmentConfigurator
import org.jetbrains.kotlin.test.services.moduleStructure
import org.jetbrains.kotlin.utils.addToStdlib.cast
import java.io.File
/**
* Verifies the `// LINE` comments in lineNumber tests.
*
* The test file is expected to contain the `// LINE(backend)` directive, followed by the line numbers that the corresponding JS statements
* are generated from.
* The test file is expected to contain the `// LINE($linePattern)` directive,
* followed by the line numbers that the corresponding JS statements are generated from.
*
* This handler traverses the JS AST and collects the actual line numbers using [LineCollector], and generates a JavaScript file
* with those line numbers printed as comments for ease of debugging these tests.
*/
class JsLineNumberHandler(testServices: TestServices) : JsBinaryArtifactHandler(testServices) {
private class JsLineNumberHandler(private val frontend: FrontendKind<*>, testServices: TestServices) : JsBinaryArtifactHandler(testServices) {
private val translationModeForIr = TranslationMode.PER_MODULE_DEV
override fun processAfterAllModules(someAssertionWasFailed: Boolean) {}
override fun processModule(module: TestModule, info: BinaryArtifacts.Js) {
when (val artifact = info.unwrap()) {
is BinaryArtifacts.Js.OldJsArtifact ->
verifyModule(module, TranslationMode.FULL_DEV, artifact.translationResult.cast<TranslationResult.Success>().program, "JS")
is BinaryArtifacts.Js.JsIrArtifact -> {
val testModules = testServices.moduleStructure.modules
val moduleId2TestModule = testModules.associateBy { it.name.safeModuleName }
@@ -58,7 +55,7 @@ class JsLineNumberHandler(testServices: TestServices) : JsBinaryArtifactHandler(
}
}
verifyModule(module, translationModeForIr, compilationOutputs.jsProgram!!, "JS_IR")
verifyModule(module, translationModeForIr, compilationOutputs.jsProgram!!)
verifiedModuleCount += 1
}
@@ -77,8 +74,7 @@ class JsLineNumberHandler(testServices: TestServices) : JsBinaryArtifactHandler(
private fun verifyModule(
module: TestModule,
translationMode: TranslationMode,
jsProgram: JsProgram,
backendPattern: String
jsProgram: JsProgram
) {
val baseOutputPath = JsEnvironmentConfigurator.getJsModuleArtifactPath(testServices, module.name, translationMode)
@@ -96,12 +92,12 @@ class JsLineNumberHandler(testServices: TestServices) : JsBinaryArtifactHandler(
writeText(generatedCode)
}
val linesPattern = Regex("^ *// *LINES\\($backendPattern\\): *(.*)$", RegexOption.MULTILINE)
val linesPattern = Regex("^ *// *LINES\\((?:$frontend )? *JS_IR\\): *(.*)$", RegexOption.MULTILINE)
val linesMatcher = module.files
.firstNotNullOfOrNull { linesPattern.find(it.originalContent) }
?: testServices.assertions.fail {
"'// LINES($backendPattern): ' comment was not found in source file. Generated code is:\n$generatedCode"
"'// LINES(${linesPattern.pattern}): ' comment was not found in source file. Generated code is:\n$generatedCode"
}
fun List<Int?>.render() = joinToString(" ") { it?.toString() ?: "*" }
@@ -117,3 +113,11 @@ class JsLineNumberHandler(testServices: TestServices) : JsBinaryArtifactHandler(
testServices.assertions.assertEquals(expectedLines, actualLines) { generatedCode }
}
}
fun createIrJsLineNumberHandler(testServices: TestServices): JsBinaryArtifactHandler {
return JsLineNumberHandler(FrontendKinds.ClassicFrontend, testServices)
}
fun createFirJsLineNumberHandler(testServices: TestServices): JsBinaryArtifactHandler {
return JsLineNumberHandler(FrontendKinds.FIR, testServices)
}
@@ -138,7 +138,17 @@ open class AbstractJsIrLineNumberTest : AbstractJsIrTest(
) {
override fun configure(builder: TestConfigurationBuilder) {
super.configure(builder)
configureJsIrLineNumberTest(builder)
with(builder) {
defaultDirectives {
+JsEnvironmentConfigurationDirectives.KJS_WITH_FULL_RUNTIME
+JsEnvironmentConfigurationDirectives.NO_COMMON_FILES
-JsEnvironmentConfigurationDirectives.GENERATE_NODE_JS_RUNNER
JsEnvironmentConfigurationDirectives.DONT_RUN_GENERATED_CODE.with(listOf("JS", "JS_IR", "JS_IR_ES6"))
}
configureJsArtifactsHandlersStep {
useHandlers(::createIrJsLineNumberHandler)
}
}
}
}
@@ -193,20 +203,6 @@ open class AbstractWebDemoExamplesTest : AbstractJsIrTest(
}
}
private fun configureJsIrLineNumberTest(builder: TestConfigurationBuilder) {
with(builder) {
defaultDirectives {
+JsEnvironmentConfigurationDirectives.KJS_WITH_FULL_RUNTIME
+JsEnvironmentConfigurationDirectives.NO_COMMON_FILES
-JsEnvironmentConfigurationDirectives.GENERATE_NODE_JS_RUNNER
JsEnvironmentConfigurationDirectives.DONT_RUN_GENERATED_CODE.with(listOf("JS", "JS_IR", "JS_IR_ES6"))
}
configureJsArtifactsHandlersStep {
useHandlers(::JsLineNumberHandler)
}
}
}
open class AbstractIrJsSteppingTest : AbstractJsIrTest(
pathToTestDir = "compiler/testData/debug/stepping/",
testGroupOutputDirPrefix = "debug/irStepping/"