code processor: added tests on single file processing

This commit is contained in:
Yaroslav Lepenkin
2014-12-25 22:31:11 +02:00
parent 802b710a15
commit e2cc09d7a5
10 changed files with 178 additions and 2 deletions
@@ -0,0 +1,11 @@
public class Test {
int a = 2;
int b = 3;
public void run() {
int myFirst = 12;
}
}
@@ -0,0 +1,11 @@
public class Test {
<selection>int a = 2;
int b = 3;</selection>
public void run() {
int myFirst = 12;
}
}
@@ -0,0 +1,19 @@
public class Test {
int a = 3;
int b = 12;
long t = 12;
public void test() {
int a = 3;
int b = 4;
}
int rest = -12;
public void run() {
}
}
@@ -0,0 +1,18 @@
public class Test {
<selection> int a = 3;
<caret>int b = 12;</selection>
long t = 12;
public void test() {
<selection> int a = 3;
<caret>int b = 4;</selection>
}
int rest = -12;
<selection>public void run() {}<caret></selection>
}
@@ -0,0 +1,10 @@
public class Test {
public int a = 3;
public void test() {
int c = 3;
}
}
@@ -0,0 +1,10 @@
public class Test {
public int a = 3;
public void test() {
int c = 3;
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2000-2014 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.
*/
package com.intellij.codeInsight.actions;
import com.intellij.JavaTestUtil;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.util.TextRange;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import java.util.List;
public class MultiActionCodeProcessorTest extends LightPlatformCodeInsightFixtureTestCase {
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath() + "/actions/codeProcessor/";
}
@Override
public void setUp() throws Exception {
super.setUp();
}
@Override
public void tearDown() throws Exception {
myFixture.getFile().putUserData(FormatChangedTextUtil.CHANGED_RANGES, null);
super.tearDown();
}
public void doTest(LayoutCodeOptions options) {
myFixture.configureByFile(getTestDataPath() + getTestName(true) + "_before.java");
CodeProcessor processor = new CodeProcessor(myFixture.getFile(), myFixture.getEditor(), options);
if (options.getTextRangeType() == TextRangeType.VCS_CHANGED_TEXT) {
CaretModel model = myFixture.getEditor().getCaretModel();
List<TextRange> ranges = ContainerUtil.mapNotNull(model.getAllCarets(), new Function<Caret, TextRange>() {
@Override
public TextRange fun(Caret caret) {
if (caret.hasSelection()) {
return new TextRange(caret.getSelectionStart(), caret.getSelectionEnd());
}
return null;
}
});
myFixture.getFile().putUserData(FormatChangedTextUtil.CHANGED_RANGES, ranges);
}
processor.processCode();
myFixture.checkResultByFile(getTestName(true) + "_after.java");
}
public void testSelectionReformat() {
doTest(new ReformatCodeRunOptions(TextRangeType.SELECTED_TEXT));
}
public void testWholeFileReformat() {
doTest(new ReformatCodeRunOptions(TextRangeType.WHOLE_FILE));
}
public void testVcsChangedTextReformat() {
doTest(new ReformatCodeRunOptions(TextRangeType.VCS_CHANGED_TEXT));
}
}
@@ -85,4 +85,10 @@ public class ReformatCodeActionTest extends AbstractLayoutCodeProcessorTest {
checkFormationAndImportsOptimizationFor(files);
}
}
@@ -581,7 +581,7 @@ public abstract class AbstractLayoutCodeProcessor {
return true;
}
protected static Collection<TextRange> getSelectedRanges(@NotNull SelectionModel selectionModel) {
protected static List<TextRange> getSelectedRanges(@NotNull SelectionModel selectionModel) {
final List<TextRange> ranges = new SmartList<TextRange>();
if (selectionModel.hasSelection()) {
TextRange range = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
@@ -15,6 +15,7 @@
*/
package com.intellij.codeInsight.actions;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.diagnostic.Logger;
@@ -27,6 +28,7 @@ import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
@@ -53,7 +55,8 @@ import java.util.*;
public class FormatChangedTextUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.actions.FormatChangedTextUtil");
protected static final Key<List<TextRange>> CHANGED_RANGES = Key.create("changed.ranges.since.last.revision");
private FormatChangedTextUtil() {
}
@@ -229,6 +232,13 @@ public class FormatChangedTextUtil {
@NotNull
public static List<TextRange> getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) throws FilesTooBigForDiffException {
if (ApplicationManager.getApplication().isUnitTestMode()) {
List<TextRange> testData = file.getUserData(CHANGED_RANGES);
if (testData != null) {
return testData;
}
}
Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document == null) return ContainerUtil.emptyList();