mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: merge a couple of test classes
GitOrigin-RevId: b0e5c686f90c5d6675d3a23ea1d05dc7b52cb077
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0bcb698bb8
commit
d3e6301c6a
@@ -1,85 +0,0 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.codeInsight;
|
||||
|
||||
import com.intellij.codeInsight.generation.EqualsHashCodeTemplatesManager;
|
||||
import com.intellij.codeInsight.generation.GenerateMembersUtil;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.util.Functions;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class GenerateEquals15Test extends GenerateEqualsTestCase {
|
||||
public void testArraysFromJava15() {
|
||||
doTest(new int[]{0, 1, 2}, new int[]{0, 1, 2}, new int[0], false);
|
||||
}
|
||||
|
||||
public void testDifferentTypes() {
|
||||
doTest(Functions.id(), Functions.id(), fields -> PsiField.EMPTY_ARRAY, true
|
||||
);
|
||||
}
|
||||
|
||||
public void testDifferentTypesGetters() {
|
||||
doTest(Functions.id(), Functions.id(), fields -> PsiField.EMPTY_ARRAY, true, true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesAllNotNull() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCode() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testDifferentTypesNoDouble() {
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
}
|
||||
|
||||
public void testNameConflicts() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testClassWithTypeParams() {
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCodeApache3() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.EQUALS_HASH_CODE_BUILDER_APACHE_COMMONS_LANG_3);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCodeGuava() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.OBJECTS_EQUAL_AND_HASH_CODE_GUAVA);
|
||||
}
|
||||
|
||||
public void testSingleArrayOfPrimitiveWithObjectsTemplate() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.JAVA_UTIL_OBJECTS_EQUALS_AND_HASH_CODE);
|
||||
}
|
||||
|
||||
public void testArrayAndNotOnlyArrayWithObjectsTemplate() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.JAVA_UTIL_OBJECTS_EQUALS_AND_HASH_CODE);
|
||||
}
|
||||
|
||||
public void testArrayAndSuperWithObjectsTemplate() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.JAVA_UTIL_OBJECTS_EQUALS_AND_HASH_CODE);
|
||||
}
|
||||
|
||||
private void doTestWithTemplate(String templateName) {
|
||||
try {
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(templateName);
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
try (InputStream is = GenerateMembersUtil.class.getResourceAsStream("equalsHelper.vm")) {
|
||||
throw new RuntimeException(new String(is.readAllBytes(), StandardCharsets.UTF_8), throwable);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
throwable.addSuppressed(t);
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +1,26 @@
|
||||
/*
|
||||
* Copyright 2000-2017 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-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.codeInsight;
|
||||
|
||||
public class GenerateEqualsTest extends GenerateEqualsTestCase {
|
||||
import com.intellij.codeInsight.generation.EqualsHashCodeTemplatesManager;
|
||||
import com.intellij.codeInsight.generation.GenerateEqualsHelper;
|
||||
import com.intellij.codeInsight.generation.GenerateMembersUtil;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.Functions;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class GenerateEqualsTest extends LightJavaCodeInsightTestCase {
|
||||
|
||||
public void testNoBaseEquals() {
|
||||
doTest(new int[0], new int[0], new int[0], false);
|
||||
}
|
||||
@@ -76,4 +81,128 @@ public class GenerateEqualsTest extends GenerateEqualsTestCase {
|
||||
doTest(new int[]{0}, new int[]{0}, new int[0], true);
|
||||
}
|
||||
|
||||
public void testArraysFromJava15() {
|
||||
doTest(new int[]{0, 1, 2}, new int[]{0, 1, 2}, new int[0], false);
|
||||
}
|
||||
|
||||
public void testDifferentTypes() {
|
||||
doTest(Functions.id(), Functions.id(), fields -> PsiField.EMPTY_ARRAY, true
|
||||
);
|
||||
}
|
||||
|
||||
public void testDifferentTypesGetters() {
|
||||
doTest(Functions.id(), Functions.id(), fields -> PsiField.EMPTY_ARRAY, true, true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesAllNotNull() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCode() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testDifferentTypesNoDouble() {
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
}
|
||||
|
||||
public void testNameConflicts() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
public void testClassWithTypeParams() {
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCodeApache3() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.EQUALS_HASH_CODE_BUILDER_APACHE_COMMONS_LANG_3);
|
||||
}
|
||||
|
||||
public void testDifferentTypesSuperEqualsAndHashCodeGuava() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.OBJECTS_EQUAL_AND_HASH_CODE_GUAVA);
|
||||
}
|
||||
|
||||
public void testSingleArrayOfPrimitiveWithObjectsTemplate() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.JAVA_UTIL_OBJECTS_EQUALS_AND_HASH_CODE);
|
||||
}
|
||||
|
||||
public void testArrayAndNotOnlyArrayWithObjectsTemplate() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.JAVA_UTIL_OBJECTS_EQUALS_AND_HASH_CODE);
|
||||
}
|
||||
|
||||
public void testArrayAndSuperWithObjectsTemplate() {
|
||||
doTestWithTemplate(EqualsHashCodeTemplatesManager.JAVA_UTIL_OBJECTS_EQUALS_AND_HASH_CODE);
|
||||
}
|
||||
|
||||
private void doTestWithTemplate(String templateName) {
|
||||
try {
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(templateName);
|
||||
doTest(Functions.id(), Functions.id(), Functions.id(), true);
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
try (InputStream is = GenerateMembersUtil.class.getResourceAsStream("equalsHelper.vm")) {
|
||||
throw new RuntimeException(new String(is.readAllBytes(), StandardCharsets.UTF_8), throwable);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
throwable.addSuppressed(t);
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
protected void doTest(final int[] equals,
|
||||
final int[] hashCode,
|
||||
final int[] nonNull,
|
||||
boolean insertOverride) {
|
||||
doTest(fields -> getIndexed(fields, equals), fields -> getIndexed(fields, hashCode), fields -> getIndexed(fields, nonNull), insertOverride);
|
||||
}
|
||||
|
||||
protected void doTest(Function<PsiField[], PsiField[]> eqFunction,
|
||||
Function<PsiField[], PsiField[]> hFunction,
|
||||
Function<PsiField[], PsiField[]> nnFunction,
|
||||
boolean insertOverride) {
|
||||
doTest(eqFunction, hFunction, nnFunction, insertOverride, false);
|
||||
}
|
||||
|
||||
protected void doTest(Function<PsiField[], PsiField[]> eqFunction,
|
||||
Function<PsiField[], PsiField[]> hFunction,
|
||||
Function<PsiField[], PsiField[]> nnFunction,
|
||||
boolean insertOverride, boolean useAccessors) {
|
||||
configureByFile("/codeInsight/generateEquals/before" + getTestName(false) + ".java");
|
||||
performTest(eqFunction, hFunction, nnFunction, insertOverride, useAccessors);
|
||||
checkResultByFile("/codeInsight/generateEquals/after" + getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
private void performTest(Function<PsiField[], PsiField[]> equals,
|
||||
Function<PsiField[], PsiField[]> hashCode,
|
||||
Function<PsiField[], PsiField[]> nonNull,
|
||||
boolean insertOverride,
|
||||
boolean useAccessors) {
|
||||
JavaCodeStyleSettings.getInstance(getProject()).GENERATE_FINAL_LOCALS = true;
|
||||
JavaCodeStyleSettings.getInstance(getProject()).INSERT_OVERRIDE_ANNOTATION = insertOverride;
|
||||
PsiElement element = getFile().findElementAt(getEditor().getCaretModel().getOffset());
|
||||
if (element == null) return;
|
||||
PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
|
||||
if (aClass == null) return;
|
||||
PsiField[] fields = aClass.getFields();
|
||||
new GenerateEqualsHelper(getProject(), aClass, equals.fun(fields), hashCode.fun(fields), nonNull.fun(fields), false, useAccessors).invoke();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
|
||||
private static PsiField[] getIndexed(PsiField[] fields, int[] indices) {
|
||||
ArrayList<PsiField> result = new ArrayList<>();
|
||||
for (int index : indices) {
|
||||
result.add(fields[index]);
|
||||
}
|
||||
return result.toArray(PsiField.EMPTY_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
// 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.codeInsight;
|
||||
|
||||
import com.intellij.codeInsight.generation.EqualsHashCodeTemplatesManager;
|
||||
import com.intellij.codeInsight.generation.GenerateEqualsHelper;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
|
||||
import com.intellij.util.Function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public abstract class GenerateEqualsTestCase extends LightJavaCodeInsightTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
EqualsHashCodeTemplatesManager.getInstance().setDefaultTemplate(EqualsHashCodeTemplatesManager.INTELLI_J_DEFAULT);
|
||||
}
|
||||
|
||||
protected void doTest(final int[] equals,
|
||||
final int[] hashCode,
|
||||
final int[] nonNull,
|
||||
boolean insertOverride) {
|
||||
doTest(fields -> getIndexed(fields, equals), fields -> getIndexed(fields, hashCode), fields -> getIndexed(fields, nonNull), insertOverride);
|
||||
}
|
||||
|
||||
protected void doTest(Function<PsiField[], PsiField[]> eqFunction,
|
||||
Function<PsiField[], PsiField[]> hFunction,
|
||||
Function<PsiField[], PsiField[]> nnFunction,
|
||||
boolean insertOverride) {
|
||||
doTest(eqFunction, hFunction, nnFunction, insertOverride, false);
|
||||
}
|
||||
|
||||
protected void doTest(Function<PsiField[], PsiField[]> eqFunction,
|
||||
Function<PsiField[], PsiField[]> hFunction,
|
||||
Function<PsiField[], PsiField[]> nnFunction,
|
||||
boolean insertOverride, boolean useAccessors) {
|
||||
configureByFile("/codeInsight/generateEquals/before" + getTestName(false) + ".java");
|
||||
performTest(eqFunction, hFunction, nnFunction, insertOverride, useAccessors);
|
||||
checkResultByFile("/codeInsight/generateEquals/after" + getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
private void performTest(Function<PsiField[], PsiField[]> equals,
|
||||
Function<PsiField[], PsiField[]> hashCode,
|
||||
Function<PsiField[], PsiField[]> nonNull,
|
||||
boolean insertOverride,
|
||||
boolean useAccessors) {
|
||||
JavaCodeStyleSettings.getInstance(getProject()).GENERATE_FINAL_LOCALS = true;
|
||||
JavaCodeStyleSettings.getInstance(getProject()).INSERT_OVERRIDE_ANNOTATION = insertOverride;
|
||||
PsiElement element = getFile().findElementAt(getEditor().getCaretModel().getOffset());
|
||||
if (element == null) return;
|
||||
PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
|
||||
if (aClass == null) return;
|
||||
PsiField[] fields = aClass.getFields();
|
||||
new GenerateEqualsHelper(getProject(), aClass, equals.fun(fields), hashCode.fun(fields), nonNull.fun(fields), false, useAccessors).invoke();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
|
||||
private static PsiField[] getIndexed(PsiField[] fields, int[] indices) {
|
||||
ArrayList<PsiField> result = new ArrayList<>();
|
||||
for (int indice : indices) {
|
||||
result.add(fields[indice]);
|
||||
}
|
||||
return result.toArray(PsiField.EMPTY_ARRAY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user