From d9473c0b6e86c0c5fb774af12fafe92c9d37554c Mon Sep 17 00:00:00 2001 From: "victor.petukhov" Date: Tue, 28 Aug 2018 19:33:53 +0300 Subject: [PATCH] Add parsing spec tests support --- .../tests/GenerateCompilerSpecTests.kt | 7 +++- .../kotlin/parsing/AbstractParsingTestSpec.kt | 33 +++++++++++++++++++ .../validators/ParsingTestTypeValidator.kt | 18 ++++++++++ .../kotlin/parsing/AbstractParsingTest.java | 27 ++++++++++----- 4 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 compiler/tests-spec/tests/org/jetbrains/kotlin/parsing/AbstractParsingTestSpec.kt create mode 100644 compiler/tests-spec/tests/org/jetbrains/kotlin/spec/validators/ParsingTestTypeValidator.kt diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/generators/tests/GenerateCompilerSpecTests.kt b/compiler/tests-spec/tests/org/jetbrains/kotlin/generators/tests/GenerateCompilerSpecTests.kt index 0baeab18915..118d3b20a63 100644 --- a/compiler/tests-spec/tests/org/jetbrains/kotlin/generators/tests/GenerateCompilerSpecTests.kt +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/generators/tests/GenerateCompilerSpecTests.kt @@ -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) { testGroup("compiler/tests-spec/tests", "compiler/tests-spec/testData") { testClass { model("diagnostics", excludeDirs = listOf("helpers")) } + + testClass { + model("psi", testMethod = "doParsingTest", excludeDirs = listOf("helpers")) + } } -} \ No newline at end of file +} diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/parsing/AbstractParsingTestSpec.kt b/compiler/tests-spec/tests/org/jetbrains/kotlin/parsing/AbstractParsingTestSpec.kt new file mode 100644 index 00000000000..1bdcd6e0f98 --- /dev/null +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/parsing/AbstractParsingTestSpec.kt @@ -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 + + 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) + } + } +} diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/validators/ParsingTestTypeValidator.kt b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/validators/ParsingTestTypeValidator.kt new file mode 100644 index 00000000000..4b95b947ff5 --- /dev/null +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/validators/ParsingTestTypeValidator.kt @@ -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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/parsing/AbstractParsingTest.java b/compiler/tests/org/jetbrains/kotlin/parsing/AbstractParsingTest.java index afb95765ea7..a23e1e1e00f 100644 --- a/compiler/tests/org/jetbrains/kotlin/parsing/AbstractParsingTest.java +++ b/compiler/tests/org/jetbrains/kotlin/parsing/AbstractParsingTest.java @@ -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 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 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); } }