Add parsing spec tests support

This commit is contained in:
victor.petukhov
2018-08-28 19:33:53 +03:00
parent f322aba768
commit d9473c0b6e
4 changed files with 75 additions and 10 deletions
@@ -7,11 +7,16 @@ package org.jetbrains.kotlin.generators.tests
import org.jetbrains.kotlin.generators.tests.generator.testGroup
import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTestSpec
import org.jetbrains.kotlin.parsing.AbstractParsingTestSpec
fun main(args: Array<String>) {
testGroup("compiler/tests-spec/tests", "compiler/tests-spec/testData") {
testClass<AbstractDiagnosticsTestSpec> {
model("diagnostics", excludeDirs = listOf("helpers"))
}
testClass<AbstractParsingTestSpec> {
model("psi", testMethod = "doParsingTest", excludeDirs = listOf("helpers"))
}
}
}
}
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.parsing
import org.jetbrains.kotlin.spec.validators.*
import org.junit.Assert
abstract class AbstractParsingTestSpec : AbstractParsingTest() {
private lateinit var testValidator: AbstractSpecTestValidator<out AbstractSpecTest>
override fun doParsingTest(filePath: String) {
testValidator = AbstractSpecTestValidator.getInstanceByType(filePath)
try {
testValidator.parseTestInfo()
} catch (e: SpecTestValidationException) {
Assert.fail(e.description)
}
testValidator.printTestInfo()
super.doParsingTest(filePath, testValidator::testInfoFilter)
try {
testValidator.validateTestType(computedTestType = ParsingTestTypeValidator.computeTestType(myFile))
} catch (e: SpecTestValidationException) {
Assert.fail(e.description)
}
}
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.spec.validators
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiErrorElement
import com.intellij.psi.PsiFile
object ParsingTestTypeValidator {
private fun checkErrorElement(psi: PsiElement): Boolean =
psi.children.any { it is PsiErrorElement || checkErrorElement(it) }
fun computeTestType(psiFile: PsiFile) =
if (checkErrorElement(psiFile)) TestType.NEGATIVE else TestType.POSITIVE
}
@@ -38,6 +38,8 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import kotlin.jvm.functions.Function1;
public abstract class AbstractParsingTest extends KtParsingTestCase {
@Override
protected void setUp() throws Exception {
@@ -87,20 +89,26 @@ public abstract class AbstractParsingTest extends KtParsingTestCase {
}
protected void doParsingTest(@NotNull String filePath) throws Exception {
doBaseTest(filePath, KtNodeTypes.KT_FILE);
doBaseTest(filePath, KtNodeTypes.KT_FILE, null);
}
protected void doParsingTest(@NotNull String filePath, Function1<String, String> contentFilter) throws Exception {
doBaseTest(filePath, KtNodeTypes.KT_FILE, contentFilter);
}
protected void doExpressionCodeFragmentParsingTest(@NotNull String filePath) throws Exception {
doBaseTest(filePath, KtNodeTypes.EXPRESSION_CODE_FRAGMENT);
doBaseTest(filePath, KtNodeTypes.EXPRESSION_CODE_FRAGMENT, null);
}
protected void doBlockCodeFragmentParsingTest(@NotNull String filePath) throws Exception {
doBaseTest(filePath, KtNodeTypes.BLOCK_CODE_FRAGMENT);
doBaseTest(filePath, KtNodeTypes.BLOCK_CODE_FRAGMENT, null);
}
private void doBaseTest(@NotNull String filePath, @NotNull IElementType fileType) throws Exception {
private void doBaseTest(@NotNull String filePath, @NotNull IElementType fileType, Function1<String, String> contentFilter) throws Exception {
String fileContent = loadFile(filePath);
myFileExt = FileUtilRt.getExtension(PathUtil.getFileName(filePath));
myFile = createFile(filePath, fileType);
myFile = createFile(filePath, fileType, contentFilter != null ? contentFilter.invoke(fileContent) : fileContent);
myFile.acceptChildren(new KtVisitorVoid() {
@Override
@@ -118,16 +126,17 @@ public abstract class AbstractParsingTest extends KtParsingTestCase {
doCheckResult(myFullDataPath, filePath.replaceAll("\\.kts?", ".txt"), toParseTreeText(myFile, false, false).trim());
}
private PsiFile createFile(@NotNull String filePath, @NotNull IElementType fileType) throws Exception {
private PsiFile createFile(@NotNull String filePath, @NotNull IElementType fileType, @NotNull String fileContent) {
KtPsiFactory psiFactory = KtPsiFactoryKt.KtPsiFactory(myProject);
if (fileType == KtNodeTypes.EXPRESSION_CODE_FRAGMENT) {
return psiFactory.createExpressionCodeFragment(loadFile(filePath), null);
return psiFactory.createExpressionCodeFragment(fileContent, null);
}
else if (fileType == KtNodeTypes.BLOCK_CODE_FRAGMENT) {
return psiFactory.createBlockCodeFragment(loadFile(filePath), null);
return psiFactory.createBlockCodeFragment(fileContent, null);
}
else {
return createPsiFile(FileUtil.getNameWithoutExtension(PathUtil.getFileName(filePath)), loadFile(filePath));
return createPsiFile(FileUtil.getNameWithoutExtension(PathUtil.getFileName(filePath)), fileContent);
}
}