diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 92dc2c3d2c1..69bfe23740f 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -390,6 +390,10 @@ fun main(args: Array) { model("checker/codeFragments/imports", testMethod = "doTestWithImport", extension = "kt") } + testClass() { + model("quickfix.special/codeFragmentAutoImport", extension = "kt", recursive = false) + } + testClass() { model("checker/js") } diff --git a/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt new file mode 100644 index 00000000000..62918ac0059 --- /dev/null +++ b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +import java.util.concurrent.locks.* + +fun foo(lock: ReentrantReadWriteLock) { + val v = 1 +} \ No newline at end of file diff --git a/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.after b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.after new file mode 100644 index 00000000000..65ebb4e0fcd --- /dev/null +++ b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.after @@ -0,0 +1 @@ +lock.read() \ No newline at end of file diff --git a/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.after.imports b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.after.imports new file mode 100644 index 00000000000..03ecdc5d069 --- /dev/null +++ b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.after.imports @@ -0,0 +1 @@ +import kotlin.concurrent.read diff --git a/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.fragment b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.fragment new file mode 100644 index 00000000000..65ebb4e0fcd --- /dev/null +++ b/idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt.fragment @@ -0,0 +1 @@ +lock.read() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractEditorForEvaluateExpressionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractEditorForEvaluateExpressionTest.kt index 7662f1e8ec7..cfcdf512616 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractEditorForEvaluateExpressionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractEditorForEvaluateExpressionTest.kt @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.completion.test.AbstractJvmBasicCompletionTest import org.jetbrains.kotlin.idea.completion.test.ExpectedCompletionUtils import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHandlerTest +import org.jetbrains.kotlin.idea.test.JetWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.name.FqName @@ -36,6 +37,8 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.JetTestUtils import java.io.File +import kotlin.test.assertNull +import kotlin.test.assertTrue public abstract class AbstractCodeFragmentHighlightingTest : AbstractJetPsiCheckerTest() { override fun doTest(filePath: String) { @@ -73,17 +76,44 @@ public abstract class AbstractCodeFragmentCompletionHandlerTest : AbstractComple override fun doTest(testPath: String) { super.doTest(testPath) + val fragment = myFixture.getFile() as KtCodeFragment + fragment.checkImports(testPath) + } +} - val importList = fragment.importsAsImportList() - val fragmentAfterFile = File(testPath + ".after.imports") +public abstract class AbstractCodeFragmentAutoImportTest : AbstractJetPsiCheckerTest() { + override fun doTest(filePath: String) { + myFixture.configureByCodeFragment(filePath) + myFixture.doHighlighting() - if (importList != null && fragmentAfterFile.exists()) { - JetTestUtils.assertEqualsToFile(fragmentAfterFile, StringUtil.convertLineSeparators(importList.getText())) - } - else if (fragmentAfterFile.exists()) { - fail("ImportList is empty") - } + val importFix = myFixture.availableIntentions.singleOrNull { it.familyName == "Import" } + ?: error("No import fix available") + importFix.invoke(project, editor, file) + + myFixture.checkResultByFile(filePath + ".after") + + val fragment = myFixture.file as KtCodeFragment + fragment.checkImports(testDataPath + File.separator + filePath) + + val fixAfter = myFixture.availableIntentions.firstOrNull { it.familyName == "Import" } + assertNull(fixAfter, "No import fix should be available after") + } + + override fun getProjectDescriptor() = JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE + override fun getTestDataPath() = JetTestUtils.getHomeDirectory() +} + +private fun KtCodeFragment.checkImports(testPath: String) { + val importList = importsAsImportList() + val importsText = StringUtil.convertLineSeparators(importList?.text ?: "") + val fragmentAfterFile = File(testPath + ".after.imports") + + if (fragmentAfterFile.exists()) { + JetTestUtils.assertEqualsToFile(fragmentAfterFile, importsText) + } + else { + assertTrue(importsText.isEmpty(), "Unexpected imports found: $importsText" ) } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentAutoImportTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentAutoImportTestGenerated.java new file mode 100644 index 00000000000..3f2eff5ed21 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentAutoImportTestGenerated.java @@ -0,0 +1,43 @@ +/* + * 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.debugger.evaluate; + +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/quickfix.special/codeFragmentAutoImport") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class CodeFragmentAutoImportTestGenerated extends AbstractCodeFragmentAutoImportTest { + public void testAllFilesPresentInCodeFragmentAutoImport() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix.special/codeFragmentAutoImport"), Pattern.compile("^(.+)\\.kt$"), false); + } + + @TestMetadata("ExtensionFun.kt") + public void testExtensionFun() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix.special/codeFragmentAutoImport/ExtensionFun.kt"); + doTest(fileName); + } +}