JavaCodeInsightSanityTest: check completion and incremental reparse

This commit is contained in:
peter
2017-07-13 08:12:30 +02:00
parent 45c9f3cb93
commit 9d7b059691
12 changed files with 111 additions and 31 deletions
@@ -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;
@@ -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<List<MadTestingAction>> genActionGroup = psiJavaFiles().flatMap(
file -> {
Generator<MadTestingAction> mutation = Generator.anyOf(DeleteRange.deletePsiRange(file),
Generator<MadTestingAction> 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;
});
@@ -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;
@@ -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<DeleteRange> deletePsiRange(@NotNull PsiFile psiFile) {
static Generator<DeleteRange> 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);
}
}
@@ -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);
}
@@ -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;
}
@@ -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<InsertString> 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));
}
}
@@ -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;
}
@@ -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;
@@ -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<FileWithActions> actions = actionsOnFileContents(myFixture, PathManager.getHomePath(), fileFilter,
file -> Generator.anyOf(InvokeIntention.randomIntentions(file),
DeleteRange.deletePsiRange(file)));
PropertyChecker.forAll(actions, FileWithActions::runActions);
Function<PsiFile, Generator<? extends MadTestingAction>> fileActions = file ->
Generator.anyOf(InvokeIntention.randomIntentions(file),
InvokeCompletion.completions(file),
DeleteRange.psiRangeDeletions(file));
PropertyChecker.forAll(actionsOnJavaFiles(fileActions), FileWithActions::runActions);
}
@NotNull
private Generator<FileWithActions> actionsOnJavaFiles(Function<PsiFile, Generator<? extends MadTestingAction>> fileActions) {
return actionsOnFileContents(myFixture, PathManager.getHomePath(), f -> f.getName().endsWith(".java"), fileActions);
}
public void testReparse() {
Function<PsiFile, Generator<? extends MadTestingAction>> 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) {
@@ -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<? extends MadTestingAction> list, Project project) {
static void runActions(List<? extends MadTestingAction> 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();
}
}
@@ -54,6 +54,7 @@ class RehighlightAllEditors implements MadTestingAction {
@NotNull
static List<HighlightInfo> 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);
}