From e2cc09d7a53948d43686b2f994dabe095126aba4 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Thu, 25 Dec 2014 21:57:36 +0200 Subject: [PATCH] code processor: added tests on single file processing --- .../selectionReformat_after.java | 11 +++ .../selectionReformat_before.java | 11 +++ .../vcsChangedTextReformat_after.java | 19 +++++ .../vcsChangedTextReformat_before.java | 18 +++++ .../wholeFileReformat_after.java | 10 +++ .../wholeFileReformat_before.java | 10 +++ .../actions/MultiActionCodeProcessorTest.java | 81 +++++++++++++++++++ .../actions/ReformatCodeActionTest.java | 6 ++ .../actions/AbstractLayoutCodeProcessor.java | 2 +- .../actions/FormatChangedTextUtil.java | 12 ++- 10 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/actions/codeProcessor/selectionReformat_after.java create mode 100644 java/java-tests/testData/actions/codeProcessor/selectionReformat_before.java create mode 100644 java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_after.java create mode 100644 java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_before.java create mode 100644 java/java-tests/testData/actions/codeProcessor/wholeFileReformat_after.java create mode 100644 java/java-tests/testData/actions/codeProcessor/wholeFileReformat_before.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/actions/MultiActionCodeProcessorTest.java diff --git a/java/java-tests/testData/actions/codeProcessor/selectionReformat_after.java b/java/java-tests/testData/actions/codeProcessor/selectionReformat_after.java new file mode 100644 index 000000000000..7e145c3004f0 --- /dev/null +++ b/java/java-tests/testData/actions/codeProcessor/selectionReformat_after.java @@ -0,0 +1,11 @@ +public class Test { + + int a = 2; + int b = 3; + + + public void run() { + int myFirst = 12; + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/codeProcessor/selectionReformat_before.java b/java/java-tests/testData/actions/codeProcessor/selectionReformat_before.java new file mode 100644 index 000000000000..d753fc6562c6 --- /dev/null +++ b/java/java-tests/testData/actions/codeProcessor/selectionReformat_before.java @@ -0,0 +1,11 @@ +public class Test { + + int a = 2; + int b = 3; + + + public void run() { + int myFirst = 12; + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_after.java b/java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_after.java new file mode 100644 index 000000000000..bf2d0b708392 --- /dev/null +++ b/java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_after.java @@ -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() { + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_before.java b/java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_before.java new file mode 100644 index 000000000000..d837a92dbf22 --- /dev/null +++ b/java/java-tests/testData/actions/codeProcessor/vcsChangedTextReformat_before.java @@ -0,0 +1,18 @@ +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() {} + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/codeProcessor/wholeFileReformat_after.java b/java/java-tests/testData/actions/codeProcessor/wholeFileReformat_after.java new file mode 100644 index 000000000000..05460c51a74b --- /dev/null +++ b/java/java-tests/testData/actions/codeProcessor/wholeFileReformat_after.java @@ -0,0 +1,10 @@ +public class Test { + + public int a = 3; + + + public void test() { + int c = 3; + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/codeProcessor/wholeFileReformat_before.java b/java/java-tests/testData/actions/codeProcessor/wholeFileReformat_before.java new file mode 100644 index 000000000000..e8d0ad4e5260 --- /dev/null +++ b/java/java-tests/testData/actions/codeProcessor/wholeFileReformat_before.java @@ -0,0 +1,10 @@ +public class Test { + + public int a = 3; + + + public void test() { + int c = 3; + } + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/actions/MultiActionCodeProcessorTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/actions/MultiActionCodeProcessorTest.java new file mode 100644 index 000000000000..b3ea6d35e421 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/actions/MultiActionCodeProcessorTest.java @@ -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 ranges = ContainerUtil.mapNotNull(model.getAllCarets(), new Function() { + @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)); + } + + +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionTest.java index 20b483c9b077..b43059389253 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionTest.java @@ -85,4 +85,10 @@ public class ReformatCodeActionTest extends AbstractLayoutCodeProcessorTest { checkFormationAndImportsOptimizationFor(files); } + + + + + + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/AbstractLayoutCodeProcessor.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/AbstractLayoutCodeProcessor.java index b47970d4de18..37ad0423e6a8 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/AbstractLayoutCodeProcessor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/AbstractLayoutCodeProcessor.java @@ -581,7 +581,7 @@ public abstract class AbstractLayoutCodeProcessor { return true; } - protected static Collection getSelectedRanges(@NotNull SelectionModel selectionModel) { + protected static List getSelectedRanges(@NotNull SelectionModel selectionModel) { final List ranges = new SmartList(); if (selectionModel.hasSelection()) { TextRange range = TextRange.create(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java index b3dfef2bcb2a..3aee7c9f9d21 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java @@ -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> CHANGED_RANGES = Key.create("changed.ranges.since.last.revision"); + private FormatChangedTextUtil() { } @@ -229,6 +232,13 @@ public class FormatChangedTextUtil { @NotNull public static List getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) throws FilesTooBigForDiffException { + if (ApplicationManager.getApplication().isUnitTestMode()) { + List testData = file.getUserData(CHANGED_RANGES); + if (testData != null) { + return testData; + } + } + Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (document == null) return ContainerUtil.emptyList();