From 0df179de3b4739ea5e1dd4141054dbf07cf92f5d Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 11 Jan 2018 20:12:25 +0300 Subject: [PATCH] 172: [Unneeded] Revert "AbstractNavigateToLibraryTest: fix for invalid files" This reverts commit e71b1757be6ca4b9ef49d65c6250a94f5f724f53. --- .../AbstractNavigateToLibraryTest.kt.172 | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateToLibraryTest.kt.172 diff --git a/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateToLibraryTest.kt.172 b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateToLibraryTest.kt.172 new file mode 100644 index 00000000000..a6cbafcb7b6 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/decompiler/navigation/AbstractNavigateToLibraryTest.kt.172 @@ -0,0 +1,131 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.decompiler.navigation + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference +import junit.framework.TestCase +import org.jetbrains.kotlin.idea.navigation.NavigationTestUtils +import org.jetbrains.kotlin.idea.references.KtReference +import org.jetbrains.kotlin.idea.test.* +import org.jetbrains.kotlin.idea.util.ProjectRootsUtil +import org.jetbrains.kotlin.test.KotlinTestUtils +import java.io.File +import java.util.* + +abstract class AbstractNavigateToLibraryTest : KotlinCodeInsightTestCase() { + + protected fun doTest(path: String): Unit = doTestEx(path) + + protected fun doWithJSModuleTest(path: String): Unit = doTestEx(path) { + val jsModule = this.createModule("js-module") + jsModule.configureAs(ModuleKind.KOTLIN_JAVASCRIPT) + } + + abstract val withSource: Boolean + abstract val expectedFileExt: String + + protected fun doTestEx(path: String, additionalConfig: (() -> Unit)? = null) { + configureByFile(path) + module.configureAs(getProjectDescriptor()) + + if (additionalConfig != null) { + additionalConfig() + } + + NavigationChecker.checkAnnotatedCode(file, File(path.replace(".kt", expectedFileExt))) + } + + override fun tearDown() { + SourceNavigationHelper.setForceResolve(false) + super.tearDown() + } + + override fun getTestDataPath(): String = + KotlinTestUtils.getHomeDirectory() + File.separator + + + open fun getProjectDescriptor(): KotlinLightProjectDescriptor = + SdkAndMockLibraryProjectDescriptor( + PluginTestCaseBase.getTestDataPathBase() + "/decompiler/navigation/library", + withSource + ) +} + +abstract class AbstractNavigateToDecompiledLibraryTest : AbstractNavigateToLibraryTest() { + override val withSource: Boolean get() = false + override val expectedFileExt: String get() = ".decompiled.expected" +} + +abstract class AbstractNavigateToLibrarySourceTest : AbstractNavigateToLibraryTest() { + override val withSource: Boolean get() = true + override val expectedFileExt: String get() = ".source.expected" +} + +class NavigationChecker(val file: PsiFile, val referenceTargetChecker: (PsiElement) -> Unit) { + fun annotatedLibraryCode(): String { + return NavigationTestUtils.getNavigateElementsText(file.project, collectInterestingNavigationElements()) + } + + private fun collectInterestingNavigationElements() = + collectInterestingReferences().map { + val target = it.resolve() + TestCase.assertNotNull(target) + target!!.navigationElement + } + + private fun collectInterestingReferences(): Collection { + val referenceContainersToReferences = LinkedHashMap() + for (offset in 0..file.textLength - 1) { + val ref = file.findReferenceAt(offset) + val refs = when (ref) { + is KtReference -> listOf(ref) + is PsiMultiReference -> ref.references.filterIsInstance() + else -> emptyList() + } + + refs.forEach { referenceContainersToReferences.addReference(it) } + } + return referenceContainersToReferences.values + } + + private fun MutableMap.addReference(ref: KtReference) { + if (containsKey(ref.element)) return + val target = ref.resolve() ?: return + + referenceTargetChecker(target) + + val targetNavPsiFile = target.navigationElement.containingFile ?: return + + val targetNavFile = targetNavPsiFile.virtualFile ?: return + + if (!ProjectRootsUtil.isProjectSourceFile(target.project, targetNavFile)) { + put(ref.element, ref) + } + } + + companion object { + fun checkAnnotatedCode(file: PsiFile, expectedFile: File, referenceTargetChecker: (PsiElement) -> Unit = {}) { + val navigationChecker = NavigationChecker(file, referenceTargetChecker) + for (forceResolve in listOf(false, true)) { + SourceNavigationHelper.setForceResolve(forceResolve) + KotlinTestUtils.assertEqualsToFile(expectedFile, navigationChecker.annotatedLibraryCode()) + } + } + } +} \ No newline at end of file