|
| 1 | +/* |
| 2 | + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.jetbrains.kotlin.gradle |
| 7 | + |
| 8 | +import com.intellij.openapi.vfs.VirtualFile |
| 9 | +import com.intellij.psi.* |
| 10 | +import com.intellij.psi.util.PsiTreeUtil |
| 11 | +import com.intellij.testFramework.runInEdtAndGet |
| 12 | +import org.jetbrains.kotlin.idea.codeInsight.gradle.MultiplePluginVersionGradleImportingTestCase |
| 13 | +import org.jetbrains.kotlin.idea.core.util.toPsiFile |
| 14 | +import org.jetbrains.kotlin.idea.util.application.runReadAction |
| 15 | +import org.jetbrains.kotlin.psi.KtTreeVisitorVoid |
| 16 | +import org.jetbrains.kotlin.psi.psiUtil.startOffset |
| 17 | +import org.jetbrains.plugins.gradle.tooling.annotation.PluginTargetVersions |
| 18 | +import org.junit.Test |
| 19 | + |
| 20 | +// TODO: run this test on the Gradle plugin from the current build |
| 21 | +// 1. specify appropriate 'pluginVersion' in [PluginTargetVersions] |
| 22 | +// 2. avoid using fixed Gradle plugin version in testdata |
| 23 | +class ImportAndCheckNavigation : MultiplePluginVersionGradleImportingTestCase() { |
| 24 | + |
| 25 | + @Test |
| 26 | + @PluginTargetVersions(gradleVersion = "5.0+", pluginVersion = "1.3.50+") |
| 27 | + fun testNavigationToCommonizedLibrary() { |
| 28 | + val files = configureAndImportProject() |
| 29 | + |
| 30 | + files.forEach { vFile -> |
| 31 | + val referencesToTest = vFile.collectReferencesToTest() |
| 32 | + referencesToTest.forEach { (psiReference, expectedElementText) -> |
| 33 | + runReadAction { |
| 34 | + val referencedElement = psiReference.resolve() |
| 35 | + assertNotNull( |
| 36 | + "PSI reference \"${psiReference.canonicalText}\" in ${vFile.relPath} can't be resolved", |
| 37 | + referencedElement |
| 38 | + ) |
| 39 | + |
| 40 | + val referencedElementText = referencedElement!!.text |
| 41 | + assertTrue( |
| 42 | + "Resolved reference \"${psiReference.canonicalText}\" from ${vFile.relPath} does not " + |
| 43 | + "contain \"$expectedElementText\", instead it contains \"$referencedElementText\"", |
| 44 | + expectedElementText in referencedElementText |
| 45 | + ) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + override fun testDataDirName() = "importAndCheckNavigation" |
| 52 | + |
| 53 | + private fun configureAndImportProject(): List<VirtualFile> { |
| 54 | + val files = configureByFiles() |
| 55 | + importProject() |
| 56 | + return files |
| 57 | + } |
| 58 | + |
| 59 | + private fun VirtualFile.collectReferencesToTest(): Map<PsiReference, String> { |
| 60 | + if (extension != "kt") return emptyMap() |
| 61 | + |
| 62 | + val referencesToTest = mutableMapOf<PsiReference, String>() |
| 63 | + |
| 64 | + runInEdtAndGet { |
| 65 | + val psiFile = toPsiFile(project) |
| 66 | + assertNotNull( |
| 67 | + "Can't get PSI file for $relPath", |
| 68 | + psiFile |
| 69 | + ) |
| 70 | + |
| 71 | + psiFile!!.accept(object : KtTreeVisitorVoid() { |
| 72 | + override fun visitComment(comment: PsiComment) { |
| 73 | + val commentText = comment.text ?: return |
| 74 | + |
| 75 | + val unwrappedCommentText = if (commentText.startsWith("/*")) { |
| 76 | + commentText.removePrefix("/*").removeSuffix("*/") |
| 77 | + } else { |
| 78 | + commentText.removePrefix("//") |
| 79 | + }.trim(Char::isWhitespace) |
| 80 | + |
| 81 | + if (unwrappedCommentText.startsWith("NAVIGATION-TARGET:")) { |
| 82 | + val expectedElementText = unwrappedCommentText.substringAfter("NAVIGATION-TARGET:").trimStart(Char::isWhitespace) |
| 83 | + assertTrue( |
| 84 | + "Empty expected element text in $relPath in comment \"$commentText\" at offset ${comment.startOffset}", |
| 85 | + expectedElementText.isNotEmpty() |
| 86 | + ) |
| 87 | + |
| 88 | + val nextElement = PsiTreeUtil.skipSiblingsForward( |
| 89 | + comment, |
| 90 | + PsiWhiteSpace::class.java, PsiComment::class.java |
| 91 | + ) |
| 92 | + assertNotNull( |
| 93 | + "Next element not found in $relPath after comment \"$commentText\" at offset ${comment.startOffset}", |
| 94 | + nextElement |
| 95 | + ) |
| 96 | + |
| 97 | + val reference = nextElement!!.findReferenceAt(0) |
| 98 | + assertNotNull( |
| 99 | + "Can find PSI reference for \"${nextElement.text}\" in $relPath at offset ${nextElement.startOffset}", |
| 100 | + reference |
| 101 | + ) |
| 102 | + |
| 103 | + referencesToTest[reference!!] = expectedElementText |
| 104 | + } |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + return referencesToTest |
| 111 | + } |
| 112 | + |
| 113 | + private val VirtualFile.relPath: String |
| 114 | + get() = canonicalPath?.removePrefix(projectPath)?.trimStart('/', '\\') ?: name |
| 115 | +} |
0 commit comments