[Test] Migrate AbstractBytecodeTextTest to new test infrastructure
This commit is contained in:
+7
-130
@@ -5,16 +5,14 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import org.jetbrains.kotlin.ObsoleteTestInfrastructure
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
import org.junit.Assert
|
||||
import org.jetbrains.kotlin.test.util.JUnit4Assertions
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.regex.Matcher
|
||||
import java.util.regex.Pattern
|
||||
|
||||
@ObsoleteTestInfrastructure
|
||||
abstract class AbstractBytecodeTextTest : CodegenTestCase() {
|
||||
override fun doMultiFileTest(wholeFile: File, files: List<TestFile>) {
|
||||
val isIgnored = InTextDirectivesUtils.isIgnoredTarget(backend, wholeFile)
|
||||
@@ -31,57 +29,26 @@ abstract class AbstractBytecodeTextTest : CodegenTestCase() {
|
||||
} else {
|
||||
val expected = readExpectedOccurrences(wholeFile.path)
|
||||
val actual = generateToText("helpers/")
|
||||
checkGeneratedTextAgainstExpectedOccurrences(actual, expected, backend, !isIgnored)
|
||||
checkGeneratedTextAgainstExpectedOccurrences(actual, expected, backend, !isIgnored, JUnit4Assertions)
|
||||
}
|
||||
}
|
||||
|
||||
private fun doTestMultiFile(files: List<TestFile>, reportProblems: Boolean) {
|
||||
val expectedOccurrencesByOutputFile = LinkedHashMap<String, List<OccurrenceInfo>>()
|
||||
for (file in files) {
|
||||
readExpectedOccurrencesForMultiFileTest(file, expectedOccurrencesByOutputFile)
|
||||
readExpectedOccurrencesForMultiFileTest(file.name, file.content, expectedOccurrencesByOutputFile)
|
||||
}
|
||||
|
||||
val generated = generateEachFileToText()
|
||||
for (expectedOutputFile in expectedOccurrencesByOutputFile.keys) {
|
||||
assertTextWasGenerated(expectedOutputFile, generated)
|
||||
assertTextWasGenerated(expectedOutputFile, generated, JUnit4Assertions)
|
||||
val generatedText = generated[expectedOutputFile]!!
|
||||
val expectedOccurrences = expectedOccurrencesByOutputFile[expectedOutputFile]!!
|
||||
checkGeneratedTextAgainstExpectedOccurrences(generatedText, expectedOccurrences, backend, reportProblems)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun readExpectedOccurrences(filename: String): List<OccurrenceInfo> {
|
||||
val result = ArrayList<OccurrenceInfo>()
|
||||
val lines = File(filename).readLines().dropLastWhile(String::isEmpty)
|
||||
var backend = TargetBackend.ANY
|
||||
for (line in lines) {
|
||||
if (line.contains(JVM_TEMPLATES)) backend = TargetBackend.JVM
|
||||
else if (line.contains(JVM_IR_TEMPLATES)) backend = TargetBackend.JVM_IR
|
||||
|
||||
val matcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line)
|
||||
if (matcher.matches()) {
|
||||
result.add(parseOccurrenceInfo(matcher, backend))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
class OccurrenceInfo constructor(private val numberOfOccurrences: Int, private val needle: String, val backend: TargetBackend) {
|
||||
fun getActualOccurrence(text: String): String? {
|
||||
val actualCount = StringUtil.findMatches(text, Pattern.compile("($needle)")).size
|
||||
return "$actualCount $needle"
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "$numberOfOccurrences $needle"
|
||||
checkGeneratedTextAgainstExpectedOccurrences(generatedText, expectedOccurrences, backend, reportProblems, JUnit4Assertions)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val AT_OUTPUT_FILE_PATTERN = Pattern.compile("^\\s*//\\s*@(.*):$")
|
||||
private val EXPECTED_OCCURRENCES_PATTERN = Pattern.compile("^\\s*//\\s*(\\d+)\\s*(.*)$")
|
||||
|
||||
private fun isMultiFileTest(files: List<TestFile>): Boolean {
|
||||
var kotlinFiles = 0
|
||||
for (file in files) {
|
||||
@@ -91,95 +58,5 @@ abstract class AbstractBytecodeTextTest : CodegenTestCase() {
|
||||
}
|
||||
return kotlinFiles > 1
|
||||
}
|
||||
|
||||
fun checkGeneratedTextAgainstExpectedOccurrences(
|
||||
text: String,
|
||||
expectedOccurrences: List<OccurrenceInfo>,
|
||||
currentBackend: TargetBackend,
|
||||
reportProblems: Boolean
|
||||
) {
|
||||
val expected = StringBuilder()
|
||||
val actual = StringBuilder()
|
||||
var lastBackend = TargetBackend.ANY
|
||||
for (info in expectedOccurrences) {
|
||||
if (lastBackend != info.backend) {
|
||||
when (info.backend) {
|
||||
TargetBackend.JVM -> JVM_TEMPLATES
|
||||
TargetBackend.JVM_IR -> JVM_IR_TEMPLATES
|
||||
else -> error("Common part should be first one: ${expectedOccurrences.joinToString("\n")}")
|
||||
}.also {
|
||||
actual.append("\n$it\n")
|
||||
expected.append("\n$it\n")
|
||||
}
|
||||
lastBackend = info.backend
|
||||
}
|
||||
|
||||
expected.append(info).append("\n")
|
||||
if (info.backend == TargetBackend.ANY || info.backend == currentBackend) {
|
||||
actual.append(info.getActualOccurrence(text)).append("\n")
|
||||
} else {
|
||||
actual.append(info).append("\n")
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Assert.assertEquals(text, expected.toString(), actual.toString())
|
||||
} catch (e: Throwable) {
|
||||
if (reportProblems) {
|
||||
println(text)
|
||||
}
|
||||
throw e
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun assertTextWasGenerated(expectedOutputFile: String, generated: Map<String, String>) {
|
||||
if (!generated.containsKey(expectedOutputFile)) {
|
||||
val failMessage = StringBuilder()
|
||||
failMessage.append("Missing output file ").append(expectedOutputFile).append(", got ").append(generated.size).append(": ")
|
||||
for (generatedFile in generated.keys) {
|
||||
failMessage.append(generatedFile).append(" ")
|
||||
}
|
||||
Assert.fail(failMessage.toString())
|
||||
}
|
||||
}
|
||||
|
||||
private const val JVM_TEMPLATES = "// JVM_TEMPLATES"
|
||||
|
||||
private const val JVM_IR_TEMPLATES = "// JVM_IR_TEMPLATES"
|
||||
|
||||
private fun readExpectedOccurrencesForMultiFileTest(file: TestFile, occurrenceMap: MutableMap<String, List<OccurrenceInfo>>) {
|
||||
var currentOccurrenceInfos: MutableList<OccurrenceInfo>? = null
|
||||
var backend = TargetBackend.ANY
|
||||
for (line in file.content.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
|
||||
if (line.contains(JVM_TEMPLATES)) backend = TargetBackend.JVM
|
||||
else if (line.contains(JVM_IR_TEMPLATES)) backend = TargetBackend.JVM_IR
|
||||
|
||||
val atOutputFileMatcher = AT_OUTPUT_FILE_PATTERN.matcher(line)
|
||||
if (atOutputFileMatcher.matches()) {
|
||||
val outputFileName = atOutputFileMatcher.group(1)
|
||||
if (occurrenceMap.containsKey(outputFileName)) {
|
||||
throw AssertionError("${file.name}: Expected occurrences for output file $outputFileName were already provided")
|
||||
}
|
||||
currentOccurrenceInfos = ArrayList()
|
||||
occurrenceMap[outputFileName] = currentOccurrenceInfos
|
||||
}
|
||||
|
||||
val expectedOccurrencesMatcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line)
|
||||
if (expectedOccurrencesMatcher.matches()) {
|
||||
if (currentOccurrenceInfos == null) {
|
||||
throw AssertionError("${file.name}: Should specify output file with '// @<OUTPUT_FILE_NAME>:' before expectations")
|
||||
}
|
||||
val occurrenceInfo = parseOccurrenceInfo(expectedOccurrencesMatcher, backend)
|
||||
currentOccurrenceInfos.add(occurrenceInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseOccurrenceInfo(matcher: Matcher, backend: TargetBackend): OccurrenceInfo {
|
||||
val numberOfOccurrences = Integer.parseInt(matcher.group(1))
|
||||
val needle = matcher.group(2)
|
||||
return OccurrenceInfo(numberOfOccurrences, needle, backend)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -24,12 +24,16 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.test.*;
|
||||
import org.jetbrains.kotlin.test.util.JUnit4Assertions;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.codegen.BytecodeTextUtilsKt.checkGeneratedTextAgainstExpectedOccurrences;
|
||||
import static org.jetbrains.kotlin.codegen.BytecodeTextUtilsKt.readExpectedOccurrences;
|
||||
|
||||
public abstract class AbstractTopLevelMembersInvocationTest extends AbstractBytecodeTextTest {
|
||||
@Override
|
||||
public void doTest(@NotNull String filename) throws Exception {
|
||||
@@ -60,6 +64,6 @@ public abstract class AbstractTopLevelMembersInvocationTest extends AbstractByte
|
||||
|
||||
List<OccurrenceInfo> expected = readExpectedOccurrences(KtTestUtil.getTestDataPathBase() + "/codegen/" + sourceFiles.get(0));
|
||||
String actual = generateToText();
|
||||
Companion.checkGeneratedTextAgainstExpectedOccurrences(actual, expected, TargetBackend.ANY, true);
|
||||
checkGeneratedTextAgainstExpectedOccurrences(actual, expected, TargetBackend.ANY, true, JUnit4Assertions.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -5,10 +5,12 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen.ir
|
||||
|
||||
import org.jetbrains.kotlin.ObsoleteTestInfrastructure
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.codegen.AbstractBytecodeTextTest
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
|
||||
@OptIn(ObsoleteTestInfrastructure::class)
|
||||
abstract class AbstractComposeLikeIrBytecodeTextTest : AbstractBytecodeTextTest() {
|
||||
override val backend = TargetBackend.JVM_IR
|
||||
|
||||
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.codegen.ir
|
||||
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
|
||||
abstract class AbstractFirBytecodeTextTest : AbstractIrBytecodeTextTest() {
|
||||
override fun updateConfiguration(configuration: CompilerConfiguration) {
|
||||
super.updateConfiguration(configuration)
|
||||
configuration.put(CommonConfigurationKeys.USE_FIR, true)
|
||||
configuration.put(JVMConfigurationKeys.IR, true)
|
||||
}
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.codegen.ir
|
||||
|
||||
import org.jetbrains.kotlin.codegen.AbstractBytecodeTextTest
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
|
||||
abstract class AbstractIrBytecodeTextTest : AbstractBytecodeTextTest() {
|
||||
override val backend = TargetBackend.JVM_IR
|
||||
}
|
||||
Reference in New Issue
Block a user