From cbec005055dca1b576c12f40c8aea7921836777a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 15 Jun 2018 17:56:28 +0300 Subject: [PATCH] Add test for KT-21710 (gutters inside library sources) --- .../kotlin/generators/tests/GenerateTests.kt | 4 + .../generators/tests/GenerateTests.kt.172 | 4 + .../generators/tests/GenerateTests.kt.as31 | 4 + .../generators/tests/GenerateTests.kt.as32 | 4 + .../_library/InterfaceClass.kt | 16 ++++ .../codeInsightInLibrary/lineMarker/dummy.kt | 3 + .../codeInsight/AbstractLineMarkersTest.kt | 76 ++++++++------- ...AbstractLineMarkersTestInLibrarySources.kt | 93 +++++++++++++++++++ ...eMarkersTestInLibrarySourcesGenerated.java | 36 +++++++ 9 files changed, 205 insertions(+), 35 deletions(-) create mode 100644 idea/testData/codeInsightInLibrary/_library/InterfaceClass.kt create mode 100644 idea/testData/codeInsightInLibrary/lineMarker/dummy.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTestInLibrarySources.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/LineMarkersTestInLibrarySourcesGenerated.java diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index f98a33c0665..2f5374f6a08 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -551,6 +551,10 @@ fun main(args: Array) { model("codeInsight/lineMarker") } + testClass { + model("codeInsightInLibrary/lineMarker", testMethod = "doTestWithLibrary") + } + testClass { model("multiModuleLineMarker", extension = null, recursive = false) } diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.172 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.172 index 24676f85e61..98bb2c6b5e8 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.172 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.172 @@ -549,6 +549,10 @@ fun main(args: Array) { model("codeInsight/lineMarker") } + testClass { + model("codeInsightInLibrary/lineMarker", testMethod = "doTestWithLibrary") + } + testClass { model("multiModuleLineMarker", extension = null, recursive = false) } diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 index 4c209da5267..5bbbac961fa 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 @@ -547,6 +547,10 @@ fun main(args: Array) { model("codeInsight/lineMarker") } + testClass { + model("codeInsightInLibrary/lineMarker", testMethod = "doTestWithLibrary") + } + testClass { model("multiModuleLineMarker", extension = null, recursive = false) } diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 index 4c209da5267..5bbbac961fa 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 @@ -547,6 +547,10 @@ fun main(args: Array) { model("codeInsight/lineMarker") } + testClass { + model("codeInsightInLibrary/lineMarker", testMethod = "doTestWithLibrary") + } + testClass { model("multiModuleLineMarker", extension = null, recursive = false) } diff --git a/idea/testData/codeInsightInLibrary/_library/InterfaceClass.kt b/idea/testData/codeInsightInLibrary/_library/InterfaceClass.kt new file mode 100644 index 00000000000..a81f31a9c41 --- /dev/null +++ b/idea/testData/codeInsightInLibrary/_library/InterfaceClass.kt @@ -0,0 +1,16 @@ +package library + +interface My { + fun foo(): Int + + val s: String +} + +class Your : My { + override fun foo(): Int { + return 42 + } + + override val s: String + get() = "" +} \ No newline at end of file diff --git a/idea/testData/codeInsightInLibrary/lineMarker/dummy.kt b/idea/testData/codeInsightInLibrary/lineMarker/dummy.kt new file mode 100644 index 00000000000..600a6f8ddc2 --- /dev/null +++ b/idea/testData/codeInsightInLibrary/lineMarker/dummy.kt @@ -0,0 +1,3 @@ +fun main(args: Array) { + +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt index ef0459c12f5..8f566226319 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.codeInsight import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings import com.intellij.codeInsight.daemon.LineMarkerInfo import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl +import com.intellij.openapi.editor.Document import com.intellij.openapi.util.io.FileUtil import com.intellij.psi.PsiDocumentManager import com.intellij.rt.execution.junit.FileComparisonFailure @@ -40,58 +41,63 @@ abstract class AbstractLineMarkersTest : KotlinLightCodeInsightFixtureTestCase() return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE } - fun doTest(path: String) { + fun doTest(path: String) = doTest(path) {} + + protected fun doAndCheckHighlighting( + documentToAnalyze: Document, expectedHighlighting: ExpectedHighlightingData, expectedFile: File + ): List> { + myFixture.doHighlighting() + + val markers = DaemonCodeAnalyzerImpl.getLineMarkers(documentToAnalyze, myFixture.project) + + try { + expectedHighlighting.checkLineMarkers(markers, documentToAnalyze.text) + + // This is a workaround for sad bug in ExpectedHighlightingData: + // the latter doesn't throw assertion error when some line markers are expected, but none are present. + if (FileUtil.loadFile(expectedFile).contains(" Unit) { val fileText = FileUtil.loadFile(File(path)) try { ConfigLibraryUtil.configureLibrariesByDirective(myFixture.module, PlatformTestUtil.getCommunityPath(), fileText) - if (InTextDirectivesUtils.findStringWithPrefixes(fileText,"METHOD_SEPARATORS") != null) { + if (InTextDirectivesUtils.findStringWithPrefixes(fileText, "METHOD_SEPARATORS") != null) { DaemonCodeAnalyzerSettings.getInstance().SHOW_METHOD_SEPARATORS = true } - myFixture.configureByFile(path) val project = myFixture.project val document = myFixture.editor.document - val data = ExpectedHighlightingData( - document, false, false, false, myFixture.file) + val data = ExpectedHighlightingData(document, false, false, false, myFixture.file) data.init() PsiDocumentManager.getInstance(project).commitAllDocuments() - myFixture.doHighlighting() - - val markers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project) - - try { - data.checkLineMarkers(markers, document.text) - - // This is a workaround for sad bug in ExpectedHighlightingData: - // the latter doesn't throw assertion error when some line markers are expected, but none are present. - if (FileUtil.loadFile(File(path)).contains("".toRegex(), "") + val cleanFile = libraryClean.resolve(file.relativeTo(libraryOriginal).path) + cleanFile.createFile() + cleanFile.write(text) + } + this.libraryClean = File(libraryCleanPath) + } + return object : SdkAndMockLibraryProjectDescriptor(getLibraryCleanPath(), false) { + override fun configureModule(module: Module, model: ModifiableRootModel) { + super.configureModule(module, model) + + val library = model.moduleLibraryTable.getLibraryByName(SdkAndMockLibraryProjectDescriptor.LIBRARY_NAME)!! + val modifiableModel = library.modifiableModel + + modifiableModel.addRoot(LocalFileSystem.getInstance().findFileByIoFile(libraryClean!!)!!, OrderRootType.SOURCES) + modifiableModel.commit() + } + } + } + + fun doTestWithLibrary(path: String) { + doTest(path) { + val fileSystem = VirtualFileManager.getInstance().getFileSystem("file") + val libraryOriginal = File(getLibraryOriginalPath()) + val project = myFixture.project + for (file in libraryOriginal.walkTopDown().filter { !it.isDirectory }) { + myFixture.openFileInEditor(fileSystem.findFileByPath(file.absolutePath)!!) + val data = ExpectedHighlightingData( + myFixture.editor.document, false, false, false, myFixture.file + ) + data.init() + + val librarySourceFile = libraryClean!!.resolve(file.relativeTo(libraryOriginal).path) + myFixture.openFileInEditor(fileSystem.findFileByPath(librarySourceFile.absolutePath)!!) + val document = myFixture.editor.document + PsiDocumentManager.getInstance(project).commitAllDocuments() + + if (!ProjectRootsUtil.isLibrarySourceFile(project, myFixture.file.virtualFile)) { + throw AssertionError("File ${myFixture.file.virtualFile.path} should be in library sources!") + } + + doAndCheckHighlighting(document, data, file) + } + } + } + + override fun tearDown() { + libraryClean?.deleteRecursively() + ConfigLibraryUtil.removeLibrary(module, SdkAndMockLibraryProjectDescriptor.LIBRARY_NAME) + + super.tearDown() + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/LineMarkersTestInLibrarySourcesGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/LineMarkersTestInLibrarySourcesGenerated.java new file mode 100644 index 00000000000..824bce6fc54 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/LineMarkersTestInLibrarySourcesGenerated.java @@ -0,0 +1,36 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.codeInsight; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TargetBackend; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/codeInsightInLibrary/lineMarker") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class LineMarkersTestInLibrarySourcesGenerated extends AbstractLineMarkersTestInLibrarySources { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithLibrary, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInLineMarker() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsightInLibrary/lineMarker"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("dummy.kt") + public void testDummy() throws Exception { + runTest("idea/testData/codeInsightInLibrary/lineMarker/dummy.kt"); + } +}