SSR: tests for "use static import if possible"

This commit is contained in:
Bas Leijdekkers
2014-07-28 20:39:12 +02:00
parent 23f5368c83
commit 29f522cc14
2 changed files with 30 additions and 1 deletions
@@ -1809,6 +1809,35 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
);
}
public void testUseStaticImport() {
final String in = "class X {{ Math.abs(-1); }}";
final String what = "Math.abs('a)";
final String by = "Math.abs($a$)";
options.setToUseStaticImport(true);
final String expected = "import static java.lang.Math.abs;class X {{ abs(-1); }}";
assertEquals("Replacing with static import", expected, replacer.testReplace(in, what, by, options, true));
}
public void testUseStaticStarImport() {
final String in = "class ImportTest {{\n" +
" Math.abs(-0.5);\n" +
" Math.sin(0.5);\n" +
" Math.max(1, 2);\n" +
"}}";
final String what = "Math.'m('a*)";
final String by = "Math.$m$($a$)";
options.setToUseStaticImport(true);
// depends on default setting being equal to 3 for names count to use import on demand
final String expected = "import static java.lang.Math.*;class ImportTest {{\n" +
" abs(-0.5);\n" +
" sin(0.5);\n" +
" max(1,2);\n" +
"}}";
assertEquals("Replacing with static star import", expected, replacer.testReplace(in, what, by, options, true));
}
public void testReformatAndShortenClassRefPerformance() throws IOException {
final String testName = getTestName(false);
final String ext = "java";
@@ -416,7 +416,7 @@ public class ImportUtils {
final List<PsiImportStaticStatement> imports = getMatchingImports(importList, qualifiedName);
final int onDemandCount = JavaCodeStyleSettingsFacade.getInstance(project).getNamesCountToUseImportOnDemand();
final PsiElementFactory elementFactory = psiFacade.getElementFactory();
if (imports.size() < onDemandCount) {
if (imports.size() + 1 < onDemandCount) {
importList.add(elementFactory.createImportStaticStatement(aClass, memberName));
}
else {