Add uast-tests files to our project

Copy files from obsolete project https://github.com/JetBrains/uast
This commit is contained in:
Nikolay Krasko
2017-09-07 18:00:24 +03:00
parent 6fd3ce07b3
commit fcab80db96
9 changed files with 426 additions and 8 deletions
-2
View File
@@ -1,13 +1,11 @@
<component name="libraryTable">
<library name="uast-java">
<CLASSES>
<root url="jar://$PROJECT_DIR$/dependencies/uast-tests.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/lib/idea.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/lib/openapi.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/dependencies/uast-tests-sources.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/sources/sources.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/sources/sources.jar!/" />
</SOURCES>
@@ -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())
}
}
@@ -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<UResolvable>(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)
}
}
@@ -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()
}
}
@@ -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()
}
}
@@ -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)
}
@@ -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)
}
}
@@ -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 <T> UElement.findElementByText(refText: String, cls: Class<T>): T {
val matchingElements = mutableListOf<T>()
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 <reified T : Any> UElement.findElementByText(refText: String): T = findElementByText(refText, T::class.java)
-6
View File
@@ -32,8 +32,6 @@
<os family="windows"/>
</condition>
<property name="uast.version" value="1.0.12"/>
<property name="kotlinx.coroutines.version" value="0.14.1"/>
<property name="generators" value="${basedir}/generators"/>
@@ -246,10 +244,6 @@
<!-- Javaslang -->
<get-maven-library prefix="io/javaslang" lib="javaslang" version="2.0.6"/>
<!-- Uast -->
<property name="uast.server" value="http://dl.bintray.com/kotlin/uast"/>
<get-maven-library prefix="org/jetbrains/uast" lib="uast-tests" version="${uast.version}" target.jar.name.base="uast-tests" server="${uast.server}"/>
<get-maven-library-with-dependencies
group="org.robolectric"
artifact="robolectric"