From 503f159f4d8f23e80864d90042b5865d48917398 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Sun, 14 Nov 2021 01:36:39 +0300 Subject: [PATCH] [Native] External tests: Preserve original line offsets --- .../group/ExtTestCaseGroupProvider.kt | 20 ++++++++++++------- .../blackboxtest/util/PsiTransformation.kt | 16 +++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/group/ExtTestCaseGroupProvider.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/group/ExtTestCaseGroupProvider.kt index 949c4639c86..37b8f100d05 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/group/ExtTestCaseGroupProvider.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/group/ExtTestCaseGroupProvider.kt @@ -228,7 +228,7 @@ private class ExtTestDataFile( addAnnotationEntry( objectDeclaration, handler.psiFactory.createAnnotationEntry(THREAD_LOCAL_ANNOTATION) - ).ensureSurroundedByWhiteSpace(" ") + ).ensureSurroundedByWhiteSpace() } super.visitObjectDeclaration(objectDeclaration) @@ -287,10 +287,10 @@ private class ExtTestDataFile( if (oldPackageDirective != null) { // Replace old package directive by the new one. - oldPackageDirective.replace(newPackageDirective).ensureSurroundedByWhiteSpace("\n\n") + oldPackageDirective.replace(newPackageDirective).ensureSurroundedByWhiteSpace() } else { // Insert the package directive immediately after file-level annotations. - file.addAfter(newPackageDirective, file.fileAnnotationList).ensureSurroundedByWhiteSpace("\n\n") + file.addAfter(newPackageDirective, file.fileAnnotationList).ensureSurroundedByWhiteSpace() } visitKtElement(file, file.collectAccessibleDeclarationNames()) @@ -482,16 +482,16 @@ private class ExtTestDataFile( override fun visitKtFile(file: KtFile) { val oldFileAnnotationList = file.fileAnnotationList - val newFileAnnotationsList = handler.psiFactory.createFile(buildString { + val newFileAnnotationList = handler.psiFactory.createFile(buildString { allFileLevelAnnotationsSorted.forEach(::appendLine) }).fileAnnotationList!! if (oldFileAnnotationList != null) { // Replace old annotations list by the new one. - oldFileAnnotationList.replace(newFileAnnotationsList).ensureSurroundedByWhiteSpace("\n\n") + oldFileAnnotationList.replace(newFileAnnotationList).ensureSurroundedByWhiteSpace() } else { // Insert the annotations list immediately before package directive. - file.addBefore(newFileAnnotationsList, file.packageDirective).ensureSurroundedByWhiteSpace("\n\n") + file.addBefore(newFileAnnotationList, file.packageDirective).ensureSurroundedByWhiteSpace() } } }) @@ -805,7 +805,13 @@ private class ExtTestDataFileStructureFactory(parentDisposable: Disposable) : Te private inner class FilesAndModules(originalTestDataFile: File, initialCleanUpTransformation: (String) -> String) { private val testFileFactory = ExtTestFileFactory() - private val generatedFiles = TestFiles.createTestFiles(DEFAULT_FILE_NAME, originalTestDataFile.readText(), testFileFactory) + + private val generatedFiles = TestFiles.createTestFiles( + /* testFileName = */ DEFAULT_FILE_NAME, + /* expectedText = */ originalTestDataFile.readText(), + /* factory = */ testFileFactory, + /* preserveLocations = */ true + ) private val lazyData: Triple, Map, MutableList> by lazy { // Clean up contents of every individual test file. Important: This should be done only after parsing testData file, diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/util/PsiTransformation.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/util/PsiTransformation.kt index a8e136a5f02..891e1b6ddac 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/util/PsiTransformation.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/util/PsiTransformation.kt @@ -11,21 +11,21 @@ import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.psiUtil.nextLeaf import org.jetbrains.kotlin.psi.psiUtil.prevLeaf -internal fun PsiElement.ensureSurroundedByWhiteSpace(whiteSpace: String): PsiElement = - ensureHasWhiteSpaceBefore(whiteSpace).ensureHasWhiteSpaceAfter(whiteSpace) +internal fun PsiElement.ensureSurroundedByWhiteSpace(): PsiElement = + ensureHasWhiteSpaceBefore().ensureHasWhiteSpaceAfter() -private fun PsiElement.ensureHasWhiteSpaceBefore(whiteSpace: String): PsiElement { +private fun PsiElement.ensureHasWhiteSpaceBefore(): PsiElement { val (fileBoundaryReached, whiteSpaceBefore) = whiteSpaceBefore() - if (!fileBoundaryReached and !whiteSpaceBefore.endsWith(whiteSpace)) { - parent.addBefore(KtPsiFactory(project).createWhiteSpace(whiteSpace), this) + if (!fileBoundaryReached and !whiteSpaceBefore.endsWith(" ")) { + parent.addBefore(KtPsiFactory(project).createWhiteSpace(" "), this) } return this } -private fun PsiElement.ensureHasWhiteSpaceAfter(whiteSpace: String): PsiElement { +private fun PsiElement.ensureHasWhiteSpaceAfter(): PsiElement { val (fileBoundaryReached, whiteSpaceAfter) = whiteSpaceAfter() - if (!fileBoundaryReached and !whiteSpaceAfter.startsWith(whiteSpace)) { - parent.addAfter(KtPsiFactory(project).createWhiteSpace(whiteSpace), this) + if (!fileBoundaryReached and !whiteSpaceAfter.startsWith(" ")) { + parent.addAfter(KtPsiFactory(project).createWhiteSpace(" "), this) } return this }