[SAM with receiver] Prepare module structure to K2 implementation

This commit is contained in:
Dmitriy Novozhilov
2022-06-22 13:22:47 +03:00
committed by teamcity
parent e54c6eeafc
commit ffc680f4a6
38 changed files with 209 additions and 136 deletions
@@ -0,0 +1,71 @@
/*
* Copyright 2010-2022 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.samWithReceiver
import com.intellij.mock.MockProject
import org.jetbrains.kotlin.ObsoleteTestInfrastructure
import org.jetbrains.kotlin.TestsCompilerError
import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTest
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerFromEnvironment
import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition
import org.jetbrains.kotlin.scripting.resolve.KtFileScriptSource
import org.junit.Assert
import java.io.File
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.compilerOptions
import kotlin.script.experimental.api.fileExtension
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.experimental.jvmhost.createJvmScriptDefinitionFromTemplate
@OptIn(ObsoleteTestInfrastructure::class)
abstract class AbstractSamWithReceiverScriptNewDefTest : AbstractDiagnosticsTest() {
override fun setupEnvironment(environment: KotlinCoreEnvironment) {
val def = ScriptDefinition.FromTemplate(
defaultJvmScriptingHostConfiguration,
ScriptForSamWithReceiversNewDef::class
)
environment.configuration.add(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, def)
// need to make a single script compilation to process definition options before registering SamWithReceiver component
val scriptCompiler = ScriptJvmCompilerFromEnvironment(environment)
val res = scriptCompiler.compile("42".toScriptSource("\$init"), ScriptForSamWithReceiversNewDefCompilationConfiguration)
Assert.assertTrue(res is ResultWithDiagnostics.Success<*>)
SamWithReceiverComponentRegistrar().registerProjectComponents(environment.project as MockProject, environment.configuration)
}
override fun analyzeAndCheck(testDataFile: File, files: List<TestFile>) {
val definition = createJvmScriptDefinitionFromTemplate<ScriptForSamWithReceiversNewDef>()
val scriptCompiler = ScriptJvmCompilerFromEnvironment(environment)
val scripts = files.filter {
it.ktFile?.virtualFile?.extension == definition.compilationConfiguration[ScriptCompilationConfiguration.fileExtension]
}
super.analyzeAndCheck(testDataFile, files)
for (file in scripts) {
val res = scriptCompiler.compile(KtFileScriptSource(file.ktFile!!), ScriptForSamWithReceiversNewDefCompilationConfiguration)
if (res is ResultWithDiagnostics.Failure && !file.name.contains("error", ignoreCase = true))
throw TestsCompilerError(
RuntimeException(
res.reports.joinToString("\n") { it.exception?.toString() ?: it.message },
res.reports.find { it.exception != null }?.exception
)
)
}
}
}
@KotlinScript(compilationConfiguration = ScriptForSamWithReceiversNewDefCompilationConfiguration::class)
abstract class ScriptForSamWithReceiversNewDef
object ScriptForSamWithReceiversNewDefCompilationConfiguration : ScriptCompilationConfiguration(
{
compilerOptions("-P", "plugin:org.jetbrains.kotlin.samWithReceiver:annotation=SamWithReceiver1")
}
)
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2022 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.samWithReceiver
import org.jetbrains.kotlin.ObsoleteTestInfrastructure
import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTest
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition
import org.jetbrains.kotlin.scripting.resolve.KotlinScriptDefinitionFromAnnotatedTemplate
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.extensions.SamWithReceiverAnnotations
import kotlin.script.templates.ScriptTemplateDefinition
@OptIn(ObsoleteTestInfrastructure::class)
abstract class AbstractSamWithReceiverScriptTest : AbstractDiagnosticsTest() {
override fun setupEnvironment(environment: KotlinCoreEnvironment) {
val def = ScriptDefinition.FromLegacy(
defaultJvmScriptingHostConfiguration,
KotlinScriptDefinitionFromAnnotatedTemplate(ScriptForSamWithReceivers::class, emptyMap())
)
environment.configuration.add(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, def)
val anns = def.annotationsForSamWithReceivers
StorageComponentContainerContributor.registerExtension(environment.project, CliSamWithReceiverComponentContributor(anns))
}
}
@ScriptTemplateDefinition
@SamWithReceiverAnnotations("SamWithReceiver1", "SamWithReceiver2")
abstract class ScriptForSamWithReceivers
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2022 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.samWithReceiver
import org.jetbrains.kotlin.ObsoleteTestInfrastructure
import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTest
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
@OptIn(ObsoleteTestInfrastructure::class)
abstract class AbstractSamWithReceiverTest : AbstractDiagnosticsTest() {
private companion object {
private val TEST_ANNOTATIONS = listOf("SamWithReceiver")
}
override fun setupEnvironment(environment: KotlinCoreEnvironment) {
StorageComponentContainerContributor.registerExtension(
environment.project,
CliSamWithReceiverComponentContributor(TEST_ANNOTATIONS)
)
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2022 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.samWithReceiver;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script")
@TestDataPath("/")
@RunWith(JUnit3RunnerWithInners.class)
public class SamWithReceiverScriptNewDefTestGenerated extends AbstractSamWithReceiverScriptNewDefTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInScript() throws Exception {
org.jetbrains.kotlin.test.util.KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script"), Pattern.compile("^(.+)\\.kts$"), null, true);
}
@TestMetadata("samConversionSimple.kts")
public void testSamConversionSimple() throws Exception {
runTest("plugins/sam-with-receiver/testData/script/samConversionSimple.kts");
}
}