use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AdvancedQuickAssistTest method testInverseIfCondition_bug119251.
@Test
public void testInverseIfCondition_bug119251() throws Exception {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=119251
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test;\n");
buf.append("public class E {\n");
buf.append(" private boolean a() { return false; }\n");
buf.append(" private void foo(int i) {}\n");
buf.append(" public void b() {\n");
buf.append(" if (!a() && !a() && !a() && !a())\n");
buf.append(" foo(1);\n");
buf.append(" else\n");
buf.append(" foo(2);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("if");
AssistContext context = getCorrectionContext(cu, offset, 0);
List proposals = collectAssists(context, false);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test;\n");
buf.append("public class E {\n");
buf.append(" private boolean a() { return false; }\n");
buf.append(" private void foo(int i) {}\n");
buf.append(" public void b() {\n");
buf.append(" if (a() || a() || a() || a())\n");
buf.append(" foo(2);\n");
buf.append(" else\n");
buf.append(" foo(1);\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AdvancedQuickAssistTest method testInverseIfConditionEquals.
@Test
public void testInverseIfConditionEquals() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(boolean a, boolean b) {\n");
buf.append(" if (a == (b && a))\n");
buf.append(" return 1;\n");
buf.append(" else\n");
buf.append(" return 2;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("if (");
AssistContext context = getCorrectionContext(cu, offset, 0);
List proposals = collectAssists(context, false);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(boolean a, boolean b) {\n");
buf.append(" if (a != (b && a))\n");
buf.append(" return 2;\n");
buf.append(" else\n");
buf.append(" return 1;\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AdvancedQuickAssistTest17 method testConvertSwitchToIf.
@Test
public void testConvertSwitchToIf() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(String s) {\n");
buf.append(" switch (s) {\n");
buf.append(" case \"abc\":\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" case \"xyz\":\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" default:\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("switch");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(String s) {\n");
buf.append(" if (\"abc\".equals(s)) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else if (\"xyz\".equals(s)) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else {\n");
buf.append(" System.out.println();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(String s) {\n");
buf.append(" if (s.equals(\"abc\")) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else if (s.equals(\"xyz\")) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else {\n");
buf.append(" System.out.println();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1, expected2 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AdvancedQuickAssistTest17 method testReplaceReturnConditionWithIf4.
@Test
public void testReplaceReturnConditionWithIf4() throws Exception {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=112443
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.Collections;\n");
buf.append("import java.util.List;\n");
buf.append("public class E {\n");
buf.append(" List<String> foo(int a) {\n");
buf.append(" return a > 0 ? new ArrayList<>() : new ArrayList<>();\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("?");
AssistContext context = getCorrectionContext(cu, offset, 0);
List proposals = collectAssists(context, false);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.Collections;\n");
buf.append("import java.util.List;\n");
buf.append("public class E {\n");
buf.append(" List<String> foo(int a) {\n");
buf.append(" if (a > 0)\n");
buf.append(" return new ArrayList<>();\n");
buf.append(" else\n");
buf.append(" return new ArrayList<>();\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AdvancedQuickAssistTest17 method testConvertIfToSwitch3.
@Test
public void testConvertIfToSwitch3() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(String s) {\n");
buf.append(" if (\"abc\".equals(s)) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else if (\"xyz\".equals(s)) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else {\n");
buf.append(" System.out.println();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset = buf.toString().indexOf("if");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 5);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(String s) {\n");
buf.append(" switch (s) {\n");
buf.append(" case \"abc\" :\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" case \"xyz\" :\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" default :\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(String s) {\n");
buf.append(" if (s == null) {\n");
buf.append(" System.out.println();\n");
buf.append(" } else {\n");
buf.append(" switch (s) {\n");
buf.append(" case \"abc\" :\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" case \"xyz\" :\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" default :\n");
buf.append(" System.out.println();\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1, expected2 });
}
Aggregations