Java: Improved test for inspection on 'module-info.java' after changes in PSI for static imports

This commit is contained in:
Pavel Dolgov
2018-04-17 15:19:48 +03:00
parent 19112d6a72
commit 71566d6310
@@ -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("</?error.*?>"), "")
?.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<IOException> { 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<String> = 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/"