Skip to content

Commit

Permalink
tests for find usages
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Oct 21, 2019
1 parent 2b3ad6b commit 0247b1e
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 19 deletions.
10 changes: 6 additions & 4 deletions test/org/jetbrains/plugins/hocon/HoconFileSetTestCase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.intellij.FileSetTestCase
import com.intellij.application.options.CodeStyle
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.fileTypes.FileTypeManager
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.{PsiFile, PsiFileFactory}
import com.intellij.testFramework.EditorTestUtil
import com.intellij.util.LocalTimeCounter
import org.jetbrains.plugins.hocon.lang.HoconLanguage
Expand All @@ -30,15 +30,17 @@ abstract class HoconFileSetTestCase(subpath: String)

protected def transform(data: Seq[String]): String

private[hocon] def createPseudoPhysicalHoconFile(text: String): HoconPsiFile = {
private[hocon] def createPseudoPhysicalFile(text: String, extension: String): PsiFile = {
val project = myProject
val tempFile = (project.getBaseDir: @silent("deprecated")) + "temp.conf"
val tempFile = (project.getBaseDir: @silent("deprecated")) + "temp." + extension
val fileType = FileTypeManager.getInstance.getFileTypeByFileName(tempFile)
PsiFileFactory.getInstance(project)
.createFileFromText(tempFile, fileType, text, LocalTimeCounter.currentTime(), true)
.asInstanceOf[HoconPsiFile]
}

def createPseudoPhysicalHoconFile(text: String): HoconPsiFile =
createPseudoPhysicalFile(text, "conf").asInstanceOf[HoconPsiFile]

private[hocon] def inWriteCommandAction[T](body: => T): T =
WriteCommandAction.writeCommandAction(myProject).compute(() => body)

Expand Down
28 changes: 13 additions & 15 deletions test/org/jetbrains/plugins/hocon/HoconMultiModuleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,20 @@ abstract class HoconMultiModuleTest extends UsefulTestCase with HoconTestUtils {
val moduleBuilders = subdirectories(rootPath)
.map((_, fixtureBuilder.addModule(classOf[JavaModuleFixtureBuilder[ModuleFixture]])))

moduleBuilders.foreach {
case (directory, builder) =>
builder.addContentRoot(directory.getPath)
moduleBuilders.foreach { case (directory, builder) =>
builder.addContentRoot(directory.getPath)

def addLibrary(libraryName: String): Unit = {
import OrderRootType._
val mapping = Map(CLASSES -> "", SOURCES -> "src").mapValues { suffix =>
Array(new File(directory, libraryName + suffix).getPath)
}

builder.addLibrary(directory.getName + libraryName, mapping.asJava)
def addLibrary(libraryName: String): Unit = {
import OrderRootType._
val mapping = Map(CLASSES -> "", SOURCES -> "src").mapValues { suffix =>
Array(new File(directory, libraryName + suffix).getPath)
}

addLibrary("lib")
addLibrary("testlib")
builder.addLibrary(directory.getName + libraryName, mapping.asJava)
}

addLibrary("lib")
addLibrary("testlib")
}

_fixture = JavaTestFixtureFactory.getFixtureFactory.createCodeInsightFixture(fixtureBuilder.getFixture)
Expand Down Expand Up @@ -88,9 +87,8 @@ object HoconMultiModuleTest {
private def setUpEntries(model: ModifiableRootModel): Unit = {
val contentEntry = model.getContentEntries.head

def addSourceFolder(name: String, kind: JavaSourceRootType): Unit = {
contentEntry.addSourceFolder(contentEntry.getFile.findChild(name), kind)
}
def addSourceFolder(name: String, kind: JavaSourceRootType): Unit =
contentEntry.getFile.findChild(name).opt.foreach(contentEntry.addSourceFolder(_, kind))

import JavaSourceRootType._
addSourceFolder("src", SOURCE)
Expand Down
68 changes: 68 additions & 0 deletions test/org/jetbrains/plugins/hocon/usages/HoconFindUsagesTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.jetbrains.plugins.hocon
package usages

import com.intellij.openapi.editor.LogicalPosition
import com.intellij.psi.search.ProjectScope
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl
import org.jetbrains.plugins.hocon.psi.HKey
import org.junit.Assert.assertEquals

class HoconFindUsagesTest extends HoconMultiModuleTest {
def rootPath: String = "testdata/findUsages"

override def moduleDependencies: Seq[(String, String)] = List("modA" -> "modB")

private val expectedUsages =
"""modA/lib/reference.conf:3:5
|modA/lib/reference.conf:6:21
|modA/lib/reference.conf:7:17
|modA/libsrc/libpkg/LibMain.java:4:44
|modA/src/application.conf:1:11
|modA/src/application.conf:2:20
|modA/src/application.conf:3:17
|modA/src/pkg/Main.java:4:44
|modB/lib/reference.conf:3:5
|modB/lib/reference.conf:6:21
|modB/lib/reference.conf:7:17
|modB/libsrc/blibpkg/LibMain.java:4:44
|modB/src/application.conf:1:11
|modB/src/application.conf:2:20
|modB/src/application.conf:3:17
|modB/src/bpkg/Main.java:4:44
|""".stripMargin

def testFindUsagesFromSources(): Unit =
testFindUsages("modA/src/application.conf", 1, 11)

def testFindUsagesFromLib(): Unit =
testFindUsages("modA/lib/reference.conf", 3, 5)

def testFindUsagesFromLibSources(): Unit =
testFindUsages("modA/libsrc/reference.conf", 3, 5)

def testFindUsagesFromDepModuleSources(): Unit =
testFindUsages("modB/src/application.conf", 1, 11)

def testFindUsagesFromDepModuleLib(): Unit =
testFindUsages("modB/lib/reference.conf", 3, 5)

def testFindUsagesFromDepModuleLibSources(): Unit =
testFindUsages("modB/libsrc/reference.conf", 3, 5)

private def testFindUsages(filename: String, line: Int, column: Int): Unit = {
val file = findHoconFile(filename, fixture.getProject)
fixture.openFileInEditor(file.getVirtualFile)
val offset = fixture.getEditor.logicalPositionToOffset(new LogicalPosition(line - 1, column - 1))
val hkey = file.findElementAt(offset).parentOfType[HKey].orNull
val fixtureImpl = fixture.asInstanceOf[CodeInsightTestFixtureImpl] // findUsages with scope is not exposed...
val usages = fixtureImpl.findUsages(hkey, ProjectScope.getAllScope(fixture.getProject))
val usagesRepr = usages.asScala.toVector.map { ui =>
val vfile = ui.getVirtualFile
fixture.openFileInEditor(vfile)
val logicalPosition = fixture.getEditor.offsetToLogicalPosition(ui.getNavigationOffset)
val usageFilename = vfile.getCanonicalPath.stripPrefix(contentRoot.getCanonicalPath + "/")
s"$usageFilename:${logicalPosition.line + 1}:${logicalPosition.column + 1}"
}.sorted.distinct.mkString("", "\n", "\n")
assertEquals(expectedUsages, usagesRepr)
}
}
7 changes: 7 additions & 0 deletions testdata/findUsages/modA/lib/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
top {
inner {
prop = 5
}
}
other = ${top.inner.prop}
dyn = top.inner.prop
8 changes: 8 additions & 0 deletions testdata/findUsages/modA/libsrc/libpkg/LibMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package libpkg;

public class LibMain {
public static String PROP = "top.inner.prop"

public static void main(String[] args) {
}
}
7 changes: 7 additions & 0 deletions testdata/findUsages/modA/libsrc/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
top {
inner {
prop = 5
}
}
other = ${top.inner.prop}
dyn = top.inner.prop
3 changes: 3 additions & 0 deletions testdata/findUsages/modA/src/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
top.inner.prop = something
moar = ${top.inner.prop}
dyn = top.inner.prop
8 changes: 8 additions & 0 deletions testdata/findUsages/modA/src/pkg/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg;

public class Main {
public static String PROP = "top.inner.prop"

public static void main(String[] args) {
}
}
7 changes: 7 additions & 0 deletions testdata/findUsages/modB/lib/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
top {
inner {
prop = 5
}
}
other = ${top.inner.prop}
dyn = top.inner.prop
8 changes: 8 additions & 0 deletions testdata/findUsages/modB/libsrc/blibpkg/LibMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package blibpkg;

public class LibMain {
public static String PROP = "top.inner.prop"

public static void main(String[] args) {
}
}
7 changes: 7 additions & 0 deletions testdata/findUsages/modB/libsrc/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
top {
inner {
prop = 5
}
}
other = ${top.inner.prop}
dyn = top.inner.prop
3 changes: 3 additions & 0 deletions testdata/findUsages/modB/src/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
top.inner.prop = something
moar = ${top.inner.prop}
dyn = top.inner.prop
8 changes: 8 additions & 0 deletions testdata/findUsages/modB/src/bpkg/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bpkg;

public class Main {
public static String PROP = "top.inner.prop"

public static void main(String[] args) {
}
}

0 comments on commit 0247b1e

Please sign in to comment.