use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractVariable.
@Test
public void testExtractVariable() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int x= /*]*/0/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int j = 0;\n");
buf.append(" int x= /*]*/j/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractMethod.
@Test
public void testExtractMethod() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public int foo(boolean b1, boolean b2) {\n");
buf.append(" int n = 0;\n");
buf.append(" int i = 0;\n");
buf.append(" /*[*/\n");
buf.append(" if (b1)\n");
buf.append(" i = 1;\n");
buf.append(" if (b2)\n");
buf.append(" n = n + i;\n");
buf.append(" /*]*/\n");
buf.append(" return n;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_METHOD_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public int foo(boolean b1, boolean b2) {\n");
buf.append(" int n = 0;\n");
buf.append(" int i = 0;\n");
buf.append(" n = extracted(b1, b2, n, i);\n");
buf.append(" return n;\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private int extracted(boolean b1, boolean b2, int n, int i) {\n");
buf.append(" /*[*/\n");
buf.append(" if (b1)\n");
buf.append(" i = 1;\n");
buf.append(" if (b2)\n");
buf.append(" n = n + i;\n");
buf.append(" /*]*/\n");
buf.append(" return n;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractConstant.
@Test
public void testExtractConstant() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int x= /*]*/0/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" private static final int _0 = /*]*/0/*[*/;\n");
buf.append("\n");
buf.append(" void m(int i){\n");
buf.append(" int x= _0;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractVariableAllOccurrence.
@Test
public void testExtractVariableAllOccurrence() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int x= /*]*/0/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int j = 0;\n");
buf.append(" int x= /*]*/j/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractField.
@Test
public void testExtractField() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int x= /*]*/0/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_FIELD_COMMAND, Arrays.asList(InitializeScope.CURRENT_METHOD.getName()), params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" private int i;\n\n");
buf.append(" void m(int i){\n");
buf.append(" this.i = 0;\n");
buf.append(" int x= /*]*/this.i/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
Aggregations