From 4db15bc6ea657de55efca36cbfea2290728ccccf Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 17 Jul 2017 14:32:33 +0200 Subject: [PATCH] JavaCodeInsightSanityTest updates * be more prepared to handling non-java files in future * check parsing more thoroughly --- .../CheckPsiTextConsistency.java | 35 +++++++++++++++++++ .../java/propertyBased/DeleteRange.java | 5 ++- .../JavaCodeInsightSanityTest.java | 28 +++++++++------ 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/java/propertyBased/CheckPsiTextConsistency.java diff --git a/java/java-tests/testSrc/com/intellij/java/propertyBased/CheckPsiTextConsistency.java b/java/java-tests/testSrc/com/intellij/java/propertyBased/CheckPsiTextConsistency.java new file mode 100644 index 000000000000..978417299056 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/CheckPsiTextConsistency.java @@ -0,0 +1,35 @@ +/* + * 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.psi.PsiFile; +import com.intellij.testFramework.PsiTestUtil; + +/** + * @author peter + */ +class CheckPsiTextConsistency implements MadTestingAction { + private final PsiFile myFile; + + CheckPsiTextConsistency(PsiFile file) { + myFile = file; + } + + @Override + public void performAction() { + PsiTestUtil.checkPsiStructureWithCommit(myFile, PsiTestUtil::checkFileStructure); + } +} 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 b6fa82124ba6..828839a83bdf 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/DeleteRange.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/DeleteRange.java @@ -31,7 +31,7 @@ class DeleteRange extends ActionOnRange { private DeleteRange(PsiFile file, int startOffset, int endOffset) { super(file.getViewProvider().getDocument(), startOffset, endOffset); - assert myMarker.getDocument().getTextLength() == file.getTextLength(); + assert myMarker.getDocument().getTextLength() == file.getTextLength() : file + " " + myMarker.getDocument(); myFile = file; } @@ -45,6 +45,9 @@ class DeleteRange extends ActionOnRange { if (start == null || end == null) return null; PsiElement commonParent = PsiTreeUtil.findCommonParent(start, end); + if (commonParent == null || commonParent.getTextRange() == null) { // directory; for multi-root files + return null; + } return new DeleteRange(psiFile, commonParent.getTextRange().getStartOffset(), commonParent.getTextRange().getEndOffset()); 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 9951f69ee98c..14264e8f761c 100644 --- a/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java +++ b/java/java-tests/testSrc/com/intellij/java/propertyBased/JavaCodeInsightSanityTest.java @@ -17,18 +17,18 @@ package com.intellij.java.propertyBased; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.WriteAction; +import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiBinaryFile; import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiPlainTextFile; 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.DataStructure; -import slowCheck.Generator; -import slowCheck.IntDistribution; -import slowCheck.PropertyChecker; +import slowCheck.*; import java.io.File; import java.io.FileFilter; @@ -58,7 +58,8 @@ public class JavaCodeInsightSanityTest extends LightPlatformCodeInsightFixtureTe public void testReparse() { Function> fileActions = file -> - Generator.anyOf(DeleteRange.psiRangeDeletions(file), + Generator.anyOf(DeleteRange.psiRangeDeletions(file), + Generator.constant(new CheckPsiTextConsistency(file)), InsertString.asciiInsertions(file)); PropertyChecker.forAll(actionsOnJavaFiles(fileActions), FileWithActions::checkIncrementalReparse); } @@ -67,19 +68,26 @@ public class JavaCodeInsightSanityTest extends LightPlatformCodeInsightFixtureTe private static Generator actionsOnFileContents(CodeInsightTestFixture fixture, String rootPath, FileFilter fileFilter, Function> actions) { - FileFilter childFilter = child -> { + FileFilter interestingIdeaFiles = child -> { String name = child.getName(); if (name.startsWith(".")) return false; - return child.isDirectory() ? shouldGoInsiderDir(name) - : fileFilter.accept(child) && child.length() < 500_000; + if (child.isDirectory()) { + return shouldGoInsiderDir(name); + } + return !FileTypeManager.getInstance().getFileTypeByFileName(name).isBinary() && + fileFilter.accept(child) && + child.length() < 500_000; }; Generator randomFiles = - Generator.from(new FileGenerator(new File(rootPath), childFilter)).suchThat(Objects::nonNull).noShrink(); + Generator.from(new FileGenerator(new File(rootPath), interestingIdeaFiles)).suchThat(Objects::nonNull).noShrink(); return randomFiles.flatMap(ioFile -> { PsiFile file = copyFileToProject(ioFile, fixture, rootPath); + if (file instanceof PsiBinaryFile || file instanceof PsiPlainTextFile) { + return Generator.constant(null); + } return Generator.nonEmptyLists(actions.apply(file)).map(a -> new FileWithActions(file, a)); - }); + }).suchThat(Objects::nonNull); } private static boolean shouldGoInsiderDir(@NotNull String name) {