[PowerAssert] Add codegen tests
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.powerassert
|
||||
|
||||
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.services.EnvironmentConfigurator
|
||||
import org.jetbrains.kotlin.test.services.RuntimeClasspathProvider
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import java.io.File
|
||||
|
||||
private val junit5Classpath = System.getProperty("junit5.classpath").split(",")
|
||||
|
||||
fun TestConfigurationBuilder.enableJunit() {
|
||||
useConfigurators(::JunitEnvironmentConfigurator)
|
||||
useCustomRuntimeClasspathProviders(::JunitRuntimeClassPathProvider)
|
||||
}
|
||||
|
||||
class JunitEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigurator(testServices) {
|
||||
override fun configureCompilerConfiguration(configuration: CompilerConfiguration, module: TestModule) {
|
||||
if (PowerAssertConfigurationDirectives.WITH_JUNIT5 in module.directives) {
|
||||
for (file in junit5Classpath.map { File(it) }) {
|
||||
configuration.addJvmClasspathRoot(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JunitRuntimeClassPathProvider(testServices: TestServices) : RuntimeClasspathProvider(testServices) {
|
||||
override fun runtimeClassPaths(module: TestModule): List<File> {
|
||||
if (PowerAssertConfigurationDirectives.WITH_JUNIT5 in module.directives) {
|
||||
return junit5Classpath.map { File(it) }
|
||||
} else {
|
||||
return emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.powerassert
|
||||
|
||||
import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer
|
||||
|
||||
object PowerAssertConfigurationDirectives : SimpleDirectivesContainer() {
|
||||
val FUNCTION by stringDirective(
|
||||
description = "Functions targeted by Power-Assert transformation",
|
||||
multiLine = true,
|
||||
)
|
||||
|
||||
val WITH_JUNIT5 by directive("Add JUnit5 to classpath")
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.powerassert
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
|
||||
import org.jetbrains.kotlin.test.directives.AdditionalFilesDirectives
|
||||
import org.jetbrains.kotlin.test.directives.ConfigurationDirectives.WITH_STDLIB
|
||||
import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives.FULL_JDK
|
||||
import org.jetbrains.kotlin.test.directives.model.DirectivesContainer
|
||||
import org.jetbrains.kotlin.test.directives.model.RegisteredDirectives
|
||||
import org.jetbrains.kotlin.test.model.TestFile
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.runners.codegen.AbstractFirLightTreeBlackBoxCodegenTest
|
||||
import org.jetbrains.kotlin.test.runners.codegen.AbstractIrBlackBoxCodegenTest
|
||||
import org.jetbrains.kotlin.test.services.AdditionalSourceProvider
|
||||
import org.jetbrains.kotlin.test.services.EnvironmentConfigurator
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import java.io.File
|
||||
|
||||
// ------------------------ codegen ------------------------
|
||||
|
||||
open class AbstractIrBlackBoxCodegenTestForPowerAssert : AbstractIrBlackBoxCodegenTest() {
|
||||
override fun configure(builder: TestConfigurationBuilder) {
|
||||
super.configure(builder)
|
||||
builder.configurePlugin()
|
||||
}
|
||||
}
|
||||
|
||||
open class AbstractFirLightTreeBlackBoxCodegenTestForPowerAssert : AbstractFirLightTreeBlackBoxCodegenTest() {
|
||||
override fun configure(builder: TestConfigurationBuilder) {
|
||||
super.configure(builder)
|
||||
builder.configurePlugin()
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------ configuration ------------------------
|
||||
|
||||
fun TestConfigurationBuilder.configurePlugin() {
|
||||
// TODO there has got to be a better way?
|
||||
val sourceRoots = File("plugins/power-assert/testData/")
|
||||
.walkTopDown()
|
||||
.filter { it.isDirectory }
|
||||
.joinToString(",") { it.path }
|
||||
System.setProperty("KOTLIN_POWER_ASSERT_ADD_SRC_ROOTS", sourceRoots)
|
||||
|
||||
|
||||
defaultDirectives {
|
||||
+FULL_JDK
|
||||
+WITH_STDLIB
|
||||
}
|
||||
useDirectives(PowerAssertConfigurationDirectives)
|
||||
|
||||
useConfigurators(::PowerAssertEnvironmentConfigurator)
|
||||
|
||||
useAdditionalSourceProviders(
|
||||
::AdditionalSourceFilesProvider,
|
||||
)
|
||||
|
||||
enableJunit()
|
||||
}
|
||||
|
||||
class PowerAssertEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigurator(testServices) {
|
||||
override fun CompilerPluginRegistrar.ExtensionStorage.registerCompilerExtensions(
|
||||
module: TestModule,
|
||||
configuration: CompilerConfiguration,
|
||||
) {
|
||||
val functions = moduleStructure.allDirectives[PowerAssertConfigurationDirectives.FUNCTION]
|
||||
.ifEmpty { listOf("kotlin.assert") }
|
||||
.mapTo(mutableSetOf()) { FqName(it) }
|
||||
|
||||
val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
|
||||
IrGenerationExtension.registerExtension(PowerAssertIrGenerationExtension(messageCollector, functions))
|
||||
}
|
||||
}
|
||||
|
||||
class AdditionalSourceFilesProvider(testServices: TestServices) : AdditionalSourceProvider(testServices) {
|
||||
override val directiveContainers: List<DirectivesContainer> =
|
||||
listOf(AdditionalFilesDirectives)
|
||||
|
||||
override fun produceAdditionalFiles(globalDirectives: RegisteredDirectives, module: TestModule): List<TestFile> {
|
||||
return buildList {
|
||||
add(File("plugins/power-assert/testData/helpers/InfixDispatch.kt").toTestFile())
|
||||
add(File("plugins/power-assert/testData/helpers/InfixExtension.kt").toTestFile())
|
||||
add(File("plugins/power-assert/testData/helpers/utils.kt").toTestFile())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user