From a39515d9972e5cbf03b147eb67117dc3d0aa6c35 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 3 Oct 2024 17:40:53 +0200 Subject: [PATCH] [Groovy] convert InlineMethodTest to Java (IDEA-327339) GitOrigin-RevId: 99e5990a15a20158307da81664664f68c4ac3fed --- .../refactoring/inline/InlineMethodTest.java | 268 +++++++++--------- 1 file changed, 128 insertions(+), 140 deletions(-) diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java index 333095981109..c993598b3a90 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/inline/InlineMethodTest.java @@ -1,247 +1,235 @@ -/* - * Copyright 2000-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. - */ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.plugins.groovy.refactoring.inline; -package org.jetbrains.plugins.groovy.refactoring.inline +import com.intellij.lang.ASTNode; +import com.intellij.lang.refactoring.InlineHandler; +import com.intellij.openapi.editor.Editor; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.impl.source.codeStyle.CodeEditUtil; +import com.intellij.psi.impl.source.tree.TreeElement; +import com.intellij.refactoring.BaseRefactoringProcessor; +import com.intellij.refactoring.inline.GenericInlineHandler; +import com.intellij.refactoring.util.CommonRefactoringUtil; +import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyFileType; +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod; +import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; +import org.jetbrains.plugins.groovy.util.TestUtils; -import com.intellij.lang.ASTNode -import com.intellij.lang.refactoring.InlineHandler -import com.intellij.openapi.editor.Editor -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiFile -import com.intellij.psi.impl.source.codeStyle.CodeEditUtil -import com.intellij.psi.impl.source.tree.TreeElement -import com.intellij.refactoring.BaseRefactoringProcessor -import com.intellij.refactoring.inline.GenericInlineHandler -import com.intellij.refactoring.util.CommonRefactoringUtil -import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture -import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase -import junit.framework.Assert -import org.jetbrains.annotations.NotNull -import org.jetbrains.plugins.groovy.GroovyFileType -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement -import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod -import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil -import org.jetbrains.plugins.groovy.util.TestUtils -class InlineMethodTest extends LightJavaCodeInsightFixtureTestCase { +import java.util.List; - final String basePath = TestUtils.testDataPath + "groovy/refactoring/inlineMethod/" +public class InlineMethodTest extends LightJavaCodeInsightFixtureTestCase { + public void testAbstr1() { doTest(); } - void testAbstr1() throws Throwable { doTest() } + public void testBlock1() { doTest(); } - void testBlock1() throws Throwable { doTest() } + public void testBlock2() { doTest(); } - void testBlock2() throws Throwable { doTest() } + public void testBlock3() { doTest(); } - void testBlock3() throws Throwable { doTest() } + public void testBlock4() { doTest(); } - void testBlock4() throws Throwable { doTest() } + public void testCase1() { doTest(); } - void testCase1() throws Throwable { doTest() } + public void testCase2() { doTest(); } - void testCase2() throws Throwable { doTest() } + public void testClos_arg1() { doTest(); } - void testClos_arg1() throws Throwable { doTest() } + public void testClos_arg2() { doTest(); } - void testClos_arg2() throws Throwable { doTest() } + public void testClos_arg3() { doTest(); } - void testClos_arg3() throws Throwable { doTest() } + public void testCond() { doTest(); } - void testCond() throws Throwable { doTest() } + public void _testExpr1() { doTest(); } - void _testExpr1() throws Throwable { doTest() } + public void testExpr2() { doTest(); } - void testExpr2() throws Throwable { doTest() } + public void testExpr3() { doTest(); } - void testExpr3() throws Throwable { doTest() } + public void _testExpr4() { doTest(); } - void _testExpr4() throws Throwable { doTest() } + public void testFact() { doTest(); } - void testFact() throws Throwable { doTest() } + public void testFact2() { doTest(); } - void testFact2() throws Throwable { doTest() } + public void testInit1() { doTest(); } - void testInit1() throws Throwable { doTest() } + public void testMap_arg1() { doTest(); } - void testMap_arg1() throws Throwable { doTest() } + public void testQual1() { doTest(); } - void testQual1() throws Throwable { doTest() } + public void testQual2() { doTest(); } - void testQual2() throws Throwable { doTest() } + public void testQual3() { doTest(); } - void testQual3() throws Throwable { doTest() } + public void testQual4() { doTest(); } - void testQual4() throws Throwable { doTest() } + public void testQual5() { doTest(); } - void testQual5() throws Throwable { doTest() } + public void testRef1() { doTest(); } - void testRef1() throws Throwable { doTest() } + public void testRename1() { doTest(); } - void testRename1() throws Throwable { doTest() } + public void testRename2() { doTest(); } - void testRename2() throws Throwable { doTest() } + public void testRet1() { doTest(); } - void testRet1() throws Throwable { doTest() } + public void testRet2() { doTest(); } - void testRet2() throws Throwable { doTest() } + public void testRet3() { doTest(); } - void testRet3() throws Throwable { doTest() } + public void testRet4() { doTest(); } - void testRet4() throws Throwable { doTest() } + public void testRet5() { doTest(); } - void testRet5() throws Throwable { doTest() } + public void testTail1() { doTest(); } - void testTail1() throws Throwable { doTest() } + public void testTail1_1() { doTest(); } - void testTail1_1() throws Throwable { doTest() } + public void testTail2() { doTest(); } - void testTail2() throws Throwable { doTest() } + public void testTail3() { doTest(); } - void testTail3() throws Throwable { doTest() } + public void testTail4() { doTest(); } - void testTail4() throws Throwable { doTest() } + public void testTail5() { doTest(); } - void testTail5() throws Throwable { doTest() } + public void testTail6() { doTest(); } - void testTail6() throws Throwable { doTest() } + public void testVen_tail() { doTest(); } - void testVen_tail() throws Throwable { doTest() } + public void testVen_tail2() { doTest(); } - void testVen_tail2() throws Throwable { doTest() } + public void testVoid() { doTest(); } - void testVoid() throws Throwable { doTest() } + public void testExpressionInParameter() { doTest(); } - void testExpressionInParameter() throws Throwable { doTest() } + public void testFinalParameter() { doTest(); } - void testFinalParameter() throws Throwable { doTest() } + public void testParameterIsUsedAfterCall() { doTest(); } - void testParameterIsUsedAfterCall() throws Throwable { doTest() } + public void testFieldAsParameter() { doTest(); } - void testFieldAsParameter() throws Throwable { doTest() } + public void testWritableVariable() { doTest(); } - void testWritableVariable() throws Throwable { doTest() } + public void testSingleExpression() { doTest(); } - void testSingleExpression() { doTest() } + public void testNamedArg() { doTest(); } - void testNamedArg() { doTest() } + public void _testInlineInGString() { doTest(); } - void _testInlineInGString() throws Throwable { doTest() } + public void testDontRemoveReturnValueExpr() { doTest(); } - void testDontRemoveReturnValueExpr() { doTest() } + public void testDontRemoveLastStatement() { doTest(); } - void testDontRemoveLastStatement() { doTest() } + public void testSideEffectInitializer() { doTest(); } - void testSideEffectInitializer() { doTest() } + public void testVarargs() { doTest(); } - void testVarargs() { doTest() } + public void testTypeParameterDeclaredInFile() { doTest(); } - void testTypeParameterDeclaredInFile() { doTest() } + public void testBadReturns() { doTest(); } - void testBadReturns() { doTest() } - - void testInlineAll() { - doInlineAllTest() + public void testInlineAll() { + doInlineAllTest(); } private void doInlineAllTest() { doTest(new GroovyInlineHandler() { @Override - InlineHandler.Settings prepareInlineElement(@NotNull PsiElement element, Editor editor, boolean invokedOnReference) { - return { false } as InlineHandler.Settings + public Settings prepareInlineElement(@NotNull PsiElement element, Editor editor, boolean invokedOnReference) { + return () -> false; } - }) + }); } - void testInlineNamedArgs() { doTest() } + public void testInlineNamedArgs() { doTest(); } - void testInlineVarargs() { doTest() } + public void testInlineVarargs() { doTest(); } - void testCannotInlineMethodRef() { + public void testCannotInlineMethodRef() { try { - doInlineAllTest() - assert false + doInlineAllTest(); + fail(); } catch (BaseRefactoringProcessor.ConflictsInTestsException e) { - assertEquals("Cannot inline reference 'new A().&foo'", e.message) + assertEquals("Cannot inline reference 'new A().&foo'", e.getMessage()); } } - void testSuperCall() { doTest() } + public void testSuperCall() { doTest(); } protected void doTest() { - doTest(new GroovyInlineHandler()) + doTest(new GroovyInlineHandler()); } protected void doTest(InlineHandler handler) { - doInlineTest(myFixture, testDataPath + getTestName(true) + ".test", handler) + doInlineTest(myFixture, getTestDataPath() + getTestName(true) + ".test", handler); } - static void doInlineTest(final JavaCodeInsightTestFixture fixture, - final String testFile, - InlineHandler inlineHandler) { - final List data = TestUtils.readInput(testFile) - String fileText = data.get(0) + public static void doInlineTest(final JavaCodeInsightTestFixture fixture, final String testFile, InlineHandler inlineHandler) { + final List data = TestUtils.readInput(testFile); + String fileText = data.get(0); - fixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, fileText) + fixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, fileText); - final PsiFile file = fixture.file - final Editor editor = fixture.editor - indentationToNode = file.node - int startOffset = editor.selectionModel.selectionStart - int endOffset = editor.selectionModel.selectionEnd - editor.caretModel.moveToOffset(endOffset) + final PsiFile file = fixture.getFile(); + final Editor editor = fixture.getEditor(); + setIndentationToNode(file.getNode()); + int startOffset = editor.getSelectionModel().getSelectionStart(); + int endOffset = editor.getSelectionModel().getSelectionEnd(); + editor.getCaretModel().moveToOffset(endOffset); - GroovyPsiElement selectedArea = PsiImplUtil.findElementInRange(file, startOffset, endOffset, GrReferenceExpression.class) + GroovyPsiElement selectedArea = PsiImplUtil.findElementInRange(file, startOffset, endOffset, GrReferenceExpression.class); if (selectedArea == null) { - PsiElement identifier = PsiImplUtil.findElementInRange(file, startOffset, endOffset, PsiElement.class) + PsiElement identifier = PsiImplUtil.findElementInRange(file, startOffset, endOffset, PsiElement.class); if (identifier != null) { - if (identifier.parent instanceof GrVariable) { - selectedArea = (GroovyPsiElement)identifier.parent + if (identifier.getParent() instanceof GrVariable) { + selectedArea = (GroovyPsiElement)identifier.getParent(); } else if (identifier instanceof GrMethod) { - selectedArea = identifier + selectedArea = ((GroovyPsiElement)(identifier)); } - else { - this.assertTrue("Selected area doesn't point to method or variable", false) + else { + fail("Selected area doesn't point to method or variable"); } } } - Assert.assertNotNull("Selected area reference points to nothing", selectedArea) - PsiElement element = selectedArea instanceof GrExpression ? selectedArea.reference.resolve() : selectedArea - Assert.assertNotNull("Cannot resolve selected reference expression", element) + + assertNotNull("Selected area reference points to nothing", selectedArea); + PsiElement element = selectedArea instanceof GrExpression ? selectedArea.getReference().resolve() : selectedArea; + assertNotNull("Cannot resolve selected reference expression", element); try { - GenericInlineHandler.invoke(element, editor, inlineHandler) - editor.selectionModel.removeSelection() - fixture.checkResult(data.get(1), true) + GenericInlineHandler.invoke(element, editor, inlineHandler); + editor.getSelectionModel().removeSelection(); + fixture.checkResult(data.get(1), true); } catch (CommonRefactoringUtil.RefactoringErrorHintException e) { - assertEquals(data.get(1), "FAIL: " + e.message) + assertEquals(data.get(1), "FAIL: " + e.getMessage()); } } - private static void setIndentationToNode(ASTNode element){ + private static void setIndentationToNode(ASTNode element) { if (element instanceof TreeElement) { - CodeEditUtil.setOldIndentation(((TreeElement) element), 0) + CodeEditUtil.setOldIndentation(((TreeElement)element), 0); } + for (ASTNode node : element.getChildren(null)) { - indentationToNode = node + setIndentationToNode(node); } } + @Override + public final String getBasePath() { + return TestUtils.getTestDataPath() + "groovy/refactoring/inlineMethod/"; + } }