From 71566d63104fda7accfcadefa1cabd3e262cf7cc Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Tue, 17 Apr 2018 15:17:55 +0300 Subject: [PATCH] Java: Improved test for inspection on 'module-info.java' after changes in PSI for static imports --- .../Java9RedundantRequiresStatementTest.kt | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/Java9RedundantRequiresStatementTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInspection/Java9RedundantRequiresStatementTest.kt index 2ca16f1402ee..1bcff19f3f2b 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/Java9RedundantRequiresStatementTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/Java9RedundantRequiresStatementTest.kt @@ -20,14 +20,11 @@ import com.intellij.codeInspection.ex.GlobalInspectionToolWrapper import com.intellij.codeInspection.java19modules.Java9RedundantRequiresStatementInspection import com.intellij.java.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase import com.intellij.java.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor -import com.intellij.openapi.application.WriteAction import com.intellij.openapi.application.ex.PathManagerEx -import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile import com.intellij.testFramework.InspectionTestUtil import com.intellij.testFramework.createGlobalContextForTool import org.intellij.lang.annotations.Language -import java.io.IOException /** * @author Pavel.Dolgov @@ -103,30 +100,18 @@ class Java9RedundantRequiresStatementTest : LightJava9ModulesCodeInsightFixtureT private fun mainModule(@Language("JAVA") text: String) { addFile("module-info.java", text, ModuleDescriptor.MAIN) - val mainJavaFile = addMainFileWithoutHighlighting() // need to remove error highlighting because it confuses global inspections + + val mainFile = myMainFile + if (mainFile != null) { + myFixture.configureFromExistingVirtualFile(mainFile) + myFixture.checkHighlighting() // Sanity check: make sure the imports work (or don't work) as expected + } val toolWrapper = GlobalInspectionToolWrapper(Java9RedundantRequiresStatementInspection()) val scope = AnalysisScope(project) val globalContext = createGlobalContextForTool(scope, project, listOf(toolWrapper)) InspectionTestUtil.runTool(toolWrapper, scope, globalContext) InspectionTestUtil.compareToolResults(globalContext, toolWrapper, true, testDataPath + getTestName(true)) - - restoreAndCheckMainFileHighlighting(mainJavaFile) // make sure the imports work (or don't work) as expected - } - - private fun addMainFileWithoutHighlighting(): VirtualFile? = - myMainClassText - ?.replace(Regex(""), "") - ?.let { t -> addFile("org.example.main/Main.java", t) } - ?.also { f -> myFixture.configureFromExistingVirtualFile(f) } - - private fun restoreAndCheckMainFileHighlighting(file: VirtualFile?) { - val text = myMainClassText - assertFalse("File and text should either both exist or both not exist", file == null && text != null || file != null && text == null) - if (file != null && text != null) { - WriteAction.runAndWait { VfsUtil.saveText(file, text) } - myFixture.checkHighlighting() - } } private fun add(packageName: String, className: String, module: ModuleDescriptor, body: String = "", vararg imports: String) { @@ -139,18 +124,19 @@ class Java9RedundantRequiresStatementTest : LightJava9ModulesCodeInsightFixtureT }""".trimIndent(), module = module) } - private var myMainClassText: String? = null + private var myMainFile: VirtualFile? = null private fun mainClass(vararg imports: String, staticImports: List = emptyList()) { val importsText = imports.joinToString("\n") { "import ${it};" } val staticImportsText = staticImports.joinToString("\n") { "import static ${it};" } - myMainClassText = """ + val mainText = """ package org.example.main; ${importsText} ${staticImportsText} public class Main { void main() {} }""".trimIndent() + myMainFile = addFile("org.example.main/Main.java", mainText) } override fun getTestDataPath() = PathManagerEx.getTestDataPath() + "/inspection/redundantRequiresStatement/"