From fe96a8563a4ada1c433f56898b6a83d82ee78d1e Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 29 Apr 2015 18:18:55 +0300 Subject: [PATCH] Inspections: Implement multi-file test class --- .../kotlin/generators/tests/GenerateTests.kt | 9 +- .../AbstractJetMultiFileInspectionTest.kt | 96 +++++++++++++++++++ .../JetMultiFileInspectionTestGenerated.java | 37 +++++++ 3 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractJetMultiFileInspectionTest.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/JetMultiFileInspectionTestGenerated.java diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 16a8122895b..ada6da94f66 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -39,10 +39,7 @@ import org.jetbrains.kotlin.generators.tests.generator.TestGenerator.TargetBacke import org.jetbrains.kotlin.generators.tests.reservedWords.generateTestDataForReservedWords import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest -import org.jetbrains.kotlin.idea.codeInsight.AbstractInsertImportOnPasteTest -import org.jetbrains.kotlin.idea.codeInsight.AbstractJetInspectionTest -import org.jetbrains.kotlin.idea.codeInsight.AbstractLineMarkersTest -import org.jetbrains.kotlin.idea.codeInsight.AbstractOutOfBlockModificationTest +import org.jetbrains.kotlin.idea.codeInsight.* import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractCodeMoverTest import org.jetbrains.kotlin.idea.codeInsight.surroundWith.AbstractSurroundWithTest import org.jetbrains.kotlin.idea.codeInsight.unwrap.AbstractUnwrapRemoveTest @@ -502,6 +499,10 @@ fun main(args: Array) { model("multiFileIntentions", extension = "test", singleClass = true) } + testClass(javaClass()) { + model("multiFileInspections", extension = "test", singleClass = true) + } + testClass(javaClass()) { model("configuration/android-gradle", pattern = """(\w+)_before\.gradle$""", testMethod = "doTestAndroidGradle") model("configuration/gradle", pattern = """(\w+)_before\.gradle$""", testMethod = "doTestGradle") diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractJetMultiFileInspectionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractJetMultiFileInspectionTest.kt new file mode 100644 index 00000000000..d584b8f80b3 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractJetMultiFileInspectionTest.kt @@ -0,0 +1,96 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.codeInsight + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.analysis.AnalysisScope +import com.intellij.codeInspection.InspectionManager +import com.intellij.codeInspection.LocalInspectionTool +import com.intellij.codeInspection.ex.InspectionManagerEx +import com.intellij.codeInspection.ex.LocalInspectionToolWrapper +import com.intellij.openapi.util.io.FileUtil +import com.intellij.testFramework.IdeaTestUtil +import com.intellij.testFramework.InspectionTestUtil +import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl +import org.jetbrains.kotlin.idea.refactoring.move.getString +import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil +import org.jetbrains.kotlin.idea.test.KotlinMultiFileTestCase +import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import java.io.File + +public abstract class AbstractJetMultiFileInspectionTest: KotlinMultiFileTestCase() { + init { + myDoCompare = false + } + + protected fun doTest(path: String) { + val configFile = File(path) + val config = JsonParser().parse(FileUtil.loadFile(configFile, true)) as JsonObject + val inspection = LocalInspectionToolWrapper(Class.forName(config.getString("inspectionClass")).newInstance() as LocalInspectionTool) + + val withRuntime = config["withRuntime"]?.getAsBoolean() ?: false + if (withRuntime) { + ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk()) + } + + val withFullJdk = config["withFullJdk"]?.getAsBoolean() ?: false + + doTest({ rootDir, rootAfter -> + try { + if (withRuntime) { + ConfigLibraryUtil.configureKotlinRuntimeAndSdk( + getModule(), + if (withFullJdk) PluginTestCaseBase.fullJdk() else PluginTestCaseBase.mockJdk() + ) + } + + val scope = AnalysisScope(myProject) + scope.invalidate() + + val globalContext = CodeInsightTestFixtureImpl.createGlobalContextForTool( + scope, + myProject, + InspectionManager.getInstance(myProject) as InspectionManagerEx, + inspection + ) + + InspectionTestUtil.runTool(inspection, scope, globalContext) + InspectionTestUtil.compareToolResults(globalContext, inspection, false, configFile.getParent()) + } + finally { + if (withRuntime) { + ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(getModule(), IdeaTestUtil.getMockJdk17()) + } + } + }, + getTestDirName(true)) + } + + protected fun getTestDirName(lowercaseFirstLetter : Boolean) : String { + val testName = getTestName(lowercaseFirstLetter) + return testName.substring(0, testName.lastIndexOf('_')).replace('_', '/') + } + + protected override fun getTestRoot() : String { + return "/multiFileInspections/" + } + + protected override fun getTestDataPath() : String { + return PluginTestCaseBase.getTestDataPathBase() + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/JetMultiFileInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/JetMultiFileInspectionTestGenerated.java new file mode 100644 index 00000000000..f16797426f8 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/JetMultiFileInspectionTestGenerated.java @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.codeInsight; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.JetTestUtils; +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/multiFileInspections") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class JetMultiFileInspectionTestGenerated extends AbstractJetMultiFileInspectionTest { + public void testAllFilesPresentInMultiFileInspections() throws Exception { + JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/multiFileInspections"), Pattern.compile("^(.+)\\.test$")); + } +}