[Tests] Generate runTest with transformer parameter + example

This commit is contained in:
Evgeniy.Zhelenskiy
2021-11-27 23:43:31 +03:00
parent faeb5d21ab
commit f92290dfdf
13 changed files with 157 additions and 34 deletions
@@ -19294,6 +19294,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/boxResultInlineClassOfConstructorCall.kt");
}
@Test
@TestMetadata("boxResultInlineClassOfConstructorCall.kt")
public void testBoxResultInlineClassOfConstructorCallWithoutJvmInlineAnnotation() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxResultInlineClassOfConstructorCall.kt", s -> s.replaceAll("@(kotlin.jvm.)?JvmInline", ""));
}
@Test
@TestMetadata("boxUnboxInlineClassesWithOperatorsGetSet.kt")
public void testBoxUnboxInlineClassesWithOperatorsGetSet() throws Exception {
@@ -24,9 +24,13 @@ class TestRunner(private val testConfiguration: TestConfiguration) {
private val allFailedExceptions = mutableListOf<WrappedException>()
private val allRanHandlers = mutableSetOf<AnalysisHandler<*>>()
fun runTest(@TestDataFile testDataFileName: String, beforeDispose: (TestConfiguration) -> Unit = {}) {
fun runTest(
@TestDataFile testDataFileName: String,
beforeDispose: (TestConfiguration) -> Unit = {},
expectedFileTransformer: ((String) -> String)? = null
) {
try {
runTestImpl(testDataFileName)
runTestImpl(testDataFileName, expectedFileTransformer)
} finally {
try {
testConfiguration.testServices.temporaryDirectoryManager.cleanupTemporaryDirectories()
@@ -38,7 +42,7 @@ class TestRunner(private val testConfiguration: TestConfiguration) {
}
}
private fun runTestImpl(@TestDataFile testDataFileName: String) {
private fun runTestImpl(@TestDataFile testDataFileName: String, expectedFileTransformer: ((String) -> String)?) {
val services = testConfiguration.testServices
@Suppress("NAME_SHADOWING")
@@ -66,12 +70,13 @@ class TestRunner(private val testConfiguration: TestConfiguration) {
if (it.shouldSkipTest()) return
}
runTestPipeline(moduleStructure, services)
runTestPipeline(moduleStructure, services, expectedFileTransformer)
}
fun runTestPipeline(
moduleStructure: TestModuleStructure,
services: TestServices
services: TestServices,
expectedFileTransformer: ((String) -> String)?,
) {
val globalMetadataInfoHandler = testConfiguration.testServices.globalMetadataInfoHandler
globalMetadataInfoHandler.parseExistingMetadataInfosFromAllSources()
@@ -100,7 +105,7 @@ class TestRunner(private val testConfiguration: TestConfiguration) {
}
if (testConfiguration.metaInfoHandlerEnabled) {
withAssertionCatching(WrappedException::FromMetaInfoHandler) {
globalMetadataInfoHandler.compareAllMetaDataInfos()
globalMetadataInfoHandler.compareAllMetaDataInfos(expectedFileTransformer)
}
}
@@ -51,7 +51,7 @@ class GlobalMetadataInfoHandler(
infos += codeMetaInfos
}
fun compareAllMetaDataInfos() {
fun compareAllMetaDataInfos(expectedTransformer: ((String) -> String)?) {
// TODO: adapt to multiple testdata files
val moduleStructure = testServices.moduleStructure
val builder = StringBuilder()
@@ -70,7 +70,14 @@ class GlobalMetadataInfoHandler(
}
}
val actualText = builder.toString()
testServices.assertions.assertEqualsToFile(moduleStructure.originalTestDataFiles.single(), actualText)
val expectedFile = moduleStructure.originalTestDataFiles.single()
if (expectedTransformer != null) {
val expectedContent = expectedFile.readText().let(expectedTransformer)
val message = "Actual data differs from transformed content of file $expectedFile"
testServices.assertions.assertEquals(expectedContent, actualText) { message }
} else {
testServices.assertions.assertEqualsToFile(expectedFile, actualText)
}
}
private fun StringBuilder.stripAdditionalEmptyLines(file: TestFile): CharSequence {
@@ -1,4 +1,5 @@
// WITH_STDLIB
// WORKS_WITHOUT_JVM_INLINE
@Suppress("OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE")
@kotlin.jvm.JvmInline
@@ -19294,6 +19294,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/boxResultInlineClassOfConstructorCall.kt");
}
@Test
@TestMetadata("boxResultInlineClassOfConstructorCall.kt")
public void testBoxResultInlineClassOfConstructorCallWithoutJvmInlineAnnotation() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/boxResultInlineClassOfConstructorCall.kt", s -> s.replaceAll("@(kotlin.jvm.)?JvmInline", ""));
}
@Test
@TestMetadata("boxUnboxInlineClassesWithOperatorsGetSet.kt")
public void testBoxUnboxInlineClassesWithOperatorsGetSet() throws Exception {
@@ -12,11 +12,9 @@ import org.jetbrains.kotlin.test.builders.testRunner
import org.jetbrains.kotlin.test.directives.ConfigurationDirectives
import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives
import org.jetbrains.kotlin.test.model.ResultingArtifact
import org.jetbrains.kotlin.test.model.TestFile
import org.jetbrains.kotlin.test.preprocessors.MetaInfosCleanupPreprocessor
import org.jetbrains.kotlin.test.services.JUnit5Assertions
import org.jetbrains.kotlin.test.services.KotlinTestInfo
import org.jetbrains.kotlin.test.services.SourceFilePreprocessor
import org.jetbrains.kotlin.test.services.TemporaryDirectoryManager
import org.jetbrains.kotlin.test.services.*
import org.jetbrains.kotlin.test.services.impl.TemporaryDirectoryManagerImpl
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.types.FlexibleTypeImpl
@@ -82,4 +80,19 @@ abstract class AbstractKotlinCompilerTest {
open fun runTest(@TestDataFile filePath: String) {
testRunner(filePath, configuration).runTest(filePath)
}
@JvmOverloads
open fun runTest(
@TestDataFile filePath: String,
actualContentModifier: (String) -> String,
expectedContentModifier: ((String) -> String)? = actualContentModifier
) {
class SourceTransformer(testServices: TestServices) : SourceFilePreprocessor(testServices) {
override fun process(file: TestFile, content: String): String = content.let(actualContentModifier)
}
testRunner(filePath) {
configuration.let { it() } // property configuration, not method
useSourcePreprocessor({ SourceTransformer(it) })
}.runTest(filePath, expectedFileTransformer = expectedContentModifier)
}
}
@@ -20,13 +20,13 @@ import org.junit.runner.RunWith
import org.junit.runners.BlockJUnit4ClassRunner
import java.io.File
import java.io.IOException
import java.util.*
private val METHOD_GENERATORS = listOf(
RunTestMethodGenerator,
SimpleTestClassModelTestAllFilesPresentMethodGenerator,
SimpleTestMethodGenerator,
SingleClassTestModelAllFilesPresentedMethodGenerator
SingleClassTestModelAllFilesPresentedMethodGenerator,
WithoutJvmInlineTestMethodGenerator,
)
object TestGeneratorImpl : TestGenerator(METHOD_GENERATORS) {