From 9d7b0596916d39ae55314ca04df3a699f0ca270f Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 13 Jul 2017 08:11:49 +0200 Subject: [PATCH] JavaCodeInsightSanityTest: check completion and incremental reparse --- .../java/propertyBased/ActionOnRange.java | 2 +- .../ApplyRandomIntentionsTest.java | 6 +-- .../CompletionConsistencyTest.java | 2 +- .../java/propertyBased/DeleteRange.java | 9 +--- .../java/propertyBased/FilePsiMutation.java | 2 + .../java/propertyBased/FileWithActions.java | 11 +++- .../java/propertyBased/InsertString.java | 52 +++++++++++++++++++ .../java/propertyBased/InvokeCompletion.java | 8 ++- .../java/propertyBased/InvokeIntention.java | 2 +- .../JavaCodeInsightSanityTest.java | 40 ++++++++++---- .../java/propertyBased/MadTestingAction.java | 7 +-- .../propertyBased/RehighlightAllEditors.java | 1 + 12 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/java/propertyBased/InsertString.java diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/ActionOnRange.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/ActionOnRange.java index cfbe3d07950a..a83f0206d252 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/ActionOnRange.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/ActionOnRange.java @@ -24,7 +24,7 @@ import org.jetbrains.annotations.Nullable; /** * @author peter */ -class ActionOnRange { +abstract class ActionOnRange implements MadTestingAction { protected final RangeMarker myMarker; private TextRange finalRange; diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/ApplyRandomIntentionsTest.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/ApplyRandomIntentionsTest.java index 1857fce029cb..687fcb92effb 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/ApplyRandomIntentionsTest.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/ApplyRandomIntentionsTest.java @@ -55,7 +55,7 @@ public class ApplyRandomIntentionsTest extends AbstractApplyAndRevertTestCase { } changeAndRevert(myProject, () -> { - MadTestingAction.runActions(list, myProject); + MadTestingAction.runActions(list); if (tracker.getModificationCount() != startModCount) { checkCompiles(myCompilerTester.make()); @@ -69,7 +69,7 @@ public class ApplyRandomIntentionsTest extends AbstractApplyAndRevertTestCase { CheckerSettings settings = CheckerSettings.DEFAULT_SETTINGS.withIterationCount(50); Generator> genActionGroup = psiJavaFiles().flatMap( file -> { - Generator mutation = Generator.anyOf(DeleteRange.deletePsiRange(file), + Generator mutation = Generator.anyOf(DeleteRange.psiRangeDeletions(file), Generator.constant(new AddNullArgument(file)), Generator.constant(new DeleteForeachInitializers(file)), Generator.constant(new DeleteSecondArgument(file)), @@ -83,7 +83,7 @@ public class ApplyRandomIntentionsTest extends AbstractApplyAndRevertTestCase { PropertyChecker.forAll(settings, Generator.listsOf(genActionGroup).map(ContainerUtil::flatten), list -> { changeAndRevert(myProject, () -> { //System.out.println(list); - MadTestingAction.runActions(list, myProject); + MadTestingAction.runActions(list); }); return true; }); diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/CompletionConsistencyTest.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/CompletionConsistencyTest.java index 39db1a2c1122..4be791c4e17a 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/CompletionConsistencyTest.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/CompletionConsistencyTest.java @@ -32,7 +32,7 @@ public class CompletionConsistencyTest extends AbstractApplyAndRevertTestCase { PropertyChecker.forAll(settings.withIterationCount(20), psiJavaFiles(), file -> { System.out.println("for file: " + file.getVirtualFile().getPresentableUrl()); PropertyChecker.forAll(settings.withIterationCount(10), Generator.listsOf(InvokeCompletion.completions(file)), list -> { - changeAndRevert(myProject, () -> MadTestingAction.runActions(list, myProject)); + changeAndRevert(myProject, () -> MadTestingAction.runActions(list)); return true; }); return true; diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/DeleteRange.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/DeleteRange.java index 4b4a826f2ab2..b6fa82124ba6 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/DeleteRange.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/DeleteRange.java @@ -20,15 +20,13 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.psi.util.PsiUtilCore; -import com.intellij.testFramework.PsiTestUtil; import org.jetbrains.annotations.NotNull; import slowCheck.Generator; import slowCheck.IntDistribution; import java.util.Objects; -class DeleteRange extends ActionOnRange implements MadTestingAction { +class DeleteRange extends ActionOnRange { private final PsiFile myFile; private DeleteRange(PsiFile file, int startOffset, int endOffset) { @@ -37,7 +35,7 @@ class DeleteRange extends ActionOnRange implements MadTestingAction { myFile = file; } - static Generator deletePsiRange(@NotNull PsiFile psiFile) { + static Generator psiRangeDeletions(@NotNull PsiFile psiFile) { return Generator.from(data -> { if (psiFile.getTextLength() == 0) return new DeleteRange(psiFile, 0, 0); @@ -64,8 +62,5 @@ class DeleteRange extends ActionOnRange implements MadTestingAction { WriteCommandAction.runWriteCommandAction(myFile.getProject(), () -> myFile.getViewProvider().getDocument() .deleteString(range.getStartOffset(), range.getEndOffset())); - - PsiUtilCore.ensureValid(myFile); - PsiTestUtil.checkPsiStructureWithCommit(myFile, PsiTestUtil::checkStubsMatchText); } } diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/FilePsiMutation.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/FilePsiMutation.java index d0cd416c78e1..5db634f7aaab 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/FilePsiMutation.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/FilePsiMutation.java @@ -16,6 +16,7 @@ package com.intellij.java.propertyBased; import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.testFramework.PsiTestUtil; @@ -36,6 +37,7 @@ abstract class FilePsiMutation implements MadTestingAction { @Override public void performAction() { + PsiDocumentManager.getInstance(myFile.getProject()).commitDocument(myFile.getViewProvider().getDocument()); WriteCommandAction.runWriteCommandAction(myFile.getProject(), this::performMutation); PsiTestUtil.checkPsiStructureWithCommit(myFile, PsiTestUtil::checkStubsMatchText); } diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/FileWithActions.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/FileWithActions.java index daf64c3e89c3..ebca5125d21d 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/FileWithActions.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/FileWithActions.java @@ -19,6 +19,7 @@ import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; +import com.intellij.testFramework.PsiTestUtil; import com.intellij.testFramework.RunAll; import java.util.List; @@ -46,7 +47,15 @@ class FileWithActions { boolean runActions() { Project project = myFile.getProject(); - new RunAll(() -> AbstractApplyAndRevertTestCase.changeAndRevert(project, () -> MadTestingAction.runActions(myActions, project)), + new RunAll(() -> AbstractApplyAndRevertTestCase.changeAndRevert(project, () -> MadTestingAction.runActions(myActions)), + () -> WriteAction.run(() -> myFile.getVirtualFile().delete(this))).run(); + return true; + } + + boolean checkIncrementalReparse() { + Project project = myFile.getProject(); + new RunAll(() -> AbstractApplyAndRevertTestCase.changeAndRevert(project, () -> MadTestingAction.runActions(myActions)), + () -> PsiTestUtil.checkPsiStructureWithCommit(getPsiFile(), PsiTestUtil::checkFileStructure), () -> WriteAction.run(() -> myFile.getVirtualFile().delete(this))).run(); return true; } diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/InsertString.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/InsertString.java new file mode 100644 index 000000000000..256b72c853b3 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/InsertString.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2017 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.java.propertyBased; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.psi.PsiFile; +import org.jetbrains.annotations.NotNull; +import slowCheck.Generator; + +class InsertString extends ActionOnRange { + private final PsiFile myFile; + private final String myToInsert; + + private InsertString(PsiFile file, int offset, String toInsert) { + super(file.getViewProvider().getDocument(), offset, offset); + myToInsert = toInsert; + assert myMarker.getDocument().getTextLength() == file.getTextLength(); + myFile = file; + } + + static Generator asciiInsertions(@NotNull PsiFile psiFile) { + return Generator.zipWith(Generator.integers(0, psiFile.getTextLength()), + Generator.stringsOf(Generator.asciiPrintableChars()), + (offset, toInsert) -> new InsertString(psiFile, offset, toInsert)); + } + + @Override + public String toString() { + return "InsertString: " + myFile.getVirtualFile().getPath() + " " + getStartOffset() + " '" + myToInsert + "'"; + } + + public void performAction() { + int offset = getStartOffset(); + if (offset < 0) return; + + WriteCommandAction.runWriteCommandAction(myFile.getProject(), () -> + myFile.getViewProvider().getDocument().insertString(offset, myToInsert)); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeCompletion.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeCompletion.java index 05b6023600af..76cdabb99c46 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeCompletion.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeCompletion.java @@ -33,6 +33,7 @@ import com.intellij.openapi.util.registry.Registry; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiUtil; import com.intellij.testFramework.PsiTestUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.UIUtil; @@ -50,7 +51,7 @@ import java.util.Set; /** * @author peter */ -class InvokeCompletion extends ActionOnRange implements MadTestingAction { +class InvokeCompletion extends ActionOnRange { final int itemIndexRaw; LookupElement selectedItem; final char completionChar; @@ -163,6 +164,11 @@ class InvokeCompletion extends ActionOnRange implements MadTestingAction { return null; } if (leaf instanceof PsiIdentifier) return null; // it's not a ref, just some name + if (leaf instanceof PsiKeyword && + leaf.getParent() instanceof PsiClassObjectAccessExpression && + PsiUtil.resolveClassInType(((PsiClassObjectAccessExpression)leaf.getParent()).getType()) == null) { + return null; + } } return leafText; } diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeIntention.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeIntention.java index 2a45d302414d..5a10301a7a92 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeIntention.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/InvokeIntention.java @@ -36,7 +36,7 @@ import slowCheck.Generator; import java.util.List; -class InvokeIntention extends ActionOnRange implements MadTestingAction { +class InvokeIntention extends ActionOnRange { private final PsiFile myFile; private final int myIntentionIndex; private IntentionAction myIntentionAction; diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java index 32300f1e410d..9951f69ee98c 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java @@ -16,15 +16,19 @@ package com.intellij.java.propertyBased; import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.psi.PsiBinaryFile; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.testFramework.SkipSlowTestLocally; import com.intellij.testFramework.fixtures.CodeInsightTestFixture; import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import slowCheck.*; +import slowCheck.DataStructure; +import slowCheck.Generator; +import slowCheck.IntDistribution; +import slowCheck.PropertyChecker; import java.io.File; import java.io.FileFilter; @@ -38,13 +42,25 @@ import java.util.function.Function; @SkipSlowTestLocally public class JavaCodeInsightSanityTest extends LightPlatformCodeInsightFixtureTestCase { - public void testRandomIntentions() { + public void testRandomActivity() { AbstractApplyAndRevertTestCase.enableAllInspections(getProject(), getTestRootDisposable()); - FileFilter fileFilter = f -> f.getName().endsWith(".java"); - Generator actions = actionsOnFileContents(myFixture, PathManager.getHomePath(), fileFilter, - file -> Generator.anyOf(InvokeIntention.randomIntentions(file), - DeleteRange.deletePsiRange(file))); - PropertyChecker.forAll(actions, FileWithActions::runActions); + Function> fileActions = file -> + Generator.anyOf(InvokeIntention.randomIntentions(file), + InvokeCompletion.completions(file), + DeleteRange.psiRangeDeletions(file)); + PropertyChecker.forAll(actionsOnJavaFiles(fileActions), FileWithActions::runActions); + } + + @NotNull + private Generator actionsOnJavaFiles(Function> fileActions) { + return actionsOnFileContents(myFixture, PathManager.getHomePath(), f -> f.getName().endsWith(".java"), fileActions); + } + + public void testReparse() { + Function> fileActions = file -> + Generator.anyOf(DeleteRange.psiRangeDeletions(file), + InsertString.asciiInsertions(file)); + PropertyChecker.forAll(actionsOnJavaFiles(fileActions), FileWithActions::checkIncrementalReparse); } @NotNull @@ -62,9 +78,6 @@ public class JavaCodeInsightSanityTest extends LightPlatformCodeInsightFixtureTe Generator.from(new FileGenerator(new File(rootPath), childFilter)).suchThat(Objects::nonNull).noShrink(); return randomFiles.flatMap(ioFile -> { PsiFile file = copyFileToProject(ioFile, fixture, rootPath); - if (file == null || file instanceof PsiBinaryFile || file.getTextLength() == 0) { - return Generator.constant(new FileWithActions(file, Collections.emptyList())); - } return Generator.nonEmptyLists(actions.apply(file)).map(a -> new FileWithActions(file, a)); }); } @@ -90,6 +103,11 @@ public class JavaCodeInsightSanityTest extends LightPlatformCodeInsightFixtureTe //todo strip test data markup try { String path = FileUtil.getRelativePath(rootPath, ioFile.getPath(), '/'); + VirtualFile existing = fixture.findFileInTempDir(path); + if (existing != null) { + WriteAction.run(() -> existing.delete(fixture)); + } + return fixture.addFileToProject(path, FileUtil.loadFile(ioFile)); } catch (IOException e) { diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/MadTestingAction.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/MadTestingAction.java index bc9c3e5e892e..edf6cd661053 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/MadTestingAction.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/MadTestingAction.java @@ -16,8 +16,6 @@ package com.intellij.java.propertyBased; import com.intellij.openapi.fileEditor.FileDocumentManager; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiDocumentManager; import java.util.List; @@ -28,14 +26,13 @@ interface MadTestingAction { void performAction(); - static void runActions(List list, Project project) { + static void runActions(List list) { for (int i = 0; i < list.size(); i++) { MadTestingAction action = list.get(i); if (i > 0 && action == list.get(i - 1)) continue; FileDocumentManager.getInstance().saveAllDocuments(); - PsiDocumentManager.getInstance(project).commitAllDocuments(); - + action.performAction(); } } diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/RehighlightAllEditors.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/RehighlightAllEditors.java index 99c814f64e81..2fe64988e026 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/RehighlightAllEditors.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/RehighlightAllEditors.java @@ -54,6 +54,7 @@ class RehighlightAllEditors implements MadTestingAction { @NotNull static List highlightEditor(Editor editor, Project project) { + PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); return CodeInsightTestFixtureImpl.instantiateAndRun(file, editor, new int[0], false); }