From 0247b1ead072e59b246fe82e34929fbf6469a2eb Mon Sep 17 00:00:00 2001 From: ghik Date: Mon, 21 Oct 2019 17:15:43 +0200 Subject: [PATCH] tests for find usages --- .../plugins/hocon/HoconFileSetTestCase.scala | 10 +-- .../plugins/hocon/HoconMultiModuleTest.scala | 28 ++++---- .../hocon/usages/HoconFindUsagesTest.scala | 68 +++++++++++++++++++ testdata/findUsages/modA/lib/reference.conf | 7 ++ .../modA/libsrc/libpkg/LibMain.java | 8 +++ .../findUsages/modA/libsrc/reference.conf | 7 ++ testdata/findUsages/modA/src/application.conf | 3 + testdata/findUsages/modA/src/pkg/Main.java | 8 +++ testdata/findUsages/modB/lib/reference.conf | 7 ++ .../modB/libsrc/blibpkg/LibMain.java | 8 +++ .../findUsages/modB/libsrc/reference.conf | 7 ++ testdata/findUsages/modB/src/application.conf | 3 + testdata/findUsages/modB/src/bpkg/Main.java | 8 +++ 13 files changed, 153 insertions(+), 19 deletions(-) create mode 100644 test/org/jetbrains/plugins/hocon/usages/HoconFindUsagesTest.scala create mode 100644 testdata/findUsages/modA/lib/reference.conf create mode 100644 testdata/findUsages/modA/libsrc/libpkg/LibMain.java create mode 100644 testdata/findUsages/modA/libsrc/reference.conf create mode 100644 testdata/findUsages/modA/src/application.conf create mode 100644 testdata/findUsages/modA/src/pkg/Main.java create mode 100644 testdata/findUsages/modB/lib/reference.conf create mode 100644 testdata/findUsages/modB/libsrc/blibpkg/LibMain.java create mode 100644 testdata/findUsages/modB/libsrc/reference.conf create mode 100644 testdata/findUsages/modB/src/application.conf create mode 100644 testdata/findUsages/modB/src/bpkg/Main.java diff --git a/test/org/jetbrains/plugins/hocon/HoconFileSetTestCase.scala b/test/org/jetbrains/plugins/hocon/HoconFileSetTestCase.scala index 2eb4c25..457945d 100644 --- a/test/org/jetbrains/plugins/hocon/HoconFileSetTestCase.scala +++ b/test/org/jetbrains/plugins/hocon/HoconFileSetTestCase.scala @@ -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 @@ -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) diff --git a/test/org/jetbrains/plugins/hocon/HoconMultiModuleTest.scala b/test/org/jetbrains/plugins/hocon/HoconMultiModuleTest.scala index 256a499..6087387 100644 --- a/test/org/jetbrains/plugins/hocon/HoconMultiModuleTest.scala +++ b/test/org/jetbrains/plugins/hocon/HoconMultiModuleTest.scala @@ -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) @@ -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) diff --git a/test/org/jetbrains/plugins/hocon/usages/HoconFindUsagesTest.scala b/test/org/jetbrains/plugins/hocon/usages/HoconFindUsagesTest.scala new file mode 100644 index 0000000..4d5f510 --- /dev/null +++ b/test/org/jetbrains/plugins/hocon/usages/HoconFindUsagesTest.scala @@ -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) + } +} diff --git a/testdata/findUsages/modA/lib/reference.conf b/testdata/findUsages/modA/lib/reference.conf new file mode 100644 index 0000000..23f3b5a --- /dev/null +++ b/testdata/findUsages/modA/lib/reference.conf @@ -0,0 +1,7 @@ +top { + inner { + prop = 5 + } +} +other = ${top.inner.prop} +dyn = top.inner.prop \ No newline at end of file diff --git a/testdata/findUsages/modA/libsrc/libpkg/LibMain.java b/testdata/findUsages/modA/libsrc/libpkg/LibMain.java new file mode 100644 index 0000000..035d122 --- /dev/null +++ b/testdata/findUsages/modA/libsrc/libpkg/LibMain.java @@ -0,0 +1,8 @@ +package libpkg; + +public class LibMain { + public static String PROP = "top.inner.prop" + + public static void main(String[] args) { + } +} diff --git a/testdata/findUsages/modA/libsrc/reference.conf b/testdata/findUsages/modA/libsrc/reference.conf new file mode 100644 index 0000000..23f3b5a --- /dev/null +++ b/testdata/findUsages/modA/libsrc/reference.conf @@ -0,0 +1,7 @@ +top { + inner { + prop = 5 + } +} +other = ${top.inner.prop} +dyn = top.inner.prop \ No newline at end of file diff --git a/testdata/findUsages/modA/src/application.conf b/testdata/findUsages/modA/src/application.conf new file mode 100644 index 0000000..af4948d --- /dev/null +++ b/testdata/findUsages/modA/src/application.conf @@ -0,0 +1,3 @@ +top.inner.prop = something +moar = ${top.inner.prop} +dyn = top.inner.prop \ No newline at end of file diff --git a/testdata/findUsages/modA/src/pkg/Main.java b/testdata/findUsages/modA/src/pkg/Main.java new file mode 100644 index 0000000..2154350 --- /dev/null +++ b/testdata/findUsages/modA/src/pkg/Main.java @@ -0,0 +1,8 @@ +package pkg; + +public class Main { + public static String PROP = "top.inner.prop" + + public static void main(String[] args) { + } +} diff --git a/testdata/findUsages/modB/lib/reference.conf b/testdata/findUsages/modB/lib/reference.conf new file mode 100644 index 0000000..23f3b5a --- /dev/null +++ b/testdata/findUsages/modB/lib/reference.conf @@ -0,0 +1,7 @@ +top { + inner { + prop = 5 + } +} +other = ${top.inner.prop} +dyn = top.inner.prop \ No newline at end of file diff --git a/testdata/findUsages/modB/libsrc/blibpkg/LibMain.java b/testdata/findUsages/modB/libsrc/blibpkg/LibMain.java new file mode 100644 index 0000000..38a5114 --- /dev/null +++ b/testdata/findUsages/modB/libsrc/blibpkg/LibMain.java @@ -0,0 +1,8 @@ +package blibpkg; + +public class LibMain { + public static String PROP = "top.inner.prop" + + public static void main(String[] args) { + } +} diff --git a/testdata/findUsages/modB/libsrc/reference.conf b/testdata/findUsages/modB/libsrc/reference.conf new file mode 100644 index 0000000..23f3b5a --- /dev/null +++ b/testdata/findUsages/modB/libsrc/reference.conf @@ -0,0 +1,7 @@ +top { + inner { + prop = 5 + } +} +other = ${top.inner.prop} +dyn = top.inner.prop \ No newline at end of file diff --git a/testdata/findUsages/modB/src/application.conf b/testdata/findUsages/modB/src/application.conf new file mode 100644 index 0000000..af4948d --- /dev/null +++ b/testdata/findUsages/modB/src/application.conf @@ -0,0 +1,3 @@ +top.inner.prop = something +moar = ${top.inner.prop} +dyn = top.inner.prop \ No newline at end of file diff --git a/testdata/findUsages/modB/src/bpkg/Main.java b/testdata/findUsages/modB/src/bpkg/Main.java new file mode 100644 index 0000000..8f6d82d --- /dev/null +++ b/testdata/findUsages/modB/src/bpkg/Main.java @@ -0,0 +1,8 @@ +package bpkg; + +public class Main { + public static String PROP = "top.inner.prop" + + public static void main(String[] args) { + } +}