use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.
the class AdvancedQuickAssistTest method testInverseConditionalStatement2.
@Test
public void testInverseConditionalStatement2() throws Exception {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=74746
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 int foo(int a) {\n");
buf.append(" return a + 6 == 9 ? 4 : 5;\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("public class E {\n");
buf.append(" public int foo(int a) {\n");
buf.append(" return a + 6 != 9 ? 5 : 4;\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 testReplaceReturnIfWithCondition.
@Test
public void testReplaceReturnIfWithCondition() 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 String foo(Object b) {\n");
buf.append(" if (b == null) {\n");
buf.append(" return null;\n");
buf.append(" } else {\n");
buf.append(" return b.toString();\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);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public String foo(Object b) {\n");
buf.append(" return b == null ? null : b.toString();\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 testConvertIfToSwitch9.
@Test
public void testConvertIfToSwitch9() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package pack;\n");
buf.append("\n");
buf.append("public class A {\n");
buf.append(" public enum TimeUnit {\n");
buf.append(" SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS\n");
buf.append(" }\n");
buf.append(" public static int getPower(TimeUnit unit) {\n");
buf.append(" if (TimeUnit.SECONDS.equals(unit)) {\n");
buf.append(" return 0;\n");
buf.append(" } else if (TimeUnit.MILLISECONDS.equals(unit)) {\n");
buf.append(" return -3;\n");
buf.append(" } else if (TimeUnit.MICROSECONDS.equals(unit)) {\n");
buf.append(" return -6;\n");
buf.append(" } else if (TimeUnit.NANOSECONDS.equals(unit)) {\n");
buf.append(" return -9;\n");
buf.append(" } else {\n");
buf.append(" throw new InternalError();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("A.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 pack;\n");
buf.append("\n");
buf.append("public class A {\n");
buf.append(" public enum TimeUnit {\n");
buf.append(" SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS\n");
buf.append(" }\n");
buf.append(" public static int getPower(TimeUnit unit) {\n");
buf.append(" switch (unit) {\n");
buf.append(" case SECONDS :\n");
buf.append(" return 0;\n");
buf.append(" case MILLISECONDS :\n");
buf.append(" return -3;\n");
buf.append(" case MICROSECONDS :\n");
buf.append(" return -6;\n");
buf.append(" case NANOSECONDS :\n");
buf.append(" return -9;\n");
buf.append(" default :\n");
buf.append(" throw new InternalError();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
buf = new StringBuffer();
buf.append("package pack;\n");
buf.append("\n");
buf.append("public class A {\n");
buf.append(" public enum TimeUnit {\n");
buf.append(" SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS\n");
buf.append(" }\n");
buf.append(" public static int getPower(TimeUnit unit) {\n");
buf.append(" if (unit == null) {\n");
buf.append(" throw new InternalError();\n");
buf.append(" } else {\n");
buf.append(" switch (unit) {\n");
buf.append(" case SECONDS :\n");
buf.append(" return 0;\n");
buf.append(" case MILLISECONDS :\n");
buf.append(" return -3;\n");
buf.append(" case MICROSECONDS :\n");
buf.append(" return -6;\n");
buf.append(" case NANOSECONDS :\n");
buf.append(" return -9;\n");
buf.append(" default :\n");
buf.append(" throw new InternalError();\n");
buf.append(" }\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 AdvancedQuickAssistTest method testAddParanoidalParentheses1.
@Test
public void testAddParanoidalParentheses1() 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, int b, int c, Object o) {\n");
buf.append(" if (a && b == 0 && b + c > 3 && o instanceof Integer) {\n");
buf.append(" b= 9;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset1 = buf.toString().indexOf("if (");
int offset2 = buf.toString().indexOf(") {", offset1);
AssistContext context = getCorrectionContext(cu, offset1, offset2 - offset1);
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, int b, int c, Object o) {\n");
buf.append(" if (a && (b == 0) && ((b + c) > 3) && (o instanceof Integer)) {\n");
buf.append(" b= 9;\n");
buf.append(" }\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 testAssignAndCastBug331195_4.
@Test
public void testAssignAndCastBug331195_4() 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(int a, Object b) {\n");
buf.append(" if (b instanceof String) {\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("String)");
AssistContext context = getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
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(int a, Object b) {\n");
buf.append(" if (b instanceof String) {\n");
buf.append(" String string = (String) b;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Aggregations