Skip to content

Instantly share code, notes, and snippets.

@elarkin
Last active September 30, 2021 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elarkin/db29bcfe35035f618dcb014db18b15cd to your computer and use it in GitHub Desktop.
Save elarkin/db29bcfe35035f618dcb014db18b15cd to your computer and use it in GitHub Desktop.
Test that produces no highlights
import com.intellij.codeInspection.{LocalInspectionTool, LocalInspectionToolSession, ProblemHighlightType, ProblemsHolder}
import com.intellij.psiPsiElementVisitor
import org.jetbrains.annotations.NotNull
import org.jetbrains.plugins.scala.lang.psi.api.ScalaElementVisitor
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScMethodCall
class ExampleMatcher extends LocalInspectionTool {
@NotNull override def buildVisitor(@NotNull holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor = {
buildVisitor(holder, isOnTheFly)
}
@NotNull override def buildVisitor(@NotNull holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = {
new ScalaElementVisitor {
override def visitMethodCallExpression(call: ScMethodCall) = {
super.visitMethodCallExpression(call)
holder.registerProblem(call, "I just want a highlight", ProblemHighlightType.ERROR)
}
}
}
}
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.psi.PsiFile
import com.intellij.testFramework.fixtures.{CodeInsightTestFixture, IdeaProjectTestFixture, IdeaTestFixtureFactory, TestFixtureBuilder}
import com.intellij.testFramework.UsefulTestCase
import com.iterable.hud.tests.util.TestUtils
import org.jetbrains.plugins.scala.ScalaFileType
import org.junit.Assert
class FixtureTest extends UsefulTestCase {
val inspectionClass: Class[_ <: LocalInspectionTool] = classOf[ExampleMatcher]
val fileType: LanguageFileType = ScalaFileType.INSTANCE
val fixtureFactory = IdeaTestFixtureFactory.getFixtureFactory
var fixtureBuilder: TestFixtureBuilder[IdeaProjectTestFixture] = _
var fixture: CodeInsightTestFixture = _
var psiFile: PsiFile = _
override def setUp(): Unit = {
super.setUp()
fixtureBuilder = fixtureFactory.createFixtureBuilder(this.getName)
val tmpFixture = fixtureBuilder.getFixture
fixture = fixtureFactory.createCodeInsightFixture(tmpFixture)
fixture.setUp()
fixture.setTestDataPath(TestUtils.getTestDataPath)
val testDataFile = getTestData()
psiFile = fixture.configureByFile(testDataFile)
fixture.enableInspections(inspectionClass)
}
def testIsThisThingOn(): Unit = {
val highlights = fixture.doHighlighting()
Assert.assertNotEquals("Should have highlights.", highlights.size(), 0)
}
def getTestData(): String = {
"/path/to/fixture"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment