Moved test data to idea-live-templates too

This commit is contained in:
Valentin Kipyatkov
2015-11-19 13:46:09 +01:00
parent b8dadeb4cc
commit f44bbfe4cd
60 changed files with 346 additions and 339 deletions
@@ -17,5 +17,6 @@
<orderEntry type="module" module-name="idea-test-framework" scope="TEST" />
<orderEntry type="module" module-name="idea" scope="RUNTIME" />
<orderEntry type="library" scope="RUNTIME" name="junit-plugin" level="project" />
<orderEntry type="module" module-name="compiler-tests" scope="TEST" />
</component>
</module>
@@ -19,12 +19,11 @@ package org.jetbrains.kotlin.idea.liveTemplates
import com.intellij.codeInsight.template.TemplateContextType
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import java.io.File
public class LiveTemplatesContextTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getTestDataPath(): String =
File(PluginTestCaseBase.getTestDataPathBase(), "/templates/context").getPath() + File.separator
File(TEST_DATA_BASE_PATH, "/context").path + File.separator
public fun testInDocComment() {
myFixture.configureByFile(getTestName(false) + ".kt")
@@ -1,337 +0,0 @@
/*
* 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.liveTemplates;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupEx;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.ide.DataManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ui.UIUtil;
import junit.framework.TestCase;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase;
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor;
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class LiveTemplatesTest extends KotlinLightCodeInsightFixtureTestCase {
@Override
protected void setUp() {
super.setUp();
myFixture.setTestDataPath(new File(PluginTestCaseBase.getTestDataPathBase(), "/templates").getPath() + File.separator);
((TemplateManagerImpl) TemplateManager.getInstance(getProject())).setTemplateTesting(true);
}
@Override
protected void tearDown() {
((TemplateManagerImpl) TemplateManager.getInstance(getProject())).setTemplateTesting(false);
super.tearDown();
}
public void testSout() {
paremeterless();
}
public void testSout_BeforeCall() {
paremeterless();
}
public void testSout_BeforeCallSpace() {
paremeterless();
}
public void testSout_BeforeBinary() {
paremeterless();
}
public void testSout_InCallArguments() {
paremeterless();
}
public void testSout_BeforeQualifiedCall() {
paremeterless();
}
public void testSout_AfterSemicolon() {
paremeterless();
}
public void testSerr() {
paremeterless();
}
public void testMain() {
paremeterless();
}
public void testSoutv() {
start();
assertStringItems("args", "x", "y");
typeAndNextTab("y");
checkAfter();
}
public void testSoutp() {
paremeterless();
}
public void testFun0() {
start();
type("foo");
nextTab(2);
checkAfter();
}
public void testFun1() {
start();
type("foo");
nextTab(4);
checkAfter();
}
public void testFun2() {
start();
type("foo");
nextTab(6);
checkAfter();
}
public void testExfun() {
start();
typeAndNextTab("Int");
typeAndNextTab("foo");
typeAndNextTab("arg : Int");
nextTab();
checkAfter();
}
public void testExval() {
start();
typeAndNextTab("Int");
nextTab();
typeAndNextTab("Int");
checkAfter();
}
public void testExvar() {
start();
typeAndNextTab("Int");
nextTab();
typeAndNextTab("Int");
checkAfter();
}
public void testClosure() {
start();
typeAndNextTab("param");
nextTab();
checkAfter();
}
public void testInterface() {
start();
typeAndNextTab("SomeTrait");
checkAfter();
}
public void testSingleton() {
start();
typeAndNextTab("MySingleton");
checkAfter();
}
public void testVoid() {
start();
typeAndNextTab("foo");
typeAndNextTab("x : Int");
checkAfter();
}
public void testIter() {
start();
assertStringItems("args", "myList", "o", "str", "stream");
type("args");
nextTab(2);
checkAfter();
}
public void testAnonymous_1() {
start();
typeAndNextTab("Runnable");
checkAfter();
}
public void testAnonymous_2() {
start();
typeAndNextTab("Thread");
checkAfter();
}
private void doTestIfnInn() {
start();
assertStringItems("b", "t", "y");
typeAndNextTab("b");
checkAfter();
}
public void testIfn() {
doTestIfnInn();
}
public void testInn() {
doTestIfnInn();
}
private void paremeterless() {
start();
checkAfter();
}
private void start() {
myFixture.configureByFile(getTestName(true) + ".kt");
myFixture.type(getTemplateName());
doAction("ExpandLiveTemplateByTab");
}
private String getTemplateName() {
String testName = getTestName(true);
if (testName.contains("_")) {
return testName.substring(0, testName.indexOf("_"));
}
return testName;
}
private void checkAfter() {
TestCase.assertNull(getTemplateState());
myFixture.checkResultByFile(getTestName(true) + ".exp.kt", true);
}
private void typeAndNextTab(String s) {
type(s);
nextTab();
}
private void type(String s) {
myFixture.type(s);
}
private void nextTab() {
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
getTemplateState().nextTab();
}
});
}
}, "nextTab", null);
}
});
}
private void nextTab(int times) {
for (int i = 0; i < times; i++) {
nextTab();
}
}
private TemplateState getTemplateState() {
return TemplateManagerImpl.getTemplateState(myFixture.getEditor());
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE;
}
private void doAction(@NotNull String actionId) {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler actionHandler = actionManager.getActionHandler(actionId);
actionHandler.execute(myFixture.getEditor(), DataManager.getInstance().getDataContext(myFixture.getEditor().getComponent()));
}
private void assertStringItems(@NonNls String... items) {
TestCase.assertEquals(Arrays.asList(items), Arrays.asList(getItemStringsSorted()));
}
private String[] getItemStrings() {
LookupEx lookup = LookupManager.getActiveLookup(myFixture.getEditor());
TestCase.assertNotNull(lookup);
ArrayList<String> result = new ArrayList<String>();
for (LookupElement element : lookup.getItems()) {
result.add(element.getLookupString());
}
return ArrayUtil.toStringArray(result);
}
private String[] getItemStringsSorted() {
String[] items = getItemStrings();
Arrays.sort(items);
return items;
}
}
@@ -0,0 +1,321 @@
/*
* 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.liveTemplates
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.template.TemplateManager
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.codeInsight.template.impl.TemplateState
import com.intellij.ide.DataManager
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.actionSystem.EditorActionManager
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.util.ArrayUtil
import com.intellij.util.ui.UIUtil
import junit.framework.TestCase
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import java.io.File
import java.util.*
class LiveTemplatesTest : KotlinLightCodeInsightFixtureTestCase() {
override fun setUp() {
super.setUp()
myFixture.testDataPath = File(TEST_DATA_BASE_PATH).getPath() + File.separator
(TemplateManager.getInstance(project) as TemplateManagerImpl).setTemplateTesting(true)
}
override fun tearDown() {
(TemplateManager.getInstance(project) as TemplateManagerImpl).setTemplateTesting(false)
super.tearDown()
}
fun testSout() {
paremeterless()
}
fun testSout_BeforeCall() {
paremeterless()
}
fun testSout_BeforeCallSpace() {
paremeterless()
}
fun testSout_BeforeBinary() {
paremeterless()
}
fun testSout_InCallArguments() {
paremeterless()
}
fun testSout_BeforeQualifiedCall() {
paremeterless()
}
fun testSout_AfterSemicolon() {
paremeterless()
}
fun testSerr() {
paremeterless()
}
fun testMain() {
paremeterless()
}
fun testSoutv() {
start()
assertStringItems("args", "x", "y")
typeAndNextTab("y")
checkAfter()
}
fun testSoutp() {
paremeterless()
}
fun testFun0() {
start()
type("foo")
nextTab(2)
checkAfter()
}
fun testFun1() {
start()
type("foo")
nextTab(4)
checkAfter()
}
fun testFun2() {
start()
type("foo")
nextTab(6)
checkAfter()
}
fun testExfun() {
start()
typeAndNextTab("Int")
typeAndNextTab("foo")
typeAndNextTab("arg : Int")
nextTab()
checkAfter()
}
fun testExval() {
start()
typeAndNextTab("Int")
nextTab()
typeAndNextTab("Int")
checkAfter()
}
fun testExvar() {
start()
typeAndNextTab("Int")
nextTab()
typeAndNextTab("Int")
checkAfter()
}
fun testClosure() {
start()
typeAndNextTab("param")
nextTab()
checkAfter()
}
fun testInterface() {
start()
typeAndNextTab("SomeTrait")
checkAfter()
}
fun testSingleton() {
start()
typeAndNextTab("MySingleton")
checkAfter()
}
fun testVoid() {
start()
typeAndNextTab("foo")
typeAndNextTab("x : Int")
checkAfter()
}
fun testIter() {
start()
assertStringItems("args", "myList", "o", "str", "stream")
type("args")
nextTab(2)
checkAfter()
}
fun testAnonymous_1() {
start()
typeAndNextTab("Runnable")
checkAfter()
}
fun testAnonymous_2() {
start()
typeAndNextTab("Thread")
checkAfter()
}
private fun doTestIfnInn() {
start()
assertStringItems("b", "t", "y")
typeAndNextTab("b")
checkAfter()
}
fun testIfn() {
doTestIfnInn()
}
fun testInn() {
doTestIfnInn()
}
private fun paremeterless() {
start()
checkAfter()
}
private fun start() {
myFixture.configureByFile(getTestName(true) + ".kt")
myFixture.type(templateName)
doAction("ExpandLiveTemplateByTab")
}
private val templateName: String
get() {
val testName = getTestName(true)
if (testName.contains("_")) {
return testName.substring(0, testName.indexOf("_"))
}
return testName
}
private fun checkAfter() {
TestCase.assertNull(templateState)
myFixture.checkResultByFile(getTestName(true) + ".exp.kt", true)
}
private fun typeAndNextTab(s: String) {
type(s)
nextTab()
}
private fun type(s: String) {
myFixture.type(s)
}
private fun nextTab() {
val project = project
UIUtil.invokeAndWaitIfNeeded(Runnable {
CommandProcessor.getInstance().executeCommand(
project,
{
ApplicationManager.getApplication().runWriteAction { templateState!!.nextTab() }
},
"nextTab",
null)
})
}
private fun nextTab(times: Int) {
for (i in 0..times - 1) {
nextTab()
}
}
private val templateState: TemplateState?
get() = TemplateManagerImpl.getTemplateState(myFixture.editor)
override fun getProjectDescriptor(): LightProjectDescriptor {
return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
}
private fun doAction(actionId: String) {
val actionManager = EditorActionManager.getInstance()
val actionHandler = actionManager.getActionHandler(actionId)
actionHandler.execute(myFixture.editor, DataManager.getInstance().getDataContext(myFixture.editor.component))
}
private fun assertStringItems(@NonNls vararg items: String) {
TestCase.assertEquals(Arrays.asList(*items), Arrays.asList(*itemStringsSorted))
}
private val itemStrings: Array<String>
get() {
val lookup = LookupManager.getActiveLookup(myFixture.editor)!!
val result = ArrayList<String>()
for (element in lookup.items) {
result.add(element.lookupString)
}
return ArrayUtil.toStringArray(result)
}
private val itemStringsSorted: Array<String>
get() {
val items = itemStrings
Arrays.sort(items)
return items
}
}
@@ -0,0 +1,23 @@
/*
* 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.liveTemplates
import org.jetbrains.kotlin.test.KotlinTestUtils
internal val RELATIVE_TEST_DATA_BASE_PATH = "idea/idea-live-templates/testData"
internal val TEST_DATA_BASE_PATH = KotlinTestUtils.getHomeDirectory() + "/" + RELATIVE_TEST_DATA_BASE_PATH