From fcab80db965485fb821b3804ec7f409dd50f7ed9 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 7 Sep 2017 18:00:24 +0300 Subject: [PATCH] Add uast-tests files to our project Copy files from obsolete project https://github.com/JetBrains/uast --- .idea/libraries/uast_java.xml | 2 - .../uast/test/common/RenderLogTestBase.kt | 37 ++++++ .../uast/test/common/ResolveTestBase.kt | 37 ++++++ .../uast/test/common/TypesTestBase.kt | 72 ++++++++++++ .../uast/test/common/ValuesTestBase.kt | 79 +++++++++++++ .../uast/test/env/AbstractCoreEnvironment.kt | 31 +++++ .../env/AbstractTestWithCoreEnvironment.kt | 108 ++++++++++++++++++ .../uast/test/env/AbstractUastTest.kt | 62 ++++++++++ update_dependencies.xml | 6 - 9 files changed, 426 insertions(+), 8 deletions(-) create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/RenderLogTestBase.kt create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ResolveTestBase.kt create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/TypesTestBase.kt create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ValuesTestBase.kt create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractCoreEnvironment.kt create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractTestWithCoreEnvironment.kt create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt diff --git a/.idea/libraries/uast_java.xml b/.idea/libraries/uast_java.xml index 13a9234595c..27ceb225acb 100644 --- a/.idea/libraries/uast_java.xml +++ b/.idea/libraries/uast_java.xml @@ -1,13 +1,11 @@ - - diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/RenderLogTestBase.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/RenderLogTestBase.kt new file mode 100644 index 00000000000..e6068d41e77 --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/RenderLogTestBase.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.common + +import org.jetbrains.uast.UFile +import org.jetbrains.uast.asRecursiveLogString +import org.jetbrains.uast.test.env.assertEqualsToFile +import java.io.File + +interface RenderLogTestBase { + fun getTestFile(testName: String, ext: String): File + + private fun getRenderFile(testName: String) = getTestFile(testName, "render.txt") + private fun getLogFile(testName: String) = getTestFile(testName, "log.txt") + + fun check(testName: String, file: UFile) { + val renderFile = getRenderFile(testName) + val logFile = getLogFile(testName) + + assertEqualsToFile("Render string", renderFile, file.asRenderString()) + assertEqualsToFile("Log string", logFile, file.asRecursiveLogString()) + } +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ResolveTestBase.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ResolveTestBase.kt new file mode 100644 index 00000000000..a7272c44abc --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ResolveTestBase.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.common + +import com.intellij.psi.PsiNamedElement +import org.jetbrains.uast.UFile +import org.jetbrains.uast.UResolvable +import org.jetbrains.uast.test.env.findElementByText +import org.junit.Assert.assertEquals + +interface ResolveTestBase { + fun check(testName: String, file: UFile) { + val refComment = file.allCommentsInFile.find { it.text.startsWith("// REF:") } ?: throw IllegalArgumentException("No // REF tag in file") + val resultComment = file.allCommentsInFile.find { it.text.startsWith("// RESULT:") } ?: throw IllegalArgumentException("No // RESULT tag in file") + + val refText = refComment.text.substringAfter("REF:") + val parent = refComment.uastParent + val matchingElement = parent.findElementByText(refText) + val resolveResult = matchingElement.resolve() ?: throw IllegalArgumentException("Unresolved reference") + val resultText = resolveResult.javaClass.simpleName + (if (resolveResult is PsiNamedElement) ":${resolveResult.name}" else "") + assertEquals(resultComment.text.substringAfter("RESULT:"), resultText) + } +} diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/TypesTestBase.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/TypesTestBase.kt new file mode 100644 index 00000000000..578c4c4535b --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/TypesTestBase.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.common + +import org.jetbrains.uast.UElement +import org.jetbrains.uast.UExpression +import org.jetbrains.uast.UFile +import org.jetbrains.uast.test.env.assertEqualsToFile +import org.jetbrains.uast.visitor.UastVisitor +import java.io.File + +interface TypesTestBase { + fun getTypesFile(testName: String): File + + private fun UFile.asLogTypes() = TypesLogger().apply { + this@asLogTypes.accept(this) + }.toString() + + fun check(testName: String, file: UFile) { + val valuesFile = getTypesFile(testName) + + assertEqualsToFile("Log values", valuesFile, file.asLogTypes()) + } + + class TypesLogger : UastVisitor { + + val builder = StringBuilder() + + var level = 0 + + override fun visitElement(node: UElement): Boolean { + val initialLine = node.asLogString() + " [" + run { + val renderString = node.asRenderString().lines() + if (renderString.size == 1) { + renderString.single() + } else { + renderString.first() + "..." + renderString.last() + } + } + "]" + + (1..level).forEach { builder.append(" ") } + builder.append(initialLine) + if (node is UExpression) { + val value = node.getExpressionType() + value?.let { builder.append(" : ").append(it) } + } + builder.appendln() + level++ + return false + } + + override fun afterVisitElement(node: UElement) { + level-- + } + + override fun toString() = builder.toString() + } +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ValuesTestBase.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ValuesTestBase.kt new file mode 100644 index 00000000000..34148ca1d0e --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/ValuesTestBase.kt @@ -0,0 +1,79 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.common + +import org.jetbrains.uast.UElement +import org.jetbrains.uast.UExpression +import org.jetbrains.uast.UFile +import org.jetbrains.uast.evaluation.UEvaluationContext +import org.jetbrains.uast.evaluation.UEvaluatorExtension +import org.jetbrains.uast.evaluation.analyzeAll +import org.jetbrains.uast.test.env.assertEqualsToFile +import org.jetbrains.uast.visitor.UastVisitor +import java.io.File + +interface ValuesTestBase { + fun getValuesFile(testName: String): File + fun getEvaluatorExtension(): UEvaluatorExtension? = null + + private fun UFile.asLogValues(): String { + val evaluationContext = analyzeAll(extensions = getEvaluatorExtension()?.let { listOf(it) } ?: emptyList()) + return ValueLogger(evaluationContext).apply { + this@asLogValues.accept(this) + }.toString() + } + + fun check(testName: String, file: UFile) { + val valuesFile = getValuesFile(testName) + + assertEqualsToFile("Log values", valuesFile, file.asLogValues()) + } + + class ValueLogger(val evaluationContext: UEvaluationContext) : UastVisitor { + + val builder = StringBuilder() + + var level = 0 + + override fun visitElement(node: UElement): Boolean { + val initialLine = node.asLogString() + " [" + run { + val renderString = node.asRenderString().lines() + if (renderString.size == 1) { + renderString.single() + } else { + renderString.first() + "..." + renderString.last() + } + } + "]" + + (1..level).forEach { builder.append(" ") } + builder.append(initialLine) + if (node is UExpression) { + val value = evaluationContext.valueOf(node) + builder.append(" = ").append(value) + } + builder.appendln() + level++ + return false + } + + override fun afterVisitElement(node: UElement) { + level-- + } + + override fun toString() = builder.toString() + } +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractCoreEnvironment.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractCoreEnvironment.kt new file mode 100644 index 00000000000..56e3ed2fb4f --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractCoreEnvironment.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.env + +import com.intellij.mock.MockProject +import java.io.File + +abstract class AbstractCoreEnvironment { + abstract val project: MockProject + + open fun dispose() { + // Do nothing + } + + abstract fun addJavaSourceRoot(root: File) + abstract fun addJar(root: File) +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractTestWithCoreEnvironment.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractTestWithCoreEnvironment.kt new file mode 100644 index 00000000000..27c707b4b06 --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractTestWithCoreEnvironment.kt @@ -0,0 +1,108 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.env + +import com.intellij.core.CoreApplicationEnvironment +import com.intellij.mock.MockProject +import com.intellij.openapi.components.ServiceManager +import com.intellij.openapi.extensions.Extensions +import com.intellij.openapi.util.text.StringUtil +import com.intellij.psi.PsiManager +import com.intellij.rt.execution.junit.FileComparisonFailure +import junit.framework.TestCase +import org.jetbrains.uast.UastContext +import org.jetbrains.uast.UastLanguagePlugin +import org.jetbrains.uast.evaluation.UEvaluatorExtension +import org.jetbrains.uast.java.JavaUastLanguagePlugin +import java.io.File + +abstract class AbstractTestWithCoreEnvironment : TestCase() { + private var myEnvironment: AbstractCoreEnvironment? = null + + protected val environment: AbstractCoreEnvironment + get() = myEnvironment!! + + protected lateinit var project: MockProject + + protected val uastContext: UastContext by lazy { + ServiceManager.getService(project, UastContext::class.java) + } + + protected val psiManager: PsiManager by lazy { + PsiManager.getInstance(project) + } + + override fun tearDown() { + disposeEnvironment() + } + + protected abstract fun createEnvironment(source: File): AbstractCoreEnvironment + + protected fun initializeEnvironment(source: File) { + if (myEnvironment != null) { + error("Environment is already initialized") + } + myEnvironment = createEnvironment(source) + project = environment.project + + CoreApplicationEnvironment.registerExtensionPoint( + Extensions.getRootArea(), + UastLanguagePlugin.extensionPointName, + UastLanguagePlugin::class.java) + + CoreApplicationEnvironment.registerExtensionPoint( + Extensions.getRootArea(), + UEvaluatorExtension.EXTENSION_POINT_NAME, + UEvaluatorExtension::class.java) + + project.registerService(UastContext::class.java, UastContext::class.java) + + registerUastLanguagePlugins() + } + + private fun registerUastLanguagePlugins() { + val area = Extensions.getRootArea() + + area.getExtensionPoint(UastLanguagePlugin.extensionPointName) + .registerExtension(JavaUastLanguagePlugin()) + } + + protected fun disposeEnvironment() { + myEnvironment?.dispose() + myEnvironment = null + } +} + +private fun String.trimTrailingWhitespacesAndAddNewlineAtEOF(): String = + this.split('\n').map(String::trimEnd).joinToString(separator = "\n").let { + result -> if (result.endsWith("\n")) result else result + "\n" + } + +fun assertEqualsToFile(description: String, expected: File, actual: String) { + if (!expected.exists()) { + expected.writeText(actual) + TestCase.fail("File didn't exist. New file was created (${expected.canonicalPath}).") + } + + val expectedText = + StringUtil.convertLineSeparators(expected.readText().trim()).trimTrailingWhitespacesAndAddNewlineAtEOF() + val actualText = + StringUtil.convertLineSeparators(actual.trim()).trimTrailingWhitespacesAndAddNewlineAtEOF() + if (expectedText != actualText) { + throw FileComparisonFailure(description, expectedText, actualText, expected.absolutePath) + } +} diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt new file mode 100644 index 00000000000..5c5a76000f6 --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2010-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 org.jetbrains.uast.test.env + +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.uast.UElement +import org.jetbrains.uast.UFile +import org.jetbrains.uast.visitor.UastVisitor +import java.io.File + +abstract class AbstractUastTest : AbstractTestWithCoreEnvironment() { + protected companion object { + val TEST_DATA_DIR = File("testData") + } + + abstract fun getVirtualFile(testName: String): VirtualFile + abstract fun check(testName: String, file: UFile) + + fun doTest(testName: String, checkCallback: (String, UFile) -> Unit = { testName, file -> check(testName, file) }) { + val virtualFile = getVirtualFile(testName) + + val psiFile = psiManager.findFile(virtualFile) ?: error("Can't get psi file for $testName") + val uFile = uastContext.convertElementWithParent(psiFile, null) ?: error("Can't get UFile for $testName") + checkCallback(testName, uFile as UFile) + } +} + +fun UElement.findElementByText(refText: String, cls: Class): T { + val matchingElements = mutableListOf() + accept(object : UastVisitor { + override fun visitElement(node: UElement): Boolean { + if (cls.isInstance(node) && node.psi?.text == refText) { + matchingElements.add(node as T) + } + return false + } + }) + + if (matchingElements.isEmpty()) { + throw IllegalArgumentException("Reference '$refText' not found") + } + if (matchingElements.size != 1) { + throw IllegalArgumentException("Reference '$refText' is ambiguous") + } + return matchingElements.single() +} + +inline fun UElement.findElementByText(refText: String): T = findElementByText(refText, T::class.java) diff --git a/update_dependencies.xml b/update_dependencies.xml index a311ee465f3..05206734c41 100644 --- a/update_dependencies.xml +++ b/update_dependencies.xml @@ -32,8 +32,6 @@ - - @@ -246,10 +244,6 @@ - - - -