Conversion on copy paste:
Introduce tests for conversion on copy paste Don't insert additional end of line after each converted element (not needed since converter takes care of formatting) Add test for some of the trivial cases of conversion on copy paste
This commit is contained in:
@@ -80,6 +80,7 @@ import org.jetbrains.jet.generators.tests.generator.SingleClassTestModel
|
||||
import org.jetbrains.jet.generators.tests.generator.TestClassModel
|
||||
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterPluginTest
|
||||
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterBasicTest
|
||||
import org.jetbrains.jet.plugin.conversion.copy.AbstractJavaToKotlinCopyPasteConversionTest
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
System.setProperty("java.awt.headless", "true")
|
||||
@@ -414,6 +415,10 @@ fun main(args: Array<String>) {
|
||||
testClass(javaClass<AbstractDataFlowValueRenderingTest>()) {
|
||||
model("dataFlowValueRendering")
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractJavaToKotlinCopyPasteConversionTest>()) {
|
||||
model("copyPaste/conversion", extension = "java")
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("j2k/tests/test", "j2k/tests/testData") {
|
||||
|
||||
@@ -115,12 +115,10 @@ public class JavaCopyPastePostProcessor implements CopyPastePostProcessor<TextBl
|
||||
List<PsiElement> buffer = getSelectedElements(code.getFile(), code.getStartOffsets(), code.getEndOffsets());
|
||||
|
||||
Project project = file.getProject();
|
||||
Converter converter = new Converter(project, J2kPackage.getPluginSettings());
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (PsiElement e : buffer) {
|
||||
String converted = new Converter(project, J2kPackage.getPluginSettings()).elementToKotlin(e);
|
||||
if (!converted.isEmpty()) {
|
||||
result.append(converted).append(EOL);
|
||||
}
|
||||
result.append(converter.elementToKotlin(e));
|
||||
}
|
||||
|
||||
return StringUtil.convertLineSeparators(result.toString());
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun main(args: Array<String>) {
|
||||
A()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
public static void main(String[] args) {
|
||||
<selection>new A()</selection>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun main(args: Array<String>) {
|
||||
<caret>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import a.b.c
|
||||
import g.e.v
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<selection>import java.util.List;
|
||||
import a.b.c;
|
||||
import java.util.Set;
|
||||
import g.e.v;</selection>
|
||||
@@ -0,0 +1,5 @@
|
||||
<caret>
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import java.util.List
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<selection>import java.util.List;</selection>
|
||||
import a.b.c;
|
||||
import java.util.Set;
|
||||
import g.e.v;
|
||||
@@ -0,0 +1,5 @@
|
||||
<caret>
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fun main(args: Array<String>) {
|
||||
val buffer = getSelectedElements(code.getFile(), code.getStartOffsets(), code.getEndOffsets())
|
||||
|
||||
val project = file.getProject()
|
||||
val converter = Converter(project, J2kPackage.getPluginSettings())
|
||||
val result = StringBuilder()
|
||||
for (e in buffer)
|
||||
{
|
||||
result.append(converter.elementToKotlin(e))
|
||||
}
|
||||
|
||||
return StringUtil.convertLineSeparators(result.toString())
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class A {
|
||||
private static String convertCopiedCodeToKotlin(CopiedCode code, PsiFile file) {
|
||||
<selection>List<PsiElement> buffer = getSelectedElements(code.getFile(), code.getStartOffsets(), code.getEndOffsets());
|
||||
|
||||
Project project = file.getProject();
|
||||
Converter converter = new Converter(project, J2kPackage.getPluginSettings());
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (PsiElement e : buffer) {
|
||||
result.append(converter.elementToKotlin(e));
|
||||
}
|
||||
|
||||
return StringUtil.convertLineSeparators(result.toString());</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun main(args: Array<String>) {
|
||||
<caret>
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
class A {
|
||||
fun someOther() = false
|
||||
|
||||
private fun formatElement(var element : PsiElement) : String {
|
||||
element = JetPsiUtil.ascendIfPropertyAccessor(element)
|
||||
if (element is JetNamedFunction || element is JetProperty)
|
||||
{
|
||||
val bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache((element.getContainingFile() as JetFile)).getBindingContext()
|
||||
|
||||
val declarationDescriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, element)
|
||||
if (declarationDescriptor is CallableMemberDescriptor)
|
||||
{
|
||||
val containingDescriptor = declarationDescriptor.getContainingDeclaration()
|
||||
if (containingDescriptor is ClassDescriptor)
|
||||
{
|
||||
return JetBundle.message("x.in.y", DescriptorRenderer.COMPACT.render(declarationDescriptor), DescriptorRenderer.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(containingDescriptor))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(element is PsiMethod) {"Method accepts only kotlin functions/properties and java methods, but '" + element.getText() + "' was found"}
|
||||
return JetRefactoringUtil.formatPsiMethod((element as PsiMethod), true, false)
|
||||
}
|
||||
|
||||
protected fun getDimensionServiceKey() : String {
|
||||
return "#org.jetbrains.jet.plugin.refactoring.safeDelete.KotlinOverridingDialog"
|
||||
}
|
||||
|
||||
public fun getSelected() : ArrayList<UsageInfo> {
|
||||
val result = ArrayList<UsageInfo>()
|
||||
for (i in 0..myChecked.length - 1) {
|
||||
if (myChecked[i])
|
||||
{
|
||||
result.add(myOverridingMethods.get(i))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
class A {
|
||||
<selection>private static String formatElement(PsiElement element) {
|
||||
element = JetPsiUtil.ascendIfPropertyAccessor(element);
|
||||
if (element instanceof JetNamedFunction || element instanceof JetProperty) {
|
||||
BindingContext bindingContext =
|
||||
AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) element.getContainingFile()).getBindingContext();
|
||||
|
||||
DeclarationDescriptor declarationDescriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, element);
|
||||
if (declarationDescriptor instanceof CallableMemberDescriptor) {
|
||||
DeclarationDescriptor containingDescriptor = declarationDescriptor.getContainingDeclaration();
|
||||
if (containingDescriptor instanceof ClassDescriptor) {
|
||||
return JetBundle.message(
|
||||
"x.in.y",
|
||||
DescriptorRenderer.COMPACT.render(declarationDescriptor),
|
||||
DescriptorRenderer.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(containingDescriptor)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert element instanceof PsiMethod
|
||||
: "Method accepts only kotlin functions/properties and java methods, but '" + element.getText() + "' was found";
|
||||
return JetRefactoringUtil.formatPsiMethod((PsiMethod) element, true, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDimensionServiceKey() {
|
||||
return "#org.jetbrains.jet.plugin.refactoring.safeDelete.KotlinOverridingDialog";
|
||||
}
|
||||
|
||||
public ArrayList<UsageInfo> getSelected() {
|
||||
ArrayList<UsageInfo> result = new ArrayList<UsageInfo>();
|
||||
for (int i = 0; i < myChecked.length; i++) {
|
||||
if (myChecked[i]) {
|
||||
result.add(myOverridingMethods.get(i));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}</selection>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
fun someOther() = false
|
||||
|
||||
<caret>
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.plugin.conversion.copy;
|
||||
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
import org.jetbrains.jet.plugin.editor.JetEditorOptions;
|
||||
|
||||
public abstract class AbstractJavaToKotlinCopyPasteConversionTest extends LightCodeInsightFixtureTestCase {
|
||||
|
||||
private static final String BASE_PATH = PluginTestCaseBase.getTestDataPathBase() + "/copyPaste/conversion";
|
||||
private JetEditorOptions oldState = null;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
oldState = JetEditorOptions.getInstance().getState();
|
||||
JetEditorOptions.getInstance().setEnableJavaToKotlinConversion(true);
|
||||
JetEditorOptions.getInstance().setDonTShowConversionDialog(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
JetEditorOptions.getInstance().loadState(oldState);
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void doTest(@SuppressWarnings("UnusedParameters") String path) throws Exception {
|
||||
myFixture.setTestDataPath(BASE_PATH);
|
||||
String testName = getTestName(false);
|
||||
myFixture.configureByFile(testName + ".java");
|
||||
myFixture.performEditorAction(IdeActions.ACTION_COPY);
|
||||
myFixture.configureByFile(testName + ".to.kt");
|
||||
myFixture.performEditorAction(IdeActions.ACTION_PASTE);
|
||||
myFixture.checkResultByFile(testName + ".expected.kt");
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.plugin.conversion.copy;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.test.InnerTestClasses;
|
||||
import org.jetbrains.jet.test.TestMetadata;
|
||||
|
||||
import org.jetbrains.jet.plugin.conversion.copy.AbstractJavaToKotlinCopyPasteConversionTest;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/copyPaste/conversion")
|
||||
public class JavaToKotlinCopyPasteConversionTestGenerated extends AbstractJavaToKotlinCopyPasteConversionTest {
|
||||
public void testAllFilesPresentInConversion() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/copyPaste/conversion"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("Constructor.java")
|
||||
public void testConstructor() throws Exception {
|
||||
doTest("idea/testData/copyPaste/conversion/Constructor.java");
|
||||
}
|
||||
|
||||
@TestMetadata("Imports1.java")
|
||||
public void testImports1() throws Exception {
|
||||
doTest("idea/testData/copyPaste/conversion/Imports1.java");
|
||||
}
|
||||
|
||||
@TestMetadata("Imports2.java")
|
||||
public void testImports2() throws Exception {
|
||||
doTest("idea/testData/copyPaste/conversion/Imports2.java");
|
||||
}
|
||||
|
||||
@TestMetadata("SampleBlock.java")
|
||||
public void testSampleBlock() throws Exception {
|
||||
doTest("idea/testData/copyPaste/conversion/SampleBlock.java");
|
||||
}
|
||||
|
||||
@TestMetadata("SeveralMethodsSample.java")
|
||||
public void testSeveralMethodsSample() throws Exception {
|
||||
doTest("idea/testData/copyPaste/conversion/SeveralMethodsSample.java");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user