From c7cefad7d5eb0bcc6ca19ac0523afd911513caf8 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Wed, 13 Apr 2016 16:45:49 +0300 Subject: [PATCH] Implement IdeReplCompletionTest to test completion of entities declared in already evaluated lines --- .../kotlin/generators/tests/GenerateTests.kt | 5 ++ .../testData/repl/completion/builtInMember.kt | 4 + idea/testData/repl/completion/definedClass.kt | 4 + .../repl/completion/definedClassMember.kt | 5 ++ .../repl/completion/definedExtension.kt | 4 + idea/testData/repl/completion/functions.kt | 8 ++ idea/testData/repl/completion/stdlib.kt | 3 + idea/testData/repl/completion/variables.kt | 5 ++ .../repl/AbstractIdeReplCompletionTest.kt | 76 ++++++++++++++++++ .../repl/IdeReplCompletionTestGenerated.java | 79 +++++++++++++++++++ 10 files changed, 193 insertions(+) create mode 100644 idea/testData/repl/completion/builtInMember.kt create mode 100644 idea/testData/repl/completion/definedClass.kt create mode 100644 idea/testData/repl/completion/definedClassMember.kt create mode 100644 idea/testData/repl/completion/definedExtension.kt create mode 100644 idea/testData/repl/completion/functions.kt create mode 100644 idea/testData/repl/completion/stdlib.kt create mode 100644 idea/testData/repl/completion/variables.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/repl/AbstractIdeReplCompletionTest.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/repl/IdeReplCompletionTestGenerated.java diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 0a21a65f0d8..3a05c894205 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -102,6 +102,7 @@ import org.jetbrains.kotlin.idea.refactoring.pullUp.AbstractPullUpTest import org.jetbrains.kotlin.idea.refactoring.pushDown.AbstractPushDownTest import org.jetbrains.kotlin.idea.refactoring.rename.AbstractRenameTest import org.jetbrains.kotlin.idea.refactoring.safeDelete.AbstractSafeDeleteTest +import org.jetbrains.kotlin.idea.repl.AbstractIdeReplCompletionTest import org.jetbrains.kotlin.idea.resolve.* import org.jetbrains.kotlin.idea.structureView.AbstractKotlinFileStructureTest import org.jetbrains.kotlin.idea.stubs.AbstractMultiFileHighlightingTest @@ -813,6 +814,10 @@ fun main(args: Array) { testClass() { model("codeInsight/generate/toString") } + + testClass() { + model("repl/completion") + } } testGroup("idea/idea-maven/test", "idea/idea-maven/testData") { diff --git a/idea/testData/repl/completion/builtInMember.kt b/idea/testData/repl/completion/builtInMember.kt new file mode 100644 index 00000000000..787c39f2ec6 --- /dev/null +++ b/idea/testData/repl/completion/builtInMember.kt @@ -0,0 +1,4 @@ +>> val c = 4 +-- c.unary +// EXIST: unaryPlus +// EXIST: unaryMinus \ No newline at end of file diff --git a/idea/testData/repl/completion/definedClass.kt b/idea/testData/repl/completion/definedClass.kt new file mode 100644 index 00000000000..4bdaae755dc --- /dev/null +++ b/idea/testData/repl/completion/definedClass.kt @@ -0,0 +1,4 @@ +>> class MyClass { fun f() {} fun g() {} } +-- val g: M +// EXIST: MyClass + diff --git a/idea/testData/repl/completion/definedClassMember.kt b/idea/testData/repl/completion/definedClassMember.kt new file mode 100644 index 00000000000..f4cbc2eeec7 --- /dev/null +++ b/idea/testData/repl/completion/definedClassMember.kt @@ -0,0 +1,5 @@ +>> class MyClass { fun f() {} fun g() {} } +-- MyClass(). +// EXIST: f +// EXIST: g + diff --git a/idea/testData/repl/completion/definedExtension.kt b/idea/testData/repl/completion/definedExtension.kt new file mode 100644 index 00000000000..42946d02a88 --- /dev/null +++ b/idea/testData/repl/completion/definedExtension.kt @@ -0,0 +1,4 @@ +>> fun String.myExt() = "a" +-- "asd".myEx +// EXIST: myExt +// NOTHING_ELSE \ No newline at end of file diff --git a/idea/testData/repl/completion/functions.kt b/idea/testData/repl/completion/functions.kt new file mode 100644 index 00000000000..a50892bca6d --- /dev/null +++ b/idea/testData/repl/completion/functions.kt @@ -0,0 +1,8 @@ +>> fun foo() {} +>> fun foo(i: Int) {} +>> fun foo(s: String = "asd") {} +>> foo(3) +-- fo +// EXIST: { itemText: "foo", tailText:"()" } +// EXIST: { itemText: "foo", tailText:"(i: Int)" } +// EXIST: { itemText: "foo", tailText:"(s: String = ...)" } \ No newline at end of file diff --git a/idea/testData/repl/completion/stdlib.kt b/idea/testData/repl/completion/stdlib.kt new file mode 100644 index 00000000000..b90ea72975e --- /dev/null +++ b/idea/testData/repl/completion/stdlib.kt @@ -0,0 +1,3 @@ +>> val str = "a" +-- str.capi +// EXIST: capitalize \ No newline at end of file diff --git a/idea/testData/repl/completion/variables.kt b/idea/testData/repl/completion/variables.kt new file mode 100644 index 00000000000..9fc2e868eb4 --- /dev/null +++ b/idea/testData/repl/completion/variables.kt @@ -0,0 +1,5 @@ +>> var variable = 5 +>> var variable2 = "Sdsd" +-- varia +// EXIST: variable +// EXIST: variable2 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/repl/AbstractIdeReplCompletionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/repl/AbstractIdeReplCompletionTest.kt new file mode 100644 index 00000000000..8a3ea368750 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/repl/AbstractIdeReplCompletionTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright 2010-2016 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.repl + +import com.intellij.codeInsight.completion.CompletionType +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.psi.PsiDocumentManager +import com.intellij.testFramework.LightProjectDescriptor +import org.jetbrains.kotlin.console.KotlinConsoleKeeper +import org.jetbrains.kotlin.console.KotlinConsoleRunner +import org.jetbrains.kotlin.idea.completion.test.KotlinFixtureCompletionBaseTestCase +import org.jetbrains.kotlin.idea.completion.test.testCompletion +import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.idea.util.application.runWriteAction +import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform +import java.io.File +import kotlin.properties.Delegates + +abstract class AbstractIdeReplCompletionTest : KotlinFixtureCompletionBaseTestCase() { + private var consoleRunner: KotlinConsoleRunner by Delegates.notNull() + + override fun setUp() { + super.setUp() + consoleRunner = KotlinConsoleKeeper.getInstance(project).run(myModule)!! + } + + override fun tearDown() { + consoleRunner.dispose() + super.tearDown() + } + + override fun getPlatform() = JvmPlatform + override fun defaultCompletionType() = CompletionType.BASIC + + override fun doTest(testPath: String) { + val file = File(testPath) + val lines = file.readLines() + lines.prefixedWith(">> ").forEach { consoleRunner.successfulLine(it) } // not actually executing anything, only simulating + val codeSample = lines.prefixedWith("-- ").joinToString("\n") + + runWriteAction { + val editor = consoleRunner.consoleView.editorDocument + editor.setText(codeSample) + FileDocumentManager.getInstance().saveDocument(consoleRunner.consoleView.editorDocument) + PsiDocumentManager.getInstance(getProject()).commitAllDocuments() + } + + myFixture.configureFromExistingVirtualFile(consoleRunner.consoleFile.virtualFile) + myFixture.editor.caretModel.moveToOffset(myFixture.editor.document.getLineEndOffset(0)) + + testCompletion(file.readText(), getPlatform(), { completionType, count -> myFixture.complete(completionType, count) }) + } + + private fun List.prefixedWith(prefix: String) = filter { it.startsWith(prefix) }.map { it.removePrefix(prefix) } + + override fun getProjectDescriptor(): LightProjectDescriptor = FullJdkProjectDescriptor +} + +private object FullJdkProjectDescriptor : KotlinWithJdkAndRuntimeLightProjectDescriptor() { + override fun getSdk() = PluginTestCaseBase.fullJdk() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/repl/IdeReplCompletionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/repl/IdeReplCompletionTestGenerated.java new file mode 100644 index 00000000000..5e1872d8489 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/repl/IdeReplCompletionTestGenerated.java @@ -0,0 +1,79 @@ +/* + * Copyright 2010-2016 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.repl; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +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/repl/completion") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class IdeReplCompletionTestGenerated extends AbstractIdeReplCompletionTest { + public void testAllFilesPresentInCompletion() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/repl/completion"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("builtInMember.kt") + public void testBuiltInMember() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/builtInMember.kt"); + doTest(fileName); + } + + @TestMetadata("definedClass.kt") + public void testDefinedClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/definedClass.kt"); + doTest(fileName); + } + + @TestMetadata("definedClassMember.kt") + public void testDefinedClassMember() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/definedClassMember.kt"); + doTest(fileName); + } + + @TestMetadata("definedExtension.kt") + public void testDefinedExtension() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/definedExtension.kt"); + doTest(fileName); + } + + @TestMetadata("functions.kt") + public void testFunctions() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/functions.kt"); + doTest(fileName); + } + + @TestMetadata("stdlib.kt") + public void testStdlib() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/stdlib.kt"); + doTest(fileName); + } + + @TestMetadata("variables.kt") + public void testVariables() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/repl/completion/variables.kt"); + doTest(fileName); + } +}