Move IR tests to module 'compiler-tests'
(due to dependencies on classes from 'compiler-tests'). Minor: remove unused imports.
This commit is contained in:
committed by
Dmitry Petrov
parent
00a74699e2
commit
00093ac901
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="module" module-name="tests-common" scope="TEST" />
|
||||
<orderEntry type="library" name="intellij-core" level="project" />
|
||||
<orderEntry type="module" module-name="cli" scope="TEST" />
|
||||
<orderEntry type="module" module-name="frontend" scope="TEST" />
|
||||
<orderEntry type="module" module-name="ir.tree" scope="TEST" />
|
||||
<orderEntry type="module" module-name="ir.psi2ir" scope="TEST" />
|
||||
<orderEntry type="library" scope="TEST" name="junit-4.12" level="project" />
|
||||
<orderEntry type="module" module-name="backend.common" scope="TEST" />
|
||||
<orderEntry type="module" module-name="compiler-tests" scope="TEST" />
|
||||
</component>
|
||||
</module>
|
||||
-83
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.ir
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.backend.common.AbstractClosureAnnotator
|
||||
import org.jetbrains.kotlin.backend.common.Closure
|
||||
import java.io.File
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
|
||||
abstract class AbstractClosureAnnotatorTestCase : AbstractIrGeneratorTestCase() {
|
||||
override fun doTest(wholeFile: File, testFiles: List<TestFile>) {
|
||||
val dir = wholeFile.parentFile
|
||||
val ignoreErrors = shouldIgnoreErrors(wholeFile)
|
||||
for ((testFile, irFile) in generateIrFilesAsSingleModule(testFiles, ignoreErrors)) {
|
||||
doTestIrFileAgainstExpectations(dir, testFile, irFile)
|
||||
}
|
||||
}
|
||||
|
||||
private fun doTestIrFileAgainstExpectations(dir: File, testFile: TestFile, irFile: IrFile) {
|
||||
val expectedFile = File(dir, testFile.name.replace(".kt", ".closure"))
|
||||
val actualClosures = renderClosures(irFile)
|
||||
KotlinTestUtils.assertEqualsToFile(expectedFile, actualClosures)
|
||||
}
|
||||
|
||||
private fun renderClosures(irFile: IrFile): String {
|
||||
val actualStringWriter = StringWriter()
|
||||
val actualOut = PrintWriter(actualStringWriter)
|
||||
|
||||
irFile.acceptChildrenVoid(object : AbstractClosureAnnotator() {
|
||||
override fun recordClassClosure(classDescriptor: ClassDescriptor, closure: Closure) {
|
||||
actualOut.println("Closure for class ${classDescriptor.name}:")
|
||||
printClosure(closure)
|
||||
actualOut.println()
|
||||
}
|
||||
|
||||
override fun recordFunctionClosure(functionDescriptor: FunctionDescriptor, closure: Closure) {
|
||||
if (functionDescriptor is ConstructorDescriptor) {
|
||||
actualOut.println("Closure for constructor ${functionDescriptor.containingDeclaration.name}:")
|
||||
}
|
||||
else {
|
||||
actualOut.println("Closure for function ${functionDescriptor.name}:")
|
||||
}
|
||||
printClosure(closure)
|
||||
actualOut.println()
|
||||
}
|
||||
|
||||
private fun printClosure(closure: Closure) {
|
||||
closure.capturedThisReferences.forEach {
|
||||
actualOut.println(" 'this' for ${it.name}")
|
||||
}
|
||||
closure.capturedReceiverParameters.forEach {
|
||||
actualOut.println(" receiver for ${it.containingDeclaration.name}")
|
||||
}
|
||||
closure.capturedVariables.forEach {
|
||||
actualOut.println(" variable ${it.name}")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return actualStringWriter.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.ir
|
||||
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.codegen.CodegenTestCase
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
|
||||
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
|
||||
import org.jetbrains.kotlin.resolve.AnalyzingUtils
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils.getAnnotationsJar
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.PrintWriter
|
||||
import java.util.*
|
||||
|
||||
abstract class AbstractIrGeneratorTestCase : CodegenTestCase() {
|
||||
override fun doMultiFileTest(wholeFile: File, files: List<TestFile>, javaFilesDir: File?) {
|
||||
setupEnvironment(files, javaFilesDir)
|
||||
|
||||
loadMultiFiles(files)
|
||||
doTest(wholeFile, files)
|
||||
}
|
||||
|
||||
private fun setupEnvironment(files: List<TestFile>, javaFilesDir: File?) {
|
||||
val jdkKind = getJdkKind(files)
|
||||
|
||||
val javacOptions = ArrayList<String>(0)
|
||||
var addRuntime = false
|
||||
var addReflect = false
|
||||
for (file in files) {
|
||||
if (InTextDirectivesUtils.isDirectiveDefined(file.content, "WITH_RUNTIME")) {
|
||||
addRuntime = true
|
||||
}
|
||||
if (InTextDirectivesUtils.isDirectiveDefined(file.content, "WITH_REFLECT")) {
|
||||
addReflect = true
|
||||
}
|
||||
|
||||
javacOptions.addAll(InTextDirectivesUtils.findListWithPrefixes(file.content, "// JAVAC_OPTIONS:"))
|
||||
}
|
||||
|
||||
val configurationKind = when {
|
||||
addReflect -> ConfigurationKind.ALL
|
||||
addRuntime -> ConfigurationKind.NO_KOTLIN_REFLECT
|
||||
else -> ConfigurationKind.JDK_ONLY
|
||||
};
|
||||
|
||||
val configuration = createConfiguration(
|
||||
configurationKind, jdkKind,
|
||||
listOf<File>(getAnnotationsJar()),
|
||||
arrayOf(javaFilesDir).filterNotNull(),
|
||||
files
|
||||
)
|
||||
|
||||
myEnvironment = KotlinCoreEnvironment.createForTests(testRootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
}
|
||||
|
||||
protected abstract fun doTest(wholeFile: File, testFiles: List<TestFile>)
|
||||
|
||||
protected fun generateIrModule(ignoreErrors: Boolean = false): IrModuleFragment {
|
||||
assert(myFiles != null) { "myFiles not initialized" }
|
||||
assert(myEnvironment != null) { "myEnvironment not initialized" }
|
||||
return generateIrModule(myFiles.psiFiles, myEnvironment, ignoreErrors)
|
||||
}
|
||||
|
||||
protected fun generateIrFilesAsSingleModule(testFiles: List<TestFile>, ignoreErrors: Boolean = false): Map<TestFile, IrFile> {
|
||||
val irModule = generateIrModule(ignoreErrors)
|
||||
val ktFiles = testFiles.filter { it.name.endsWith(".kt") }
|
||||
return ktFiles.zip(irModule.files).toMap()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val IGNORE_ERRORS_PATTERN = Regex("""// !IGNORE_ERRORS""")
|
||||
|
||||
internal fun shouldIgnoreErrors(wholeFile: File): Boolean =
|
||||
IGNORE_ERRORS_PATTERN.containsMatchIn(wholeFile.readText())
|
||||
|
||||
internal fun createExpectedTextFile(testFile: TestFile, dir: File, fileName: String): File {
|
||||
val textFile = File(dir, fileName)
|
||||
if (!textFile.exists()) {
|
||||
TestCase.assertTrue("Can't create an expected text containingFile: ${textFile.absolutePath}", textFile.createNewFile())
|
||||
PrintWriter(FileWriter(textFile)).use {
|
||||
it.println("$fileName: new expected text containingFile for ${testFile.name}")
|
||||
}
|
||||
}
|
||||
return textFile
|
||||
}
|
||||
|
||||
fun generateIrModule(ktFiles: List<KtFile>, environment: KotlinCoreEnvironment, ignoreErrors: Boolean = false): IrModuleFragment {
|
||||
val analysisResult = JvmResolveUtil.analyze(ktFiles, environment)
|
||||
if (!ignoreErrors) {
|
||||
analysisResult.throwIfError()
|
||||
AnalyzingUtils.throwExceptionOnErrors(analysisResult.bindingContext)
|
||||
}
|
||||
return generateIrModule(ktFiles, analysisResult.moduleDescriptor, analysisResult.bindingContext, ignoreErrors)
|
||||
}
|
||||
|
||||
fun generateIrModule(ktFiles: List<KtFile>, moduleDescriptor: ModuleDescriptor, bindingContext: BindingContext, ignoreErrors: Boolean = false) =
|
||||
Psi2IrTranslator(Psi2IrConfiguration(ignoreErrors)).generateModule(moduleDescriptor, ktFiles, bindingContext)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.ir
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.ir.util.dumpTreesFromLineNumber
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.utils.rethrow
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.PrintWriter
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
abstract class AbstractIrTextTestCase : AbstractIrGeneratorTestCase() {
|
||||
override fun doTest(wholeFile: File, testFiles: List<TestFile>) {
|
||||
val dir = wholeFile.parentFile
|
||||
val ignoreErrors = shouldIgnoreErrors(wholeFile)
|
||||
for ((testFile, irFile) in generateIrFilesAsSingleModule(testFiles, ignoreErrors)) {
|
||||
doTestIrFileAgainstExpectations(dir, testFile, irFile)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun doTestIrFileAgainstExpectations(dir: File, testFile: TestFile, irFile: IrFile) {
|
||||
val expectations = parseExpectations(dir, testFile)
|
||||
val irFileDump = irFile.dump()
|
||||
|
||||
val expected = StringBuilder()
|
||||
val actual = StringBuilder()
|
||||
for (expectation in expectations.regexps) {
|
||||
expected.append(expectation.numberOfOccurrences).append(" ").append(expectation.needle).append("\n")
|
||||
val actualCount = StringUtil.findMatches(irFileDump, Pattern.compile("(" + expectation.needle + ")")).size
|
||||
actual.append(actualCount).append(" ").append(expectation.needle).append("\n")
|
||||
}
|
||||
|
||||
for (irTreeFileLabel in expectations.irTreeFileLabels) {
|
||||
val actualTrees = irFile.dumpTreesFromLineNumber(irTreeFileLabel.lineNumber)
|
||||
KotlinTestUtils.assertEqualsToFile(irTreeFileLabel.expectedTextFile, actualTrees)
|
||||
}
|
||||
|
||||
try {
|
||||
TestCase.assertEquals(irFileDump, expected.toString(), actual.toString())
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
println(irFileDump)
|
||||
throw rethrow(e)
|
||||
}
|
||||
}
|
||||
|
||||
internal class Expectations(val regexps: List<RegexpInText>, val irTreeFileLabels: List<IrTreeFileLabel>)
|
||||
|
||||
internal class RegexpInText(val numberOfOccurrences: Int, val needle: String) {
|
||||
constructor(countStr: String, needle: String) : this(Integer.valueOf(countStr), needle)
|
||||
}
|
||||
|
||||
internal class IrTreeFileLabel(val expectedTextFile: File, val lineNumber: Int)
|
||||
|
||||
companion object {
|
||||
private val EXPECTED_OCCURRENCES_PATTERN = Regex("""^\s*//\s*(\d+)\s*(.*)$""")
|
||||
private val IR_FILE_TXT_PATTERN = Regex("""// IR_FILE: (.*)$""")
|
||||
|
||||
internal fun parseExpectations(dir: File, testFile: TestFile): Expectations {
|
||||
val regexps = ArrayList<RegexpInText>()
|
||||
val treeFiles = ArrayList<IrTreeFileLabel>()
|
||||
|
||||
for (line in testFile.content.split("\n")) {
|
||||
EXPECTED_OCCURRENCES_PATTERN.matchEntire(line)?.let { matchResult ->
|
||||
regexps.add(RegexpInText(matchResult.groupValues[1], matchResult.groupValues[2].trim()))
|
||||
}
|
||||
?: IR_FILE_TXT_PATTERN.find(line)?.let { matchResult ->
|
||||
val fileName = matchResult.groupValues[1].trim()
|
||||
val file = createExpectedTextFile(testFile, dir, fileName)
|
||||
treeFiles.add(IrTreeFileLabel(file, 0))
|
||||
}
|
||||
}
|
||||
|
||||
if (treeFiles.isEmpty()) {
|
||||
val file = createExpectedTextFile(testFile, dir, testFile.name.replace(".kt", ".txt"))
|
||||
treeFiles.add(IrTreeFileLabel(file, 0))
|
||||
}
|
||||
|
||||
return Expectations(regexps, treeFiles)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.ir
|
||||
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractPsi2IrBoxTestCase : AbstractIrGeneratorTestCase() {
|
||||
override fun doTest(wholeFile: File, testFiles: List<TestFile>) {
|
||||
val irModule = generateIrModule(false)
|
||||
val irModuleDump = irModule.dump()
|
||||
val expectedPath = wholeFile.canonicalPath
|
||||
// .replace("/codegen/box/", "/ir/irTextBox/")
|
||||
.replace(".kt", ".txt")
|
||||
val expectedFile = File(expectedPath)
|
||||
KotlinTestUtils.assertEqualsToFile(expectedFile, irModuleDump)
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.kotlin.ir
|
||||
|
||||
import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTest
|
||||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractPsi2IrDiagnosticsTest : AbstractDiagnosticsTest() {
|
||||
override fun performAdditionalChecksAfterDiagnostics(
|
||||
testDataFile: File,
|
||||
testFiles: MutableList<TestFile>,
|
||||
moduleFiles: MutableMap<TestModule?, MutableList<TestFile>>,
|
||||
moduleDescriptors: MutableMap<TestModule?, ModuleDescriptorImpl>,
|
||||
moduleBindings: MutableMap<TestModule?, BindingContext>
|
||||
) {
|
||||
val actualIrDump = StringBuilder()
|
||||
for (module in moduleFiles.keys) {
|
||||
val ktFiles = moduleFiles[module]?.mapNotNull { it.jetFile } ?: continue
|
||||
val moduleDescriptor = moduleDescriptors[module] ?: continue
|
||||
val moduleBindingContext = moduleBindings[module] ?: continue
|
||||
val irModule = AbstractIrGeneratorTestCase.generateIrModule(ktFiles, moduleDescriptor, moduleBindingContext, true)
|
||||
actualIrDump.append(irModule.dump())
|
||||
}
|
||||
|
||||
val expectedIrFile = File(testDataFile.canonicalPath.replace(".kt", ".ir.txt"))
|
||||
KotlinTestUtils.assertEqualsToFile(expectedIrFile, actualIrDump.toString())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user