[extract duplicates]: copy existing tests

GitOrigin-RevId: 0cf0aa8d5875e2bc52dfaa96c1259ccb314bbd96
This commit is contained in:
Alexandr Suhinin
2021-07-23 12:16:23 +00:00
committed by intellij-monorepo-bot
parent 57bf594709
commit 820529b67a
36 changed files with 605 additions and 0 deletions
@@ -0,0 +1,13 @@
public class Test {
public int test(int x) {
int i;
for (i = 0; i <= x; i++) {
<selection>if (i == 42) {
break;
} else if (i == 17) {
break;
}</selection>
}
return i;
}
}
@@ -0,0 +1,18 @@
public class Test {
public int test(int x) {
int i;
for (i = 0; i <= x; i++) {
if (extracted(i)) break;
}
return i;
}
private boolean extracted(int i) {
if (i == 42) {
return true;
} else if (i == 17) {
return true;
}
return false;
}
}
@@ -0,0 +1,9 @@
class Test {
void test() {
<selection>System.out.println();</selection>
}
void conflict(){
}
}
@@ -0,0 +1,10 @@
class Test {
void test() {
<selection>int x = 42;</selection>
System.out.println(x);
}
int getX(){
return 42;
}
}
@@ -0,0 +1,15 @@
class Test {
void test() {
int x = getAnInt();
System.out.println(x);
}
private int getAnInt() {
int x = 42;
return x;
}
int getX(){
return 42;
}
}
@@ -0,0 +1,6 @@
class Test {
void test() {
<selection>System.out.println();</selection>
System.out.println();
}
}
@@ -0,0 +1,10 @@
class Test {
void test() {
extracted();
extracted();
}
private void extracted() {
System.out.println();
}
}
@@ -0,0 +1,14 @@
class Test {
void test() {
<selection>int x = 42;</selection>
System.out.println(x);
}
int getX(){
return 0;
}
int getAnInt(){
return 0;
}
}
@@ -0,0 +1,19 @@
class Test {
void test() {
int x = getX1();
System.out.println(x);
}
private int getX1() {
int x = 42;
return x;
}
int getX(){
return 0;
}
int getAnInt(){
return 0;
}
}
@@ -0,0 +1,5 @@
class Test {
void test() {
<selection>System.out.println();</selection>
}
}
@@ -0,0 +1,16 @@
class Test {
void test1(){
<selection>System.out.println("string");
System.out.println("string");</selection>
}
void test2(){
System.out.println("message");
System.out.println("message");
}
void test3(){
System.out.println("first");
System.out.println("second");
}
}
@@ -0,0 +1,18 @@
class Test {
void test1(){
extracted("string", "string");
}
private void extracted(String string, String string2) {
System.out.println(string);
System.out.println(string2);
}
void test2(){
extracted("message", "message");
}
void test3(){
extracted("first", "second");
}
}
@@ -0,0 +1,5 @@
class Test {
void test() {
<selection>System.out.println();</selection>
}
}
@@ -0,0 +1,6 @@
class Test {
void test() {
<selection>System.out.println();</selection>
System.out.println();
}
}
@@ -0,0 +1,10 @@
class Test {
void test() {
renamed();
renamed();
}
private void renamed() {
System.out.println();
}
}
@@ -0,0 +1,6 @@
class Test {
void test(){
int avgA = <selection>10 + 20 / 2</selection>;
int avgB = 100 + 200 / 2;
}
}
@@ -0,0 +1,10 @@
class Test {
void test(){
int avgA = average(10, 20);
int avgB = average(100, 200);
}
private int average(int i, int i2) {
return i + i2 / 2;
}
}
@@ -0,0 +1,11 @@
public class Test {
public int test(int x) {
int y = 42;
try {
<selection>y = y + x;
y = y / x;</selection>
} catch (ArithmeticException e) {
}
return y;
}
}
@@ -0,0 +1,13 @@
public class Test {
public int test(int x) {
int y = 42;
try {
<selection>y = y / x;
foo();</selection>
} catch (ArithmeticException e) {
}
return y;
}
void foo(){ }
}
@@ -0,0 +1,13 @@
public class Test {
public int test(int x) {
int y = 42;
try {
<selection>foo();
y = y / x;</selection>
} catch (ArithmeticException e) {
}
return y;
}
void foo() {}
}
@@ -0,0 +1,18 @@
public class Test {
public int test(int x) {
int y = 42;
try {
y = getY(x, y);
} catch (ArithmeticException e) {
}
return y;
}
private int getY(int x, int y) {
foo();
y = y / x;
return y;
}
void foo() {}
}
@@ -0,0 +1,8 @@
import java.util.ArrayList;
class Test {
void test() {
var s = new ArrayList<>().stream();
<selection>System.out.println(s);</selection>
}
}
@@ -0,0 +1,13 @@
import java.util.ArrayList;
import java.util.stream.Stream;
class Test {
void test() {
var s = new ArrayList<>().stream();
extracted(s);
}
private void extracted(Stream<Object> s) {
System.out.println(s);
}
}
@@ -0,0 +1,16 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public int test(int x) {
int y = 42;
try {
<selection>new Scanner(new File("file.txt"));
y = y + x;
y = y / x;</selection>
} catch (FileNotFoundException e) {
}
return y;
}
}
@@ -0,0 +1,21 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public int test(int x) {
int y = 42;
try {
y = getY(x, y);
} catch (FileNotFoundException e) {
}
return y;
}
private int getY(int x, int y) throws FileNotFoundException {
new Scanner(new File("file.txt"));
y = y + x;
y = y / x;
return y;
}
}
@@ -0,0 +1,5 @@
class Test {
void test() {
<selection>System.out.println();</selection>
}
}
@@ -0,0 +1,9 @@
class Test {
void test() {
extracted();
}
private void extracted() {
System.out.println();
}
}
@@ -0,0 +1,6 @@
class Test {
static void test() {
<selection>int x = 42;</selection>
System.out.println(x);
}
}
@@ -0,0 +1,11 @@
class Test {
static void test() {
int x = getX();
System.out.println(x);
}
private static int getX() {
int x = 42;
return x;
}
}
@@ -0,0 +1,16 @@
class Test {
void test1(){
<selection>System.out.println("Hello!");
System.out.println("user");</selection>
}
void test2(String name){
System.out.println("Hello!");
System.out.println(name);
}
void test3(){
System.out.println("Good morning!");
System.out.println("user");
}
}
@@ -0,0 +1,18 @@
class Test {
void test1(){
sayHello("Hello!", "user");
}
private void sayHello(String s, String user) {
System.out.println(s);
System.out.println(user);
}
void test2(String name){
sayHello("Hello!", name);
}
void test3(){
sayHello("Good morning!", "user");
}
}
@@ -0,0 +1,9 @@
class Test {
void test() {
<selection>System.out.println();</selection>
}
void conflict(){
}
}
@@ -0,0 +1,13 @@
class Test {
void test() {
valid();
}
private void valid() {
System.out.println();
}
void conflict(){
}
}
@@ -0,0 +1,6 @@
class Test {
void test() {
<selection>int x = 42;</selection>
System.out.println(x);
}
}
@@ -0,0 +1,11 @@
class Test {
void test() {
int x = getX();
System.out.println(x);
}
private int getX() {
int x = 42;
return x;
}
}
@@ -0,0 +1,198 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.refactoring
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.codeInsight.template.impl.TemplateState
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.LanguageLevelProjectExtension
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.TextRange
import com.intellij.pom.java.LanguageLevel
import com.intellij.refactoring.extractMethod.newImpl.MethodExtractor
import com.intellij.refactoring.listeners.RefactoringEventData
import com.intellij.refactoring.listeners.RefactoringEventListener
import com.intellij.refactoring.util.CommonRefactoringUtil.RefactoringErrorHintException
import com.intellij.testFramework.LightJavaCodeInsightTestCase
import com.intellij.util.ui.UIUtil
import org.jetbrains.annotations.NonNls
class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
private val BASE_PATH: @NonNls String = "/refactoring/extractMethodAndDuplicatesInplace"
fun testStatement(){
doTest()
}
fun testConflictedNamesFiltered(){
doTest()
}
fun testVariableGetterSuggested(){
doTest()
}
fun testExactDuplicates(){
doTest()
}
fun testInvalidRename(){
doTest(changedName = "invalid! name", checkResults = false)
require(getActiveTemplate() != null)
}
fun testConflictRename(){
doTest(changedName = "conflict", checkResults = false)
require(getActiveTemplate() != null)
}
fun testValidRename(){
doTest(changedName = "valid")
require(getActiveTemplate() == null)
}
fun testGeneratedDefault(){
doTest()
}
fun testRenamedExactDuplicate(){
doTest(changedName = "renamed")
}
fun testRenamedParametrizedDuplicate(){
doTest(changedName = "average")
}
fun testStaticMustBePlaced(){
doTest()
}
fun testShortenClassReferences(){
withLanguageLevel(project, LanguageLevel.JDK_11) {
doTest()
}
}
fun testThreeDuplicates(){
doTest(changedName = "sayHello")
}
fun testParameterGrouping(){
doTest()
}
fun testConditionalExitPoint(){
doTest()
}
fun testRuntimeCatchMayChangeSemantic1(){
assertThrows(RefactoringErrorHintException::class.java, JavaRefactoringBundle.message("extract.method.error.many.exits")) {
doTest()
}
}
fun testRuntimeCatchMayChangeSemantic2(){
assertThrows(RefactoringErrorHintException::class.java, JavaRefactoringBundle.message("extract.method.error.many.exits")) {
doTest()
}
}
fun testRuntimeCatchWithLastAssignment(){
doTest()
}
fun testSpecificCatch(){
doTest()
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")
var startReceived = false
var doneReceived = false
project.messageBus.connect().subscribe(RefactoringEventListener.REFACTORING_EVENT_TOPIC, object : RefactoringEventListener {
override fun refactoringStarted(refactoringId: String, beforeData: RefactoringEventData?) {
startReceived = true
}
override fun refactoringDone(refactoringId: String, afterData: RefactoringEventData?) {
doneReceived = true
}
override fun conflictsDetected(refactoringId: String, conflictsData: RefactoringEventData) = Unit
override fun undoRefactoring(refactoringId: String) = Unit
})
val template = startRefactoring(editor)
require(startReceived)
finishTemplate(template)
require(doneReceived)
}
}
private inline fun withLanguageLevel(project: Project, languageLevel: LanguageLevel, body: () -> Unit) {
val extension = LanguageLevelProjectExtension.getInstance(project)
val previousLanguageLevel = extension.languageLevel
try {
extension.languageLevel = languageLevel
body()
} finally {
extension.languageLevel = previousLanguageLevel
}
}
private fun doTest(checkResults: Boolean = true, changedName: String? = null){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")
val template = startRefactoring(editor)
if (changedName != null) {
renameTemplate(template, changedName)
}
finishTemplate(template)
if (checkResults) {
checkResultByFile("$BASE_PATH/${getTestName(false)}_after.java")
}
}
}
private fun startRefactoring(editor: Editor): TemplateState {
val selection = with(editor.selectionModel) { TextRange(selectionStart, selectionEnd) }
MethodExtractor().doExtract(file, selection)
val templateState = getActiveTemplate()
require(templateState != null) { "Failed to start refactoring" }
return templateState
}
private fun getActiveTemplate() = TemplateManagerImpl.getTemplateState(editor)
private fun finishTemplate(templateState: TemplateState){
try {
templateState.gotoEnd(false)
UIUtil.dispatchAllInvocationEvents()
} catch (ignore: RefactoringErrorHintException) {
}
}
private fun renameTemplate(templateState: TemplateState, name: String) {
WriteCommandAction.runWriteCommandAction(project) {
val range = templateState.currentVariableRange!!
editor.document.replaceString(range.startOffset, range.endOffset, name)
}
}
private inline fun templateTest(test: () -> Unit) {
val disposable = Disposer.newDisposable()
try {
TemplateManagerImpl.setTemplateTesting(disposable)
test()
}
finally {
Disposer.dispose(disposable)
}
}
override fun tearDown() {
val template = getActiveTemplate()
if (template != null) Disposer.dispose(template)
super.tearDown()
}
}