Sample testing in kotlin

CompletionHandlerTest.java -> CompletionHandlerTest.kt
This commit is contained in:
Nikolay Krasko
2013-08-23 18:33:16 +04:00
parent ba0c9892d6
commit cb10c53040
2 changed files with 192 additions and 230 deletions
@@ -1,230 +0,0 @@
/*
* Copyright 2010-2013 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.jet.completion.handlers;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.codeInsight.lookup.LookupEvent;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import junit.framework.Assert;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
import java.io.File;
public class CompletionHandlerTest extends JetLightCodeInsightFixtureTestCase {
public void testClassCompletionImport() {
doTest(CompletionType.BASIC, 2, "SortedSet", null, '\n');
}
public void testDoNotInsertImportForAlreadyImported() {
doTest();
}
public void testDoNotInsertDefaultJsImports() {
doTest();
}
public void testDoNotInsertImportIfResolvedIntoJavaConstructor() {
doTest();
}
public void testNonStandardArray() {
doTest(CompletionType.BASIC, 2, "Array", "java.lang.reflect", '\n');
}
public void testNoParamsFunction() {
doTest();
}
public void testParamsFunction() {
doTest();
}
public void testInsertJavaClassImport() {
doTest();
}
public void testInsertVoidJavaMethod() {
doTest();
}
public void testPropertiesSetter() {
doTest();
}
public void testSingleBrackets() {
myFixture.configureByFile(fileName());
myFixture.type('(');
myFixture.checkResultByFile(afterFileName());
}
public void testExistingSingleBrackets() {
doTest();
}
public void testExtFunction() {
doTest();
}
public void testFunctionLiteralInsertOnSpace() {
doTest(CompletionType.BASIC, 2, null, null, ' ');
}
public void testInsertFunctionWithBothParentheses() {
myFixture.configureByFile(fileName());
myFixture.type("test()");
myFixture.checkResultByFile(afterFileName());
}
public void testInsertImportOnTab() {
doTest(CompletionType.BASIC, 2, "ArrayList", null, '\t');
}
public void testFunctionLiteralInsertWhenNoSpacesForBraces() {
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
JetCodeStyleSettings jetSettings = settings.getCustomSettings(JetCodeStyleSettings.class);
try {
jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = false;
doTest();
} finally {
jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true;
}
}
public void testHigherOrderFunction() {
doTest();
}
public void testInsertFqnForJavaClass() {
doTest(CompletionType.BASIC, 2, "SortedSet", "java.util", '\n');
}
public void testHigherOrderFunctionWithArg() {
doTest(CompletionType.BASIC, 2, "filterNot", null, '\n');
}
public void doTest() {
doTest(CompletionType.BASIC, 2, null, null, '\n');
}
public void doTest(CompletionType type, int time, @Nullable String lookupString, @Nullable String tailText, char completionChar) {
myFixture.configureByFile(fileName());
if (lookupString != null || tailText != null) {
myFixture.complete(type, time);
LookupElement item = getExistentLookupElement(lookupString, tailText);
if (item != null) {
selectItem(item, completionChar);
}
}
else {
forceCompleteFirst(type, time);
}
myFixture.checkResultByFile(afterFileName());
}
@Nullable
public LookupElement getExistentLookupElement(@Nullable String lookupString, @Nullable String tailText) {
LookupImpl lookup = (LookupImpl) LookupManager.getInstance(getProject()).getActiveLookup();
LookupElement foundElement = null;
if (lookup != null) {
LookupElementPresentation presentation = new LookupElementPresentation();
for (LookupElement lookupElement : lookup.getItems()) {
boolean lookupOk;
if (lookupString != null) {
lookupOk = (lookupElement.getLookupString().contains(lookupString));
}
else {
lookupOk = true;
}
boolean tailOk;
if (tailText != null) {
lookupElement.renderElement(presentation);
String itemTailText = presentation.getTailText();
tailOk = itemTailText != null && itemTailText.contains(tailText);
}
else {
tailOk = true;
}
if (lookupOk && tailOk) {
if (foundElement != null) {
Assert.fail("Several elements satisfy to completion restrictions");
}
foundElement = lookupElement;
}
}
}
return foundElement;
}
protected String afterFileName() {
return getTestName(false) + ".kt.after";
}
@NotNull
@Override
protected String getTestDataPath() {
return new File(PluginTestCaseBase.getTestDataPathBase(), "/completion/handlers/").getPath() + File.separator;
}
protected void forceCompleteFirst(CompletionType type, int time) {
myFixture.complete(type, time);
LookupElement[] items = myFixture.getLookupElements();
if (items != null && items.length > 1) {
selectItem(items[0]);
}
}
protected void selectItem(LookupElement item) {
selectItem(item, (char)0);
}
protected void selectItem(LookupElement item, final char completionChar) {
final LookupImpl lookup = (LookupImpl) myFixture.getLookup();
lookup.setCurrentItem(item);
if (LookupEvent.isSpecialCompletionChar(completionChar)) {
new WriteCommandAction.Simple(getProject()) {
@Override
protected void run() throws Throwable {
lookup.finishLookup(completionChar);
}
}.execute().throwException();
} else {
myFixture.type(completionChar);
}
}
}
@@ -0,0 +1,192 @@
/*
* Copyright 2010-2013 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.jet.completion.handlers
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase
import com.intellij.codeInsight.completion.CompletionType
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings
import com.intellij.codeInsight.lookup.LookupElement
import java.io.File
import org.jetbrains.jet.plugin.PluginTestCaseBase
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.codeInsight.lookup.LookupEvent
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.LookupElementPresentation
import org.junit.Assert
import com.intellij.openapi.application.Result
import kotlin.properties.Delegates
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
public class CompletionHandlerTest() : JetLightCodeInsightFixtureTestCase() {
fun testClassCompletionImport() = doTest(CompletionType.BASIC, 2, "SortedSet", null, '\n')
fun testDoNotInsertImportForAlreadyImported() = doTest()
fun testDoNotInsertDefaultJsImports() = doTest()
fun testDoNotInsertImportIfResolvedIntoJavaConstructor() = doTest()
fun testNonStandardArray() = doTest(CompletionType.BASIC, 2, "Array", "java.lang.reflect", '\n')
fun testNoParamsFunction() = doTest()
fun testParamsFunction() = doTest()
fun testInsertJavaClassImport() = doTest()
fun testInsertVoidJavaMethod() = doTest()
fun testPropertiesSetter() = doTest()
fun testExistingSingleBrackets() = doTest()
fun testExtFunction() = doTest()
fun testFunctionLiteralInsertOnSpace() = doTest(CompletionType.BASIC, 2, null, null, ' ')
fun testInsertImportOnTab() = doTest(CompletionType.BASIC, 2, "ArrayList", null, '\t')
fun testHigherOrderFunction() = doTest()
fun testInsertFqnForJavaClass() = doTest(CompletionType.BASIC, 2, "SortedSet", "java.util", '\n')
fun testHigherOrderFunctionWithArg() = doTest(CompletionType.BASIC, 2, "filterNot", null, '\n')
var fixture by Delegates.notNull<JavaCodeInsightTestFixture>()
protected override fun setUp() {
super.setUp()
fixture = myFixture!!
}
fun testSingleBrackets() {
fixture.configureByFile(fileName())
fixture.`type`('(')
fixture.checkResultByFile(afterFileName())
}
fun testInsertFunctionWithBothParentheses() {
fixture.configureByFile(fileName())
fixture.`type`("test()")
fixture.checkResultByFile(afterFileName())
}
fun testFunctionLiteralInsertWhenNoSpacesForBraces() {
val settings = CodeStyleSettingsManager.getSettings(getProject())
val jetSettings = settings.getCustomSettings(javaClass<JetCodeStyleSettings>())!!
try {
jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = false
doTest()
}
finally {
jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true
}
}
fun doTest() = doTest(CompletionType.BASIC, 2, null, null, '\n')
private fun doTest(`type` : CompletionType, time : Int, lookupString : String?, tailText : String?, completionChar : Char) : Unit {
fixture.configureByFile(fileName())
if (lookupString != null || tailText != null) {
fixture.complete(`type`, time)
val item = getExistentLookupElement(lookupString, tailText)
if (item != null) {
selectItem(item, completionChar)
}
}
else {
forceCompleteFirst(`type`, time)
}
fixture.checkResultByFile(afterFileName())
}
private fun getExistentLookupElement(lookupString : String?, tailText : String?) : LookupElement? {
val lookup = LookupManager.getInstance(getProject())?.getActiveLookup() as LookupImpl?
var foundElement : LookupElement? = null
if (lookup != null) {
val presentation = LookupElementPresentation()
for (lookupElement in lookup.getItems()!!) {
val lookupOk : Boolean
if (lookupString != null) {
lookupOk = lookupElement.getLookupString().contains(lookupString)
}
else {
lookupOk = true
}
val tailOk : Boolean
if (tailText != null) {
lookupElement.renderElement(presentation)
val itemTailText : String? = presentation.getTailText()
tailOk = itemTailText != null && (itemTailText.contains(tailText))
}
else {
tailOk = true
}
if (lookupOk && tailOk) {
if (foundElement != null) {
Assert.fail("Several elements satisfy to completion restrictions")
}
foundElement = lookupElement
}
}
}
return foundElement
}
fun afterFileName() = getTestName(false) + ".kt.after"
fun forceCompleteFirst(`type` : CompletionType, time : Int) {
fixture.complete(`type`, time)
val items : Array<LookupElement>? = fixture.getLookupElements()
if (items != null && items.isNotEmpty()) {
selectItem(items[0])
}
}
protected override fun getTestDataPath() : String = File(PluginTestCaseBase.getTestDataPathBase(), "/completion/handlers/").getPath() + File.separator
protected fun selectItem(item : LookupElement?) {
selectItem(item, 0.toChar())
}
protected fun selectItem(item : LookupElement?, completionChar : Char) {
val lookup = (fixture.getLookup() as LookupImpl)
lookup.setCurrentItem(item)
if (LookupEvent.isSpecialCompletionChar(completionChar)) {
(object : WriteCommandAction.Simple(getProject()) {
protected override fun run(result: Result<Any?>?) {
run()
}
protected override fun run() {
lookup.finishLookup(completionChar)
}
}).execute().throwException()
}
else {
fixture.`type`(completionChar)
}
}
}