Add test infrastructure for completion in mpp

Allow to specify specific expected module in test data for completion items
This commit is contained in:
Pavel V. Talanov
2018-03-28 17:34:29 +02:00
parent 08b62d3564
commit 549bebbd19
4 changed files with 73 additions and 7 deletions
@@ -0,0 +1,52 @@
/*
* 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.completion.test
import com.intellij.codeInsight.completion.CodeCompletionHandlerBase
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil
import org.jetbrains.kotlin.idea.multiplatform.setupMppProjectFromDirStructure
import org.jetbrains.kotlin.idea.stubs.AbstractMultiModuleTest
import org.jetbrains.kotlin.idea.test.extractMarkerOffset
import org.jetbrains.kotlin.idea.test.findFileWithCaret
import org.jetbrains.kotlin.psi.KtFile
import java.io.File
abstract class AbstractMultiPlatformCompletionTest : AbstractMultiModuleTest() {
protected fun doTest(testPath: String) {
setupMppProjectFromDirStructure(File(testPath))
val file = project.findFileWithCaret()
val doc = PsiDocumentManager.getInstance(myProject).getDocument(file)!!
val offset = doc.extractMarkerOffset(project)
val editor = EditorFactory.getInstance().createEditor(doc, myProject)!!
editor.caretModel.moveToOffset(offset)
try {
testCompletion(file, editor)
} finally {
EditorFactory.getInstance().releaseEditor(editor)
}
}
private fun testCompletion(file: KtFile, editor: Editor) {
testCompletion(file.text, null, { completionType, invocationCount ->
CodeCompletionHandlerBase(completionType).invokeCompletion(
myProject, InjectedLanguageUtil
.getEditorForInjectedLanguageNoCommit(editor, file), invocationCount
)
val lookup = LookupManager.getActiveLookup(editor) as LookupImpl
lookup.items?.toTypedArray()
})
}
override fun getTestDataPath(): String {
return COMPLETION_TEST_DATA_BASE_PATH + "/smartMultiFile/" + getTestName(false) + "/"
}
}
@@ -27,7 +27,9 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation
import com.intellij.codeInsight.lookup.impl.LookupCellRenderer
import com.intellij.ui.JBColor
import com.intellij.util.ArrayUtil
import org.jetbrains.kotlin.idea.core.completion.DeclarationLookupObject
import org.jetbrains.kotlin.idea.test.AstAccessControl
import org.jetbrains.kotlin.idea.util.module
import org.jetbrains.kotlin.js.resolve.JsPlatform
import org.jetbrains.kotlin.resolve.TargetPlatform
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
@@ -88,7 +90,11 @@ object ExpectedCompletionUtils {
val PRESENTATION_TYPE_TEXT: String = "typeText"
val PRESENTATION_TAIL_TEXT: String = "tailText"
val PRESENTATION_TEXT_ATTRIBUTES: String = "attributes"
val validKeys: Set<String> = setOf(LOOKUP_STRING, ALL_LOOKUP_STRINGS, PRESENTATION_ITEM_TEXT, PRESENTATION_TYPE_TEXT, PRESENTATION_TAIL_TEXT, PRESENTATION_TEXT_ATTRIBUTES)
val MODULE_NAME: String = "module"
val validKeys: Set<String> = setOf(
LOOKUP_STRING, ALL_LOOKUP_STRINGS, PRESENTATION_ITEM_TEXT, PRESENTATION_TYPE_TEXT,
PRESENTATION_TAIL_TEXT, PRESENTATION_TEXT_ATTRIBUTES, MODULE_NAME
)
}
}
@@ -315,12 +321,20 @@ object ExpectedCompletionUtils {
if (presentation.tailText != null) {
map.put(CompletionProposal.PRESENTATION_TAIL_TEXT, presentation.tailText)
}
item.moduleName?.let {
map.put(CompletionProposal.MODULE_NAME, it)
}
result.add(ExpectedCompletionUtils.CompletionProposal(map))
}
return result
}
private val LookupElement.moduleName: String?
get() {
return (`object` as? DeclarationLookupObject)?.psiElement?.module?.name
}
private fun textAttributes(presentation: LookupElementPresentation): String {
return buildString {
if (presentation.isItemTextBold) {