From 2bd4aebd1fe6277926d0a20e5999bccfad5a9628 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 8 Nov 2021 17:38:00 +0700 Subject: [PATCH] [kotlin] Kotlin property tests for intention actions (including preview); KTIJ-20104 GitOrigin-RevId: 2df895af87021104623bf2f27db179753d06a3de --- .../testSrc/tests/testGroups.properties | 2 + .../KotlinCodeInsightSanityTest.kt | 62 +++++++++++++++++++ .../propertyBased/KotlinIntentionPolicy.kt | 29 +++++++++ 3 files changed, 93 insertions(+) create mode 100644 plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt create mode 100644 plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt diff --git a/platform/testFramework/testSrc/tests/testGroups.properties b/platform/testFramework/testSrc/tests/testGroups.properties index f604f850da95..5a01473b8662 100644 --- a/platform/testFramework/testSrc/tests/testGroups.properties +++ b/platform/testFramework/testSrc/tests/testGroups.properties @@ -209,6 +209,7 @@ com.intellij.lang.properties.propertyBased.PropertiesCodeInsightSanityTest org.jetbrains.plugins.groovy.GroovySanityTest com.intellij.workspaceModel.storage.propertyBased.* com.intellij.compiler.artifacts.propertybased.* +org.jetbrains.kotlin.idea.propertyBased.* [GROOVY_TESTS] org.jetbrains.plugins.groovy.* @@ -246,6 +247,7 @@ com.intellij.testDiscovery.* org.jetbrains.kotlin.* org.jetbrains.uast.test.kotlin.* com.intellij.codeInspection.tests.kotlin.* +-org.jetbrains.kotlin.idea.propertyBased.* [KOTLIN_FIR_TESTS] org.jetbrains.kotlin.idea.fir.* diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt new file mode 100644 index 000000000000..5ea068732287 --- /dev/null +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt @@ -0,0 +1,62 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.kotlin.idea.propertyBased + +import com.intellij.openapi.application.PathManager +import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl +import com.intellij.openapi.util.RecursionManager +import com.intellij.psi.PsiFile +import com.intellij.testFramework.LightProjectDescriptor +import com.intellij.testFramework.SkipSlowTestLocally +import com.intellij.testFramework.propertyBased.* +import org.jetbrains.jetCheck.Generator +import org.jetbrains.jetCheck.PropertyChecker +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.test.ProjectDescriptorWithStdlibSources +import java.io.File +import java.util.function.Function +import java.util.function.Supplier + +@SkipSlowTestLocally +class KotlinCodeInsightSanityTest : KotlinLightCodeInsightFixtureTestCase() { + override fun setUp() { + super.setUp() + RecursionManager.disableMissedCacheAssertions(testRootDisposable) + } + + override fun tearDown() { + // remove jdk if it was created during highlighting to avoid leaks + try { + JavaAwareProjectJdkTableImpl.removeInternalJdkInTests() + } catch (e: Throwable) { + addSuppressedException(e) + } finally { + super.tearDown() + } + } + + override fun getProjectDescriptor(): LightProjectDescriptor = ProjectDescriptorWithStdlibSources.INSTANCE + + fun testRandomActivity() { + enableInspections() + val actionSupplier = actionOnKotlinFiles { file: PsiFile -> + Generator.sampledFrom( + InvokeIntention(file, KotlinIntentionPolicy()), + //TODO: support completion mutators + //InvokeCompletion(file, KotlinCompletionPolicy()), + StripTestDataMarkup(file), + DeleteRange(file) + ) + } + PropertyChecker + .checkScenarios(actionSupplier) + } + + private fun enableInspections() { + MadTestingUtil.enableAllInspections(project) + } + + private fun actionOnKotlinFiles(fileActions: Function>): Supplier { + return MadTestingUtil.actionsOnFileContents(myFixture, PathManager.getHomePath(), + { f: File -> f.name.endsWith(".kt") }, fileActions) + } +} diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt new file mode 100644 index 000000000000..b8d5bb92892f --- /dev/null +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt @@ -0,0 +1,29 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.kotlin.idea.propertyBased + +import com.intellij.codeInsight.daemon.impl.HighlightInfo +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.codeInsight.intention.IntentionActionDelegate +import com.intellij.openapi.editor.Editor +import com.intellij.psi.PsiFile +import com.intellij.testFramework.propertyBased.IntentionPolicy +import org.jetbrains.kotlin.idea.intentions.ConvertToScopeIntention + +internal class KotlinIntentionPolicy : IntentionPolicy() { + override fun shouldSkipIntention(actionText: String): Boolean = + // These intentions don't modify code (probably should not start in write-action?) + actionText == "Enable a trailing comma by default in the formatter" || + actionText == "Disable a trailing comma by default in the formatter" + + override fun mayBreakCode(action: IntentionAction, editor: Editor, file: PsiFile): Boolean = false + + override fun shouldTolerateIntroducedError(info: HighlightInfo): Boolean = false + + override fun shouldCheckPreview(action: IntentionAction): Boolean { + val unwrapped = IntentionActionDelegate.unwrap(action) + val skipPreview = + action.familyName == "Create from usage" || // Starts template but may also perform modifications before that; thus not so easy to support + unwrapped is ConvertToScopeIntention // Performs reference search which must be run under progress. Probably we can generate diff excluding references?.. + return !skipPreview + } +}